stime = Time.now.utc.strftime(’%Y-%m-%d’) @items = Product.find(:all, :conditions => [‘id > ? AND publish = 1 AND
(publish_from is null OR publish_from <= ?) AND (publish_to is null OR
publish_to >= ?)’,10, stime, stime ])
How can i do this, so that i keep the orginal conditions, and expand it
with 3 other condition…
Product.with_scope(:conditions => [“id < ?”, 10]) do
stime = Time.now.utc.strfime(“%Y-%m-%d”) @items = Product.find(:all, :conditions => [“publish = 1 and
(publish_from is null or publish_from <= ?) and (publish_to is null or
publish_to >= ?)”, stime, stime])
end
all queries within the block automatically have the defined
conditions, ie, they have been scoped.
Product.with_scope(:conditions => [“id < ?”, 10]) do
stime = Time.now.utc.strfime(“%Y-%m-%d”) @items = Product.find(:all, :conditions => [“publish = 1 and
(publish_from is null or publish_from <= ?) and (publish_to is null or
publish_to >= ?)”, stime, stime])
end
all queries within the block automatically have the defined
conditions, ie, they have been scoped.
hmm, i have to version in my website, a backend (CMS) and a frontend
(the website)
In the backend i just need the find()
in the frontend i need the public_find() (=find()+the public filter)
It seems this is on model level?? How can i then make the diff between
the CMS end the website… i need something like a module (only in
frontend) thats extends the find…
take a look at the second link i listed. scan down the page until you
get to the section titled ‘around filter’. study the example he has
provided as it sounds exactly like what you are trying to accomplish.
class ScopedAccess::Filter
def initialize (klass, method_scoping)
…
end
def before (controller) @klass.scoped_methods << @method_scoping # means enable
‘with_scope’
end
def after (controller) @klass.scoped_methods.pop # means disable
‘with_scope’
end
end
protected
def publish
{
:find => {:conditions => [‘publish = 1 AND (publish_from is null OR
publish_from <= ?) AND (publish_to is null OR publish_to >= ?)’,
Date.today, Date.today ]},
}
end
but i need this in all my controllers in my frontend, so its also
duplicate code, if i have to put it in every controller…Can this also
be any better?
i cant put it in my appliction controller, because thats is used for my
backend as my frontend.
take a look at the second link i listed. scan down the page until you
get to the section titled ‘around filter’. study the example he has
provided as it sounds exactly like what you are trying to accomplish.
class Frontend::ProductsController < ApplicationController
around_filter ScopedAccess::Filter.new(Product, :publish)
protected
def publish
{
:find => {:conditions => [‘publish = 1 AND (publish_from is null OR
publish_from <= ?) AND (publish_to is null OR publish_to >= ?)’,
Date.today, Date.today ]},
}
end
public
def index
redirect_to :action => ‘list’
end
def list
@items = Product.find(:all, :order => "name ASC")
end
end
\app\models\scoped_access.rb:
class ScopedAccess::Filter
def initialize (klass, method_scoping)
#…
end
def before (controller) @klass.scoped_methods << @method_scoping # means enable
‘with_scope’
end
def after (controller) @klass.scoped_methods.pop # means disable
‘with_scope’
end
end