Module: Reform::Form::ActiveModel::ClassMethods

Defined in:
lib/reform/form/active_model.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object

this module is only meant to extend (not include). # DISCUSS: is this a sustainable concept?



23
24
25
26
27
28
# File 'lib/reform/form/active_model.rb', line 23

def self.extended(base)
  base.class_eval do
    extend Uber::InheritableAttribute
    inheritable_attr :model_options
  end
end

Instance Method Details

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

Set a model name for this form if the infered is wrong.

class CoverSongForm < Reform::Form
  model :song

or we can setup a isolated namespace model ( which defined in isolated rails egine )

class CoverSongForm < Reform::Form
  model "api/v1/song", namespace: "api"


55
56
57
# File 'lib/reform/form/active_model.rb', line 55

def model(main_model, options={})
  self.model_options = [main_model, options]
end

#model_nameObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/reform/form/active_model.rb', line 59

def model_name
  if model_options
    form_name = model_options.first.to_s.camelize
    namespace = model_options.last[:namespace].present? ? model_options.last[:namespace].to_s.camelize.constantize : nil
  else
    if name
      form_name = name.sub(/(::)?Form$/, "") # Song::Form => "Song"
      namespace = nil
    else # anonymous forms. let's drop AM and forget about all this.
      form_name = "reform"
      namespace = nil
    end
  end

  active_model_name_for(form_name, namespace)
end

#process_inline!(mod, definition) ⇒ Object

DISCUSS: can we achieve that somehow via features in build_inline?



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/reform/form/active_model.rb', line 31

def process_inline!(mod, definition)
  _name = definition.name
  mod.instance_eval do
    @_name = _name.singularize.camelize
    # this adds Form::name for AM::Validations and I18N.
    # i have a feeling that this is also needed for Rails autoloader, which is scary.
    # beside that, it is a nice way for debugging to find out which anonymous form class you're working on:
    #   anonymous_nested_form.class.name
    def name
      @_name
    end
  end
end