-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/performant-software/user-…
…defined-fields into feature/udf35_fuzzy_dates
- Loading branch information
Showing
2 changed files
with
48 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |