Class: Coalla::FormBuilder

Inherits:
ActionView::Helpers::FormBuilder
  • Object
show all
Includes:
ActionView::Helpers::FormTagHelper
Defined in:
lib/coalla/builders/form_builder.rb

Constant Summary collapse

COLS_CLASS =
'col-md-8'

Instance Method Summary collapse

Instance Method Details

#apply(text = I18n.t('admin.common.apply')) ⇒ Object



247
248
249
# File 'lib/coalla/builders/form_builder.rb', line 247

def apply(text = I18n.t('admin.common.apply'))
  submit text, name: :apply, class: 'btn btn-default wymupdate'
end

#cancelObject



251
252
253
# File 'lib/coalla/builders/form_builder.rb', line 251

def cancel
  @template.back_action
end

#check_box(method, options = {}, checked_value = "1", unchecked_value = "0") ⇒ Object



10
11
12
13
# File 'lib/coalla/builders/form_builder.rb', line 10

def check_box(method, options = {}, checked_value = "1", unchecked_value = "0")
  return unless policy.field_enabled?(method)
  super
end

#checkbox(method, options = {}, checked_value = '1', unchecked_value = '0') ⇒ Object



128
129
130
# File 'lib/coalla/builders/form_builder.rb', line 128

def checkbox(method, options = {}, checked_value = '1', unchecked_value = '0')
  row method, (:div, check_box(method, options, checked_value, unchecked_value), class: 'checkbox'), options
end

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



136
137
138
# File 'lib/coalla/builders/form_builder.rb', line 136

def ckeditor(method, options = {})
  row method, cktext_area(method, with_default_options(options))
end

#collection_check_boxes(method, collection, value_method, text_method, options = {}, html_options = {}, &block) ⇒ Object



15
16
17
18
# File 'lib/coalla/builders/form_builder.rb', line 15

def collection_check_boxes(method, collection, value_method, text_method, options = {}, html_options = {}, &block)
  return unless policy.field_enabled?(method)
  super
end

#collection_radio_buttons(method, collection, value_method, text_method, options = {}, html_options = {}, &block) ⇒ Object



20
21
22
23
# File 'lib/coalla/builders/form_builder.rb', line 20

def collection_radio_buttons(method, collection, value_method, text_method, options = {}, html_options = {}, &block)
  return unless policy.field_enabled?(method)
  super
end

#collection_select(method, collection, value_method, text_method, options = {}, html_options = {}) ⇒ Object



25
26
27
28
# File 'lib/coalla/builders/form_builder.rb', line 25

def collection_select(method, collection, value_method, text_method, options = {}, html_options = {})
  return unless policy.field_enabled?(method)
  super
end

#combobox(method, choices, options = {}, html_options = {}) ⇒ Object



104
105
106
# File 'lib/coalla/builders/form_builder.rb', line 104

def combobox(method, choices, options = {}, html_options = {})
  row method, select(method, choices, options, with_default_options(html_options)), options
end

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



144
145
146
# File 'lib/coalla/builders/form_builder.rb', line 144

def date(method, options = {})
  string(method, merge_options({data: {'calendar-date' => true}, div_class: 'col-md-2'}, options))
end

#date_select(method, options = {}, html_options = {}) ⇒ Object



30
31
32
33
# File 'lib/coalla/builders/form_builder.rb', line 30

def date_select(method, options = {}, html_options = {})
  return unless policy.field_enabled?(method)
  super
end

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



148
149
150
151
# File 'lib/coalla/builders/form_builder.rb', line 148

def datetime(method, options = {})
  value = object.try(method).try(:strftime, '%Y-%m-%d %H:%M')
  string(method, merge_options({data: {'calendar-datetime' => true}, div_class: 'col-md-3', value: value}, options))
end

#datetime_select(method, options = {}, html_options = {}) ⇒ Object



35
36
37
38
# File 'lib/coalla/builders/form_builder.rb', line 35

def datetime_select(method, options = {}, html_options = {})
  return unless policy.field_enabled?(method)
  super
end

#enum(method, options = {}, html_options = {}) ⇒ Object



108
109
110
111
# File 'lib/coalla/builders/form_builder.rb', line 108

def enum(method, options = {}, html_options = {})
  return unless policy.field_enabled?(method)
  combobox method, object.class.send(method.to_s.pluralize).collect { |s| [s[0].humanize, s[0]] }, options, html_options
