Skip to content

Commit

Permalink
Modified controllers and models to enable downloading annotations.
Browse files Browse the repository at this point in the history
modified:   app/controllers/application_controller.rb
modified:   app/controllers/audio_events_controller.rb
modified:   app/models/audio_event.rb
modified:   app/models/audio_event_tag.rb
modified:   app/models/audio_recording.rb
modified:   app/models/tag.rb
new file:   app/views/audio_events/download.csv.erb

modified:   config/routes.rb
-- added route for downloading audio tags

modified:   db/development_seeds.rb
-- tags are now properly connected to audio events
  • Loading branch information
cofiem committed Nov 22, 2012
1 parent d344be5 commit cbefa51
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 5 deletions.
21 changes: 20 additions & 1 deletion app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,26 @@ class ApplicationController < ActionController::Base

# userstamp
include Userstamp


# from http://stackoverflow.com/a/94626
def render_csv(filename = nil)
filename ||= params[:action]
filename += '.csv'

if request.env['HTTP_USER_AGENT'] =~ /msie/i
headers['Pragma'] = 'public'
headers["Content-type"] = "text/plain"
headers['Cache-Control'] = 'no-cache, must-revalidate, post-check=0, pre-check=0'
headers['Content-Disposition'] = "attachment; filename=\"#{filename}\""
headers['Expires'] = "0"
else
headers["Content-Type"] ||= 'text/csv'
headers["Content-Disposition"] = "attachment; filename=\"#{filename}\""
end

render :layout => false
end

private

#def set_stamper
Expand Down
11 changes: 11 additions & 0 deletions app/controllers/audio_events_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,15 @@ def destroy
format.json { head :no_content }
end
end

def download

@annotations = AudioEvent.includes(:tags).all

respond_to do |format|
format.xml { render xml: @annotations }
format.json { render json: @annotations.to_json(:includes => :tags) }
format.csv { render_csv("annotations-#{Time.now.strftime("%Y%m%d")}") }
end
end
end
4 changes: 2 additions & 2 deletions app/models/audio_event.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
class AudioEvent < ActiveRecord::Base
# relations
belongs_to :audio_recording
has_many :audio_event_tags
has_many :audio_event_tags, :inverse_of => :audio_event
has_many :tags, :through => :audio_event_tags

accepts_nested_attributes_for :tags
accepts_nested_attributes_for :tags, :audio_event_tags

# attr
attr_accessible :audio_recording_id, :end_time_seconds, :high_frequency_hertz, :is_reference,
Expand Down
3 changes: 3 additions & 0 deletions app/models/audio_event_tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ class AudioEventTag < ActiveRecord::Base
belongs_to :audio_event
belongs_to :tag

# attr
attr_accessible :audio_event, :tag

# userstamp
stampable
belongs_to :user, :class_name => 'User', :foreign_key => :creator_id
Expand Down
2 changes: 1 addition & 1 deletion app/models/audio_recording.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class AudioRecording < ActiveRecord::Base
attr_accessible :bit_rate_bps, :channels, :data_length_bytes,
:duration_seconds, :file_hash, :media_type, :notes,
:recorded_date, :sample_rate_hertz, :status, :uploader_id,
:site_id
:site_id, :uuid

accepts_nested_attributes_for :site

Expand Down
4 changes: 3 additions & 1 deletion app/models/tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ class Tag < ActiveRecord::Base
extend Enumerize

# relations
has_many :audio_event_tags
has_many :audio_event_tags, :inverse_of => :tag
has_many :audio_events, :through => :audio_event_tags

accepts_nested_attributes_for :audio_events, :audio_event_tags

# attr
attr_accessible :is_taxanomic, :text, :type_of_tag

Expand Down
6 changes: 6 additions & 0 deletions app/views/audio_events/download.csv.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"Tag", "Start Date", "Start Time", "End Date", "End Time", "Max Frequency (hz)", "Min Frequency (hz)", "Projects", "Sites", "Sensor Name", "Score", "Tagged by", "Player link"
<%= CSV.generate do |csv|
@annotations.each do |annotation|
csv << [ annotation[:audio_recording_id] ]
end
end %>

0 comments on commit cbefa51

Please sign in to comment.