Module: Artifact::Helpers
- Defined in:
- lib/artifact/helpers.rb
Instance Method Summary collapse
- #action ⇒ Object
- #active?(link) ⇒ Boolean
-
#app_name ⇒ Object
main.
- #delete_link(resource, options = {}) ⇒ Object
-
#get_class_name(klass) ⇒ Object
paths, links.
- #guess_model_path(m) ⇒ Object
- #in_words(time) ⇒ Object
- #is_image?(filename) ⇒ Boolean
- #link_to(string, resource) ⇒ Object
-
#partial(name, *args) ⇒ Object
rendering.
- #post_preview_path(post) ⇒ Object
- #requires!(required, hash = params) ⇒ Object
- #time_ago_in_words(time) ⇒ Object
- #upload_relative_path(file) ⇒ Object
- #url_for(object) ⇒ Object
- #view(view_name, *args) ⇒ Object
Instance Method Details
#action ⇒ Object
15 16 17 |
# File 'lib/artifact/helpers.rb', line 15 def action request.path_info.gsub('/','').empty? ? 'home' : request.path_info.gsub('/',' ') end |
#active?(link) ⇒ Boolean
62 63 64 |
# File 'lib/artifact/helpers.rb', line 62 def active?(link) request.path[link] ? 'active' : '' end |
#app_name ⇒ Object
main
11 12 13 |
# File 'lib/artifact/helpers.rb', line 11 def app_name 'Artifact' end |
#delete_link(resource, options = {}) ⇒ Object
82 83 84 85 86 87 88 89 90 |
# File 'lib/artifact/helpers.rb', line 82 def delete_link(resource, = {}) css_class = [:class] || '' text = [:text] || 'Delete' path = url_for(resource) '<form class="destroy" style="display:inline" method="post" action="'+ path +'" onsubmit="return confirm(\'Are you sure?\');"> <input type="hidden" name="_method" value="delete" /> <button type="submit" class="' + css_class +' btn btn-danger">' + text + '</button> </form>' end |
#get_class_name(klass) ⇒ Object
paths, links
45 46 47 |
# File 'lib/artifact/helpers.rb', line 45 def get_class_name(klass) klass.name.to_s.split("::").last end |
#guess_model_path(m) ⇒ Object
49 50 51 |
# File 'lib/artifact/helpers.rb', line 49 def guess_model_path(m) '/' + get_class_name(m).pluralize.downcase end |
#in_words(time) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/artifact/helpers.rb', line 92 def in_words(time) minutes = (((Time.now - time).abs)/60).round return nil if minutes < 0 case minutes when 0..1 then 'less than a minute' when 2..4 then 'less than 5 minutes' when 5..14 then 'less than 15 minutes' when 15..29 then "half an hour" when 30..59 then "#{minutes} minutes" when 60..119 then '1 hour' when 120..239 then '2 hours' when 240..479 then '4 hours' when 480..719 then '8 hours' when 720..1439 then '12 hours' when 1440..11519 then "#{(minutes/1440).floor} days" when 11520..43199 then "#{(minutes/11520).floor} weeks" when 43200..525599 then "#{(minutes/43200).floor} months" else "#{(minutes/525600).floor} years" end end |
#is_image?(filename) ⇒ Boolean
71 72 73 |
# File 'lib/artifact/helpers.rb', line 71 def is_image?(filename) filename.match(/\.(png|jpg|jpeg|gif|bmp)$/i) end |
#link_to(string, resource) ⇒ Object
58 59 60 |
# File 'lib/artifact/helpers.rb', line 58 def link_to(string, resource) "<a href='#{url_for(resource)}'>#{string}</a>" end |
#partial(name, *args) ⇒ Object
rendering
31 32 33 34 |
# File 'lib/artifact/helpers.rb', line 31 def partial(name, *args) partial_name = name.to_s["/"] ? name.to_s.reverse.sub("/", "_/").reverse : "_#{name}" erb(partial_name.to_sym, {:layout => false}, *args) end |
#post_preview_path(post) ⇒ Object
75 76 77 78 79 80 |
# File 'lib/artifact/helpers.rb', line 75 def post_preview_path(post) str = post.path.sub(Artifact.config.source_root, '') if Artifact.config.directory_indexes str.sub(post.filename, post.filename.split('.').first) end end |
#requires!(required, hash = params) ⇒ Object
19 20 21 22 23 24 25 |
# File 'lib/artifact/helpers.rb', line 19 def requires!(required, hash = params) if required.is_a?(Hash) required.each { |k, vals| requires!(vals, hash[k]) } elsif hash.nil? or hash.is_a?(String) or required.any?{ |p| hash[p].nil? } halt(400, 'Missing parameters.') end end |
#time_ago_in_words(time) ⇒ Object
114 115 116 117 118 |
# File 'lib/artifact/helpers.rb', line 114 def time_ago_in_words(time) if str = in_words(time) "#{str} ago" end end |
#upload_relative_path(file) ⇒ Object
66 67 68 69 |
# File 'lib/artifact/helpers.rb', line 66 def upload_relative_path(file) # we need to start with a /, otherwise middleman will insert his image_path as prefix '/' + Artifact.config.uploads_path + file.path.sub(Artifact.uploads.path, '') end |
#url_for(object) ⇒ Object
53 54 55 56 |
# File 'lib/artifact/helpers.rb', line 53 def url_for(object) object.nil? ? "#" : object.is_a?(String) ? object \ : object.respond_to?(:path) ? url(object.path) : url("#{guess_model_path(object.class)}/#{object.to_param}") end |
#view(view_name, *args) ⇒ Object
36 37 38 39 |
# File 'lib/artifact/helpers.rb', line 36 def view(view_name, *args) layout = request.xhr? ? false : @mobile_host ? :'layout.mobile' : true erb(view_name.to_sym, {:layout => layout}, *args) end |