Skip to content

Instantly share code, notes, and snippets.

@svs
svs / gist:2478214
Created April 24, 2012 09:23
Question about Rich Hickey's 'use hashes not classes' statement

So I don't have a model called Person, but I use hashes like {:name => "Siddharth", :sex => "Y"}. Great. Now I want to validate the presence of certain fields before persisting to the database. What do I do?

Module PersonValidation
  function validate_presence_of_name(person_hash)
    person_hash[:errors][:name] = "EEENKKK!" unless person_hash[:name]
  end
  
  ...
 ...
@svs
svs / workflow_spec.rb
Created July 13, 2012 12:12
Unit Testing a Workflow
describe Document do
context "with a scan" do
subject {FactoryGirl.build(:scannable)}
it {should have_events([:scan])}
end
context "taggable" do
subject { FactoryGirl.build(:taggable) }
it {should have_events([:to_edit])}
@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
@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: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: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: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
class UserStatistics
# This class is responsible for aggregating User data into meaningful reports
attr_accessor :user
#
#
# ... snip ...
#
@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]
@svs
svs / painless
Created October 5, 2012 06:20
RSpec painless output
ItemsController
unauthorised
does not edit
does not new
does not index
does not show
does not update
authorised
index