Class: Basepack::Forms::Groups::Base
- Inherits:
-
Object
- Object
- Basepack::Forms::Groups::Base
- Includes:
- Delegation
- Defined in:
- lib/basepack/forms/groups/base.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#delegate ⇒ Object
readonly
Returns the value of attribute delegate.
-
#field_names ⇒ Object
Returns the value of attribute field_names.
-
#form ⇒ Object
readonly
Returns the value of attribute form.
Instance Method Summary collapse
- #content_for_field(field_name, &block) ⇒ Object
- #field(name, delegate_or_attributes = nil, &block) ⇒ Object
- #fields ⇒ Object
-
#initialize(form, delegate_or_attributes = nil) ⇒ Base
constructor
A new instance of Base.
- #remove ⇒ Object
- #update_attributes(delegate_or_attributes) ⇒ Object
- #values ⇒ Object
- #values_without_blank ⇒ Object
- #visible_fields ⇒ Object
Constructor Details
#initialize(form, delegate_or_attributes = nil) ⇒ Base
Returns a new instance of Base.
12 13 14 15 16 |
# File 'lib/basepack/forms/groups/base.rb', line 12 def initialize(form, delegate_or_attributes = nil) @form = form @field_names = [] update_attributes(delegate_or_attributes) end |
Instance Attribute Details
#delegate ⇒ Object (readonly)
Returns the value of attribute delegate.
9 10 11 |
# File 'lib/basepack/forms/groups/base.rb', line 9 def delegate @delegate end |
#field_names ⇒ Object
Returns the value of attribute field_names.
10 11 12 |
# File 'lib/basepack/forms/groups/base.rb', line 10 def field_names @field_names end |
#form ⇒ Object (readonly)
Returns the value of attribute form.
9 10 11 |
# File 'lib/basepack/forms/groups/base.rb', line 9 def form @form end |
Instance Method Details
#content_for_field(field_name, &block) ⇒ Object
34 35 36 37 38 |
# File 'lib/basepack/forms/groups/base.rb', line 34 def content_for_field(field_name, &block) field(field_name) do |f| f.content(&block) end end |
#field(name, delegate_or_attributes = nil, &block) ⇒ Object
28 29 30 31 32 |
# File 'lib/basepack/forms/groups/base.rb', line 28 def field(name, delegate_or_attributes = nil, &block) field = @form.field(name, delegate_or_attributes, &block) @field_names << name if field and !@field_names.include?(name) field end |
#fields ⇒ Object
40 41 42 |
# File 'lib/basepack/forms/groups/base.rb', line 40 def fields @field_names.map {|f| @form.field(f)}.compact end |
#remove ⇒ Object
48 49 50 |
# File 'lib/basepack/forms/groups/base.rb', line 48 def remove @form.groups.delete self end |
#update_attributes(delegate_or_attributes) ⇒ Object
18 19 20 21 22 23 24 25 26 |
# File 'lib/basepack/forms/groups/base.rb', line 18 def update_attributes(delegate_or_attributes) if delegate_or_attributes.is_a? Hash delegate_or_attributes.each do |a, v| send("#{a}=", v) end else @delegate = delegate_or_attributes end end |
#values ⇒ Object
52 53 54 55 56 |
# File 'lib/basepack/forms/groups/base.rb', line 52 def values result = {} visible_fields.each {|f| result[f.name] = f.value} result end |
#values_without_blank ⇒ Object
58 59 60 61 62 63 64 65 |
# File 'lib/basepack/forms/groups/base.rb', line 58 def values_without_blank result = {} visible_fields.each do |f| val = f.value.presence result[f.name] = val unless val.nil? end result end |
#visible_fields ⇒ Object
44 45 46 |
# File 'lib/basepack/forms/groups/base.rb', line 44 def visible_fields @field_names.map {|f| @form.visible_field(f)}.compact end |