Class: Formotion::Section

Inherits:
Base show all
Defined in:
lib/formotion/section/section.rb

Constant Summary collapse

PROPERTIES =
[
  # Displayed in the section header row
  :title,
  # Displayed below the entire section; good for giving
  # detailed information regarding the section.
  :footer,
  # Arranges the section as a 'radio' section,
  # such that only one row can be checked at a time.
  :select_one,
  # IF :select_one is true, then @form.render will contain
  # the checked row's value as the value for this key.
  # ELSE it does nothing.
  :key
]
SERIALIZE_PROPERTIES =
PROPERTIES + [:rows]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#copyWithZone, #encodeWithCoder, #hash, #initWithCoder, #isEqual

Constructor Details

#initialize(params = {}) ⇒ Section

Returns a new instance of Section.



32
33
34
35
36
37
38
39
40
41
# File 'lib/formotion/section/section.rb', line 32

def initialize(params = {})
  super
  self.form = params[:form]
  Formotion::Conditions.assert_nil_or_boolean(self.select_one)

  rows = params[:rows] || params["rows"]
  rows && rows.each {|row_hash|
    row = create_row(row_hash)
  }
end

Instance Attribute Details

#formObject

This section’s form



27
28
29
# File 'lib/formotion/section/section.rb', line 27

def form
  @form
end

#indexObject

This section’s index in it’s form.



30
31
32
# File 'lib/formotion/section/section.rb', line 30

def index
  @index
end

Instance Method Details

#build_row(&block) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/formotion/section/section.rb', line 53

def build_row(&block)
  row = generate_row
  block.call(row)
  row.after_create
  self.rows << row
  row
end

#create_row(hash = {}) ⇒ Object



61
62
63
64
65
66
# File 'lib/formotion/section/section.rb', line 61

def create_row(hash = {})
  row = generate_row(hash)
  row.after_create
  self.rows << row
  row
end

#generate_row(hash = {}) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/formotion/section/section.rb', line 43

def generate_row(hash = {})
  row = hash
  if hash.class == Hash
    row = Formotion::Row.new(hash)
  end
  row.section = WeakRef.new(self)
  row.index = self.rows.count
  row
end

#next_sectionObject



95
96
97
98
99
100
101
102
# File 'lib/formotion/section/section.rb', line 95

def next_section
  # if there are more sections in this form, use that.
  if self.index < self.form.sections.count - 1
    return self.form.sections[self.index + 1]
  end

  nil
end

#previous_sectionObject



104
105
106
107
108
109
110
111
# File 'lib/formotion/section/section.rb', line 104

def previous_section
  # if there are more sections in this form, use that.
  if self.index > 0
    return self.form.sections[self.index - 1]
  end

  nil
end

#refresh_row_indexesObject



113
114
115
116
117
# File 'lib/formotion/section/section.rb', line 113

def refresh_row_indexes
  rows.each_with_index do |row, index|
    row.index = index
  end
end

#rowsObject

attribute overrides



71
72
73
# File 'lib/formotion/section/section.rb', line 71

def rows
  @rows ||= []
end

#rows=(rows) ⇒ Object



75
76
77
78
79
80
# File 'lib/formotion/section/section.rb', line 75

def rows=(rows)
  rows.each {|row|
    Formotion::Conditions.assert_class(row, Formotion::Row)
  }
  @rows = rows
end

#select_one?Boolean

should be done with alias_method but there’s currently a bug in RM which messes up attr_accessors with alias_method

Returns:

  • (Boolean)


91
92
93
# File 'lib/formotion/section/section.rb', line 91

def select_one?
  self.select_one
end

#to_hashObject

Retreiving data



121
122
123
124
125
126
127
128
# File 'lib/formotion/section/section.rb', line 121

def to_hash
  h = super
  h[:rows] = []
  self.rows.each do |row|
    h[:rows] << row.to_hash if row.template_parent.nil?
  end
  h
end