From f5b6b6f79de07eb4a10198c300c1e2041dc7c53b Mon Sep 17 00:00:00 2001 From: Mark Date: Tue, 20 Nov 2012 16:47:02 +1000 Subject: [PATCH] Made changes to tests and test fixtures, database seeds in seeds.rb, 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 --- app/controllers/home_controller.rb | 5 ++++ app/models/audio_event_tag.rb | 2 +- app/models/audio_recording.rb | 8 ++++++ app/models/project.rb | 7 +++++ app/models/site.rb | 6 ++++ lib/search.rb | 44 ++++++++++++++++++++++++++++++ 6 files changed, 71 insertions(+), 1 deletion(-) diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb index 95f29929..4fd9ef81 100644 --- a/app/controllers/home_controller.rb +++ b/app/controllers/home_controller.rb @@ -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 diff --git a/app/models/audio_event_tag.rb b/app/models/audio_event_tag.rb index 90c8c90f..ac38e54f 100644 --- a/app/models/audio_event_tag.rb +++ b/app/models/audio_event_tag.rb @@ -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 diff --git a/app/models/audio_recording.rb b/app/models/audio_recording.rb index 764d1eaf..f77047ce 100644 --- a/app/models/audio_recording.rb +++ b/app/models/audio_recording.rb @@ -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 diff --git a/app/models/project.rb b/app/models/project.rb index 6208e354..425eaa81 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -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 diff --git a/app/models/site.rb b/app/models/site.rb index f0ca7f1e..90b8ceb0 100644 --- a/app/models/site.rb +++ b/app/models/site.rb @@ -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 diff --git a/lib/search.rb b/lib/search.rb index 4daff663..30b79fce 100644 --- a/lib/search.rb +++ b/lib/search.rb @@ -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 @@ -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 \ No newline at end of file