class Lolita::Configuration::NestedForm
Accept those attributes
-
:name
- Name of nested relation, like :comments. -
:field_style
- Is fields rendered with as normal (with lable and staff) or like in table (:simple). Default :simple -
:expandable
- Show or not “Add new” and “Delete” links in form,
by default, it is expandable if association macro is :many
-
:field_rejection_proc
- Proc, that contains condition of how to reject field.
By default form rejects all fields from parent tab that doesn't have current form as field nested_form
Example¶ ↑
form = Lolita::Configuration::NestedForm.new(Lolita::Configuration::Tab::Content.new,:comments) form.field_rejection_proc = Proc.new{|field| field.name.to_s.match(/_id$/) } # form exclude all fields that ends with _id
Attributes
Public Class Methods
# File lib/lolita/configuration/nested_form.rb, line 24 def initialize parent,name=nil, options ={} @parent=parent @options = options self.name=name || "nested_form_#{next_nested_form}" set_attributes_from(options) end
Public Instance Methods
# File lib/lolita/configuration/nested_form.rb, line 31 def allow_destroy? dbi.klass.nested_attributes_options[name][:allow_destroy] end
Create field, that is not real field, but represents nested attributes as one. It is used to create label.
# File lib/lolita/configuration/nested_form.rb, line 56 def as_field Lolita::Configuration::Factory::Field.add(dbi,self.name, :string) end
# File lib/lolita/configuration/nested_form.rb, line 39 def build_method @build_method || self.name end
Parent (a.k.a tab) dbi
# File lib/lolita/configuration/nested_form.rb, line 61 def dbi @parent.dbi end
Detect if it's possible to add more than one field group, like if model has many other objects.
# File lib/lolita/configuration/nested_form.rb, line 50 def expandable? @expandable == true || (@expandable == nil && macro == :many) end
Set field style - normal or simple. Default - normal.
# File lib/lolita/configuration/nested_form.rb, line 43 def field_style=(value) allowed_values = [:normal,:simple] raise ArgumentError, "Only #{allowed_values.inspect} are allowed" unless allowed_values.include?(value) @field_style = value end
Return all fields. Each time fields ar returned from @fields if its defined or calculated by using field_rejection_proc
or collected from parent (tab) where fields nested form is same with self.
# File lib/lolita/configuration/nested_form.rb, line 72 def fields if @fields @fields elsif field_rejection_proc self.parent.fields.reject(&field_rejection_proc) else self.parent.fields.reject{|f| f.nested_form!=self} end end
Fields
setter. Fields
should be array and each element should be Lolita::Configuration::Field
object.
# File lib/lolita/configuration/nested_form.rb, line 66 def fields=(new_fields) @fields = new_fields end
Parent (tab) dbi klass
# File lib/lolita/configuration/nested_form.rb, line 83 def klass dbi.reflect_on_association(name).klass end
Parent (tab) dbi klass reflection with name
and macros of that.
# File lib/lolita/configuration/nested_form.rb, line 88 def macro dbi.reflect_on_association(name).macro end
# File lib/lolita/configuration/nested_form.rb, line 35 def update_only? dbi.klass.nested_attributes_options[name][:update_only] end
Private Instance Methods
# File lib/lolita/configuration/nested_form.rb, line 94 def next_nested_form @@last_nested_form+=1 end
# File lib/lolita/configuration/nested_form.rb, line 98 def set_attributes_from(options) options.each{|key,value| instance_variable_set(:"@#{key}",value) } end