Skip to content

Instantly share code, notes, and snippets.

@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}
@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 / 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: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 / 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 / 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 / articles_controller.rb
Created October 14, 2012 15:05
update action
# POST {:id => <article_id>, :action => :update, :set_state => :published, :article => {:title => "foo"}}
def update
@article = Article.find(params[:id])
@article.update!(params) # we call a method in Article which deals with our params
...
end
@svs
svs / things_controller.rb
Created October 14, 2012 15:17
standard rails update action
# things controller
def update
@thing = Thing.find(params[:id])
@thing.update_attributes(params[:thing])
...
end
# let the argument errors come from the typecast.
# one should not worry about the class of the object, just its capabilities.
def initialize(hash = {})
@name = hash[:name].to_s
@price = hash[:price].to_f
@quantity = Integer(hash[:quantity] || 1)
@list = List.new(hash[:list])
end
@svs
svs / QuotationsIndexParams.rb
Created December 16, 2012 20:54
QuotationsIndexParams
class QuotationsIndexParams
include Virtus
include DataMapper::Validations
attribute :scope, String, :default => 'pending'
attribute :sort, String, :default => 'poolability'
attribute :quotation, Hash, :default => {}
attribute :date, Date
validates_within :scope, :set => [nil,'all','pending','to_call','to_pool', 'cabbed']