zycte
6/17/2006 11:37:00 AM
So, if I get you right you have a few public class methods on Article
and Event that will do the queries. You want to get rid of the "active"
parameter in each query.
One way to do this would be create a class method: active_items. It
returns an object that is has the same query methods as the class, that
is, both have the methods recent_articles, featured_articles, ... with
the exact same parameter list.
On the implementation side, this object could use your class again to
perform its queries (& share query code), but this is hidden to the
client, the controller.
In summary, you have:
News.recent_articles(*args)
News.active_items.recent_articles(*args)
When the user checks the "show only active" checkbox, you just replace
@query_handler with News.active_items. You don't have to change any
code and the parameter is implicit.
On 2006-06-17 05:20:13 +0200, daywalk@gmail.com said:
> Hey,
>
> I have a system that I am trying to design, it contains news articles,
> events, etc... There is model representing articles, one representing
> events, etc...
>
> Articles or events can be active or inactive, so I can do a query to
> return all active articles, all articles or all inactive articles.
>
> The question that I have is what would be the best way to design this.
> Right now I have each method (such as recent articles, featured
> articles, etc...) taking an 'active' parameter. This requires littering
> the code with method calls taking this parameter.
>
> I have also thought about setting a class variable on the models to set
> whether active/inactive/all articles are returned, so that anytime you
> want to set the type returned you set this class variable and then call
> your functions (recent_articles, etc...).
>
> What I would like to know is if there are any suggestions as to a good
> practice to follow in how to implement this functionality, one of the
> ways I have stated or some other way that I haven't considered.
>
> I hope I have been clear. Thanks for the help in advance.
>
> P.S. The application is done in ruby on rails.