- C
- D
- I
- J
Instance Public methods
coerce_boolean_values() Link
#### Example
class MyModel < ApplicationRecord
include Settings::Util::JsonBooleans
json_booleans members: :enable_signup
end
MyModel.new.members['enable_signup'] = '0' #=> saves members['enable_signup'] = false
MyModel.new.members['enable_signup'] = '1' #=> saves members['enable_signup'] = true
Source: show
# File app/models/concerns/settings/util/json_booleans.rb, line 108 def coerce_boolean_values # :doc: # # FIXME: A single use-case of a "legacy customer" spec # needs the members->enable_signup key to stay nil # guarding against nil on all keys works for now # self.class.boolean_keys_map.each do |attribute, keys| Array(keys) .select { |k| public_send(attribute)[k.to_s].present? } .each { |k| public_send(attribute)[k.to_s] = is_true?(attribute, k) } end end
define_instance_method_getters() Link
#### Example
class MyModel < ApplicationRecord
include Settings::Util::JsonBooleans
json_booleans members: :enable_signup
end
MyModel.new.enable_signup? #=> is_true?(:members, :enable_signup)
MyModel.new.members_enable_signup? #=> is_true?(:members, :enable_signup)
Source: show
# File app/models/concerns/settings/util/json_booleans.rb, line 61 def define_instance_method_getters # :doc: boolean_keys_map.each do |attribute, keys| Array(keys).each do |key| define_method("#{key}?") { is_true?(attribute, key) } alias_method "#{attribute}_#{key}?", "#{key}?" end end end
define_instance_method_setters() Link
#### Example
class MyModel < ApplicationRecord
include Settings::Util::JsonBooleans
json_booleans members: :enable_signup
end
MyModel.new.enable_signup = true #=> instance.enable_signup? #=> true
MyModel.new.members_enable_signup = false #=> instance.enable_signup? #=> false
MyModel.new.enable_signup = '0' #=> false
MyModel.new.enable_signup = '1' #=> true
Source: show
# File app/models/concerns/settings/util/json_booleans.rb, line 85 def define_instance_method_setters # :doc: boolean_keys_map.each do |attribute, keys| Array(keys).each do |key| define_method("#{key}=") do |value| self[attribute][key] = ActiveModel::Type::Boolean.new.cast(value) end end end end
is_true?(attribute, key) Link
Source: show
# File app/models/concerns/settings/util/json_booleans.rb, line 122 def is_true?(attribute, key) # :doc: ActiveModel::Type::Boolean.new.cast(public_send(attribute)[key.to_s]) end
json_booleans(map) Link
Helps with coercing and reading booleans in ActiveRecord model json fields
#### Example
class MyModel < ApplicationRecord
include Settings::Util::JsonBooleans
json_booleans({
members: :enable_signup,
retention_options: %w[discount_coupon extend_trial]
})
end
Each given root key (members, retention_options in the example above) must refer to to a json attribute on the model
The primary purpose of this utlity is for coercing the given json keys before validation
A secondary gain is getting my_json_key_name? style helper methods that work on any json keys mapped in the example above, by giving them behavior similar to how proper ActiveRecord boolean fields work
The json key will map directly as a question? method onto the model, but will preserve the value as a key inside the parent database field. The examples on the private methods below should help to illuminate the idea.
private methods documented as a courtesy, please see their examples for how this utility helps
Source: show
# File app/models/concerns/settings/util/json_booleans.rb, line 40 def json_booleans(map) self.boolean_keys_map = boolean_keys_map.deep_merge(map) define_instance_method_getters define_instance_method_setters after_initialize :coerce_boolean_values before_validation :coerce_boolean_values end