Module: Poodle::ActionView::FormHelper
- Defined in:
- lib/poodle/action_view/form_helper.rb
Overview
This module creates Bootstrap wrappers around basic View Tags
Instance Method Summary collapse
-
#theme_form_assoc_group(object, foreign_key, **options) ⇒ Object
Example assoc_collection = Client.select(“id, name”).order(“name ASC”).all options = client, assoc_display_method: :name, assoc_collection: Client.select(“id, name”).order(“name ASC”).all, label: “Client”, required: true theme_form_assoc_group(project, :client_id, **options) is equivalent to: ————————— <% Choose Client - Drop Down %> <div class=“form-group ”> <label for=“inp_name” class=“col-md-4 control-label”> Client <span class=“text-color-red ml-10 mr-5 pull-right”>*</span> </label> <div class=“col-md-8”> <% if editable && @client %> <%= @client.name %> <%= hidden_field_tag “project”, @client.id %> <% else %> <%= collection_select(:project, :client_id, Client.select(“id, name”).order(“name ASC”).all, :id, :name, :prompt=>true, => ‘form-control’) %> <% end %> </div> </div>.
-
#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”.
-
#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).
-
#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_select_group(form, 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: ————————— <div class=“form-group ”> <label for=“inp_name” class=“col-md-4 control-label”>Choose Plan <span class=“text-color-red ml-10 mr-5 pull-right”>*</span> </label> <div class=“col-md-8”> <% options_list = ConfigCenter::Roles::LIST %> <%= f.select(“proposal“, options_for_select(options_list, :selected => f.object.name), :prompt=>true, => ‘form-control’) %> </div> </div>.
Instance Method Details
#theme_form_assoc_group(object, foreign_key, **options) ⇒ Object
Example
assoc_collection = Client.select("id, name").order("name ASC").all
= {assoc_object: client, assoc_display_method: :name, assoc_collection: Client.select("id, name").order("name ASC").all, label: "Client", required: true}
theme_form_assoc_group(project, :client_id, **)
is equivalent to:
<% Choose Client - Drop Down %> <div class=“form-group ”>
<label for="inp_name" class="col-md-4 control-label">
Client
<span class="text-color-red ml-10 mr-5 pull-right">*</span>
</label>
<div class="col-md-8">
<% if editable && @client %>
<%= @client.name %>
<%= hidden_field_tag "project[client_id]", @client.id %>
<% else %>
<%= collection_select(:project, :client_id, Client.select("id, name").order("name ASC").all, :id, :name, {:prompt=>true}, {:class => 'form-control'}) %>
<% end %>
</div>
</div>
162 163 164 165 166 167 168 169 170 171 172 173 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/poodle/action_view/form_helper.rb', line 162 def theme_form_assoc_group(object, foreign_key, **) .symbolize_keys! assoc_method_name = foreign_key.to_s.chomp("_id") .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: true, editable: true, error_class: "has-error" ) # Populating Errors errors = object.errors[foreign_key.to_s] # if foreign_key is an id, check errors for the association # errors = project.errors[:client_id] + project.errors[:client] if object.errors[foreign_key.to_s.gsub("_id", "")] errors += object.errors[foreign_key.to_s.gsub("_id", "")] end error_class = errors.any? ? [:error_class] : "" if errors.any? error_class = [:error_class] = content_tag(:span, errors.first, class: "help-block") else error_class = "" = "" end selected_id = object.send(foreign_key) theme_form_group([:label], required: [:required], error_class: error_class) do if ![:editable] && [:assoc_object] raw([:assoc_object].send([:assoc_display_method]) + hidden_field_tag("#{[:param_name]}[#{foreign_key}]", [:assoc_object].id)) else collection_select([:object_name], foreign_key, [:assoc_collection], :id, [:assoc_display_method], {prompt: [:prompt], selected: selected_id}, {:class => 'form-control'}) end + 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 , "data-reset-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”
250 251 252 253 |
# File 'lib/poodle/action_view/form_helper.rb', line 250 def (object, ="", ="Saving ...", classes="btn btn-primary ml-10") = "#{object.new_record? ? "Create" : "Update"}" if .blank? submit_tag(, "data-reset-text"=>, "data-loading-text"=>, :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)
<div class=“form-group ”>
<label class="col-md-4 control-label" for="inp_name">
Name
<span class="text-color-red ml-10 mr-5 pull-right">*</span>
</label>
<div class="col-md-8">
<input class="text input form-control" id="inp_name" name="link_type[name]" placeholder="" type="text">
</div>
</div>
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 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 |
# File 'lib/poodle/action_view/form_helper.rb', line 97 def theme_form_field(object, field_name, **) .symbolize_keys! .reverse_merge!( object_name: object.class.name.underscore, label: field_name.to_s.gsub("_", " ").titleize, required: true, error_class: "has-error", html_options: {} ) .reverse_merge!( param_name: "#{[:object_name]}[#{field_name}]" ) if object.errors[field_name.to_s].any? error_class = [:error_class] = content_tag(:span, object.errors[field_name].first, class: "help-block") else error_class = "" = "" end theme_form_group([:label], required: [:required], error_class: error_class) do [:html_options].reverse_merge!( type: "text", id: "inp_#{[:label].to_s.gsub(" ", "_").downcase}", class: "text input form-control", place_holder: "" ) case [:html_options][:type].to_sym when :text, :email, :search, :password, :date, :time, :tel, :url, :month text_field_tag([:param_name], object.send(field_name.to_s), **[:html_options]) when :textarea [:html_options].merge!(style: "height: 80px;") text_area_tag([:param_name], object.send(field_name.to_s), **[:html_options]) when :file file_field_tag([:param_name], **[:html_options]) when :checkbox [:html_options][:class] = "checkbox mt-10" check_box_tag([:param_name], field_name, object.send(field_name.to_s), **[:html_options]) end + 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 |
# File 'lib/poodle/action_view/form_helper.rb', line 58 def theme_form_group(label, **) .reverse_merge!( error_class: "", param_name: label.gsub(" ", "_").underscore, required: true, label_col_class: "col-md-4", field_col_class: "col-md-8" ) content_tag(:div, class: "form-group #{[:error_class]}") do content_tag(:label, class: "#{[:label_col_class]} control-label") do star_content = [:required] ? "*" : raw(" ") raw(label + content_tag(:span, star_content, class: "text-color-red ml-10 mr-5 pull-right")) end + content_tag(:div, class: [:field_col_class]) do if block_given? yield else "No Block Passed" end end end end |
#theme_form_select_group(form, object, field_name, options_list, **options) ⇒ Object
Example
roles = ConfigCenter::Roles::LIST
= Array[*roles.collect {|v,i| [v,v] }].sort
theme_form_select_group(f, @proposal, :plan, , label: "Choose Plan", param_name: "proposal[:plan]", prompt: true)
is equivalent to:
<div class=“form-group ”>
<label for="inp_name" class="col-md-4 control-label">Choose Plan
<span class="text-color-red ml-10 mr-5 pull-right">*</span>
</label>
<div class="col-md-8">
<% options_list = ConfigCenter::Roles::LIST %>
<%= f.select("proposal[:plan]", options_for_select(options_list, :selected => f.object.name), {:prompt=>true}, {:class => 'form-control'}) %>
</div>
</div>
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/poodle/action_view/form_helper.rb', line 222 def theme_form_select_group(form, object, field_name, , **) .reverse_merge!( label: "Label", param_name: "Param", prompt: true, error_class: "has-error", required: false ) error_class = object.errors[field_name.to_s].any? ? [:error_class] : "" if object.errors[field_name.to_s].any? error_class = [:error_class] = content_tag(:span, object.errors[field_name].first, class: "help-block") else error_class = "" = "" end theme_form_group([:label], required: [:required], error_class: error_class) do form.select([:param_name], (, :selected => object.send(field_name)), {:prompt=>[:prompt]}, {:class => 'form-control'}) + end end |