Module: DailyShare::Helpers
- Defined in:
- lib/dailyshare/helpers.rb
Instance Method Summary collapse
-
#assetPath(url) ⇒ String
Generate a cache-busted URL for assets.
- #authorized? ⇒ Boolean
- #context_from_date(date) ⇒ Object
- #date_from_params(params) ⇒ Object
- #next_entry(date) ⇒ Object
-
#ordinalize(number) ⇒ String
Find the ordinal suffix for a number.
-
#partial(template, *args) ⇒ String
Render a partial template using slim.
- #prev_entry(date) ⇒ Object
- #protected! ⇒ Object
- #save_photo(params, file) ⇒ Object
-
#send_email(to, subject, body, from = CONFIG['email']) ⇒ Boolean
Send an multi-part email using mail gem.
- #valid_date?(date) ⇒ Boolean
Instance Method Details
#assetPath(url) ⇒ String
Generate a cache-busted URL for assets.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/dailyshare/helpers.rb', line 12 def assetPath(url) file = File.join(settings.public_folder,'assets',url) if File.exists?(file) cachebust = File.mtime(file).strftime('%s') url = "cb#{cachebust}/#{url}" end if ENV['RACK_ENV'] == 'production' CONFIG['url']['cdn']+"/assets/"+url else "/assets/#{url}" end end |
#authorized? ⇒ Boolean
133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/dailyshare/helpers.rb', line 133 def return true if !session[:member_id].nil? @auth ||= Rack::Auth::Basic::Request.new(request.env) if @auth.provided? && @auth.basic? && @auth.credentials && @auth.credentials[1] == AUTH['pass'] && (member = Member.byName(@auth.credentials[0])) session[:member_id] = member.id session[:member_name] = member.name end end |
#context_from_date(date) ⇒ Object
157 158 159 160 161 162 163 164 165 166 |
# File 'lib/dailyshare/helpers.rb', line 157 def context_from_date(date) edition = (date.strftime('%Y').to_i)-(CONFIG['dailyshare']['start'].strftime('%Y').to_i) { :ymd => date.strftime('%Y-%m-%d'), :date => date, :year => (edition+1).en.numwords, :count => (((date-CONFIG['dailyshare']['start']).to_i)-((edition)*365).to_i).en.numwords, :members => Member.order(:id).all } end |
#date_from_params(params) ⇒ Object
145 146 147 148 149 150 151 |
# File 'lib/dailyshare/helpers.rb', line 145 def date_from_params(params) if params[:y] && params[:m] && params[:d] Date.parse("#{params[:y]}-#{params[:m]}-#{params[:d]}") else Date.today end end |
#next_entry(date) ⇒ Object
172 173 174 |
# File 'lib/dailyshare/helpers.rb', line 172 def next_entry(date) (date+1).strftime('/%Y/%m/%d/') end |
#ordinalize(number) ⇒ String
Find the ordinal suffix for a number.
120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/dailyshare/helpers.rb', line 120 def ordinalize(number) if (11..13).include?(number.to_i.abs % 100) "#{number}th" else case number.to_i.abs % 10 when 1; "#{number}st" when 2; "#{number}nd" when 3; "#{number}rd" else "#{number}th" end end end |
#partial(template, *args) ⇒ String
Render a partial template using slim
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/dailyshare/helpers.rb', line 38 def partial(template, *args) template_array = template.to_s.split('/') template = template_array[0..-2].join('/') + "/_#{template_array[-1]}" = args.last.is_a?(Hash) ? args.pop : {} .merge!(:layout => false) locals = [:locals] || {} if collection = .delete(:collection) then collection.inject([]) do |buffer, member| buffer << slim(:"#{template}", .merge(:layout => false, :locals => { :item => member }.merge(locals))) end.join("\n") else slim(:"#{template}", ) end end |
#prev_entry(date) ⇒ Object
168 169 170 |
# File 'lib/dailyshare/helpers.rb', line 168 def prev_entry(date) (date-1).strftime('/%Y/%m/%d/') end |
#protected! ⇒ Object
104 105 106 107 108 109 |
# File 'lib/dailyshare/helpers.rb', line 104 def protected! unless response['WWW-Authenticate'] = %(Basic realm="Restricted Area") throw(:halt, [401, "Not authorized\n"]) end end |
#save_photo(params, file) ⇒ Object
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/dailyshare/helpers.rb', line 176 def save_photo(params, file) begin photo = Photo.new photo.set_fields(params, [:title, :description, :date_added, :member_id]) puts photo.inspect photo.save_original(file) photo.generate_sizes photo.save_to_s3 rescue false else photo.save send_email( CONFIG['email']['to'], "new submisson", slim(:'emails/uploaded', {:locals=>{:photo=>photo},:layout=>false}) ) end end |
#send_email(to, subject, body, from = CONFIG['email']) ⇒ Boolean
Send an multi-part email using mail gem.
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/dailyshare/helpers.rb', line 69 def send_email(to,subject,body,from=CONFIG['email']['from']) # build email mail = Mail.new do # assign to/from/subject to to from from subject subject # cache html body body_html = body # strip html from sent body body_text = body.gsub(/<\/?[^>]*>/,"") # send text email by default text_part do body body_text end # if body contained html, send that part too if body_html != body_text html_part do content_type 'text/html; charset=UTF-8' body body end end end # send it mail.delivery_method :sendmail mail.deliver! end |
#valid_date?(date) ⇒ Boolean
153 154 155 |
# File 'lib/dailyshare/helpers.rb', line 153 def valid_date?(date) (date.year > 2011 || date <= Date.today) end |