Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…defined-fields into feature/udf35_fuzzy_dates
  • Loading branch information
dleadbetter committed Sep 11, 2024
2 parents d3803c8 + e7f9431 commit 747154c
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def apply_filters(query)
query = super

query = filter_defineable(query)

query = filter_required(query)
query = filter_table(query)

query
Expand All @@ -22,6 +22,12 @@ def filter_defineable(query)
query.where(defineable_id: params[:defineable_id], defineable_type: params[:defineable_type])
end

def filter_required(query)
return query unless params[:required].to_s.to_bool

query.where(required: true)
end

def filter_table(query)
return query unless params[:table_name].present?

Expand Down
41 changes: 41 additions & 0 deletions app/services/user_defined_fields/converter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
module UserDefinedFields
module Converter
extend ActiveSupport::Concern

included do
def convert_value(user_defined_field, value)
return nil unless value.present?

if user_defined_field.data_type == UserDefinedField::DATA_TYPES[:boolean]
value.to_bool
elsif user_defined_field.data_type == UserDefinedField::DATA_TYPES[:date]
convert_date(value)
elsif user_defined_field.data_type == UserDefinedField::DATA_TYPES[:number]
value.to_i
elsif user_defined_field.data_type == UserDefinedField::DATA_TYPES[:select] && user_defined_field.allow_multiple
convert_array(value)
else
value
end
end

private

def convert_array(value)
begin
JSON.parse(value)
rescue StandardError
nil
end
end

def convert_date(value)
begin
DateTime.parse(value)&.to_time&.to_i
rescue Date::Error
nil
end
end
end
end
end

0 comments on commit 747154c

Please sign in to comment.