Skip to content

Commit

Permalink
Use ActiveRecord to do the boolean casting
Browse files Browse the repository at this point in the history
As suggested in #2929 (comment)

Follows the same pattern as in FinderSchema
  • Loading branch information
ChrisBAshton committed Jan 6, 2025
1 parent 910c706 commit 2901c3b
Showing 1 changed file with 12 additions and 21 deletions.
33 changes: 12 additions & 21 deletions app/models/facet.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
class Facet
include ActiveModel::Model
include ActiveModel::Attributes

attr_accessor(
:key,
:name,
:short_name,
:type,
:preposition,
:display_as_result_metadata,
:filterable,
:allowed_values,
:specialist_publisher_properties,
)
attribute :key
attribute :name
attribute :short_name
attribute :type
attribute :preposition
attribute :display_as_result_metadata, :boolean
attribute :filterable, :boolean
attribute :allowed_values
attribute :specialist_publisher_properties

def to_finder_schema_attributes
{
Expand All @@ -35,8 +34,8 @@ def from_finder_admin_form_params(params)
facet.short_name = nil_if_blank(params["short_name"])
facet.type = facet_type(params["type"])
facet.preposition = nil_if_blank(params["preposition"])
facet.display_as_result_metadata = str_to_bool(params["display_as_result_metadata"])
facet.filterable = str_to_bool(params["filterable"])
facet.display_as_result_metadata = params["display_as_result_metadata"]
facet.filterable = params["filterable"]
facet.allowed_values = facet_allowed_values(params["allowed_values"], params["type"])
facet.specialist_publisher_properties = facet_specialist_publisher_properties(params["type"])
facet
Expand All @@ -52,14 +51,6 @@ def nil_if_blank(str)
str.presence
end

def str_to_bool(str)
if str == "true"
true
elsif str == "false"
false
end
end

def facet_type(type)
facet_types_that_allow_enum_values.include?(type) ? "text" : type
end
Expand Down

0 comments on commit 2901c3b

Please sign in to comment.