Module: Nifty::Utils::ViewHelpers
- Defined in:
- lib/nifty/utils/view_helpers.rb
Instance Method Summary collapse
-
#boolean_tag(bool, tip = nil, options = {}) ⇒ Object
Renders a tick or cross character based on the provided boolean.
-
#display_flash(options = {}) ⇒ Object
Displays the full contents of the ‘flash` hash within an appropriate <div> element.
-
#gravatar(email, options = {}) ⇒ Object
Renders an ‘<img>` containing a link to the gravatar for the given e-mail address.
-
#length_of_time_in_words(seconds, options = {}) ⇒ Object
Returns a length of time in works (no I18N).
-
#rfc4226_qrcode(token) ⇒ Object
Return an image path for an RFC4226 QR code for a tiven RTP token.
-
#twitter_share_url(options = {}) ⇒ Object
Returns a URL to share some content on Twitter.
Instance Method Details
#boolean_tag(bool, tip = nil, options = {}) ⇒ Object
Renders a tick or cross character based on the provided boolean. Additional options can be passed if needed.
-
:true_text- text to display next to a tick -
:false_text- text to display next to a cross
46 47 48 49 50 51 |
# File 'lib/nifty/utils/view_helpers.rb', line 46 def boolean_tag(bool, tip = nil, = {}) true_text, false_text = "", "" true_text = " <b>#{options[:true_text]}</b>" if [:true_text] false_text = " <b>#{options[:false_text]}</b>" if [:false_text] content_tag :span, (bool ? "<span class='true'>✔#{true_text}</span>" : "<span class='false'>✘#{false_text}</span>").html_safe, :class => "boolean", :title => tip end |
#display_flash(options = {}) ⇒ Object
Displays the full contents of the ‘flash` hash within an appropriate <div> element. The ID of the outputted div will be `flash-alert` where alert is the type of flash.
-
:types: the names of flash messages to display with this helper
10 11 12 13 14 15 16 17 |
# File 'lib/nifty/utils/view_helpers.rb', line 10 def display_flash( = {}) [:types] ||= [:alert, :warning, :notice] [:types].map do |key| if flash[key] content_tag :div, content_tag(:p, h(flash[key])), :id => "flash-#{key}", :class => "flashMessage flashMessage--#{key}" end end.join.html_safe end |
#gravatar(email, options = {}) ⇒ Object
Renders an ‘<img>` containing a link to the gravatar for the given e-mail address. Available options as follows:
-
:size: the size in pixels of the outputted gravatar. Defaults to 35. -
:default: the gravatar to fallback to if the user has no gravatar (see gravatar for available options or pass a URL). Defaults to ‘identicon’. -
:rating: the maximum rating to use. Defaults to PG. -
:class: the value for the class attribute for the outputted img tag. Defaults to ‘gravatar’. -
:secure: wherher or not to output an HTTPS version of the gravatar or not. Defaults to the value of ‘request.ssl?`.
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/nifty/utils/view_helpers.rb', line 30 def gravatar(email, = {}) [:size] ||= 35 [:default] ||= 'identicon' [:rating] ||= 'PG' [:class] ||= 'gravatar' [:secure] ||= request.ssl? host = ([:secure] ? 'https://secure.gravatar.com' : 'http://gravatar.com') path = "/avatar.php?gravatar_id=#{Digest::MD5.hexdigest(email.to_s.downcase)}&rating=#{options[:rating]}&size=#{options[:size] * 2}&d=#{options[:default]}" image_tag([host,path].join, :class => [:class], :width => [:size], :height => [:size]) end |
#length_of_time_in_words(seconds, options = {}) ⇒ Object
Returns a length of time in works (no I18N)
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/nifty/utils/view_helpers.rb', line 63 def length_of_time_in_words(seconds, = {}) days = (seconds / 60 / 60 / 24).floor hours = ((seconds / 60 / 60) - (days * 24)).floor minutes = ((seconds / 60) - (days * 24 * 60) - (hours * 60)).floor seconds = (seconds - (days * 24 * 60 * 60) - (hours * 60 * 60) - (minutes * 60)).floor Array.new.tap do |s| if [:short] s << "#{days}d" if days > 0 s << "#{hours}h" if hours > 0 s << "#{minutes}m" if minutes > 0 s << "#{seconds}s" if seconds > 0 else s << pluralize(days, 'day') if days > 0 s << pluralize(hours, 'hour') if hours > 0 s << pluralize(minutes, 'minute') if minutes > 0 s << pluralize(seconds, 'second') if seconds > 0 end end.join([:short] ? ' ' : ', ') end |
#rfc4226_qrcode(token) ⇒ Object
Return an image path for an RFC4226 QR code for a tiven RTP token
84 85 86 87 88 89 |
# File 'lib/nifty/utils/view_helpers.rb', line 84 def rfc4226_qrcode(token) data = "otpauth://totp/#{request.host}?secret=#{token}" data = Rack::Utils.escape(data) url = "https://chart.googleapis.com/chart?chs=200x200&chld=M|0&cht=qr&chl=#{data}" image_tag(url, :alt => 'Google Authenticator QRCode', :width => 200, :height => 200) end |
#twitter_share_url(options = {}) ⇒ Object
Returns a URL to share some content on Twitter
-
:text- the text you wish to tweet -
:url- the URL you want to tweet
57 58 59 |
# File 'lib/nifty/utils/view_helpers.rb', line 57 def twitter_share_url( = {}) "https://twitter.com/share?text=#{CGI.escape(options[:text])}&url=#{CGI.escape(options[:url])}" end |