Skip to content

Commit

Permalink
Merge pull request #2451 from MushroomObserver/query-flavors
Browse files Browse the repository at this point in the history
Start to refactor Query flavors
  • Loading branch information
nimmolo authored Oct 14, 2024
2 parents f65b414 + a7cb166 commit 8a8b83b
Show file tree
Hide file tree
Showing 75 changed files with 498 additions and 805 deletions.
2 changes: 1 addition & 1 deletion app/classes/api2/comment_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def update_params
end

def query_flavor
@target ? :for_target : :all
:all
end

def validate_create_params!(params)
Expand Down
4 changes: 2 additions & 2 deletions app/classes/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
# Each model has a default search flavor (:default), which is used by the prev
# and next actions when the specified query no longer exists. For example, if
# you click on an observation from the main index, prev and next travserse the
# results of an :Observation :by_rss_log query. If the user comes back a day
# later, this query will have been culled by the garbage collector (see
# results of an :Observation :all by: :rss_log query. If the user comes back
# a day later, this query will have been culled by the garbage collector (see
# below), so prev and next need to be able to create a default query on the
# fly. In this case it may be :Observation :all (see default_flavors array
# below).
Expand Down
2 changes: 2 additions & 0 deletions app/classes/query/article_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ def parameter_declarations
created_at?: [:time],
updated_at?: [:time],
users?: [User],
ids?: [Article],
title_has?: :string,
body_has?: :string
)
end

def initialize_flavor
add_owner_and_time_stamp_conditions("articles")
add_ids_condition
add_search_condition("articles.title", params[:title_has])
add_search_condition("articles.body", params[:body_has])
super
Expand Down
12 changes: 0 additions & 12 deletions app/classes/query/article_by_rss_log.rb

This file was deleted.

14 changes: 0 additions & 14 deletions app/classes/query/article_in_set.rb

This file was deleted.

23 changes: 23 additions & 0 deletions app/classes/query/collection_number_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,21 @@ def parameter_declarations
created_at?: [:time],
updated_at?: [:time],
users?: [User],
observation?: Observation,
observations?: [:string],
pattern?: :string,
name?: [:string],
number?: [:string],
name_has?: :string,
number_has?: :string
)
end

# rubocop:disable Metrics/AbcSize
def initialize_flavor
add_owner_and_time_stamp_conditions("collection_numbers")
add_for_observation_condition
add_pattern_condition
add_id_condition("observation_collection_numbers.observation_id",
params[:observations], :observation_collection_numbers)
add_exact_match_condition("collection_numbers.name", params[:name])
Expand All @@ -28,6 +33,24 @@ def initialize_flavor
add_search_condition("collection_numbers.number", params[:number_has])
super
end
# rubocop:enable Metrics/AbcSize

def add_for_observation_condition
return if params[:observation].blank?

obs = find_cached_parameter_instance(Observation, :observation)
@title_tag = :query_title_for_observation
@title_args[:observation] = obs.unique_format_name
where << "observation_collection_numbers.observation_id = '#{obs.id}'"
add_join(:observation_collection_numbers)
end

def search_fields
"CONCAT(" \
"collection_numbers.name," \
"collection_numbers.number" \
")"
end

def self.default_order
"name_and_number"
Expand Down
17 changes: 0 additions & 17 deletions app/classes/query/collection_number_for_observation.rb

This file was deleted.

21 changes: 0 additions & 21 deletions app/classes/query/collection_number_pattern_search.rb

This file was deleted.

48 changes: 47 additions & 1 deletion app/classes/query/comment_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,68 @@ def parameter_declarations
super.merge(
created_at?: [:time],
updated_at?: [:time],
ids?: [Comment],
by_user?: User,
for_user?: User,
users?: [User],
types?: [{ string: Comment.all_type_tags }],
summary_has?: :string,
content_has?: :string
content_has?: :string,
pattern?: :string,
target?: AbstractModel,
type?: :string
)
end

def initialize_flavor
add_owner_and_time_stamp_conditions("comments")
add_by_user_condition("comments")
add_ids_condition
add_for_user_condition
add_for_target_condition
add_pattern_condition
add_string_enum_condition("comments.target_type", params[:types],
Comment.all_type_tags)
add_search_condition("comments.summary", params[:summary_has])
add_search_condition("comments.comment", params[:content_has])
super
end

def add_for_user_condition
return if params[:for_user].blank?

user = find_cached_parameter_instance(User, :for_user)
@title_tag = :query_title_for_user
@title_args[:user] = user.legal_name
add_join(:observations)
where << "observations.user_id = '#{params[:for_user]}'"
end

def add_for_target_condition
return if params[:target].blank? || params[:type].blank?

target = target_instance
@title_tag = :query_title_for_target
@title_args[:object] = target.unique_format_name
where << "comments.target_id = '#{target.id}'"
where << "comments.target_type = '#{target.class.name}'"
end

def target_instance
unless (type = Comment.safe_model_from_name(params[:type]))
raise("The model #{params[:type].inspect} does not support comments!")
end

find_cached_parameter_instance(type, :target)
end

def search_fields
"CONCAT(" \
"comments.summary," \
"COALESCE(comments.comment,'')" \
")"
end

def self.default_order
"created_at"
end
Expand Down
16 changes: 0 additions & 16 deletions app/classes/query/comment_by_user.rb

This file was deleted.

26 changes: 0 additions & 26 deletions app/classes/query/comment_for_target.rb

This file was deleted.

17 changes: 0 additions & 17 deletions app/classes/query/comment_for_user.rb

This file was deleted.

14 changes: 0 additions & 14 deletions app/classes/query/comment_in_set.rb

This file was deleted.

21 changes: 0 additions & 21 deletions app/classes/query/comment_pattern_search.rb

This file was deleted.

11 changes: 10 additions & 1 deletion app/classes/query/glossary_term_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,27 @@ def parameter_declarations
updated_at?: [:time],
users?: [User],
name_has?: :string,
description_has?: :string
description_has?: :string,
pattern?: :string
)
end

def initialize_flavor
add_owner_and_time_stamp_conditions("glossary_terms")
add_pattern_condition
add_search_condition("glossary_terms.name", params[:name_has])
add_search_condition("glossary_terms.description",
params[:description_has])
super
end

def search_fields
"CONCAT(" \
"glossary_terms.name," \
"COALESCE(glossary_terms.description,'')" \
")"
end

def self.default_order
"name"
end
Expand Down
23 changes: 0 additions & 23 deletions app/classes/query/glossary_term_pattern_search.rb

This file was deleted.

Loading

0 comments on commit 8a8b83b

Please sign in to comment.