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?



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

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"


69
70
71
# File 'lib/reform/form/active_model.rb', line 69

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

#model_nameObject



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

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?



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

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



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

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



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

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

#validates_each(*args, &block) ⇒ Object



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

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

#validates_with(*args, &block) ⇒ Object



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

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