Module: LinkToActiveState::ViewHelpers::UrlHelper

Defined in:
lib/link_to_active_state/view_helpers/url_helper.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



7
8
9
# File 'lib/link_to_active_state/view_helpers/url_helper.rb', line 7

def self.included(base)
  base.send(:alias_method_chain, :link_to, :active_state)
end

Instance Method Details



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/link_to_active_state/view_helpers/url_helper.rb', line 11

def link_to_with_active_state(*args, &block)
  link_options, html_options = if block_given?
    [args.first, args.second]
  else
    [args[1], args[2]]
  end

  options = {}
  link_options ||= {}
  html_options ||= {}
  wrapper_options = html_options.delete(:active_wrapper_options) || {}

  if html_options.present? && html_options[:active_on].present?
    active_on = html_options.delete(:active_on)
    active_state = html_options.delete(:active_state) || "active"

    if is_active?(active_on, link_options, html_options)
      case active_state
      when Proc
        options = options.merge(active_state.call(html_options))
        wrapper_options = wrapper_options.merge(active_state.call(html_options))
      when String
        options[:class] = merge_class(html_options[:class], active_state)
        wrapper_options[:class] = merge_class(wrapper_options[:class], active_state)
      end
    end
  end

  if html_options.present? && html_options[:active_wrapper]
    element_or_proc = html_options.delete(:active_wrapper)

    render_with_wrapper(element_or_proc, wrapper_options) do
      link_to_without_active_state(*args, &block)
    end
  else
    html_options.merge!(options)
    link_to_without_active_state(*args, &block)
  end
end