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



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

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



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

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

.default_class_names(type = nil) ⇒ Object



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

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



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

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



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

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

#field_set(*args, &block) ⇒ Object



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

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



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

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

#tab(name, &block) ⇒ Object



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

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

#tabsObject



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

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