Skip to content

Instantly share code, notes, and snippets.

class QuotationWorkflow
def initialize(object)
@object = object
end
include Workflow
workflow do
state :created do
class QuotationStatusPolicy
def initialize(quotation)
@quotation = quotation
end
# STATUSES
def unconfirmed?
[:mildly_interested, :negotiating].include?(quality)
end
@svs
svs / quotation.rb
Created December 30, 2012 15:11
Before extracting workflow object
class Quotation
include DataMapper::Resource
# ...snip...
property :quoted_kms, Float
property :quoted_per_km, Float
property :quoted_price, Float
property :start_date, Date, :required => true
property :end_date, Date, :required => true
@svs
svs / user.rb
Last active December 10, 2015 04:38
Showing a delegation pattern to handle fat models
require 'forwardable'
class User
extend Forwardable
attr_accessor :view_delegate_class
def_delegators :view_delegate, :full_name
def initialize(first_name, last_name)
@first_name = first_name
@svs
svs / params_example1.rb
Created December 16, 2012 21:07
params example 1
class FooController < ApplicationController
def index
search_params = (params[:quotation] || {}).select{|k,v| !v.blank?}
q = search_params.delete("user")
if q
q = "%#{q}%"
search_params[:intended_trip] = {:user => {:conditions => ['email ilike ? or first_name ilike ? or last_name ilike ? or mobile_number ilike ?', q, q, q, q]}}
end
if params[:all]
@quotations = Quotation.all(search_params)
class QuotationsController < ApplicationController
def index
@params = QuotationsIndexParams.new(params)
(redirect_to request.referer || quotations_path, :error => "Bad Request", :status => 400 and return) unless @params.valid?
@quotations = Quotation.search(@params.search_params).
send(@params.scope, @params.scope_params).
sorted_by(@params.sort.to_sym).
page(params[:page])
respond_to do |format|
format.html # index.html.erb
@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']
# 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 / 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
@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