Module: ScaffoldLogic::FormHelper

Defined in:
lib/scaffold_logic/form_helper.rb

Constant Summary collapse

SELECT_PROMPT_OPTION =
'<option>Select...</option>'

Instance Method Summary collapse

Instance Method Details

#checkmarkObject

Returns a check mark.

Usage:

<%= checkmark -%>


10
11
12
# File 'lib/scaffold_logic/form_helper.rb', line 10

def checkmark
  %{<div class="checkmark"></div>}.html_safe
end

#count_field(field_id, update_id, options = {}) ⇒ Object



19
20
21
22
# File 'lib/scaffold_logic/form_helper.rb', line 19

def count_field(field_id,update_id,options = {})
  function = "$('##{update_id}').innerHTML = $('##{field_id}').val().length;"
  count_field_tag(field_id,function,options)
end

#count_field_tag(field_id, function, options = {}) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/scaffold_logic/form_helper.rb', line 24

def count_field_tag(field_id,function,options = {})
  %{
    <script type="text/javascript">
      Event.observe($('##{field_id}'), 'keydown', function (){ #{function} });
    </script>
  }.html_safe
end

#countdown_field(field_id, update_id, max, options = {}) ⇒ Object



14
15
16
17
# File 'lib/scaffold_logic/form_helper.rb', line 14

def countdown_field(field_id,update_id,max,options = {})
  function = "$('##{update_id}').innerHTML = (#{max} - $('##{field_id}').val().length);"
  count_field_tag(field_id,function,options)
end

#faux_field(label, content, options = {:field_id => nil, :help => nil}) ⇒ Object

Returns a simulated text-field for read-only versions of a form.

Usage:

<%= faux_field 'Name', @user.name -%>


37
38
39
40
41
42
43
44
# File 'lib/scaffold_logic/form_helper.rb', line 37

def faux_field(label, content, options={:field_id => nil, :help => nil})
  if options[:help]
    options[:field_id] ||= "field_#{Time.zone.now.to_i}#{rand(10000)}"
    %{#{tag_for_label_with_inline_help label, options[:field_id], options[:help]}<div class="faux_field">#{content}</div>}.html_safe
  else
    %{<label>#{label}</label><div class="faux_field">#{content}</div>}.html_safe
  end
end

#legend_tag(text, options = {}) ⇒ Object

Returns a legend tag that renders correctly in all browsers.

Usage:

<%= legend_tag "Report Criteria" -%>

With help text:

<%= legend_tag "Report Criteria", :help => "Some descriptive copy here." -%>
  <span id="hide_or_show_backlinks" class="show_link" style="background-color: #999999;
  border: 1px solid #999999;" onclick="javascript:hide_or_show('backlinks');"></span>Backlinks (<%=
  @google_results.size -%>)
<%- end -%>

Recommended CSS to support display of help icon and text:

.help_icon {
  display: block;
  float: left;
  margin-top: -16px;
  margin-left: 290px;
  border-left: 1px solid #444444;
  padding: 3px 6px;
  cursor: pointer;
}

div.popup_help {
  color: #666666;
  width: 98%;
  background: #ffffff;
  padding: 1em;
  border: 1px solid #999999;
  margin: 1em 0em;
}


80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/scaffold_logic/form_helper.rb', line 80

def legend_tag(text, options={})
  options[:id] ||= "#{text.downcase.gsub(' ', '-')}-legend"
  if options[:help]
    _html = %{<div id="#{options[:id]}" class="faux_legend">#{text}<span class="help_icon" onclick="$('##{options[:id]}-help').toggle();">?</span></div>\r}
    _html << %{<div id="#{options[:id]}-help" class="popup_help" style="display: none;">#{options[:help]}<br /></div>\r}
  else
    _html = %{<div id="#{options[:id]}" class="faux_legend">#{text}</div>\r}
  end
  _html.gsub!(/ id=""/,'')
  _html.gsub!(/ class=""/,'')
  _html.html_safe
end

#tag_for_label_with_inline_help(label_text, field_id, help_text) ⇒ Object

Create a set of tags for displaying a field label with inline help. Field label text is appended with a ? icon, which responds to a click by showing or hiding the provided help text.

Sample usage:

<%= tag_for_label_with_inline_help 'Relative Frequency', 'rel_frequency', 'Relative frequency of search traffic for this keyword across multiple search engines, as measured by WordTracker.' %>

Yields:

<label for="rel_frequency">Relative Frequency: <%= image_tag "/images/help_icon.png", :onclick => "$('#rel_frequency_help').toggle();", :class => 'inline_icon' %></label>
<div class="inline_help" id="rel_frequency_help" style="display: none;">
  <p>Relative frequency of search traffic for this keyword across multiple search engines, as measured by WordTracker.</p>
</div>


107
108
109
110
111
112
113
114
115
116
# File 'lib/scaffold_logic/form_helper.rb', line 107

def tag_for_label_with_inline_help( label_text, field_id, help_text )
  _html = ""
  _html << %{<label for="#{field_id}">#{label_text}}
  _html << %{<img alt="Help Icon" class="inline_icon" src="/images/icons/help_icon.png" width="14" height="12" onclick="$('##{field_id}_help').toggle();" />}
  _html << %{</label>}
  _html << %{<div class="inline_help" id="#{field_id}_help" style="display: none;">}
  _html << %{<p>#{help_text}</p>}
  _html << %{</div>}
  _html
end

#tag_for_label_with_instructions(label_text, field_id, instructions) ⇒ Object

Create a set of tags for displaying a field label followed by instructions. The instructions are displayed on a new line following the field label.

Usage:

<%= tag_for_label_with_instructions 'Status', 'is_active', 'Only active widgets will be visible to the public.' %>

Yields:

<label for="is_active">
  Status
  <span class="instructions">Only active widgets will be visible to the public.</span>
</label>


131
132
133
134
135
136
137
# File 'lib/scaffold_logic/form_helper.rb', line 131

def tag_for_label_with_instructions( label_text, field_id, instructions )
  _html = ""
  _html << %{<label for="#{field_id}">#{label_text}}
  _html << %{<span class="instructions">#{instructions}</span>}
  _html << %{</label>}
  _html
end

#tags_for_option_values(a, selected = nil, prompt_option = SELECT_PROMPT_OPTION) ⇒ Object

Returns a string of HTML option tags for the given array of option values. Specify the selected value for stickiness.

Usage:

<%= select_tag :user, tags_for_option_values(User.all.map{ |c| [c.name, c.id] }, params[:user]) -%>


144
145
146
# File 'lib/scaffold_logic/form_helper.rb', line 144

def tags_for_option_values( a, selected = nil, prompt_option = SELECT_PROMPT_OPTION )
  "#{prompt_option}#{a.map{ |_e| _flag = _e[1].to_s == selected ? 'selected="1"' : ''; _e.is_a?(Array) ? "<option value=\"#{_e[1]}\" #{_flag}>#{_e[0]}</option>" : "<option>#{_e}</option>" }}".html_safe
end