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?



18
19
20
21
22
23
# File 'lib/reform/form/active_model.rb', line 18

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"


66
67
68
# File 'lib/reform/form/active_model.rb', line 66

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

#model_nameObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/reform/form/active_model.rb', line 70

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

#propertyObject

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



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/reform/form/active_model.rb', line 26

def property(*)
  super.tap do |dfn|
    return dfn unless dfn[:nested]
    _name = dfn[:name]
    dfn[:nested].instance_eval do
      @_name = _name.singularize.camelize
      # this adds Form::name for AM::Validations and I18N.
      def name
        @_name
      end
    end
  end
end

#validate(*args, &block) ⇒ Object



45
46
47
# File 'lib/reform/form/active_model.rb', line 45

def validate(*args, &block)
  validation(name: :default, inherit: true) { validate *args, &block }
end

#validates(*args, &block) ⇒ Object

moved from reform as not applicable to dry



41
42
43
# File 'lib/reform/form/active_model.rb', line 41

def validates(*args, &block)
  validation(name: :default, inherit: true) { validates *args, &block }
end

#validates_each(*args, &block) ⇒ Object



53
54
55
# File 'lib/reform/form/active_model.rb', line 53

def validates_each(*args, &block)
  validation(name: :default, inherit: true) { validates_each *args, &block }
end

#validates_with(*args, &block) ⇒ Object



49
50
51
# File 'lib/reform/form/active_model.rb', line 49

def validates_with(*args, &block)
  validation(name: :default, inherit: true) { validates_with *args, &block }
end