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

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

Returns:

  • (Boolean)


53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/active_interaction/extras/model_fields.rb', line 53

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

    given?(field) && value_changed
  end
end

#changed_model_fields(model_name) ⇒ Object

returns hash of only changed model fields and their values



11
12
13
14
15
# File 'lib/active_interaction/extras/model_fields.rb', line 11

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



18
19
20
21
22
# File 'lib/active_interaction/extras/model_fields.rb', line 18

def given_model_fields(model_name)
  model_fields(model_name).select do |field, _value|
    given?(field)
  end
end

#model_fields(model_name) ⇒ Object

returns hash of all model fields and their values



5
6
7
8
# File 'lib/active_interaction/extras/model_fields.rb', line 5

def model_fields(model_name)
  fields = self.class.model_field_cache[model_name]
  inputs.slice(*fields)
end

#populate_filters(_inputs) ⇒ Object

overwritten to pre-populate model fields



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/active_interaction/extras/model_fields.rb', line 67

def populate_filters(_inputs)
  super.tap do
    self.class.filters.each do |name, filter|
      next if given?(name)

      model_field = self.class.model_field_cache_inverse[name]
      next if model_field.nil?

      value = public_send(model_field)&.public_send(name)
      public_send("#{name}=", filter.clean(value, self))
    end
  end
end