Class: Basepack::Forms::Groups::Base

Inherits:
Object
  • Object
show all
Includes:
Delegation
Defined in:
lib/basepack/forms/groups/base.rb

Direct Known Subclasses

Diff

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#delegateObject (readonly)

Returns the value of attribute delegate.



9
10
11
# File 'lib/basepack/forms/groups/base.rb', line 9

def delegate
  @delegate
end

#field_namesObject

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

#formObject (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

#fieldsObject



40
41
42
# File 'lib/basepack/forms/groups/base.rb', line 40

def fields
  @field_names.map {|f| @form.field(f)}.compact
end

#removeObject



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

#valuesObject



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_blankObject



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_fieldsObject



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