Skip to content

Commit

Permalink
Made changes to tests and test fixtures, database seeds in seeds.rb, …
Browse files Browse the repository at this point in the history
…added re-usable scoped queries to models.

modified:   app/controllers/home_controller.rb
-- this was just for testing, ignore

modified:   app/models/audio_event_tag.rb
modified:   app/models/audio_recording.rb
modified:   app/models/project.rb
modified:   app/models/site.rb
-- added scoped queries that can be composed and re-used

modified:   config/application.rb
-- added time threshold for automatically printing sql explain

modified:   db/development_seeds.rb
modified:   db/schema.rb
modified:   db/seeds.rb
-- fixed up some errors in seed data

modified:   lib/search.rb
-- added execute and querystring methods

modified:   test/fixtures/bookmarks.yml
modified:   test/fixtures/progresses.yml
modified:   test/fixtures/saved_searches.yml
-- fixed errors in test fixtures

new file:   test/unit/class_search_test.rb
-- added test for search class
  • Loading branch information
cofiem committed Nov 20, 2012
1 parent b23a0cf commit f5b6b6f
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 1 deletion.
5 changes: 5 additions & 0 deletions app/controllers/home_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
class HomeController < ApplicationController
def index
#the_search = Search.new( { :body_params => { :project_ids => [ 4 ],:site_ids => [ ],:audio_recording_ids => [ ] } } )
#params[:test1] = the_search
#params[:test2] = the_search.execute_query.all

#raise RuntimeError
end
end
2 changes: 1 addition & 1 deletion app/models/audio_event_tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class AudioEventTag < ActiveRecord::Base

# userstamp
stampable
bbelongs_to :user, :class_name => 'User', :foreign_key => :creator_id
belongs_to :user, :class_name => 'User', :foreign_key => :creator_id

# validations
validates :audio_event_id, :presence => true
Expand Down
8 changes: 8 additions & 0 deletions app/models/audio_recording.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ class AudioRecording < ActiveRecord::Base
attr_protected :uuid
include UUIDHelper

# scoped, re-usable queries
#scope :recordings_from_projects, lambda { |project_ids| joins(:site) }
#scope :filter_by_branch, lambda{|branch_id| includes(:branches).where(:branches => {:id => branch_id})

def self.recording_projects(project_ids)
joins(:site => :projects).where(:projects => { :id => project_ids}).order('audio_recordings.id').select('audio_recordings.id')
end

private

# default values
Expand Down
7 changes: 7 additions & 0 deletions app/models/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,11 @@ class Project < ActiveRecord::Base
validates :name, :presence => true, :uniqueness => { :case_sensitive => false }
validates :urn, :presence => true

# commonly used queries (these return
# ActiveRecord::Relation object which will allow for
# further methods (such as other scopes) to be called on it)
#scope :specified_projects, lambda { |project_ids| where('id in (:ids)',{:ids => project_ids}) }
#scope :project_sites, lambda { |project_ids| includes(:sites).where('id in (:ids)',{:ids => project_ids}) }
scope :project_sites, lambda{|site_ids| includes(:sites).where(:sites => {:id => site_ids}) }

end
6 changes: 6 additions & 0 deletions app/models/site.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,10 @@ class Site < ActiveRecord::Base
validates :name, :presence => true, :length => { :minimum => 2 }
validates :latitude, :numericality => true
validates :longitude, :numericality => true

# commonly used queries
#scope :specified_sites, lambda { |site_ids| where('id in (:ids)', { :ids => site_ids } ) }
#scope :sites_in_project, lambda { |project_ids| where(Project.specified_projects, { :ids => project_ids } ) }
scope :site_projects, lambda{ |project_ids| includes(:projects).where(:projects => {:id => project_ids} ) }

end
44 changes: 44 additions & 0 deletions lib/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,22 @@ def initialize(parameters = {})
end
end

# execute a query using the state of this Search instance.
def execute_query
if @body_params.blank?
{}
else
#projects = Project.project_sites(@body_params.project_ids)
#sites = projects.collect {|project| project.sites }

#sites = projects.collect{ |project| project.sites }
#sites = Site.find(@body_params.site_ids.merge(.collect { |site| site}))
#projects = Site.filter_by_projects(1)
#sites = Site.where(:id => @body_params.site_ids)
audio_recordings = AudioRecording.recording_projects(@body_params.project_ids)
end
end

class SearchPre

attr_accessor :created_by_id, :is_temporary
Expand Down Expand Up @@ -128,6 +144,34 @@ def initialize(parameters = {})
end

class DataSet
attr_accessor :search, :items

def initialize(parameters = {})
if parameters.include? :search
@search = parameters[:search]
end

if parameters.include? :items
@items = parameters[:items]
end
end
end

class DataSetItem
attr_accessor :audio_recording_id, :start_offset_seconds, :end_offset_seconds

def initialize(parameters = {})
if parameters.include? :audio_recording_id
@audio_recording_id = parameters[:audio_recording_id]
end

if parameters.include? :start_offset_seconds
@start_offset_seconds = parameters[:start_offset_seconds]
end

if parameters.include? :end_offset_seconds
@end_offset_seconds = parameters[:end_offset_seconds]
end
end
end
end

0 comments on commit f5b6b6f

Please sign in to comment.