Module: TransForms::MainModel::Active

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

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#errorsObject

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



83
84
85
86
87
88
89
90
# File 'lib/trans_forms/main_model.rb', line 83

def errors
  @errors ||= ActiveModel::Errors.new(self)
  if respond_to?(:main_instance) && main_instance && main_instance.errors.any?
    main_instance.errors
  else
    @errors
  end
end

#main_instanceObject

Instance method to retrieve the current main model instance



78
79
80
# File 'lib/trans_forms/main_model.rb', line 78

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


116
117
118
119
120
121
122
123
# File 'lib/trans_forms/main_model.rb', line 116

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