Class: ExtensibleFormBuilder

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

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



69
70
71
# File 'lib/extensible_forms.rb', line 69

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



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

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

.default_class_names(type = nil) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/extensible_forms.rb', line 57

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



73
74
75
76
# File 'lib/extensible_forms.rb', line 73

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



154
155
156
157
158
# File 'lib/extensible_forms.rb', line 154

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

#field_set(*args, &block) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/extensible_forms.rb', line 115

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



160
161
162
# File 'lib/extensible_forms.rb', line 160

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

#tab(name, &block) ⇒ Object



148
149
150
151
152
# File 'lib/extensible_forms.rb', line 148

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

#tabsObject



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/extensible_forms.rb', line 131

def tabs
  yield if block_given?
  assign_ivars!
  @template.(:div, :class => 'tabs') {
    @template.(:ul) {
      self.class.tabs.map { |name, block|
        klass = self.class.tabs.first.first == name ? 'active' : nil
        @template. 'li', @template.link_to(I18n.t(name, :scope => :'adva.titles'), "##{name}"), :class => klass
      }.join.html_safe
    } +
    self.class.tabs.map { |name, block|
      klass = self.class.tabs.first.first == name ? 'tab active' : 'tab'
      @template. 'div', block.call(self), :id => "tab_#{name}", :class => klass
    }.join.html_safe
  }.html_safe
end