Class: Adva::ExtensibleFormBuilder

Inherits:
ActionView::Helpers::FormBuilder
  • Object
show all
Defined in:
lib/adva/extensible_forms.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.after(object_name, method, string = nil, &block) ⇒ Object



60
61
62
# File 'lib/adva/extensible_forms.rb', line 60

def after(object_name, method, string = nil, &block)
  add_callback(:after, object_name, method, string || block)
end

.before(object_name, method, string = nil, &block) ⇒ Object



56
57
58
# File 'lib/adva/extensible_forms.rb', line 56

def before(object_name, method, string = nil, &block)
  add_callback(:before, object_name, method, string || block)
end

.default_class_names(type = nil) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/adva/extensible_forms.rb', line 48

def default_class_names(type = nil)
  if type
    self.options[:default_class_names][type] ||= []
  else
    self.options[:default_class_names]
  end
end

.tab(name, options = {}, &block) ⇒ Object



64
65
66
67
# File 'lib/adva/extensible_forms.rb', line 64

def tab(name, options = {}, &block)
  self.tabs.reject! { |n, b| name == n }
  self.tabs += [[name, block]]
end

Instance Method Details

#buttons(name = :submit_buttons, &block) ⇒ Object



126
127
128
129
130
# File 'lib/adva/extensible_forms.rb', line 126

def buttons(name = :submit_buttons, &block)
  @template.concat with_callbacks(name) {
    @template.capture { @template.buttons(&block) }
  }
end

#field_set(*args, &block) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/adva/extensible_forms.rb', line 79

def field_set(*args, &block)
  options = args.extract_options!
  options = add_default_class_names(options, :field_set)

  name    = args.first
  name ||= :default_fields

  @template.concat with_callbacks(name) {
    legend = options.delete(:legend) || ''
    legend = @template.('legend', legend) unless legend.blank?
    @template.field_set(@object_name, name, nil, objectify_options(options)) do
      legend.to_s + (block ? block.call.to_s : '')
    end
  }
end

#render(*args) ⇒ Object



132
133
134
# File 'lib/adva/extensible_forms.rb', line 132

def render(*args)
  @template.send(:render, *args)
end

#tab(name, &block) ⇒ Object



120
121
122
123
124
# File 'lib/adva/extensible_forms.rb', line 120

def tab(name, &block)
  with_callbacks(:"tab_#{name}") {
    self.class.tab(name, &block)
  }
end

#tabsObject



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/adva/extensible_forms.rb', line 95

def tabs
  yield if block_given?
  assign_ivars!
  @template.(:div, :class => 'tabs') {
    self.class.tabs.map.with_index { |(name, _), index|
      active = self.class.tabs.first.first == name
      %(<input type="radio" id="adva_current_tab_#{index}" name="adva_current_tab" #{"checked" if active}>)
    }.join.html_safe +

    @template.(:ul) {
      self.class.tabs.map.with_index { |(name, _), index|
        @template.(:li) {
          title = I18n.t(name, :scope => :'adva.titles')
          %(<label for="adva_current_tab_#{index}">#{title}</label>).html_safe
        }
      }.join.html_safe
    } +

    self.class.tabs.map.with_index { |(name, block), index|
      klass = self.class.tabs.first.first == name ? 'tab active' : 'tab'
      @template. 'fieldset', block.call(self), id: "tab_#{name}", class: klass, for: "adva_current_tab_#{index}"
    }.join.html_safe
  }.html_safe
end