Module: FormObject::ClassMethods
- Defined in:
- lib/form_object.rb
Instance Method Summary collapse
-
#form_object_for(resource_name, options = {}) ⇒ Object
Set up the Form Object class to be a form object for a given model.
Instance Method Details
#form_object_for(resource_name, options = {}) ⇒ Object
Set up the Form Object class to be a form object for a given model.
Example:
class NewWorkflow
include FormObject
form_object_for :workflow, :fields => [:name], :class => MyOtherClass
...
This creates a :workflow attr method and delegates methods to it for use with form_for.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/form_object.rb', line 22 def form_object_for(resource_name, ={}) # create an attribute for the underlying resource name, (eg :workflow) and make an alias method "resource" # for the attr reader attr resource_name alias_method :resource, resource_name # create a class attribute with the model name of the underlying model name so that polymorphic_url will # see the form object as if it were an instance of the underlying model object. class_attribute :model_name klass = [:class] || resource_name.to_s klass = klass.camelize.constantize unless klass.is_a?(Class) self.model_name = klass.model_name # delegate the following methods to the underlying model object, also helps polymorphic_url do the right thing # for `form_for @form_object` methods_to_delegate = [:id, :persisted?, :to_param] if [:fields] [:fields].each do |field| methods_to_delegate << "#{field}".to_sym methods_to_delegate << "#{field}=".to_sym end end delegate *methods_to_delegate, :to => resource_name include DelegateEverything if [:delegate_everything] # create a default initializer which will set the resource when a single parameter is passed in. define_method :initialize do |object=nil| instance_variable_set("@#{resource_name}", object) end end |