Skip to content

Commit

Permalink
More work on search/query/dataset
Browse files Browse the repository at this point in the history
modified:   Gemfile.lock
modified:   app/models/audio_recording.rb
-- added /fixed scoped queries

modified:   db/development_seeds.rb
modified:   db/seeds.rb
-- this is the only place seed/fixture data comes from now

modified:   lib/search.rb
-- can build and execute queries from state of search instance. Queries need to be LEFT OUTER joins, rather than INNER

deleted:    test/fixtures/audio_events.yml
deleted:    test/fixtures/audio_recordings.yml
deleted:    test/fixtures/authorizations.yml
deleted:    test/fixtures/bookmarks.yml
deleted:    test/fixtures/permissions.yml
deleted:    test/fixtures/photos.yml
deleted:    test/fixtures/progresses.yml
deleted:    test/fixtures/projects.yml
deleted:    test/fixtures/saved_searches.yml
deleted:    test/fixtures/sites.yml
deleted:    test/fixtures/tags.yml
deleted:    test/fixtures/users.yml
-- deleted these in favour of seeds.rb

modified:   test/test_helper.rb
-- databsae is cleaned before tests run, and tests use transactions/truncation to clean between tests

modified:   test/unit/class_search_test.rb
-- working on tests for the search class
  • Loading branch information
cofiem committed Nov 21, 2012
1 parent f5b6b6f commit d344be5
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 13 deletions.
44 changes: 43 additions & 1 deletion app/models/audio_recording.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,53 @@ class AudioRecording < ActiveRecord::Base
include UUIDHelper

# scoped, re-usable queries
# when chaining a lambda scope you must also wrap it with a
# lambda or else you will end up with the wrong result
# http://www.slashdotdash.net/2010/09/25/rails-3-scopes-with-chaining/
#scope :recordings_from_projects, lambda { |project_ids| joins(:site) }
#scope :filter_by_branch, lambda{|branch_id| includes(:branches).where(:branches => {:id => branch_id})

# these need ot be left outer joins. includes should do this, ut does not.
# use joins with the join in sql text :(
# http://guides.rubyonrails.org/active_record_querying.html#specifying-conditions-on-eager-loaded-associations
def self.recording_projects(project_ids)
joins(:site => :projects).where(:projects => { :id => project_ids}).order('audio_recordings.id').select('audio_recordings.id')
includes(:site => :projects).where(:projects => { :id => project_ids})
end

def self.recording_sites(site_ids)
includes(:site).where(:sites => { :id => site_ids })
end

def self.recordings(recording_ids)
where(:id => recording_ids)
end

# only one of these can be included.
def self.recording_within_date(start_date, end_date)
rel_query = scoped
if start_date.is_a?(Time)
rel_query = rel_query.where('recorded_date + duration_seconds >= :start_date', {:start_date => start_date})
end

if end_date.is_a?(Time)
rel_query = rel_query.where('recorded_date <= :end_date', {:end_date => end_date})
end

rel_query
end

def self.recording_tags(tags)
rel_query = includes(:audio_events => :tags)

tags.each do |tag|
rel_query = rel_query.where(Tag.arel_table[:text].matches("%#{tag}%"))
end

rel_query
end

def self.recording_time_ranges(time_ranges)
scoped
end

private
Expand Down
47 changes: 35 additions & 12 deletions lib/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,39 @@ def initialize(parameters = {})
end
end

# execute a query using the state of this Search instance.
def execute_query
# create a query using the state of this Search instance.
def create_query

if @body_params.blank?
{}
recordings_search = AudioRecording.scoped
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)
recordings_search = AudioRecording.scoped

recordings_search = recordings_search.recording_projects(@body_params.project_ids) if @body_params.project_ids
recordings_search = recordings_search.recording_sites(@body_params.site_ids) if @body_params.site_ids
recordings_search = recordings_search.recordings(@body_params.audio_recording_ids) if @body_params.audio_recording_ids

recordings_search = recordings_search.recording_tags(@body_params.tags) if @body_params.tags
recordings_search = recordings_search.recording_time_ranges(@body_params.time_ranges) if @body_params.time_ranges

if @body_params.date_start && @body_params.date_end
recordings_search = recordings_search.recording_within_date(@body_params.date_start, @body_params.date_end)
elsif @body_params.date_start
recordings_search = recordings_search.recording_within_date(@body_params.date_start,@body_params.date_start)
elsif @body_params.date_end
recordings_search = recordings_search.recording_within_date(@body_params.date_end,@body_params.date_end)
end

end

recordings_search
end

# execute a query to get the dataset
def execute_query
the_query = create_query.select('audio_recordings.id').order('audio_recordings.recorded_date')
results = the_query.all.collect{ |result| DataSetItem.new({ :audio_recording_id => result.id }) }
DataSet.new({ :search => self, :query => the_query, :items => results })
end

class SearchPre
Expand Down Expand Up @@ -144,13 +163,17 @@ def initialize(parameters = {})
end

class DataSet
attr_accessor :search, :items
attr_accessor :search, :query, :items

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

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

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

0 comments on commit d344be5

Please sign in to comment.