Module: TransForms::MainModel::ClassMethods

Defined in:
lib/trans_forms/main_model.rb

Instance Method Summary collapse

Instance Method Details

#set_main_model(model, options = {}) ⇒ Object

This method will extend the BaseForm functionality with the TransForms::MainModel::Active module.

The model argument is a symbol in the underscore format of a Class name, i.e. :post or :product_group

The options argument is a Hash that can have the following options set:

:proxy

With proxy defined, the method +model_name+, +to_key+ and
+persisted?+ will refer to the main_model and main_instance
instead of the Form Model.

class PostForm < TransForms::FormBase
  set_main_model :post, proxy: true
end

You can also configure the proxy further by defining the
+attributes+ option inside the proxy. If the value is +:all+
then it will define all the columns of the main model. But
you can also set the value to an array with the names of the
columns you wish to proxy:

class PostForm < TransForms::FormBase
  set_main_model :post, proxy: { attributes: :all }
end

class PostForm < TransForms::FormBase
  set_main_model :post, proxy: { attributes: %w(title body status) }
end


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/trans_forms/main_model.rb', line 38

def set_main_model(model, options = {})
  include TransForms::MainModel::Active

  # Stores the main_model record in a class_attribute
  self.main_model = model

  # If model is in namespace then it might be needed to specify manually
  self._class_name = options[:class_name] if options.has_key?(:class_name)

  # Defines an instance accessor for the main_model
  attr_accessor model

  # Implements proxy module that overwrites model_name method
  # to instead return an ActiveModel::Mame class for the
  # main_model class
  if options[:proxy]
    include TransForms::MainModel::Proxy

    configure_proxy options[:proxy]
  end
end