Module: AccessKit::UrlHelperAdditions

Extended by:
ActiveSupport::Concern
Defined in:
lib/access_kit/url_helper_additions.rb

Constant Summary collapse

DEFAULT_KEY_NAME_FOR_PRECEDING_TEXT =
:intro
DEFAULT_KEY_NAME_FOR_FOLLOWING_TEXT =
:outro
DEFAULT_CSS_CLASS_FOR_SUPPLEMENT =
"access_kit"

Class Method Summary collapse

Class Method Details

.add_supplement_to_text(text, opts = {}) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/access_kit/url_helper_additions.rb', line 11

def add_supplement_to_text(text, opts={})        
  default_opts = {  DEFAULT_KEY_NAME_FOR_PRECEDING_TEXT => nil, 
                    DEFAULT_KEY_NAME_FOR_FOLLOWING_TEXT => nil, 
                    :auto_pad => true, 
                    :css_class_for_supplement => css_class_for_supplement
  }
  opts = default_opts.merge(opts)

  padding = begin
    if opts[:auto_pad] === true
      ' '
    elsif opts[:auto_pad] === false
      ''
    else
      opts[:auto_pad].to_s
    end        
  end
  
  before_span = span_tag opts[DEFAULT_KEY_NAME_FOR_PRECEDING_TEXT], :padding_after => padding, :class => opts[:css_class_for_supplement]
  after_span  = span_tag opts[DEFAULT_KEY_NAME_FOR_FOLLOWING_TEXT], :padding_before => padding, :class => opts[:css_class_for_supplement]
  
  "#{before_span}#{text}#{after_span}".html_safe
end

Returns:

  • (Boolean)


35
36
37
38
39
40
# File 'lib/access_kit/url_helper_additions.rb', line 35

def args_for_link_to_with_supplement?(*args)
  args[2].is_a?(Hash) && ( 
    args[2].keys.include?(DEFAULT_KEY_NAME_FOR_PRECEDING_TEXT) || 
    args[2].keys.include?(DEFAULT_KEY_NAME_FOR_FOLLOWING_TEXT) 
  )
end

TODO: maybe rename to ‘process_args’



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/access_kit/url_helper_additions.rb', line 43

def process_options_for_link_to_with_supplement(*args)
  if args_for_link_to_with_supplement?(*args)
    # TODO: move to separate method: 'extract_supplement_opts'
    supplement_opts = [ DEFAULT_KEY_NAME_FOR_PRECEDING_TEXT, DEFAULT_KEY_NAME_FOR_FOLLOWING_TEXT, :auto_pad, :css_class_for_supplement ].inject({}) do |memo, k|
      value = args[2].delete(k)
      value.blank? ? memo : memo.merge( k => value )
    end
  
    new_args = args.dup
    # replace the name argument with new_name
    new_args[0] = AccessKit::UrlHelperAdditions.add_supplement_to_text(args[0], supplement_opts)
    new_args
  else
    nil
  end
end