Class: ExtForm::Builders::FormBuilderBase

Inherits:
SimpleForm::FormBuilder
  • Object
show all
Includes:
NestedForm::BuilderMixin
Defined in:
lib/ext_form/builders/form_builder_base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFormBuilderBase

Returns a new instance of FormBuilderBase.



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/ext_form/builders/form_builder_base.rb', line 18

def initialize(*)
  super

  self.input_seq = 0
  self.l = ['ExtForm','Layouts', ExtForm.layout].join('::').constantize.new
  l.setup_config(options[:l])

  self.before_row_start_callbacks = []
  self.after_row_start_callbacks = []
  self.before_row_end_callbacks = []
  self.after_row_end_callbacks = []
end

Instance Attribute Details

#after_row_end_callbacksObject

Returns the value of attribute after_row_end_callbacks.



8
9
10
# File 'lib/ext_form/builders/form_builder_base.rb', line 8

def after_row_end_callbacks
  @after_row_end_callbacks
end

#after_row_start_callbacksObject

Returns the value of attribute after_row_start_callbacks.



8
9
10
# File 'lib/ext_form/builders/form_builder_base.rb', line 8

def after_row_start_callbacks
  @after_row_start_callbacks
end

#before_row_end_callbacksObject

Returns the value of attribute before_row_end_callbacks.



8
9
10
# File 'lib/ext_form/builders/form_builder_base.rb', line 8

def before_row_end_callbacks
  @before_row_end_callbacks
end

#before_row_start_callbacksObject

Returns the value of attribute before_row_start_callbacks.



8
9
10
# File 'lib/ext_form/builders/form_builder_base.rb', line 8

def before_row_start_callbacks
  @before_row_start_callbacks
end

#input_seqObject

Returns the value of attribute input_seq.



8
9
10
# File 'lib/ext_form/builders/form_builder_base.rb', line 8

def input_seq
  @input_seq
end

#lObject

Returns the value of attribute l.



8
9
10
# File 'lib/ext_form/builders/form_builder_base.rb', line 8

def l
  @l
end

Instance Method Details

#after_row_end(&block) ⇒ Object



114
115
116
117
# File 'lib/ext_form/builders/form_builder_base.rb', line 114

def after_row_end(&block)
  after_row_end_callbacks << block
  ''
end

#after_row_start(&block) ⇒ Object



109
110
111
112
# File 'lib/ext_form/builders/form_builder_base.rb', line 109

def after_row_start(&block)
  after_row_start_callbacks << block
  ''
end

#auto_complete(method, options = {}) ⇒ Object



155
156
157
# File 'lib/ext_form/builders/form_builder_base.rb', line 155

def auto_complete(method, options={})
  @template.auto_complete(@object_name, method, objectify_options(options))
end

#before_row_end(&block) ⇒ Object



104
105
106
107
# File 'lib/ext_form/builders/form_builder_base.rb', line 104

def before_row_end(&block)
  before_row_end_callbacks << block
  ''
end

#before_row_start(&block) ⇒ Object



99
100
101
102
# File 'lib/ext_form/builders/form_builder_base.rb', line 99

def before_row_start(&block)
  before_row_start_callbacks << block
  ''
end

#collection_select2(method, collection, value_method, label_method, options = {}, html_options = {}) ⇒ Object



159
160
161
162
163
# File 'lib/ext_form/builders/form_builder_base.rb', line 159

def collection_select2(method, collection, value_method, label_method,
    options={}, html_options={})
  @template.collection_select2(@object_name, method, collection, value_method, label_method,
                               objectify_options(options), html_options)
end

#date_picker(method, options = {}) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/ext_form/builders/form_builder_base.rb', line 129

def date_picker(method, options={})
  fixed_data_options = {
      pick_time: false
  }
  default_data_options = {
      format: 'yyyy-MM-dd',
      language: ExtForm.locale
  }
  options[:data] ||= {}
  options[:data].merge!(fixed_data_options).reverse_merge!(default_data_options)
  @template.dt_picker(@object_name, method, objectify_options(options))
end

#dt_picker(method, options = {}) ⇒ Object



119
120
121
122
123
124
125
126
127
# File 'lib/ext_form/builders/form_builder_base.rb', line 119

def dt_picker(method, options={})
  default_data_options = {
      format: 'yyyy-MM-dd hh:mm:ss',
      language: ExtForm.locale
  }
  options[:data] ||= {}
  options[:data].reverse_merge!(default_data_options)
  @template.dt_picker(@object_name, method, objectify_options(options))
end

#ext_fields_for(record_name, record_object = nil, options = {}, &block) ⇒ Object

Public: Wrapper for using ExtForm inside a ext rails form. Example:

ext_form_for @user do |f|
  f.ext_fields_for :posts do |posts_form|
    # Here you have all simple_form methods available
    posts_form.input :title
  end
end


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ext_form/builders/form_builder_base.rb', line 41

def ext_fields_for(record_name, record_object = nil, options = {}, &block)
  record_object, options = nil, record_object if record_object.is_a?(Hash)

  options[:wrapper] ||= self.options[:wrapper]
  options[:defaults] ||= self.options[:defaults]
  options[:l] ||= self.options[:l]

  if self.class < ActionView::Helpers::FormBuilder
    options[:builder] ||= self.class
  else
    options[:builder] ||= ExtForm::Builders::FormBuilder
  end

  simple_fields_for(record_name, record_object, options, &block)

end

#grouped_collection_select2(method, grouped_collection, group_method, group_label_method, value_method, label_method, options = {}, html_options = {}) ⇒ Object



165
166
167
168
169
170
# File 'lib/ext_form/builders/form_builder_base.rb', line 165

def grouped_collection_select2(method, grouped_collection,
    group_method, group_label_method, value_method, label_method,
    options={}, html_options={})
  @template.collection_select2(@object_name, method, grouped_collection, group_method, group_label_method,
                               value_method, label_method, objectify_options(options), html_options)
end

#input(attribute_name, options = {}, &block) ⇒ Object



88
89
90
91
92
93
94
95
96
97
# File 'lib/ext_form/builders/form_builder_base.rb', line 88

def input(attribute_name, options={}, &block)
  options[:input_seq] = self.input_seq
  output = []
  output << row_start
  output << super(attribute_name, options, &block)
  output << row_end
  self.input_seq += 1

  output.compact.join.html_safe
end

#inputs(*args, &block) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/ext_form/builders/form_builder_base.rb', line 58

def inputs(*args, &block)
  if args.first.is_a?(String) && args.first.match(/^\d+(:?\d?)?$/)
    layout = args.shift
    attributes = args
  else
    layout, attributes = nil, args
  end

  if layout.blank?
    self.input_seq = 0

    output = []
    attributes.each do |a|
      if find_attribute_column(a)
        output << input(a)
      elsif find_association_reflection(a)
        output << association(a)
      else
        raise 'Attribute must be a column or an association of this model.'
      end
    end

    output << @template.capture(self, &block) if block_given?
    output << all_row_end.try(:html_safe)
    output.compact.join.html_safe
  else
    swap_layout(layout, attributes, &block)
  end
end

#time_picker(method, options = {}) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/ext_form/builders/form_builder_base.rb', line 142

def time_picker(method, options={})
  fixed_data_options = {
      pick_date: false
  }
  default_data_options = {
      format: 'hh:mm:ss',
      language: ExtForm.locale
  }
  options[:data] ||= {}
  options[:data].merge!(fixed_data_options).reverse_merge!(default_data_options)
  @template.dt_picker(@object_name, method, objectify_options(options))
end