Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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 / 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
@svs
svs / painful_rspec
Created October 5, 2012 06:23
Painful RSpec
ReceiptsController
unauthenticated user
should not be able to access index page
POST create
does not create a new Receipt
customer user
GET index
assigns all users documents as @documents
searches properly
@svs
svs / gist:3870436
Created October 11, 2012 05:43
valid_loan_product_implementation
def is_valid_loan_product(method)
loan_attr = self.send(method)
return [false, "No #{method} specified"] if not loan_attr or loan_attr===""
return [false, "No loan product chosen"] unless self.loan_product
product = self.loan_product
#Checking if the loan adheres to minimum and maximums of the loan product
{:min => :minimum, :max => :maximum}.each{|k, v|
product_attr = product.send("#{k}_#{method}")
if method==:interest_rate
product_attr = product_attr.to_f/100.round(6)
before :all do
@loan_product = LoanProduct.new
@loan_product.name = "LP1"
@loan_product.max_amount = 1000
@loan_product.min_amount = 1000
@loan_product.max_interest_rate = 100
@loan_product.min_interest_rate = 0.1
@loan_product.installment_frequency = :weekly
@loan_product.max_number_of_installments = 25
@loan_product.min_number_of_installments = 25