module Reform::Form::ActiveModel::ClassMethods
Public Class Methods
extended(base)
click to toggle source
this module is only meant to extend (not include). # DISCUSS: is this a sustainable concept?
# File lib/reform/form/active_model.rb, line 22 def self.extended(base) base.class_eval do extend Uber::InheritableAttribute inheritable_attr :model_options end end
Public Instance Methods
model(main_model, options={})
click to toggle source
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"
# File lib/reform/form/active_model.rb, line 70 def model(main_model, options={}) self.model_options = [main_model, options] end
model_name()
click to toggle source
# File lib/reform/form/active_model.rb, line 74 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
name()
click to toggle source
this adds Form::name for AM::Validations and I18N.
# File lib/reform/form/active_model.rb, line 37 def name @_name end
property(*)
click to toggle source
DISCUSS: can we achieve that somehow via features in build_inline?
Calls superclass method
# File lib/reform/form/active_model.rb, line 30 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)
click to toggle source
# File lib/reform/form/active_model.rb, line 49 def validate(*args, &block) validation(name: :default, inherit: true) { validate *args, &block } end
validates(*args, &block)
click to toggle source
moved from reform as not applicable to dry
# File lib/reform/form/active_model.rb, line 45 def validates(*args, &block) validation(name: :default, inherit: true) { validates *args, &block } end
validates_each(*args, &block)
click to toggle source
# File lib/reform/form/active_model.rb, line 57 def validates_each(*args, &block) validation(name: :default, inherit: true) { validates_each *args, &block } end
validates_with(*args, &block)
click to toggle source
# File lib/reform/form/active_model.rb, line 53 def validates_with(*args, &block) validation(name: :default, inherit: true) { validates_with *args, &block } end
Private Instance Methods
active_model_name_for(string, namespace=nil)
click to toggle source
# File lib/reform/form/active_model.rb, line 92 def active_model_name_for(string, namespace=nil) ::ActiveModel::Name.new(self, namespace, string) end