Class: Modular::Components::Container

Inherits:
Base
  • Object
show all
Defined in:
lib/modular/components/container.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#title, #uniqid, #width

Instance Method Summary collapse

Methods inherited from Base

attr_accessor, attr_reader, attr_writer, #persisted?, #to_hash, #to_json, #type, type, use_mustached_template!

Methods included from Rendering

#action_name, #execute, #render

Constructor Details

#initialize(attributes = {}) ⇒ Container

Returns a new instance of Container.



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/modular/components/container.rb', line 6

def initialize(attributes = {})
  @attributes = ActiveSupport::HashWithIndifferentAccess.new(:children => [])
  attributes = attributes.with_indifferent_access
  attributes["children"] ||= []

  attributes["children"].each do |value|
    @attributes[:children].push value if value.is_a? Modular::Components::Base
  end
  
  super(attributes.except("children"))
end

Instance Attribute Details

#childrenObject

Returns the value of attribute children.



4
5
6
# File 'lib/modular/components/container.rb', line 4

def children
  @children
end

Instance Method Details

#add(type = nil, args = {}, &block) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/modular/components/container.rb', line 18

def add(type = nil, args = {}, &block)
  unless type.nil?
    cont = Modular.create(type, args)
    cont.instance_eval &block if block_given?
    children.push cont
  end
end

#all_errorsObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/modular/components/container.rb', line 37

def all_errors
  if valid_with_no_children?
    errs = {} 
  else
    errs = super
  end

  children.each do |child|
    e = child.all_errors
    e.each do |key, value|
      unless value.empty?
        errs[key] ||= []
        errs[key] += value
      end
    end
  end

  errs
end

#find_by_uniqid(id) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/modular/components/container.rb', line 56

def find_by_uniqid(id)
  id = id.to_s

  result = super

  if result.nil?
    children.each do |child|
      result = child.find_by_uniqid(id)
      return result unless result.nil?
    end
  end

  result
end

#valid_with_children?(context = nil) ⇒ Boolean Also known as: valid?

Returns:

  • (Boolean)


26
27
28
29
30
31
32
# File 'lib/modular/components/container.rb', line 26

def valid_with_children?(context = nil)
  children.each do |child|
    return false if child.invalid?
  end

  valid_with_no_children?
end