module Artifact::Helpers
Public Instance Methods
action()
click to toggle source
# File lib/artifact/helpers.rb, line 15 def action request.path_info.gsub('/','').empty? ? 'home' : request.path_info.gsub('/',' ') end
active?(link)
click to toggle source
# File lib/artifact/helpers.rb, line 62 def active?(link) request.path[link] ? 'active' : '' end
app_name()
click to toggle source
main
# File lib/artifact/helpers.rb, line 11 def app_name 'Artifact' end
delete_link(resource, options = {})
click to toggle source
# File lib/artifact/helpers.rb, line 82 def delete_link(resource, options = {}) css_class = options[:class] || '' text = options[: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)
click to toggle source
paths, links
# File lib/artifact/helpers.rb, line 45 def get_class_name(klass) klass.name.to_s.split("::").last end
guess_model_path(m)
click to toggle source
# File lib/artifact/helpers.rb, line 49 def guess_model_path(m) '/' + get_class_name(m).pluralize.downcase end
in_words(time)
click to toggle source
# 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)
click to toggle source
# File lib/artifact/helpers.rb, line 71 def is_image?(filename) filename.match(/\.(png|jpg|jpeg|gif|bmp)$/i) end
link_to(string, resource)
click to toggle source
# File lib/artifact/helpers.rb, line 58 def link_to(string, resource) "<a href='#{url_for(resource)}'>#{string}</a>" end
partial(name, *args)
click to toggle source
rendering
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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