· 1 min read

Screencast: Refactoring und Dynamic Delegator

Download:

Download (11.2 MB, 7:58) Alternativer Download für iPod & Apple TV (10.9 MB, 7:58)

Resourcen:

Quellcode:

[ruby] # rails console Product.search({}).class Object.instance_methods BasicObject.instance_methods

# products_controller.rb @products = Product.search(params)

# models/product.rb def self.search(params) products = scope_builder products.where(“name like ?”, ”%” + params[:name] + ”%”) if params[:name] products.where(“price >= ?”, params[:price_gt]) if params[:price_gt] products.where(“price <= ?”, params[:price_lt]) if params[:price_lt] products end

def self.scope_builder DynamicDelegator.new(scoped) end

# lib/dynamic_delegator.rb class DynamicDelegator < BasicObject def initialize(target) @target = target end def method_missing(*args, &block) result = @target.send(*args, &block) @target = result if result.kind_of? @target.class result end end [/ruby]

Back to Blog