Module: GreenMonkey::ViewHelper
- Defined in:
- lib/green_monkey/ext/view_helper.rb
Instance Method Summary collapse
- #breadcrumb_link_to(title, path, options = {}) ⇒ Object
- #mida_scope(object) ⇒ Object
-
#time_tag(time, *args) ⇒ Object
time_tag post.created_at = time_tag post.created_at, format: “%d %h %Y %R%p” = time_tag post.created_at, itemprop: “datePublished”.
-
#time_tag_interval(from, to, *args) ⇒ Object
as second argumnts can get as Time/DateTime object as duration in seconds.
- #time_to_iso8601(time) ⇒ Object
Instance Method Details
#breadcrumb_link_to(title, path, options = {}) ⇒ Object
116 117 118 119 120 |
# File 'lib/green_monkey/ext/view_helper.rb', line 116 def (title, path, = {}) content_tag(:span, itemscope: true, itemtype: 'http://data-vocabulary.org/Breadcrumb') do link_to(content_tag(:span, title, itemprop: 'title'), path, .merge(itemprop: 'url')) + '' end end |
#mida_scope(object) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/green_monkey/ext/view_helper.rb', line 93 def mida_scope(object) = {itemscope: true} if object.respond_to?(:html_schema_type) if object.html_schema_type.kind_of?(Mida::Vocabulary) .merge!(itemtype: object.html_schema_type.itemtype.source) else raise "No vocabulary found (#{object.html_schema_type})" unless Mida::Vocabulary.find(object.html_schema_type) .merge!(itemtype: object.html_schema_type) end elsif object.is_a?(Symbol) .merge!(itemtype: Mida(object).itemtype.source) elsif object.is_a?(String) .merge!(itemtype: object) end if respond_to?(:tag_options) || private_methods.include?(:tag_options) send(:tag_options, ) else tag_builder.() end end |
#time_tag(time, *args) ⇒ Object
time_tag post.created_at
time_tag post.created_at, format: “%d %h %Y %R%p”
time_tag post.created_at, itemprop: “datePublished”
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/green_monkey/ext/view_helper.rb', line 22 def time_tag(time, *args) = args. format = .delete(:format) || :long datetime = time_to_iso8601(time) if time.acts_like?(:time) title = nil content = args.first || I18n.l(time, format: format) elsif time.kind_of?(Numeric) title = ChronicDuration.output(time, format: format) content = args.first || distance_of_time_in_words(time) else content = time.to_s end content_tag(:time, content, .reverse_merge(datetime: datetime, title: title)) end |
#time_tag_interval(from, to, *args) ⇒ Object
as second argumnts can get as Time/DateTime object as duration in seconds
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/green_monkey/ext/view_helper.rb', line 41 def time_tag_interval(from, to, *args) = args. format = .delete(:format) || :long datetime = [from, to].map(&method(:time_to_iso8601)).join("/") content = args.first || [from, to].map do |time| if time.acts_like?(:time) I18n.l(from, format: format) else ChronicDuration.output(time, format: format) end end if to.acts_like?(:time) content = content.join(" - ") else content = content.join(" in ") end content_tag(:time, content, .reverse_merge(datetime: datetime)) end |
#time_to_iso8601(time) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/green_monkey/ext/view_helper.rb', line 63 def time_to_iso8601(time) # http://www.ostyn.com/standards/scorm/samples/ISOTimeForSCORM.htm # P[yY][mM][dD][T[hH][mM][s[.s]S]] minute = 60 hour = minute * 60 day = hour * 24 year = day * 365.25 month = year / 12 if time.acts_like?(:time) time.iso8601 elsif time.kind_of?(Numeric) time = time.to_f return "PT0H0M0S" if time == 0 parts = ["P"] parts << "#{(time / year).floor}Y" if time >= year parts << "#{(time % year / month).floor}M" if time % year >= month parts << "#{(time % month / day).floor}D" if time % month >= day time = time % month parts << "T" if time % day > 0 parts << "#{(time % day / hour).floor}H" if time % day >= hour parts << "#{(time % hour / minute).floor}M" if time % hour >= minute parts << "#{(time % 1 == 0 ? time.to_i : time) % minute}S" if time % minute > 0 return parts.join end end |