Module: HelperMethods

Defined in:
lib/helper_methods.rb,
lib/helper_methods/version.rb

Constant Summary collapse

VERSION =
"0.0.20"

Instance Method Summary collapse

Instance Method Details



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/helper_methods.rb', line 77

def active_link_to(*args, &block)
  if block_given?
    name          = capture(&block)
    options       = args[0] || {}
    html_options  = args[1] || {}
  else
    name          = args[0]
    options       = args[1] || {}
    html_options  = args[2] || {}
  end
  url = url_for(options)

  active_options  = { }
  link_options    = { }
  html_options.each do |k, v|
    if [:active, :class_active, :class_inactive, :active_disable, :wrap_tag].member?(k)
      active_options[k] = v
    else
      link_options[k] = v
    end
  end

  css_class = link_options.delete(:class).to_s + ' '
  css_class << active_link_to_class(url, active_options)
  css_class.strip!

  wrap_tag = active_options[:wrap_tag].present? ? active_options[:wrap_tag] : nil
  link_options[:class] = css_class if css_class.present?

  link = if active_options[:active_disable] === true && is_active_link?(url, active_options[:active])
    (:span, name, link_options)
  else
    link_to(name, url, link_options)
  end

  wrap_tag ? (wrap_tag, link, :class => (css_class if css_class.present?)) : link
end


115
116
117
118
119
120
121
# File 'lib/helper_methods.rb', line 115

def active_link_to_class(url, options = {})
  if is_active_link?(url, options[:active])
    options[:class_active] || 'active'
  else
    options[:class_inactive] || ''
  end
end

#attachment_url(file, style = :original) ⇒ Object



63
64
65
66
# File 'lib/helper_methods.rb', line 63

def attachment_url(file, style = :original)
  protocol = request.ssl? ? 'https' : 'http'
  "#{protocol}//#{request.host_with_port}#{file.url(style)}"
end

#bootstrap_badge(text, name = "") ⇒ Object



54
55
56
# File 'lib/helper_methods.rb', line 54

def bootstrap_badge(text, name="")
  return %(<span class="#{name}">#{text}</span>).html_safe
end

#bootstrap_icon(name, text = "", direction = "l") ⇒ Object



45
46
47
48
# File 'lib/helper_methods.rb', line 45

def bootstrap_icon(name, text="", direction="l")
  return %(<span class="#{name}"></span> #{text}).html_safe if direction == 'l'
  return %(#{text} <span class="#{name}"></span>).html_safe if direction == 'r'
end

#bootstrap_label(text, name = "") ⇒ Object



50
51
52
# File 'lib/helper_methods.rb', line 50

def bootstrap_label(text, name="")
  return %(<span class="#{name}">#{text}</span>).html_safe
end

#error_messages_for(resource, style = 'danger') ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/helper_methods.rb', line 7

def error_messages_for(resource, style = 'danger')
  if resource.errors.any?
     :div, class: "alert alert-#{style} alert-dismissable" do
       :ol do
        resource.errors.collect do |key, value|
           :li, value
        end.join.html_safe
      end
    end
  end
end

#flash_messagesObject



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/helper_methods.rb', line 19

def flash_messages
  if flash.any?
     :div, class: "alert alert-success alert-dismissable" do
       :ul do
        flash.collect do |key, value|
          unless [true, false, nil].include?(value)
             :li, value
          end
        end.join.html_safe
      end
    end
  end
end

#full_url_for(resource) ⇒ Object



68
69
70
71
# File 'lib/helper_methods.rb', line 68

def full_url_for(resource)
  protocol = request.ssl? ? 'https' : 'http'
  "#{protocol}//#{request.host_with_port}#{url_for(resource)}"
end

#gravatar(email, html_options = {}) ⇒ Object



58
59
60
61
# File 'lib/helper_methods.rb', line 58

def gravatar(email, html_options = {})
  email = Digest::MD5.hexdigest(email)
  image_tag "http://www.gravatar.com/avatar/#{email}?size=48", html_options
end

#is_active_link?(url, condition = nil) ⇒ Boolean

Returns:

  • (Boolean)


123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/helper_methods.rb', line 123

def is_active_link?(url, condition = nil)
  url = url_for(url).sub(/\?.*/, '') # ignore GET params
  case condition
  when :inclusive, nil
    !request.fullpath.match(/^#{Regexp.escape(url).chomp('/')}(\/.*|\?.*)?$/).blank?
  when :exclusive
    !request.fullpath.match(/^#{Regexp.escape(url)}\/?(\?.*)?$/).blank?
  when Regexp
    !request.fullpath.match(condition).blank?
  when Array
    controllers = [*condition[0]]
    actions     = [*condition[1]]
    (controllers.blank? || controllers.member?(params[:controller])) &&
    (actions.blank? || actions.member?(params[:action]))
  when TrueClass
    true
  when FalseClass
    false
  end
end

#mobile_device?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/helper_methods.rb', line 33

def mobile_device?
	request.user_agent =~ /Mobile|webOS/
end

#qr_code_for(data, size) ⇒ Object



73
74
75
# File 'lib/helper_methods.rb', line 73

def qr_code_for(data, size)
  image_tag "https://chart.googleapis.com/chart?chs=#{size}&cht=qr&chl=#{data}"
end


41
42
43
# File 'lib/helper_methods.rb', line 41

def youtube_link(video)
  link_to "www.youtube.com/watch?v=#{video}", "http://www.youtube.com/watch?v=#{video}", class: 'various fancybox-media'
end

#youtube_videop(video, width = 580, height = 420) ⇒ Object



37
38
39
# File 'lib/helper_methods.rb', line 37

def youtube_videop(video, width = 580, height = 420)
  "<iframe width='#{width}' height='#{height}' src='http://www.youtube.com/embed/#{video}' frameborder='0' allowfullscreen></iframe>".html_safe
end