Skip to content

Instantly share code, notes, and snippets.

@kinopyo
kinopyo / gist:2224867
Created March 28, 2012 08:52
Check whether a Paperclip attachment exists

Check whether a Paperclip attachment exists

Don’t simply test for the presence of the magic Paperclip attribute, it will return a paperclip Attachment object and thus always be true:

- if user.photo.present? # always true
  = image_tag(user.photo.url)
@kinopyo
kinopyo / gist:2224385
Created March 28, 2012 06:57
rspec: should raise exception matcher
# http://rspec.rubyforge.org/rspec/1.3.0/classes/Spec/Matchers.html#M000183
lambda { do_something_risky }.should raise_exception
lambda { do_something_risky }.should raise_exception(PoorRiskDecisionError)
lambda { do_something_risky }.should raise_exception(PoorRiskDecisionError) { |exception| exception.data.should == 42 }
lambda { do_something_risky }.should raise_exception(PoorRiskDecisionError, "that was too risky")
lambda { do_something_risky }.should raise_exception(PoorRiskDecisionError, /oo ri/)
lambda { do_something_risky }.should_not raise_exception
lambda { do_something_risky }.should_not raise_exception(PoorRiskDecisionError)
lambda { do_something_risky }.should_not raise_exception(PoorRiskDecisionError, "that was too risky")
@kinopyo
kinopyo / application_helper.rb
Created March 27, 2012 14:28
Rails: Detect if mobile agent
# from https://github.com/ruby-china/ruby-china/blob/master/app/helpers/application_helper.rb
MOBILE_USER_AGENTS = 'palm|blackberry|nokia|phone|midp|mobi|symbian|chtml|ericsson|minimo|' +
'audiovox|motorola|samsung|telit|upg1|windows ce|ucweb|astel|plucker|' +
'x320|x240|j2me|sgh|portable|sprint|docomo|kddi|softbank|android|mmp|' +
'pdxgw|netfront|xiino|vodafone|portalmmm|sagem|mot-|sie-|ipod|up\\.b|' +
'webos|amoi|novarra|cdm|alcatel|pocket|iphone|mobileexplorer|mobile'
def mobile?
agent_str = request.user_agent.to_s.downcase
return false if agent_str =~ /ipad/
@kinopyo
kinopyo / spec_helper.rb
Created March 23, 2012 08:02
Rails Test : Quick hack to get Rack to be quiet about "warning: regexp match /.../n against to UTF-8 string" until we upgrade to Rails 3.1 with Rack 1.3
RSpec.configure do |config|
# ...
end
# quick hack to get Rack to be quiet about "warning: regexp match /.../n against to UTF-8 string" until we upgrade to Rails 3.1 with Rack 1.3
module Rack
module Utils
def escape(s)
CGI.escape(s.to_s)
@kinopyo
kinopyo / 0_before.rb
Created February 23, 2012 04:03
Rspec Array Matching =~
it "finds upcoming published lessons" do
upcoming1 = Factory :lesson, start_at: 1.day.from_now
upcoming2 = Factory :lesson, start_at: 2.days.from_now
past = Factory :lesson, start_at: 1.day.ago
draft = Factory :lesson, visibility: 'draft'
Lesson.upcoming.should include(upcoming1, upcoming2)
Lesson.upcoming.should_not include(past, draft)
end
@kinopyo
kinopyo / gist:1833094
Created February 15, 2012 04:03
Nokogiri remove all image tags
f = Nokogiri::XML.fragment(some_html_str)
f.search('.//img').remove
puts f.to_html
@kinopyo
kinopyo / gist:1728550
Created February 3, 2012 06:38
Uploading image file(w/ paperclip) in Active Admin with Formtastic syntax, the "multipart/form-data" is missing in every docs.
form html: { multipart: true } do |f|
f.inputs "Basic Info" do
# ...
f.input :photo, as: :file
end
f.buttons
end
@kinopyo
kinopyo / 1. Add to Gemfile
Created February 2, 2012 06:26
sitemap_generator with Heroku and Amazon S3 problem: Internal server error
# in gemfile i have these lines
gem 'sitemap_generator', '2.0.1.pre1'
gem 'carrierwave'
gem 'fog'
@kinopyo
kinopyo / gist:1680766
Created January 26, 2012 03:23
FactoryGirl: How to define a value based on an already defined one.
# Say you have two columns: start_at, end_at
# When start_at is defined, you want end_at's value based on the start_at
# Here is the solution, passing a block to get the object of the context.
start_at Random.new.rand(14..45).days.from_now
end_at { |l| l.start_at + 2.hours }
# Full example
FactoryGirl.define do
factory :lesson do
@kinopyo
kinopyo / gist:1667607
Created January 24, 2012 03:28
Heroku, how to check and add ENV vars.
# check all your ENV vars.
heroku config -long
# add ENV vars
heroku config:add S3_KEY=8N029N81 S3_SECRET=9s83109d3+583493190