defself.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
enddefself.scope_builderDynamicDelegator.new(scoped)
end
lib/dynamic_delegator.rb
classDynamicDelegator < BasicObjectdefinitialize(target)
@target = target
enddefmethod_missing(*args, &block)
result = @target.send(*args, &block)
@target = result if result.kind_of? @target.class
result
endend