Skip to content

Instantly share code, notes, and snippets.

@svs
svs / authorised_actions.rb
Created October 5, 2012 06:15
authorised_actions
shared_examples "authorised action" do
before :each do
action.call
end
it "should assign proper items" do
if defined?(variable)
variable.each do |k,v|
assigns[k].should v.call
end
@svs
svs / authorised_index.rb
Created October 5, 2012 06:05
authorised_index
shared_examples "authorised index" do |user, items|
describe "index" do
before :each do
@request.env["devise.mapping"] = Devise.mappings[:user]
sign_in user
get :index
end
it "should assign proper items" do
assigns[:items].to_a.should =~ items
end
@svs
svs / gist:3838232
Created October 5, 2012 05:18
Standard RSpec Controller Test Output
ItemsController
DELETE destroy
destroys the requested item
redirects to the items list
GET show
assigns the requested item as @item
GET new
assigns a new item as @item
POST create
with valid params
@svs
svs / gist:3758355
Created September 20, 2012 21:06
Easy Tag Search Using Sequel
class Search
ALLOWED_MODELS = [:receipt, :business_card, :other_document]
SEARCH_COLS = {
:receipt => [:title,:organisation],
:business_card => [:first_name,:last_name,:organisation,:designation,:locality,:city,:pincode, :email, :twitter, :mobile, :notes],
:other_document => [:title,:notes]
}
NON_SEARCH_KEYS = [:tags, :q, :model, :all, :only, :from, :to, :by, :page]
class UserStatistics
# This class is responsible for aggregating User data into meaningful reports
attr_accessor :user
#
#
# ... snip ...
#
@svs
svs / gist:3704962
Created September 12, 2012 07:31
descriptive method calls
class Person
attr_accessor :name, :role
def initialize(name, role)
@name, @role = name, role
end
# options is a Hash with the following keys
# :to -> the role to set to
@svs
svs / gist:3496733
Created August 28, 2012 09:54
update action
# API is POST {:action => "update", :event => "approve", :id => 3, :document => {:comment => "some comment"}}
def update
@document = Document.get(params[:id])
if @document.send(params[:event], current_user)
@document.update_attributes(params[:document])
end
respond_to do |format|
# ...
# ...
end
@svs
svs / gist:3496546
Created August 28, 2012 09:30
AWSM - After Workflow State Machine
class Document
# ....
# ..snip...
# ....
workflow do
state :created do
event :scan, :transitions_to => :scanned, :if => Proc.new{|t| !(t.scans.empty?)}
event :to_edit, :transitions_to => :editing
end
state :scanned do
@svs
svs / gist:3496414
Created August 28, 2012 09:10
B.S. i.e. Before StateMachine
class Document
property :name, Text
.... # all the other properties
....
property :scanned_on, Date
property :tagged_on, Date
property :approved_on, Date
@svs
svs / gist:3105262
Created July 13, 2012 14:44
omg spec!
it "should move properly between states" do
@doc = Factory.build(:taggable, :user => @customer)
@doc.scan!(@customer)
@doc.scanned?.should == true
@doc.digi_document_state_transitions.count.should == 1
expect { @doc.tag_complete!}.should raise_exception # should not 'complete'
@doc.completed?.should == false
@doc.to_edit!(@customer) # should 'edit'
@doc.editing?.should == true
@doc.digi_document_state_transitions.count.should == 2