Class: AdminWidgets::BaseFormWidget

Inherits:
BaseWidget
  • Object
show all
Defined in:
lib/admin_widgets/base_form_widget.rb

Direct Known Subclasses

FiltersWidget, FormWidget, WizardWidget

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from BaseWidget

#capture, #content_block, #helper, #method_missing, #root

Methods included from Memoization

#memoize

Methods included from Delegation

#delegate

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class AdminWidgets::BaseWidget

Instance Attribute Details

#formObject (readonly)

Returns the value of attribute form.



4
5
6
# File 'lib/admin_widgets/base_form_widget.rb', line 4

def form
  @form
end

Instance Method Details

#around_formObject



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/admin_widgets/base_form_widget.rb', line 25

def around_form
  form_method = @form ? :simple_fields_for : :simple_form_for

  helper.send(form_method, resource, :url => url, :as => as, :class => css_class, :html => @html) do |f|
    @form = f # IMPORTANT!

    capture do
      form_content
    end
  end
end

#autofocus!(options = {}) ⇒ Object



46
47
48
49
# File 'lib/admin_widgets/base_form_widget.rb', line 46

def autofocus!(options = {})
  root.instance_variable_set(:@autofocus_done, true)
  { autofocus: true }.merge(options)
end

#autofocus_done?Boolean

FIXME: Beware, arcane magic below. We are polluting the root widget with a instance variable, is there a better way to do this?

Returns:

  • (Boolean)


43
44
45
# File 'lib/admin_widgets/base_form_widget.rb', line 43

def autofocus_done?
  root.instance_variable_get(:@autofocus_done)
end

#builderObject

TODO: WTF? Who the hell is overriding the ‘form’ method?



7
8
9
# File 'lib/admin_widgets/base_form_widget.rb', line 7

def builder
  form
end

#contentObject



21
22
23
# File 'lib/admin_widgets/base_form_widget.rb', line 21

def content
  rawtext around_form
end

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

DSL methods



53
54
55
56
57
58
59
60
61
62
# File 'lib/admin_widgets/base_form_widget.rb', line 53

def field(name, options = {})
  options = autofocus!(options) unless autofocus_done?

  condition = options[:if]
  return if (condition.present? && !resource.instance_eval(&condition))

  form = (options[:nested] === false && self.respond_to?(:parent)) ? parent.form : self.form

  rawtext form.input(name, options)
end

#form_contentObject



37
38
39
40
# File 'lib/admin_widgets/base_form_widget.rb', line 37

def form_content
  content_block
  buttons
end