Class: Lolita::Configuration::NestedList

Inherits:
List show all
Defined in:
lib/lolita/configuration/nested_list.rb

Instance Attribute Summary collapse

Attributes inherited from List

#initialized_attributes, #page_criteria, #title

Attributes inherited from Base

#component, #dbi

Instance Method Summary collapse

Methods inherited from List

#action, #by_path, #column, #columns, #columns=, #filter, #filter?, #list, #paginate, #search

Methods included from Builder

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

Constructor Details

#initialize(dbi, parent, options = {}, &block) ⇒ NestedList

Returns a new instance of NestedList.



9
10
11
12
13
14
15
16
17
# File 'lib/lolita/configuration/nested_list.rb', line 9

def initialize dbi,parent,options={},&block
  init_nested_list_attributes(parent)
  set_and_validate_dbi(dbi)
  set_list_attributes do
    set_attributes(options)
    self.instance_eval(&block) if block_given?
  end
  validate
end

Instance Attribute Details

#association_nameObject

Returns the value of attribute association_name.



7
8
9
# File 'lib/lolita/configuration/nested_list.rb', line 7

def association_name
  @association_name
end

#parentObject

Returns the value of attribute parent.



7
8
9
# File 'lib/lolita/configuration/nested_list.rb', line 7

def parent
  @parent
end

Instance Method Details

#associationObject

Returns association with given name from parent object DBI



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

def association
  self.parent && self.parent.dbi.reflect_on_association(self.association_name)
end

#depthObject

Return how deep is this list, starging with 1.



82
83
84
# File 'lib/lolita/configuration/nested_list.rb', line 82

def depth
  self.parents.size
end

#mappingObject

Return mapping that class matches dbi klass.



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/lolita/configuration/nested_list.rb', line 25

def mapping
  if @mapping.nil?
    mapping_class = if self.association && self.association.macro == :one
      self.parent.dbi.klass
    else
      dbi.klass
    end
    @mapping = Lolita::Mapping.new(:"#{mapping_class.to_s.downcase.pluralize}") || false
  end
  @mapping
end

#nested_options_for(record) ⇒ Object

Return Hash with key :nested thas is Hash with one key that is foreign key that links parent with this list.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/lolita/configuration/nested_list.rb', line 55

def nested_options_for(record)
  if self.parent
    association = self.association
    attr_name = [:one,:many_to_many].include?(association.macro) ? :id : association.foreign_key
    association_record = if association.through? && record.send(association.through)
      record.send(association.through)
    elsif association.macro == :one
      record.send(association.name)
    else
      record
    end
    attr_value = association_record && association_record.id
    base_options = {
      attr_name => attr_value,
      :parent => self.root.dbi.klass.to_s,
      :path => self.parents.map{|parent| parent.is_a?(Lolita::Configuration::List) ? "l_" : "c_#{parent.name}"}
    }
    if association.macro == :many_to_many
      base_options.merge({
        :association => association.name
      })
    end
    {:nested => base_options}
  end
end

#parentsObject

Return all parent object where first is self.parent and last is root list or column.



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/lolita/configuration/nested_list.rb', line 38

def parents
  unless @parents
    @parents = []
    object = self
    while object.respond_to?(:parent) && object.parent
      @parents << object.parent
      object = object.parent
    end
  end
  @parents
end

#rootObject



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

def root
  parents.last
end