class Lolita::Configuration::NestedList

Attributes

association_name[RW]
parent[RW]

Public Class Methods

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

Public Instance Methods

association() click to toggle source

Returns association with given name from parent object DBI

# File lib/lolita/configuration/nested_list.rb, line 20
def association
  self.parent && self.parent.dbi.reflect_on_association(self.association_name)
end
depth() click to toggle source

Return how deep is this list, starging with 1.

# File lib/lolita/configuration/nested_list.rb, line 82
def depth
  self.parents.size
end
mapping() click to toggle source

Return mapping that class matches dbi klass.

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

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

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

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

# 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
root() click to toggle source
# File lib/lolita/configuration/nested_list.rb, line 50
def root
  parents.last
end

Private Instance Methods

init_nested_list_attributes(parent) click to toggle source
# File lib/lolita/configuration/nested_list.rb, line 88
def init_nested_list_attributes parent
  @parent = parent
end
validate() click to toggle source
# File lib/lolita/configuration/nested_list.rb, line 92
def validate
  msg = "#{parent.class} must be kind of Lolita::Configuration::List or Lolita::Configuration::Column"
  raise(Lolita::ParentNotAListError, msg) unless [
    Lolita::Configuration::List,
    Lolita::Configuration::Column
  ].detect{|pattern| parent.kind_of?(pattern)}
end