Skip to content

Instantly share code, notes, and snippets.

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 / 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)
@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 / 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
class QuotationStatusPolicy
def initialize(quotation)
@quotation = quotation
end
# STATUSES
def unconfirmed?
[:mildly_interested, :negotiating].include?(quality)
end
class QuotationWorkflow
def initialize(object)
@object = object
end
include Workflow
workflow do
state :created do
class Quotation
# ...snip...
# methods to be forwarded to the status policy class
def self.status_policy_methods
[:unconfirmed?, :expirable?, :approvable?, :rejectable?, :poolable?, :on_holdable?, :payable?, :callbackable?, :sendable?]
end
# methods to be forwarded to workflow class
@svs
svs / foo.rb
Last active December 10, 2015 14:58
# following are extracts from the relevant classes and not real Ruby code.
class Foo
belongs_to :user
end
class User
has n, :foos
end
class ActivityLog
include DataMapper::Resource
property :id, Serial
property :type, Discriminator
property :model_id, Integer
property :event, String
class FoosController < ActionController::Base
# ..snip ..
def create
@foo = Foo.new(params[:foo])
@foo.save_with_log(current_user)
# ...
end