Module: Rapid::Web::SelectHelpers

Defined in:
lib/rapid/web/select_helpers.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.registered(app) ⇒ Object



6
7
8
9
# File 'lib/rapid/web/select_helpers.rb', line 6

def self.registered(app)
  app.set :default_builder, 'Rapid::Web::SettingsFormBuilder'
  app.helpers self
end

Instance Method Details

#options_for_select(option_items, selected_values = []) ⇒ Object

Returns the options tags for a select based on the given option items



12
13
14
15
16
17
18
19
20
21
# File 'lib/rapid/web/select_helpers.rb', line 12

def options_for_select(option_items, selected_values=[])
  return '' if option_items.blank?
  selected_values = [selected_values].compact unless selected_values.is_a?(Array)
  items = option_items.collect { |caption, value|
    value ||= caption
    selected = selected_values.find {|v| v.to_s =~ /^(#{Regexp.escape(value)}|#{Regexp.escape(caption)})$/}
    (:option, caption, :value => value, :selected => !!selected)
  }
  items.join("\n")
end

#time_zone_options_for_select(options = {}) ⇒ Object

File actionpack/lib/action_view/helpers/form_options_helper.rb, line 504



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rapid/web/select_helpers.rb', line 34

def time_zone_options_for_select(options = {})
  selected = options.delete(:selected)
  priority_zones = options.delete(:priority)
  model = options.delete(:model) || ::ActiveSupport::TimeZone
  include_blank = options.delete(:include_blank)
  
  zone_options = ""
  zone_options += %(<option value=""></option>) if include_blank

  zones = model.all
  convert_zones = lambda { |list| list.map { |z| [ z.to_s, z.name ] } }

  if priority_zones
          if priority_zones.is_a?(Regexp)
      priority_zones = model.all.find_all {|z| z =~ priority_zones}
          end
    zone_options += options_for_select(convert_zones[priority_zones], selected)
    zone_options += "<option value=\"\" disabled=\"disabled\">-------------</option>\n"

    zones = zones.reject { |z| priority_zones.include?( z ) }
  end

  zone_options += options_for_select(convert_zones[zones], selected)
  zone_options.html_safe
end

#time_zone_select(name, options = {}) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/rapid/web/select_helpers.rb', line 23

def time_zone_select name, options = {}
  options.reverse_merge!(:name => name)
  collection, fields = options.delete(:collection), options.delete(:fields)
  options[:options] = options_from_collection(collection, fields) if collection
  # options[:options].unshift('') if options.delete(:include_blank)
  select_options_html = time_zone_options_for_select(options)
  options.merge!(:name => "#{options[:name]}[]") if options[:multiple]
  (:select, select_options_html, options)
end