Skip to content

Instantly share code, notes, and snippets.

@svs
svs / gist:3888291
Created October 14, 2012 11:21
nested resources and games controller
# routes.rb
...
resources :games do
resources :players
resources :moves
end
...
@svs
svs / games_controller.rb
Created October 14, 2012 10:16
naive game controller
# POST /games/1/join_game {:id => <game_id>, :user_id => <player_id> }
def join_game
@game = Game.find(params[:id])
@game.players << User.find(:user_id)
...
end
# POST /games/1/leave_game {:id => <game_id>, :user_id => <player_id> }
def leave_game
@game = Game.find(params[:id])
@svs
svs / loan_spec.rb
Created October 11, 2012 06:45
readable loan spec
describe "scheduled first payment date must be a center meeting date" do
let(:center) { FactoryGirl.create(:center) }
subject { FactoryGirl.build(:loan,
:center => center,
:scheduled_first_payment_date => Date.new(2012,2,2) }
before { center.stub(:center_meeting_dates).and_return([Date.new(2012,2,1)])
it { should_not be_valid }
@svs
svs / loan.rb
Created October 11, 2012 06:41
loan snippet
def scheduled_dates_must_be_center_meeting_days #this function is only for repayment dates
return [false, "Not client defined"] unless client
center = client.center
failed = []
correct_weekday = nil
["scheduled_first_payment_date"].each do |d|
# if the loan disbursal date is set and it is not being set right now, no need to check as the loan has been already disbursed
# hence we need not check it again
if self.disbursal_date and not self.dirty_attributes.keys.find{|da| da.name == :disbursal_date}
return true
@svs
svs / loan_spec.rb
Created October 11, 2012 06:40
loan_date_spec
it "should not be valid if scheduled disbursal date and scheduled first payment date are not center meeting dates" do
@loan.scheduled_disbursal_date = @center.meeting_day
@loan.should be_valid
@loan.scheduled_first_payment_date = @center.meeting_day
@loan.should be_valid
@loan.scheduled_disbursal_date = @center.meeting_day + 1
@loan.should_not be_valid
@loan.scheduled_disbursal_date = @center.meeting_day - 1
@loan.should_not be_valid
@loan.scheduled_first_payment_date = @center.meeting_day + 1
@svs
svs / game_spec.rb
Created October 11, 2012 06:29
game_spec
require 'spec_helper'
describe Game do
let!(:player1) { FactoryGirl.create(:user) }
let!(:player2) { FactoryGirl.create(:user) }
let(:started_game) { FactoryGirl.build(:game).tap { |g| g.users << player1 ; g.users << player2 ; g.start! }}
it { should have_many :moves }
it { should have_and_belong_to_many :users}
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
@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)
@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 / 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