Skip to content

Commit

Permalink
root (::) AR models in lib/queries
Browse files Browse the repository at this point in the history
  • Loading branch information
mjy committed Jan 25, 2019
1 parent 63f0552 commit 4e9e1bc
Show file tree
Hide file tree
Showing 13 changed files with 53 additions and 58 deletions.
14 changes: 1 addition & 13 deletions app/controllers/documents_controller.rb
Expand Up @@ -77,19 +77,7 @@ def search
end

def autocomplete
@documents = Queries::DocumentAutocompleteQuery.new(params[:term], project_id: params[:project_id]).all
data = @documents.collect do |t|
{
id: t.id,
label: ApplicationController.helpers.document_tag(t),
response_values: {
params[:method] => t.id
},
label_html: ApplicationController.helpers.document_tag(t)
}
end

render json: data
@documents = Queries::Document::Autocomplete.new(params[:term], project_id: params[:project_id]).all
end

private
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/repositories_controller.rb
Expand Up @@ -79,7 +79,7 @@ def search

def autocomplete

@repositories = Queries::RepositoryAutocompleteQuery.new(params[:term]).all
@repositories = Queries::Repository::Autocomplete.new(params[:term]).all

data = @repositories.collect do |t|
{id: t.id,
Expand Down
12 changes: 12 additions & 0 deletions app/views/documents/autocomplete.json.jbuilder
@@ -0,0 +1,12 @@
json.array! @documents do |s|
v = document_tag(s)
json.id s.id
json.label v
json.label_html v

json.response_values do
if params[:method]
json.set! params[:method], s.id
end
end
end
3 changes: 2 additions & 1 deletion lib/queries/README.md
Expand Up @@ -8,11 +8,12 @@ These are the primary filtering, and autocomplete queries. The directory is bei

# Design guidelines

* All references to ActiveRecord models must begin with `::`, e.g. `::Otu.all`
* Params should be passed as a `Hash`, do the permitting of attributes in the controller.

## Filter queries

* should have an `#all method that returns a _Scope_
* should have an `#all` method that returns a _Scope_

## Autocomplete queries

Expand Down
23 changes: 12 additions & 11 deletions lib/queries/collection_object_filter_query.rb
@@ -1,5 +1,6 @@
module Queries

# TODO: deprecate/move into lib/queries/collection_object/filter
class CollectionObjectFilterQuery < Queries::Query

# Query variables
Expand Down Expand Up @@ -83,7 +84,7 @@ def user_date_set?
def otu_scope
# Challenge: Refactor to use a join pattern instead of SELECT IN
inner_scope = with_descendants? ? ::Otu.self_and_descendants_of(query_otu_id) : ::Otu.where(id: query_otu_id)
CollectionObject.joins(:otus).where(otus: {id: inner_scope})
::CollectionObject.joins(:otus).where(otus: {id: inner_scope})
end

# @return [Scope]
Expand All @@ -93,27 +94,27 @@ def geographic_area_scope
query_geographic_area_ids.each do |ga_id|
target_geographic_item_ids.push(GeographicArea.joins(:geographic_items).find(ga_id).default_geographic_item.id)
end
CollectionObject.joins(:geographic_items)
::CollectionObject.joins(:geographic_items)
.where(GeographicItem.contained_by_where_sql(target_geographic_item_ids))
end

# @return [Scope]
def shape_scope
GeographicItem.gather_map_data(query_shape, 'CollectionObject', project_id)
::GeographicItem.gather_map_data(query_shape, 'CollectionObject', project_id)
end

# @return [Scope]
def date_scope
sql = Queries::CollectingEvent::Filter.new(start_date: query_start_date, end_date: query_end_date, partial_overlap_dates: query_date_partial_overlap).between_date_range.to_sql
CollectionObject.joins(:collecting_event)
::CollectionObject.joins(:collecting_event)
.where(sql)
end

# @return [Scope]
def identifier_scope
ns = nil
ns = Namespace.where(short_name: query_id_namespace).first if query_id_namespace.present?
CollectionObject.with_identifier_type_and_namespace('Identifier::Local::CatalogNumber', ns, true)
ns = ::Namespace.where(short_name: query_id_namespace).first if query_id_namespace.present?
::CollectionObject.with_identifier_type_and_namespace('Identifier::Local::CatalogNumber', ns, true)
.where('CAST(identifiers.identifier AS integer) between ? and ?',
query_range_start.to_i, query_range_stop.to_i)
end
Expand All @@ -128,11 +129,11 @@ def user_date_scope

scope = case query_date_type_select
when 'created_at', nil
CollectionObject.created_in_date_range(@user_date_start, @user_date_end)
::CollectionObject.created_in_date_range(@user_date_start, @user_date_end)
when 'updated_at'
CollectionObject.updated_in_date_range(@user_date_start, @user_date_end)
::CollectionObject.updated_in_date_range(@user_date_start, @user_date_end)
else
CollectionObject.all
::CollectionObject.all
end

unless query_user == 'All users' || query_user == 0
Expand Down Expand Up @@ -163,8 +164,8 @@ def applied_scopes

# @return [Scope]
def result
return CollectionObject.none if applied_scopes.empty?
a = CollectionObject.all
return ::CollectionObject.none if applied_scopes.empty?
a = ::CollectionObject.all
applied_scopes.each do |scope|
a = a.merge(self.send(scope))
end
Expand Down
4 changes: 2 additions & 2 deletions lib/queries/container_autocomplete_query.rb
Expand Up @@ -8,12 +8,12 @@ def where_sql

# @return [Scope]
def result
Container.includes(:identifiers).where(where_sql).references(:identifiers)
::Container.includes(:identifiers).where(where_sql).references(:identifiers)
end

# @return [Arel::Table]
def table
Container.arel_table
::Container.arel_table
end

end
Expand Down
13 changes: 2 additions & 11 deletions lib/queries/content_filter_query.rb
Expand Up @@ -40,7 +40,7 @@ def where_sql

# @return [Scope]
def all
q = Content.includes(:otu, :topic)
q = ::Content.includes(:otu, :topic)
s = where_sql
q = q.where(s).references(:topics, :otus) if s
q = q.order(updated_at: :desc).limit(most_recent_updates) unless most_recent_updates.zero?
Expand All @@ -49,7 +49,7 @@ def all

# @return [Arel::Table]
def table
Content.arel_table
::Content.arel_table
end

# @return [Arel::Nodes::Equatity]
Expand All @@ -66,15 +66,6 @@ def for_otu
def recent
table[:updated_at].gt(hours_ago.hours.ago) if hours_ago
end

#def recently_updated
# if most_recent_updates.nonzero?
# table['id'].eq_any(
# Content.order(table['updated_at']).take(most_recent_updates)
# )
# end
#end

end

end
@@ -1,6 +1,6 @@
module Queries

class DocumentAutocompleteQuery < Queries::Query
class Document::Autocomplete < Queries::Query

# @return [String]
def where_sql
Expand Down Expand Up @@ -28,12 +28,12 @@ def file_name_like

# @return [Scope]
def all
Document.where(where_sql).limit(200).order(:id)
::Document.where(where_sql).limit(200).order(:id)
end

# @return [Arel::Table]
def table
Document.arel_table
::Document.arel_table
end

end
Expand Down
22 changes: 12 additions & 10 deletions lib/queries/otu/filter.rb
Expand Up @@ -131,17 +131,17 @@ def geographic_area_scope

geographic_area_ids.each do |ga_id|
target_geographic_item_ids.push(
GeographicArea.joins(:geographic_items).find(ga_id).default_geographic_item.id
::GeographicArea.joins(:geographic_items).find(ga_id).default_geographic_item.id
)
end

gi_sql = GeographicItem.contained_by_where_sql(target_geographic_item_ids)
gi_sql = ::GeographicItem.contained_by_where_sql(target_geographic_item_ids)

::Otu.where(id: (::Otu.joins(:asserted_distributions)
.where(asserted_distributions: {id: AssertedDistribution.joins(:geographic_items)
.where(asserted_distributions: {id: ::AssertedDistribution.joins(:geographic_items)
.where(gi_sql).distinct})) +
(::Otu.joins(:collection_objects)
.where(collection_objects: {id: CollectionObject.joins(:geographic_items)
.where(collection_objects: {id: ::CollectionObject.joins(:geographic_items)
.where(gi_sql).distinct})).distinct)


Expand All @@ -155,14 +155,16 @@ def geographic_area_scope
#
def shape_scope
::Otu.where(id: (::Otu.joins(:asserted_distributions)
.where(asserted_distributions: {id: GeographicItem.gather_map_data(shape,
'AssertedDistribution',
project_id)
.where(asserted_distributions: {id: ::GeographicItem.gather_map_data(
shape,
'AssertedDistribution',
project_id)
.distinct}) +
::Otu.joins(:collection_objects)
.where(collection_objects: {id: GeographicItem.gather_map_data(shape,
'CollectionObject',
project_id)
.where(collection_objects: {id: ::GeographicItem.gather_map_data(
shape,
'CollectionObject',
project_id)
.distinct}))
.uniq)
end
Expand Down
@@ -1,6 +1,6 @@
module Queries

class RepositoryAutocompleteQuery < Queries::Query
class Repository::Autocomplete < Queries::Query

# @return [String]
def where_sql
Expand All @@ -9,12 +9,12 @@ def where_sql

# @return [Scope]
def all
Repository.where(where_sql).order('name, char_length(name)').limit(40)
::Repository.where(where_sql).order('name, char_length(name)').limit(40)
end

# @return [Arel::Table]
def table
Repository.arel_table
::Repository.arel_table
end

# @return [Arel::Nodes::Matches]
Expand Down
2 changes: 1 addition & 1 deletion lib/queries/source/autocomplete.rb
Expand Up @@ -202,7 +202,7 @@ def table

# @return [Arel::Table]
def project_sources_table
ProjectSource.arel_table
::ProjectSource.arel_table
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/queries/taxon_name_relationships_filter_query.rb
Expand Up @@ -79,7 +79,7 @@ def where_sql

# @return [Arel::Table]
def table
TaxonNameRelationship.arel_table
::TaxonNameRelationship.arel_table
end

# @return [ActiveRecord::Relation, nil]
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/queries/collection_object_filter_query_spec.rb
Expand Up @@ -160,7 +160,7 @@
# 2 from previous (simple_world), and (2 + 10) generated above
expect(result.count).to eq(14)

params = {user: 'All users', date_type_select: 'created_at',
params = {user: 'All users', date_type_select: 'created_at',
user_date_range_start: '2005-01-01', user_date_range_end: Date.yesterday.to_s}

result = Queries::CollectionObjectFilterQuery.new(params).result
Expand Down

0 comments on commit 4e9e1bc

Please sign in to comment.