module ActiveInteraction::Extras::ModelFields

Public Instance Methods

any_changed?(*fields) click to toggle source

checks if value was given to the service and the value is different from the one on the model

# File lib/active_interaction/extras/model_fields.rb, line 57
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) click to toggle source

returns hash of only changed model fields and their values

# 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) click to toggle source

returns hash of only given model fields and their values

# 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_field_cache() click to toggle source
# File lib/active_interaction/extras/model_fields.rb, line 86
def model_field_cache
  @model_field_cache ||= Hash.new { [] }
end
model_field_cache_inverse() click to toggle source
# File lib/active_interaction/extras/model_fields.rb, line 90
def model_field_cache_inverse
  @model_field_cache_inverse ||= model_field_cache.each_with_object({}) do |(model, fields), result|
    fields.each do |field|
      result[field] = model
    end
  end
end
model_fields(model_name) click to toggle source

returns hash of all model fields and their values

# 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_and_inputs(_inputs) click to toggle source

overwritten to pre-populate model fields

Calls superclass method
# File lib/active_interaction/extras/model_fields.rb, line 71
def populate_filters_and_inputs(_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