Skip to content

Instantly share code, notes, and snippets.

@andreypronin
andreypronin / previous_changes.rb
Created April 3, 2013 22:43
Using previous_changes after after_save, in after_commit. Taken from http://logicalfriday.com/2012/08/21/rails-callbacks-workers-and-the-race-you-never-expected-to-lose/: Available on every object is a method called previous_changes which contains a hash of attributes that changed the last time the object was saved. Sadly, with this we don’t get…
class User < ActiveRecord::Base
after_commit :update_external_email, :if => :email_previously_changed?
private
def update_external_email
# do whatever you need to do
end
def gravatar_for email, options = {}
options = {:alt => 'avatar', :class => 'avatar', :size => 80}.merge! options
id = Digest::MD5::hexdigest email.strip.downcase
url = 'http://www.gravatar.com/avatar/' + id + '.jpg?s=' + options[:size].to_s
options.delete :size
image_tag url, options
end
# Using identicons etc through gravatar (see http://en.gravatar.com/site/implement/images/)
id = Digest::MD5::hexdigest email.strip.downcase
@andreypronin
andreypronin / setup_hstore.rb
Last active August 22, 2016 02:09
Migration to enable hstore for PostgreSQL with Rails 4. See also http://platformonrails.wordpress.com/2013/03/17/using-postgres…e-with-rails-4/
class SetupHstore < ActiveRecord::Migration
def self.up
enable_extension "hstore"
end
def self.down
disable_extension "hstore"
end
end
@andreypronin
andreypronin / spec_features_samplefeature.rb
Last active July 6, 2016 19:42
A little spec library I use to test session/persistent cookies behavior in Rails apps with Capybara. Plus, example on how I use it. Implemented for Rack::Test and Poltergeist (PhantomJS) drivers only, as these are my default test preferences.
require 'spec_helper'
feature "Login" do
scenario "remembered user" do
password = "12345678"
user = create(:user, password: password)
access_protected_page_should_ask_to_login
current_path.should eq login_path
@andreypronin
andreypronin / registrations_controller.rb
Created February 27, 2013 16:55
Devise with strong_parameters. In prep to Rails 4, I'm using strong_parameters gem in my Rails 3.2.x apps. Though I'm aware about the Rails 4 support work going on for devise, I want to use it now with 3.2.x but in the 'Rails 4'-ish situation when you need to call 'permit' on 'params'. Solution: use custom RegistrationController with the followi…
class RegistrationsController < Devise::RegistrationsController
def resource_params
permit_keys = [ :password, :password_confirmation ] + resource_class.authentication_keys
params.require(resource_name).permit( *permit_keys )
end
end
@andreypronin
andreypronin / spec_helper.rb
Last active January 18, 2016 23:41
RSpec hooks I use with DatabaseCleaner. We can't use transactional fixtures with Selenium/Poltergeist, but it's an overkill to truncate/delete for non-JS specs. Thus this optimization here. Read more at http://railscasts.com/episodes/257-request-specs-and-capybara. Assumption (just in case): only javascript_driver is set to Poltergeist/Selenium …
config.use_transactional_fixtures = false
config.before(:suite) do
# Do truncation once per suite to vacuum for Postgres
DatabaseCleaner.clean_with :truncation
# Normally do transactions-based cleanup
DatabaseCleaner.strategy = :transaction
end
config.around(:each) do |spec|