class Lolita::Configuration::NestedForm

Accept those attributes

by default, it is expandable if association macro is :many

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

build_method[W]
expandable[RW]
field_rejection_proc[RW]
field_style[R]
name[RW]
options[R]
parent[R]

Public Class Methods

new(parent,name=nil, options ={}) click to toggle source
# 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

allow_destroy?() click to toggle source
# File lib/lolita/configuration/nested_form.rb, line 31
def allow_destroy?
  dbi.klass.nested_attributes_options[name][:allow_destroy]
end
as_field() click to toggle source

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
build_method() click to toggle source
# File lib/lolita/configuration/nested_form.rb, line 39
def build_method
  @build_method || self.name
end
dbi() click to toggle source

Parent (a.k.a tab) dbi

# File lib/lolita/configuration/nested_form.rb, line 61
def dbi
  @parent.dbi
end
expandable?() click to toggle source

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
field_style=(value) click to toggle source

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
fields() click to toggle source

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=(new_fields) click to toggle source

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
klass() click to toggle source

Parent (tab) dbi klass

# File lib/lolita/configuration/nested_form.rb, line 83
def klass
  dbi.reflect_on_association(name).klass
end
macro() click to toggle source

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
update_only?() click to toggle source
# File lib/lolita/configuration/nested_form.rb, line 35
def update_only?
  dbi.klass.nested_attributes_options[name][:update_only]
end

Private Instance Methods

next_nested_form() click to toggle source
# File lib/lolita/configuration/nested_form.rb, line 94
def next_nested_form
  @@last_nested_form+=1
end
set_attributes_from(options) click to toggle source
# File lib/lolita/configuration/nested_form.rb, line 98
def set_attributes_from(options)
  options.each{|key,value|
    instance_variable_set(:"@#{key}",value)
  }
end