Skip to content

Instantly share code, notes, and snippets.

@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|
@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_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 / 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
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 / 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
@andreypronin
andreypronin / custom_plan_zeus_rspec.rb
Created April 6, 2013 15:18
Custom_plan.rb and zeus.json for smooth RSpec experience with Zeus.
require 'zeus/rails'
class CustomPlan < Zeus::Rails
def spec(argv=ARGV)
# disable autorun in case the user left it in spec_helper.rb
RSpec::Core::Runner.disable_autorun!
exit RSpec::Core::Runner.run(argv)
end
end
@andreypronin
andreypronin / hstore_accessor_for_model.rb
Created May 15, 2013 19:13
HStore accessor: Postgres, Rails
def hstore_get(field_name)
method(field_name).call
end
def hstore_set(field_name,value)
method("#{field_name}=").call(value)
end
def hstore_accessor(field_name,name)
define_method(name) do
hstore = hstore_get(field_name)
@andreypronin
andreypronin / .gitignore
Last active August 29, 2015 14:06
My standard .gitignore: MacOS, TextMate, Vagrant, dotenv | figaro, rspec, Capybara, fog Gem-compatible
# Ignore application configuration
/config/application.yml
/config/fog_credentials.yml
# Ignore the default SQLite database.
/db/*.sqlite3
/db/*.sqlite3-journal
# Ignore all logfiles and tempfiles and local system/bin files
/log
@andreypronin
andreypronin / 0_reuse_code.js
Last active August 29, 2015 14:07
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console