Module: TransForms::MainModel::Active

Extended by:
ActiveSupport::Concern
Defined in:
lib/trans_forms/main_model.rb

Instance Method Summary collapse

Instance Method Details

#errorsObject

Combines the errors from the FormModel as well as the main model instances



46
47
48
# File 'lib/trans_forms/main_model.rb', line 46

def errors
  @errors ||= FormErrors.new(self, super)
end

#main_instanceObject

Called from FormError to collect error messages from all possible models involved in the form transation.



41
42
43
# File 'lib/trans_forms/main_model.rb', line 41

def main_instance
  send main_model
end

#model=(model) ⇒ Object

In it’s default implementation, this method will look at the class name and try to match it to an attribute defined in the form model. If a match is found, then it will assign the model to that attribute.

This method is encouraged to overwrite when there is custom conditions for how to handle the assignments.

For example:

class UserUpdater < ApplicationTransForm
  attr_accessor :admin
  set_main_model :user

  def model=(model)
    if model.role == :admin
      self.admin = model
    else
      self.user = model
    end
  end

  # ...
end


74
75
76
77
78
79
80
81
# File 'lib/trans_forms/main_model.rb', line 74

def model=(model)
  element = to_element(model)
  if respond_to?("#{element}=")
    send("#{element}=", model)
  else
    raise TransForms::NotImplementedError
  end
end