Module: Kuppayam::ActionView::FormHelper

Defined in:
lib/kuppayam/action_view/form_helper.rb

Overview

This module creates Bootstrap wrappers around basic View Tags

Instance Method Summary collapse

Instance Method Details

#theme_form_assoc_group(object, foreign_key, **options) ⇒ Object

Example assoc_collection = Customer.select("id, name").order("name ASC").all options = customer, assoc_display_method: :name, assoc_collection: Customer.select("id, name").order("name ASC").all, label: "Customer", required: true theme_form_assoc_group(project, :customer_id, **options) is equivalent to:

<% Choose Customer - Drop Down %>

<% if editable && @customer %> <%= @customer.name %> <%= hidden_field_tag "project[customer_id]", @customer.id %> <% else %> <%= collection_select(:project, :customer_id, Customer.select("id, name").order("name ASC").all, :id, :name, :prompt=>true, => 'form-control') %> <% end %>


190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/kuppayam/action_view/form_helper.rb', line 190

def theme_form_assoc_group(object, foreign_key, **options)
  options.symbolize_keys!
  assoc_method_name = foreign_key.to_s.chomp("_id")
  options.reverse_merge!(
    assoc_object: object.respond_to?(assoc_method_name) ? object.send(assoc_method_name) : nil,
    assoc_display_method: :name,
    assoc_collection: [],
    param_name: object.class.name.underscore,
    object_name: object.class.name.underscore,
    required: true,
    label: foreign_key.to_s.titleize,
    prompt: "Please Select",
    editable: true,
    error_class: "has-error",
    form_style: "left-right"
  )

  # Populating Errors
  errors = object.errors[foreign_key.to_s]
  # if foreign_key is an id, check errors for the association
  # errors = project.errors[:customer_id] + project.errors[:customer]
  if object.errors[foreign_key.to_s.gsub("_id", "")]
    errors += object.errors[foreign_key.to_s.gsub("_id", "")]
  end

  error_class = errors.any? ? options[:error_class] : ""
  if errors.any?
    error_class =  options[:error_class]
    error_message = (:span, errors.first, class: "help-block")
  else
    error_class = ""
    error_message = ""
  end

  selected_id = object.send(foreign_key)
  
  theme_form_group(options[:label], required: options[:required], error_class: error_class, form_style: options[:form_style]) do
    if !options[:editable] && options[:assoc_object]
      raw(options[:assoc_object].send(options[:assoc_display_method]) + hidden_field_tag("#{options[:param_name]}[#{foreign_key}]", options[:assoc_object].id))
    else
      collection_select(options[:object_name], foreign_key, options[:assoc_collection], :id, options[:assoc_display_method], {include_blank: options[:include_blank], selected: selected_id}, {:class => 'form-control'})
    end + error_message
  end
end

#theme_form_button(object, button_text = "", saving_message = "Saving ...", classes = "btn btn-primary ml-10") ⇒ Object

Creates a submit button with basic styles Example submit_tag button_text, "data-reset-text"=>button_text, "data-loading-text"=>"Saving ...", :class=>"btn btn-primary ml-10" is equivalent to:

submit_tag button_text, "data-reset-text"=>button_text, "data-loading-text"=>"Saving ...", :class=>"btn btn-primary ml-10"



284
285
286
287
# File 'lib/kuppayam/action_view/form_helper.rb', line 284

def theme_form_button(object, button_text="", saving_message="Saving ...", classes="btn btn-primary ml-10")
  button_text = "#{object.new_record? ? "Create" : "Update"}" if button_text.blank?
  submit_tag(button_text, "data-reset-text"=>button_text, "data-loading-text"=>saving_message, :class=>classes)
end

#theme_form_field(object, field_name, **options) ⇒ Object

Creates the form group with label, and text field Supports the following input type: "text", "email", "search", "password", "date", "time", "tel", "url", "month" Example theme_form_field(@project, :name)



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/kuppayam/action_view/form_helper.rb', line 115

