Skip to content

Commit

Permalink
Refactor SQL to ActiveRecord
Browse files Browse the repository at this point in the history
This commit refactors the sql query into an ActiveRecord query but left
notes just in case we need it again later.
  • Loading branch information
kirkkwang committed Mar 9, 2023
1 parent 9a63961 commit 6664c1b
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions lib/iiif_print.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,16 +146,23 @@ def self.allinson_flex_fields_for(_work, fields: allinson_flex_fields)
end
end

# sql query method was refactored to active record
# original query:
# AllinsonFlex::ProfileProperty
# .find_by_sql(
# "SELECT DISTINCT allinson_flex_profile_texts.value AS label, " \
# "allinson_flex_profile_properties.name AS name " \
# "FROM allinson_flex_profile_properties " \
# "JOIN allinson_flex_profile_texts " \
# "ON allinson_flex_profile_properties.id = " \
# "allinson_flex_profile_texts.profile_property_id " \
# "WHERE allinson_flex_profile_texts.name = 'display_label'"
# )
def self.allinson_flex_fields
AllinsonFlex::ProfileProperty
.find_by_sql(
"SELECT DISTINCT allinson_flex_profile_texts.value AS label, " \
"allinson_flex_profile_properties.name AS name " \
"FROM allinson_flex_profile_properties " \
"JOIN allinson_flex_profile_texts " \
"ON allinson_flex_profile_properties.id = " \
"allinson_flex_profile_texts.profile_property_id " \
"WHERE allinson_flex_profile_texts.name = 'display_label'"
)
@allinson_flex_fields ||= AllinsonFlex::ProfileProperty
.joins(:texts)
.where(allinson_flex_profile_texts: { name: 'display_label' })
.distinct
.select(:name, 'allinson_flex_profile_texts.value as label')
end
end

0 comments on commit 6664c1b

Please sign in to comment.