Module: ActiveInteraction::Extras::ModelFields
- Extended by:
- ActiveSupport::Concern
- Included in:
- All
- Defined in:
- lib/active_interaction/extras/model_fields.rb
Defined Under Namespace
Classes: Context
Instance Method Summary collapse
-
#any_changed?(*fields) ⇒ Boolean
checks if value was given to the service and the value is different from the one on the model.
-
#changed_model_fields(model_name) ⇒ Object
returns hash of only changed model fields and their values.
-
#given_model_fields(model_name) ⇒ Object
returns hash of only given model fields and their values.
-
#initialize ⇒ Object
overwritten to pre-populate model fields.
-
#model_fields(model_name) ⇒ Object
returns hash of all model fields and their values.
Instance Method Details
#any_changed?(*fields) ⇒ Boolean
checks if value was given to the service and the value is different from the one on the model
61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/active_interaction/extras/model_fields.rb', line 61 def any_changed?(*fields) fields.any? do |field| model_field = self.class.model_field_cache_inverse[field] value_changed = true if model_field value_changed = send(model_field).send(field) != send(field) end inputs.given?(field) && value_changed end end |
#changed_model_fields(model_name) ⇒ Object
returns hash of only changed model fields and their values
15 16 17 18 19 |
# File 'lib/active_interaction/extras/model_fields.rb', line 15 def changed_model_fields(model_name) model_fields(model_name).select do |field, _value| any_changed?(field) end end |
#given_model_fields(model_name) ⇒ Object
returns hash of only given model fields and their values
22 23 24 25 26 |
# File 'lib/active_interaction/extras/model_fields.rb', line 22 def given_model_fields(model_name) model_fields(model_name).select do |field, _value| inputs.given?(field) end end |
#initialize ⇒ Object
overwritten to pre-populate model fields
75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/active_interaction/extras/model_fields.rb', line 75 def initialize(...) super self.class.filters.each do |name, filter| next if inputs.given?(name) model_field = self.class.model_field_cache_inverse[name] next if model_field.nil? value = public_send(model_field)&.public_send(name) input = filter.process(value, self) public_send("#{name}=", input.value) end end |
#model_fields(model_name) ⇒ Object
returns hash of all model fields and their values
7 8 9 10 11 12 |
# File 'lib/active_interaction/extras/model_fields.rb', line 7 def model_fields(model_name) fields = self.class.model_field_cache[model_name] fields.to_h do |field| [field, public_send(field)] end end |