end

#enumerize(method, options = {}, html_options = {}) ⇒ Object



113
114
115
116
# File 'lib/coalla/builders/form_builder.rb', line 113

def enumerize(method, options = {}, html_options = {})
  return unless policy.field_enabled?(method)
  combobox method, object.class.send(method).values.collect { |s| [s.text, s] }, options, html_options
end

#errorsObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/coalla/builders/form_builder.rb', line 75

def errors
  return nil unless @object.errors.any?

  messages = @object.errors.full_messages.collect do |msg|
    "<p>#{ERB::Util.h(msg)}</p>".html_safe
  end

  (:div,
              (:div,
                          (:div,
                                      (:button, '&times;'.html_safe, class: 'close', type: 'button', :'data-dismiss' => 'alert') + messages.join('').html_safe,
                                      class: 'alert alert-danger'),
                          class: 'col-md-8 col-md-offset-2'),
              class: 'row')
end

#fields_for(record_name, record_object = nil, fields_options = {}, &block) ⇒ Object



40
41
42
43
# File 'lib/coalla/builders/form_builder.rb', line 40

def fields_for(record_name, record_object = nil, fields_options = {}, &block)
  return unless policy.field_enabled?(record_name)
  super
end

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



45
46
47
48
# File 'lib/coalla/builders/form_builder.rb', line 45

def file_field(method, options = {})
  return unless policy.field_enabled?(method)
  super
end

#file_upload(method) ⇒ Object



207
208
209
210
211
212
213
214
# File 'lib/coalla/builders/form_builder.rb', line 207

def file_upload(method)
  return unless policy.field_enabled?(method)

  @template.render partial: '/admin/common/file_upload_template', locals: {
      f: self,
      field: method
  }
end

#grouped_collection_select(method, collection, group_method, group_label_method, option_key_method, option_value_method, options = {}, html_options = {}) ⇒ Object



50
51
52
53
# File 'lib/coalla/builders/form_builder.rb', line 50

def grouped_collection_select(method, collection, group_method, group_label_method, option_key_method, option_value_method, options = {}, html_options = {})
  return unless policy.field_enabled?(method)
  super
end

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



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/coalla/builders/form_builder.rb', line 174

def image_upload(method, options = {})
  return unless policy.field_enabled?(method)

  version = options[:version]

  uri = if version.present?
          @object.send("#{method}_url", version)
        else
          @object.send("#{method}_url")
        end

  if options[:size]
    url = uri || 'placeholder'
  else
    url = uri
  end

  upload_uri = options[:upload_path]
  unless upload_uri
    upload_uri = @template.admin_upload_image_path(image_class: @object.class.name.underscore, field: method)
  end
  @template.render partial: '/admin/common/image_upload_template', locals: {
      f: self, url: url, size: options[:size],
      version: version || '',
      title: options[:title] || '',
      text: options[:text],
      field: method, upload_uri: upload_uri,
      container: options[:parent_id] || upload_container_id(method),
      options: options.delete(:options) || {},
      image_style: options.delete(:image_style) || ''
  }
end

#list_all_combobox(association_name, options = {}, html_options = {}) ⇒ Object



118
119
120
121
122
# File 'lib/coalla/builders/form_builder.rb', line 118

def list_all_combobox(association_name, options = {}, html_options = {})
  return unless policy.field_enabled?(association_name)
  collection_class = object.class.reflect_on_association(association_name).klass
  combobox("#{association_name}_id", collection_class.all.sort_by(&:name).map { |element| [element.name, element.id] }, {include_blank: true}.merge(options), html_options)
end

#multi_field(relation_name, options = {}) ⇒ Object



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/coalla/builders/form_builder.rb', line 216

def multi_field(relation_name, options = {})
  reflection = self.object.class.reflections[relation_name] || self.object.class.reflections[relation_name.to_s]
  options = {search_field_name: :name,
             show_all_on_focus: false,
             use_cache: true,
             relation_model_name: reflection.klass.model_name.singular}.merge!(options)
  options[:source] ||= @template.admin_autocomplete_path(options[:relation_model_name], options[:search_field_name])
  string "#{relation_name}_tokens", title: self.object.class.human_attribute_name(relation_name),
         data: {
             multi_field: true,
             source: options[:source],
             pre: self.object.send("#{relation_name}_json", options[:search_field_name]),
             show_all_on_focus: options[:show_all_on_focus],
             use_cache: options[:use_cache],
             object_url_name: options[:object_url_name]}.merge!(options[:data] || {})
