Module: MirExtensions

Defined in:
lib/mir_form_builder.rb,
lib/mir_extensions.rb

Overview

Configuration

Configure your application to default to the custom form builder

In app/controllers/application_controller.rb:

class ApplicationController < ActionController::Base
  ActionView::Base.default_form_builder = MirFormBuilder

Usage

Default form field with label:

<%= f.text_field :last_name -%>

Returns:

<fieldset>
  <label for="user_last_name">Last Name</label><br />
  <input id="user_last_name" name="user[last_name]" size="30" type="text" />
</fieldset>

Form field with custom label:

<%= f.text_field :first_name, :label => 'Custom' -%>

Returns:

<fieldset>
  <label for="user_first_name">Custom</label><br />
  <input id="user_first_name" name="user[first_name]" size="30" type="text" />
</fieldset>

Form field with no label

<%= f.text_field :search, :label => false -%>

Returns:

<input id="search" name="search" size="30" type="text" />

Form field with inline help (? icon which reveals help content when clicked):

<%= f.password_field :password %>

Returns:

<fieldset>
  <label for="password">Password: <img src="/images/icons/help_icon.png"
  onclick="$('password_help').toggle();" class='inline_icon' /></label><br />
  <div class="inline_help" id="password_help" style="display: none;">
    <p>Here are some detailed instructions on valid passwords.</p>
  </div>
  <input id="user_password" name="user[password]" size="30" type="password" />
</fieldset>

Form field with instructions (show immediately below the label):

<%= f.password_field :password_confirmation %>

Returns:

<fieldset>
  <label for="password_confirmation">
    Confirm Password:
    <span class="instructions">Enter your password again to confirm.</span>
  </label><br />
  <input id="user_password_confirmation" name="user[password_confirmation]" size="30" type="password" />
</fieldset>

Check box with label in addition to checkbox value text

(E.g. ‘Foo’ appears above the checkbox, and ‘Something’ next to it):

<%= f.check_box :foo, :inline_label => 'Something' -%>

Returns:

<fieldset>
  <label for="user_foo">Foo</label><br />
  <input name="user[foo]" type="hidden" value="0" />
  <input id="user_foo" name="user[foo]" type="checkbox" value="1" />
  <label class="inline" for="user_foo">Something</label><br style='clear: both;'/><br />
</fieldset>

Don’t wrap fields in a fieldset

<%= f.text_field :query, :label => 'Search terms:', :fieldset => false -%>

Returns

<label for="search_terms_query"><br />
<input id="search_terms_query" name="search_terms_query" size="30" />

Troubleshooting

If you’re seeing double form labels, it’s because you still have <%= label -%> elements in your forms.

Defined Under Namespace

Modules: HelperExtensions Classes: MirFormBuilder

Constant Summary collapse

MONTHS =

Constants ======================================================================================

{0 => "JAN", 1 => "FEB", 2 => "MAR", 3 => "APR", 4 => "MAY", 5 => "JUN", 6 => "JUL", 7 => "AUG", 8 => "SEP", 9 => "OCT", 10 => "NOV", 11 => "DEC"}
STATE_CODES =
{ 'Alabama' => 'AL', 'Alaska' => 'AK', 'Arizona' => 'AZ', 'Arkansas' => 'AR', 'California' => 'CA', 'Colorado' => 'CO', 'Connecticut' => 'CT', 'Delaware' => 'DE', 'Florida' => 'FL', 'Georgia' => 'GA', 'Hawaii' => 'HI', 'Idaho' => 'ID', 'Illinois' => 'IL', 'Indiana' => 'IN', 'Iowa' => 'IA', 'Kansas' => 'KS', 'Kentucky' => 'KY', 'Louisiana' => 'LA', 'Maine' => 'ME', 'Maryland' => 'MD', 'Massachusetts' => 'MA', 'Michigan' => 'MI', 'Minnesota' => 'MN', 'Mississippi' => 'MS', 'Missouri' => 'MO', 'Montana' => 'MT', 'Nebraska' => 'NE', 'Nevada' => 'NV', 'New Hampshire' => 'NH', 'New Jersey' => 'NJ', 'New Mexico' => 'NM', 'New York' => 'NY', 'North Carolina' => 'NC', 'North Dakota' => 'ND', 'Ohio' => 'OH', 'Oklahoma' => 'OK', 'Oregon' => 'OR', 'Pennsylvania' => 'PA', 'Puerto Rico' => 'PR', 'Rhode Island' => 'RI', 'South Carolina' => 'SC', 'South Dakota' => 'SD', 'Tennessee' => 'TN', 'Texas' => 'TX', 'Utah' => 'UT', 'Vermont' => 'VT', 'Virginia' => 'VA', 'Washington' => 'WA', 'Washington DC' => 'DC', 'West Virginia' => 'WV', 'Wisconsin' => 'WI', 'Wyoming' => 'WY', 'Alberta' => 'AB', 'British Columbia' => 'BC', 'Manitoba' => 'MB', 'New Brunswick' => 'NB', 'Newfoundland and Labrador' => 'NL', 'Northwest Territories' => 'NT', 'Nova Scotia' => 'NS', 'Nunavut' => 'NU', 'Ontario' => 'ON', 'Prince Edward Island' => 'PE', 'Quebec' => 'QC', 'Saskatchewan' => 'SK', 'Yukon' => 'YT' }

Class Method Summary collapse

Class Method Details

.canonical_url(url) ⇒ Object

General Utility Methods ========================================================================



15
16
17
# File 'lib/mir_extensions.rb', line 15

def self.canonical_url(url)
  (url + '/').gsub(/\/\/$/,'/')
end

.month_name_for(number) ⇒ Object



19
20
21
# File 'lib/mir_extensions.rb', line 19

def self.month_name_for(number)
  MONTHS[number]
end

.normalize_slug(text) ⇒ Object



23
24
25
26
27
# File 'lib/mir_extensions.rb', line 23

def self.normalize_slug(text)
  _normalized = text.gsub(/[\W]/u, ' ').strip.gsub(/\s+/u, '-').gsub(/-\z/u, '').downcase.to_s
  _normalized.gsub!(/[-]+/, '-') # truncate multiple -
  _normalized
end

.state_code_for(state_name) ⇒ Object



29
30
31
# File 'lib/mir_extensions.rb', line 29

def self.state_code_for(state_name)
  STATE_CODES[state_name]
end

.state_name_for(abbreviation) ⇒ Object



33
34
35
# File 'lib/mir_extensions.rb', line 33

def self.state_name_for(abbreviation)
  STATE_CODES.invert[abbreviation]
end