Module: Grandstand::MainHelper

Defined in:
app/helpers/grandstand/main_helper.rb

Instance Method Summary collapse

Instance Method Details

#active_on(*conditions) ⇒ Object

Returns ‘active’ if the current page matches the passed URL hash. This will require the controller to match. If one or more action is passed, those actions (or params) must match as well. If not, it lets the controller mean it’s active.



6
7
8
9
10
11
12
13
14
15
16
17
# File 'app/helpers/grandstand/main_helper.rb', line 6

def active_on(*conditions)
  match = lambda do |condition|
    controllers = Array(condition.delete(:controller)).compact.flatten.map(&:to_s).uniq
    actions = Array(condition.delete(:action)).compact.map(&:to_s).flatten.uniq
    controllers.include?(controller_name) && (actions.empty? || actions.include?(action_name) || actions.include?(params[:return_to]))
  end
  options = {}
  if conditions.any?(&match)
    options[:class] = 'active'
  end
  options
end

#button(*args) ⇒ Object

Renders a <button> tag. Helpful for forms and the like.

<%= button("Save Changes", :class => "blue") %>

… produces

<button class="button blue"><span class="inner"><span>Save Changes</span></span></button>


26
27
28
29
30
31
# File 'app/helpers/grandstand/main_helper.rb', line 26

def button(*args)
  label = args.shift
  options = {:name => 'submit', :value => label}.merge(args.extract_options! || {})
  options, icon = button_options(options)
  (:button, options) { (:span, :class => icon ? "#{icon} icon" : nil) { label } }
end

Similar to button, but generates a link instead of a button element. Useful for providing buttons to GET actions:

<%= button_link_to("Get Help", support_path, :icon => "help") %>

… produces

<a class="button blue" href="/support"><span class="inner"><span class="help icon">Get Help</span></span></button>

The extra spans are for any sliding door styling you may be interested in adding. Adding :icon to the options will give the inner-most span a class of “#:icon icon”, allowing you to add extra images inside of your button.



44
45
46
47
# File 'app/helpers/grandstand/main_helper.rb', line 44

def button_link_to(*args)
  options, icon = button_options(args.extract_options! || {})
  link_to((:span, :class => icon ? "#{icon} icon" : nil) { args.shift }, *args.push(options))
end

#button_options(options) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
# File 'app/helpers/grandstand/main_helper.rb', line 49

def button_options(options)
  classes = %w(button)
  if icon = options.delete(:icon)
    classes.push('has-icon')
  end
  if options.delete(:default)
    classes.push('default')
  end
  classes.push(options[:class].to_s.split(' ')) if options[:class]
  options[:class] = classes.uniq.join(' ')
  [options, icon]
end

#expand(*controllers) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
# File 'app/helpers/grandstand/main_helper.rb', line 62

def expand(*controllers)
  options = controllers.extract_options!
  options[:class] = Array(options[:class]).compact
  options[:class].push(:expandable)
  section = controllers.first
  controllers.map!(&:to_s)
  if controllers.include?(controller_name) || !((session[:expand] ||= []) & controllers).empty?
    options[:class].push(:expanded)
  end
  raw %( class="#{options[:class].join(' ')}")
end


74
75
76
# File 'app/helpers/grandstand/main_helper.rb', line 74

def expand_link(section)
  link_to(raw('<span></span>'), '#', :class => 'expand', :rel => section)
end

#hide(condition) ⇒ Object



78
79
80
# File 'app/helpers/grandstand/main_helper.rb', line 78

def hide(condition)
  raw ' style="display:none;"' if condition
end

#wrap_grandstand_form(&block) ⇒ Object

A form wrapper that’s used to override the default field_error_proc in a thread-safeish way. The new field_error_proc returns a <div> with class “errors” on it, instead of an irritating “fieldWithErrors” classname that nobody likes or wants to use. Only used in admin at the moment.



85
86
87
88
89
90
# File 'app/helpers/grandstand/main_helper.rb', line 85

def wrap_grandstand_form(&block)
  field_error_proc = ActionView::Base.field_error_proc
  ActionView::Base.field_error_proc = Proc.new {|html_tag, instance| raw("<div class=\"errors\">#{html_tag}</div>") }
  concat(capture(&block))
  ActionView::Base.field_error_proc = field_error_proc
end