end

#nested_fields_for(association, record_object = nil, options = {}, &block) ⇒ Object

Editing has_many collection parameters: association - name of has_many association association should be defined as accepts_nested_attributes with allow_destroy = true in model You should create partial with name #{association)_fields



162
163
164
165
166
167
168
169
170
171
172
# File 'lib/coalla/builders/form_builder.rb', line 162

def nested_fields_for(association, record_object = nil, options = {}, &block)
  return unless policy.field_enabled?(association)

  @template.render('admin/common/nested_fields_for',
                   f: self,
                   section_name: options[:title] || @object.class.human_attribute_name(association),
                   collection: association,
                   record_object: record_object,
                   block: block,
                   options: {include_id: false, destroy: true, add: true, sort: false}.merge(options))
end

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



140
141
142
# File 'lib/coalla/builders/form_builder.rb', line 140

def password(method, options = {})
  row method, password_field(method, with_default_options(options)), options
end

#radio_button(method, tag_value, options = {}) ⇒ Object



55
56
57
58
# File 'lib/coalla/builders/form_builder.rb', line 55

def radio_button(method, tag_value, options = {})
  return unless policy.field_enabled?(method)
  super
end

#row(method, controls, options = {}) ⇒ Object



233
234
235
236
237
238
239
240
241
# File 'lib/coalla/builders/form_builder.rb', line 233

def row(method, controls, options = {})
  return unless policy.field_enabled?(method)

  label_class = options[:label_class] || 'control-label col-md-4 col-lg-2'
  label_tag = label(method, options[:title], class: label_class) unless options[:hide_label]
  options[:div_class] ||= COLS_CLASS
  div_tag =  :div, controls.html_safe, class: options[:div_class]
   :div, "#{label_tag}#{div_tag}".html_safe, class: 'form-group'
end

#save(text = I18n.t('admin.common.save')) ⇒ Object



243
244
245
# File 'lib/coalla/builders/form_builder.rb', line 243

def save(text = I18n.t('admin.common.save'))
  submit text, name: :save, class: 'btn btn-success wymupdate'
end

#select(method, choices = nil, options = {}, html_options = {}, &block) ⇒ Object



60
61
62
63
# File 'lib/coalla/builders/form_builder.rb', line 60

def select(method, choices = nil, options = {}, html_options = {}, &block)
  return unless policy.field_enabled?(method)
  super
end

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

Displays multi-line static text in form



96
97
98
# File 'lib/coalla/builders/form_builder.rb', line 96

def static_memo_text(method, options = {})
  row method, simple_format(@object.send(method), class: 'form-control-static'), options
end

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



91
92
93
# File 'lib/coalla/builders/form_builder.rb', line 91

def static_text(method, options = {})
  row method, (:p, static_text_formatter.format(method), class: 'form-control-static'), options
end

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



100
101
102
# File 'lib/coalla/builders/form_builder.rb', line 100

def string(method, options = {})
  row method, text_field(method, with_default_options(options)), options
end

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



124
125
126
# File 'lib/coalla/builders/form_builder.rb', line 124

def textarea(method, options = {})
  row method, text_area(method, with_default_options(options)), options
end

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



153
154
155
156
# File 'lib/coalla/builders/form_builder.rb', line 153

def time(method, options = {})
  value = object.try(method).try(:strftime, '%H:%M')
  string(method, merge_options({data: {'calendar-time' => true}, div_class: 'col-md-2', value: value}, options))
end

#time_select(method, options = {}, html_options = {}) ⇒ Object



65
66
67
68
# File 'lib/coalla/builders/form_builder.rb', line 65

def time_select(method, options = {}, html_options = {})
  return unless policy.field_enabled?(method)
  super
end

#time_zone_select(method, priority_zones = nil, options = {}, html_options = {}) ⇒ Object



70
71
72
73
# File 'lib/coalla/builders/form_builder.rb', line 70

def time_zone_select(method, priority_zones = nil, options = {}, html_options = {})
  return unless policy.field_enabled?(method)
  super
end

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



132
133
134
# File 'lib/coalla/builders/form_builder.rb', line 132

def wysiwyg(method, options = {})
  textarea method, with_default_options(merge_classes(options, 'wymeditor'))
end