Class: Coalla::FormBuilder
- Inherits:
-
ActionView::Helpers::FormBuilder
- Object
- ActionView::Helpers::FormBuilder
- Coalla::FormBuilder
- Includes:
- ActionView::Helpers::FormTagHelper
- Defined in:
- lib/coalla/builders/form_builder.rb
Constant Summary collapse
- COLS_CLASS =
'col-md-8'
Instance Method Summary collapse
- #apply(text = I18n.t('admin.common.apply')) ⇒ Object
- #cancel ⇒ Object
- #check_box(method, options = {}, checked_value = "1", unchecked_value = "0") ⇒ Object
- #checkbox(method, options = {}, checked_value = '1', unchecked_value = '0') ⇒ Object
- #ckeditor(method, options = {}) ⇒ Object
- #collection_check_boxes(method, collection, value_method, text_method, options = {}, html_options = {}, &block) ⇒ Object
- #collection_radio_buttons(method, collection, value_method, text_method, options = {}, html_options = {}, &block) ⇒ Object
- #collection_select(method, collection, value_method, text_method, options = {}, html_options = {}) ⇒ Object
- #combobox(method, choices, options = {}, html_options = {}) ⇒ Object
- #date(method, options = {}) ⇒ Object
- #date_select(method, options = {}, html_options = {}) ⇒ Object
- #datetime(method, options = {}) ⇒ Object
- #datetime_select(method, options = {}, html_options = {}) ⇒ Object
- #enum(method, options = {}, html_options = {}) ⇒ Object
- #enumerize(method, options = {}, html_options = {}) ⇒ Object
- #errors ⇒ Object
- #fields_for(record_name, record_object = nil, fields_options = {}, &block) ⇒ Object
- #file_field(method, options = {}) ⇒ Object
- #file_upload(method) ⇒ Object
- #grouped_collection_select(method, collection, group_method, group_label_method, option_key_method, option_value_method, options = {}, html_options = {}) ⇒ Object
- #image_upload(method, options = {}) ⇒ Object
- #list_all_combobox(association_name, options = {}, html_options = {}) ⇒ Object
- #multi_field(relation_name, options = {}) ⇒ Object
-
#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.
- #password(method, options = {}) ⇒ Object
- #radio_button(method, tag_value, options = {}) ⇒ Object
- #row(method, controls, options = {}) ⇒ Object
- #save(text = I18n.t('admin.common.save')) ⇒ Object
- #select(method, choices = nil, options = {}, html_options = {}, &block) ⇒ Object
-
#static_memo_text(method, options = {}) ⇒ Object
Displays multi-line static text in form.
- #static_text(method, options = {}) ⇒ Object
- #string(method, options = {}) ⇒ Object
- #textarea(method, options = {}) ⇒ Object
- #time(method, options = {}) ⇒ Object
- #time_select(method, options = {}, html_options = {}) ⇒ Object
- #time_zone_select(method, priority_zones = nil, options = {}, html_options = {}) ⇒ Object
- #wysiwyg(method, options = {}) ⇒ Object
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 |
#cancel ⇒ Object
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, = {}, 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, = {}, checked_value = '1', unchecked_value = '0') row method, content_tag(:div, check_box(method, , checked_value, unchecked_value), class: 'checkbox'), end |
#ckeditor(method, options = {}) ⇒ Object
136 137 138 |
# File 'lib/coalla/builders/form_builder.rb', line 136 def ckeditor(method, = {}) row method, cktext_area(method, ()) 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, = {}, = {}, &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 (method, collection, value_method, text_method, = {}, = {}, &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, = {}, = {}) 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, = {}, = {}) row method, select(method, choices, , ()), end |
#date(method, options = {}) ⇒ Object
144 145 146 |
# File 'lib/coalla/builders/form_builder.rb', line 144 def date(method, = {}) string(method, ({data: {'calendar-date' => true}, div_class: 'col-md-2'}, )) 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, = {}, = {}) 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, = {}) value = object.try(method).try(:strftime, '%Y-%m-%d %H:%M') string(method, ({data: {'calendar-datetime' => true}, div_class: 'col-md-3', value: value}, )) 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, = {}, = {}) 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, = {}, = {}) return unless policy.field_enabled?(method) combobox method, object.class.send(method.to_s.pluralize).collect { |s| [s[0].humanize, s[0]] }, , end |
#enumerize(method, options = {}, html_options = {}) ⇒ Object
113 114 115 116 |
# File 'lib/coalla/builders/form_builder.rb', line 113 def enumerize(method, = {}, = {}) return unless policy.field_enabled?(method) combobox method, object.class.send(method).values.collect { |s| [s.text, s] }, , end |
#errors ⇒ Object
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? = @object.errors..collect do |msg| "<p>#{ERB::Util.h(msg)}</p>".html_safe end content_tag(:div, content_tag(:div, content_tag(:div, content_tag(:button, '×'.html_safe, class: 'close', type: 'button', :'data-dismiss' => 'alert') + .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, = {}, &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, = {}) 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, = {}, = {}) 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, = {}) return unless policy.field_enabled?(method) version = [:version] uri = if version.present? @object.send("#{method}_url", version) else @object.send("#{method}_url") end if [:size] url = uri || 'placeholder' else url = uri end upload_uri = [: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: [:size], version: version || '', title: [:title] || '', text: [:text], field: method, upload_uri: upload_uri, container: [:parent_id] || upload_container_id(method), options: .delete(:options) || {}, image_style: .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, = {}, = {}) 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(), ) 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, = {}) reflection = self.object.class.reflections[relation_name] || self.object.class.reflections[relation_name.to_s] = {search_field_name: :name, show_all_on_focus: false, use_cache: true, relation_model_name: reflection.klass.model_name.singular}.merge!() [:source] ||= @template.admin_autocomplete_path([:relation_model_name], [:search_field_name]) string "#{relation_name}_tokens", title: self.object.class.human_attribute_name(relation_name), data: { multi_field: true, source: [:source], pre: self.object.send("#{relation_name}_json", [:search_field_name]), show_all_on_focus: [:show_all_on_focus], use_cache: [:use_cache], object_url_name: [:object_url_name]}.merge!([: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, = {}, &block) return unless policy.field_enabled?(association) @template.render('admin/common/nested_fields_for', f: self, section_name: [: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()) end |
#password(method, options = {}) ⇒ Object
140 141 142 |
# File 'lib/coalla/builders/form_builder.rb', line 140 def password(method, = {}) row method, password_field(method, ()), end |
#radio_button(method, tag_value, options = {}) ⇒ Object
55 56 57 58 |
# File 'lib/coalla/builders/form_builder.rb', line 55 def (method, tag_value, = {}) 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, = {}) return unless policy.field_enabled?(method) label_class = [:label_class] || 'control-label col-md-4 col-lg-2' label_tag = label(method, [:title], class: label_class) unless [:hide_label] [:div_class] ||= COLS_CLASS div_tag = content_tag :div, controls.html_safe, class: [:div_class] content_tag :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, = {}, = {}, &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, = {}) row method, simple_format(@object.send(method), class: 'form-control-static'), end |
#static_text(method, options = {}) ⇒ Object
91 92 93 |
# File 'lib/coalla/builders/form_builder.rb', line 91 def static_text(method, = {}) row method, content_tag(:p, static_text_formatter.format(method), class: 'form-control-static'), end |
#string(method, options = {}) ⇒ Object
100 101 102 |
# File 'lib/coalla/builders/form_builder.rb', line 100 def string(method, = {}) row method, text_field(method, ()), end |
#textarea(method, options = {}) ⇒ Object
124 125 126 |
# File 'lib/coalla/builders/form_builder.rb', line 124 def textarea(method, = {}) row method, text_area(method, ()), end |
#time(method, options = {}) ⇒ Object
153 154 155 156 |
# File 'lib/coalla/builders/form_builder.rb', line 153 def time(method, = {}) value = object.try(method).try(:strftime, '%H:%M') string(method, ({data: {'calendar-time' => true}, div_class: 'col-md-2', value: value}, )) 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, = {}, = {}) 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, = {}, = {}) 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, = {}) textarea method, (merge_classes(, 'wymeditor')) end |