Skip to content

Instantly share code, notes, and snippets.

View metalelf0's full-sized avatar

Andrea Schiavini metalelf0

View GitHub Profile
class ApplicationController < ActionController::Base
protect_from_forgery
def current_ability
@current_ability ||= Spree::Ability.new(current_user)
end
rescue_from CanCan::AccessDenied do |exception|
redirect_to :root, :alert => exception.message
end
# create a file in config/initializers, e.g. add_abilities_to_spree.rb,
# with the following content:
Spree::Ability.register_ability MyAppAbility
# create a file under app/models (or lib/) to define your abilities (in
# this example I protect only the HostAppCoolPage model):
class MyAppAbility
include CanCan::Ability
HostApplication::Application.routes.draw do
devise_for :user,
:class_name => 'Spree::User',
:controllers => { :sessions => 'spree/user_sessions',
:registrations => 'spree/user_registrations',
:passwords => 'spree/user_passwords' },
:skip => [:unlocks, :omniauth_callbacks],
:path_names => { :sign_out => 'logout' }
# ...
end
composed_of :expire_date_minus_one_day,
:class_name => 'Date',
:mapping => %w(Date to_s),
:constructor => Proc.new{ |item| item },
:converter => Proc.new{ |item| item }
<p>
<%= f.label :expire_date %>
<%= f.date_select :expire_date_minus_one_day %>
</p>
def expire_date_minus_one_day
self.expire_date - 1.day
end
def expire_date_minus_one_day= date
self.expire_date = date + 1.day
end
# person.rb
validates_presence_of :address
# blueprints.rb
Person.blueprint do
name { "John" }
surname { "Doe" }
address { Address.make }
AREL (0.5ms) INSERT INTO "addresses" ("country", "planet", "created_at",
"updated_at") VALUES ('Italy', 'Earth', '2011-06-18 16:45:00.268423',
'2011-06-18 16:45:00.268423')
# person.rb
validates_presence_of :address
# person_factory.rb
Factory.define :person do |p|
p.name 'John'
p.surname 'Doe'
p.association :address
@metalelf0
metalelf0 / rollback.rb
Created March 10, 2011 13:54
Snippet to rollback DB state after a ruby script
ActiveRecord::Base.connection.increment_open_transactions
ActiveRecord::Base.connection.begin_db_transaction
at_exit do
ActiveRecord::Base.connection.rollback_db_transaction
ActiveRecord::Base.connection.decrement_open_transactions
end