Module: UtilityHelper
- Defined in:
- app/helpers/utility_helper.rb
Instance Method Summary collapse
-
#add_to_page_title(*elements) ⇒ Object
Public: Set the first part of the page title.
-
#any_to_list?(records, options = {}, &block) ⇒ Boolean
Public: Render a block of content or fallback to a message.
-
#format_date(date, options = {}) ⇒ Object
date - (DateTime) The date to format.
-
#page_title(separator = " | ") ⇒ Object
Public: Generate the full page title.
-
#render_json(path, locals = {}) ⇒ Object
path - (String) The path to the json partial.
Instance Method Details
#add_to_page_title(*elements) ⇒ Object
Public: Set the first part of the page title.
elements - Zero or more strings to prepend onto the page title.
The final argument can optionally be hash:
:separator - (String) The separator to use between
elements (default: " | ").
Examples
set_page_title "Events", "Forum", separator: " :: "
# => "Events :: Forum"
Returns String of the first element, meant to allow you to use this method to add to the page title and display a header at the same time.
97 98 99 100 101 |
# File 'app/helpers/utility_helper.rb', line 97 def add_to_page_title(*elements) @TITLE_ELEMENTS ||= [] @TITLE_ELEMENTS += elements elements.first end |
#any_to_list?(records, options = {}, &block) ⇒ Boolean
Public: Render a block of content or fallback to a message.
records - (Array) The list of records to check. options - (Hash) A hash of options (default: {}):
:message - (String) The fallback message to display.
:title - (String) The title of the collection.
block - The block to capture if the collection is present.
Examples
records = []
any_to_list? records, message: "Empty!" do
"Hidden"
end
# => "Empty!"
records = [1, 2, 3]
any_to_list? records, message: "Empty!" do
"Numbers!"
end
# => "Numbers!"
Returns String of the captured block.
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'app/helpers/utility_helper.rb', line 67 def any_to_list?(records, ={}, &block) if records.present? return capture(&block) else if [:message].blank? if [:title].present? [:message] = "There are currently no #{[:title]}" else [:message] = "There is nothing here to list." end end return [:message].html_safe end end |
#format_date(date, options = {}) ⇒ Object
date - (DateTime) The date to format. options - (Hash) A hash of options (default: {}):
:with - (String) The custom strftime string to use to
format the date.
:format - (String) The format to use. Options are:
* "iso" (2012-10-11)
* "full_date" (October 11th, 2011)
* "event" (Wednesday, October 11)
* "short_date" (Oct 11, 2011)
:time - (Boolean) Whether or not to show the time.
Examples
format_date(Time.zone.now, format: "iso", time: true)
# => "2013-03-13, 1:10pm"
format_date(Time.zone.now, with: "%D")
# => "03/13/13"
format_date(Time.zone.now)
# => "Mar 13, 2013"
Returns String of the formatted date
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'app/helpers/utility_helper.rb', line 27 def format_date(date, ={}) return nil if !date.respond_to?(:strftime) format_str = [:with] if [:with].present? format_str ||= case [:format].to_s when "iso" then "%F" when "full_date" then "%B #{date.day.ordinalize}, %Y" when "event" then "%A, %B %-d" else "%b %-d, %Y" end format_str += ", %l:%M%P" if [:time] == true date.strftime(format_str) end |
#page_title(separator = " | ") ⇒ Object
Public: Generate the full page title.
last_element - (String) The string that will be appendended onto the
rest of the title.
separator - (String) The separator to use between the title and the last
element (default: " | ").
Examples
content_for(:page_title) # => "Home"
page_title("KPCC", "::")
# => "Home :: KPCC"
Returns String of the full title.
117 118 119 |
# File 'app/helpers/utility_helper.rb', line 117 def page_title(separator=" | ") @TITLE_ELEMENTS.compact.join(separator).html_safe end |
#render_json(path, locals = {}) ⇒ Object
path - (String) The path to the json partial. location - (Hash) A hash of locals to pass directly to the partial
(default: {}).
Examples
render_json('api/private/v1/posts/collection', posts: @posts)
Returns String of a JSON object.
133 134 135 |
# File 'app/helpers/utility_helper.rb', line 133 def render_json(path, locals={}) raw(j(render(partial: path, formats: [:json], locals: locals))) end |