Module: HelperMethods
- Defined in:
- lib/helper_methods.rb,
lib/helper_methods/version.rb
Constant Summary collapse
- VERSION =
"0.0.20"
Instance Method Summary collapse
- #active_link_to(*args, &block) ⇒ Object
- #active_link_to_class(url, options = {}) ⇒ Object
- #attachment_url(file, style = :original) ⇒ Object
- #bootstrap_badge(text, name = "") ⇒ Object
- #bootstrap_icon(name, text = "", direction = "l") ⇒ Object
- #bootstrap_label(text, name = "") ⇒ Object
- #error_messages_for(resource, style = 'danger') ⇒ Object
- #flash_messages ⇒ Object
- #full_url_for(resource) ⇒ Object
- #gravatar(email, html_options = {}) ⇒ Object
- #is_active_link?(url, condition = nil) ⇒ Boolean
- #mobile_device? ⇒ Boolean
- #qr_code_for(data, size) ⇒ Object
- #youtube_link(video) ⇒ Object
- #youtube_videop(video, width = 580, height = 420) ⇒ Object
Instance Method Details
#active_link_to(*args, &block) ⇒ Object
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) = args[0] || {} = args[1] || {} else name = args[0] = args[1] || {} = args[2] || {} end url = url_for() = { } = { } .each do |k, v| if [:active, :class_active, :class_inactive, :active_disable, :wrap_tag].member?(k) [k] = v else [k] = v end end css_class = .delete(:class).to_s + ' ' css_class << active_link_to_class(url, ) css_class.strip! wrap_tag = [:wrap_tag].present? ? [:wrap_tag] : nil [:class] = css_class if css_class.present? link = if [:active_disable] === true && is_active_link?(url, [:active]) content_tag(:span, name, ) else link_to(name, url, ) end wrap_tag ? content_tag(wrap_tag, link, :class => (css_class if css_class.present?)) : link end |
#active_link_to_class(url, options = {}) ⇒ Object
115 116 117 118 119 120 121 |
# File 'lib/helper_methods.rb', line 115 def active_link_to_class(url, = {}) if is_active_link?(url, [:active]) [:class_active] || 'active' else [:class_inactive] || '' end end |
#attachment_url(file, style = :original) ⇒ Object
63 64 65 66 |
# File 'lib/helper_methods.rb', line 63 def (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 (resource, style = 'danger') if resource.errors.any? content_tag :div, class: "alert alert-#{style} alert-dismissable" do content_tag :ol do resource.errors.collect do |key, value| content_tag :li, value end.join.html_safe end end end end |
#flash_messages ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/helper_methods.rb', line 19 def if flash.any? content_tag :div, class: "alert alert-success alert-dismissable" do content_tag :ul do flash.collect do |key, value| unless [true, false, nil].include?(value) content_tag :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, = {}) email = Digest::MD5.hexdigest(email) image_tag "http://www.gravatar.com/avatar/#{email}?size=48", end |
#is_active_link?(url, condition = nil) ⇒ 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
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 |
#youtube_link(video) ⇒ Object
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 |