def theme_form_field(object, field_name, **options)
  options.symbolize_keys!
  options.reverse_merge!(
    object_name: object.class.name.underscore,
    label: field_name.to_s.gsub("_", " ").titleize,
    required: true,
    error_class: "has-error",
    html_options: { readonly: false },
    form_style: "left-right"
  )
  options.reverse_merge!(
    param_name: "#{options[:object_name]}[#{field_name}]"
  )

  if object.errors[field_name.to_s].any?
    error_class =  options[:error_class]
    error_message = (:span, object.errors[field_name].first, class: "help-block")
  else
    error_class = ""
    error_message = ""
  end

  theme_form_group(options[:label], required: options[:required], error_class: error_class, form_style: options[:form_style]) do
    options[:html_options].reverse_merge!(
      type: "text",
      id: "inp_#{options[:label].to_s.gsub(" ", "_").downcase}",
      class: "text input form-control",
      place_holder: ""
    )
    case options[:html_options][:type].to_sym
    when :text, :email, :search, :password, :time, :tel, :url, :month, :number
      text_field_tag(options[:param_name], object.send(field_name.to_s), **options[:html_options])
    when :date
      begin
        date_value = object.send(field_name.to_s).strftime("%Y-%m-%d")
      rescue
        date_value = Date.today.strftime("%Y-%m-%d")
      end
      options[:html_options].reverse_merge!(value: date_value)
      text_field_tag(options[:param_name], object.send(field_name.to_s), **options[:html_options])
    when :textarea
      options[:html_options].reverse_merge!(style: "height: 100px;")
      text_area_tag(options[:param_name], object.send(field_name.to_s), **options[:html_options])
    when :file
      file_field_tag(options[:param_name], **options[:html_options])
    when :checkbox
      options[:html_options][:class] = "iswitch iswitch-secondary checkbox mt-10"
      current_value = object.send(field_name.to_s)
      options[:html_options][:checked] = current_value
      check_box_tag(options[:param_name], "1", current_value, **options[:html_options])
    end + error_message
  end
end

#theme_form_group(label, **options) ⇒ Object

Example 4 If you want to add error_class to form-group div tag you can pass it through options

theme_form_group("Please select a Movie", error_class: "error-class") do
....
end


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
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/kuppayam/action_view/form_helper.rb', line 58

def theme_form_group(label, **options)
  options.reverse_merge!(
    error_class: "",
    param_name: label.gsub(" ", "_").underscore,
    required: true,
    form_style: "left-right"
  )

  if options[:form_style] == "left-right"
    options.reverse_merge!(
      error_class: "",
      param_name: label.gsub(" ", "_").underscore,
      required: true,
      label_col_class: "col-md-4",
      field_col_class: "col-md-8"
    )
  elsif options[:form_style] == "top-down" || options[:form_style] == "top-bottom"
    options.reverse_merge!(
      error_class: "",
      param_name: label.gsub(" ", "_").underscore,
      required: true,
      label_col_class: "col-md-12 text-align-left pb-2",
      field_col_class: "col-md-12"
    )
  end


  (:div, class: "form-group #{options[:error_class]}") do
    (:label, class: "#{options[:label_col_class]} control-label kuppayam-label") do
      star_content = options[:required] ? "*" : raw("&nbsp;&nbsp;")
      raw(label + (:span, star_content, class: "text-color-red ml-10 mr-5"))
    end +
    (:div, class: options[:field_col_class]) do
      if block_given?
        yield
      else
        "No Block Passed"
      end
    end
  end
end

#theme_form_select_group(object, field_name, options_list, **options) ⇒ Object

Example roles = ConfigCenter::Roles::LIST

options_list = Array[*roles.collect {|v,i| [v,v] }].sort theme_form_select_group(f, @proposal, :plan, options_list, label: "Choose Plan", param_name: "proposal", prompt: true) is equivalent to:

<% options_list = ConfigCenter::Roles::LIST %> <%= f.select("proposal[:plan]", options_for_select(options_list, :selected => f.object.name), :prompt=>true, => 'form-control') %>


250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/kuppayam/action_view/form_helper.rb', line 250

def theme_form_select_group(object, field_name, options_list, **options)
  options.reverse_merge!(
    label: field_name.to_s.titleize,
    param_name: field_name,
    prompt: "Select",
    error_class: "has-error",
    required: false,
    form_style: "left-right"
  )
  error_class = object.errors[field_name.to_s].any? ? options[:error_class] : ""
  if object.errors[field_name.to_s].any?
    error_class =  options[:error_class]
    error_message = (:span, object.errors[field_name].first, class: "help-block")
  else
    error_class = ""
    error_message = ""
  end
  begin
    selected = object.send(field_name)
    selected = options_list.first.last if selected.blank?
  rescue
    selected = nil
  end
  theme_form_group(options[:label], required: options[:required], error_class: error_class, form_style: options[:form_style]) do
    select_tag(options[:param_name], options_for_select(options_list, :selected => selected), {:prompt=>options[:prompt], :class => 'form-control'}) + error_message
  end
end