Class: Lolita::Configuration::NestedForm

Inherits:
Object
  • Object
show all
Includes:
Builder
Defined in:
lib/lolita/configuration/nested_form.rb

Overview

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

Constant Summary collapse

@@last_nested_form =
0

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Builder

#build, #builder, #builder=, #builder_default_name, #builder_default_options, #builder_default_state

Constructor Details

#initialize(parent, name = nil, options = {}) ⇒ NestedForm

Returns a new instance of NestedForm.



24
25
26
27
28
29
# 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

Instance Attribute Details

#build_methodObject



39
40
41
# File 'lib/lolita/configuration/nested_form.rb', line 39

def build_method
  @build_method || self.name
end

#expandableObject

Returns the value of attribute expandable.



21
22
23
# File 'lib/lolita/configuration/nested_form.rb', line 21

def expandable
  @expandable
end

#field_rejection_procObject

Returns the value of attribute field_rejection_proc.



21
22
23
# File 'lib/lolita/configuration/nested_form.rb', line 21

def field_rejection_proc
  @field_rejection_proc
end

#field_styleObject

Returns the value of attribute field_style.



20
21
22
# File 'lib/lolita/configuration/nested_form.rb', line 20

def field_style
  @field_style
end

#nameObject

Returns the value of attribute name.



21
22
23
# File 'lib/lolita/configuration/nested_form.rb', line 21

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



20
21
22
# File 'lib/lolita/configuration/nested_form.rb', line 20

def options
  @options
end

#parentObject (readonly)

Returns the value of attribute parent.



20
21
22
# File 'lib/lolita/configuration/nested_form.rb', line 20

def parent
  @parent
end

Instance Method Details

#allow_destroy?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/lolita/configuration/nested_form.rb', line 31

def allow_destroy?
  dbi.klass.nested_attributes_options[name][:allow_destroy]
end

#as_fieldObject

Create field, that is not real field, but represents nested attributes as one. It is used to create label.



56
57
58
# File 'lib/lolita/configuration/nested_form.rb', line 56

def as_field
  Lolita::Configuration::Factory::Field.add(dbi,self.name, :string)
end

#dbiObject

Parent (a.k.a tab) dbi



61
62
63
# File 'lib/lolita/configuration/nested_form.rb', line 61

def dbi
  @parent.dbi
end

#expandable?Boolean

Detect if it’s possible to add more than one field group, like if model has many other objects.

Returns:

  • (Boolean)


50
51
52
# File 'lib/lolita/configuration/nested_form.rb', line 50

def expandable?
  @expandable == true || (@expandable == nil && macro == :many)
end

#fieldsObject

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.



72
73
74
75
76
77
78
79
80
# 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) ⇒ Object

Fields setter. Fields should be array and each element should be Lolita::Configuration::Field object.



66
67
68
# File 'lib/lolita/configuration/nested_form.rb', line 66

def fields=(new_fields)
  @fields = new_fields
end

#klassObject

Parent (tab) dbi klass



83
84
85
# File 'lib/lolita/configuration/nested_form.rb', line 83

def klass
  dbi.reflect_on_association(name).klass
end

#macroObject

Parent (tab) dbi klass reflection with #name and macros of that.



88
89
90
# File 'lib/lolita/configuration/nested_form.rb', line 88

def macro
  dbi.reflect_on_association(name).macro
end

#update_only?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/lolita/configuration/nested_form.rb', line 35

def update_only?
  dbi.klass.nested_attributes_options[name][:update_only]
end