Class: Formize::Definition::Group

Inherits:
FormElement show all
Defined in:
lib/formize/definition/field_set.rb

Overview

Represents a group of fields which can depend on other fields

Direct Known Subclasses

FieldSet

Instance Attribute Summary collapse

Attributes inherited from FormElement

#children, #depend_on, #form, #html_id, #id, #parent, #unique_name

Instance Method Summary collapse

Methods inherited from FormElement

#all_elements, #all_fields, #arguments, #dependeds, #dependents, #dependents_on, #hidden_if, #mono_choices, #prototype, #shown_if

Constructor Details

#initialize(form, parent, name = nil, options = {}) ⇒ Group

Returns a new instance of Group.

Raises:

  • (ArgumentError)

9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/formize/definition/field_set.rb', line 9

def initialize(form, parent, name=nil, options={})
  super(form, parent, options)
  @name = if name.blank?
            rand.to_s[2..-1].to_i.to_s(36)
          else
            raise ArgumentError.new("Name of group must be written only with a-z and 0-9 and _ (not #{name.inspect})") unless name.to_s == name.to_s.downcase.gsub(/[^a-z0-9\_]/, '')
            name.to_s
          end
  @depend_on = options.delete(:depend_on)
  raise ArgumentError.new("A depended element must defined before its dependencies (#{@depended.inspect})") if !@depend_on.blank? and form.all_fields[@depend_on].nil?
  @html_options = @options.delete(:html_options)||{}
end

Instance Attribute Details

#html_optionsObject (readonly)

Returns the value of attribute html_options


7
8
9
# File 'lib/formize/definition/field_set.rb', line 7

def html_options
  @html_options
end

#nameObject (readonly)

Returns the value of attribute name


7
8
9
# File 'lib/formize/definition/field_set.rb', line 7

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options


7
8
9
# File 'lib/formize/definition/field_set.rb', line 7

def options
  @options
end

Instance Method Details

#field(name, options = {}) ⇒ Object


36
37
38
# File 'lib/formize/definition/field_set.rb', line 36

def field(name, options={})
  self.new_child(Field, name, options)
end

#field_set(name = nil, options = {}) {|field_set| ... } ⇒ Object

Yields:

Raises:

  • (ArgumentError)

23
24
25
26
27
# File 'lib/formize/definition/field_set.rb', line 23

def field_set(name=nil, options={}, &block)
  raise ArgumentError.new("Missing block") unless block_given?
  field_set = self.new_child(FieldSet, name, options)
  yield field_set
end

#fields(*args) ⇒ Object


40
41
42
43
44
45
46
# File 'lib/formize/definition/field_set.rb', line 40

def fields(*args)
  options = {}
  options = args.delete_at(-1) if args[-1].is_a?(Hash)
  for name in args
    self.new_child(Field, name, options)
  end
end

#group(name = nil, options = {}) {|group| ... } ⇒ Object

Yields:

Raises:

  • (ArgumentError)

29
30
31
32
33
34
# File 'lib/formize/definition/field_set.rb', line 29

def group(name=nil, options={}, &block)
  raise ArgumentError.new("Missing block") unless block_given?
  name, options = nil, name if name.is_a? Hash
  group = self.new_child(Group, name, options)
  yield group
end