Aguante named_scope !
Tuesday, 14 October , 2008
Hace un tiempito que vengo usando named_sope en mis modelos y la verdad que cada vez me sorprendo más, en esta ocasión me hice uno que utilizo con el plugin acts_as_taggable_on_steroids, que retorna los posts que poseen cierto tag:
#############################################################
# Scopes
named_scope :tagged_with, lambda{|tags|
conditions = {:conditions => { :published => true},
rder => 'created_at DESC',
:match_all => true}
find_options_for_find_tagged_with(tags, conditions)
}
Además acá les dejo los specs que escribí para este scope:
describe Post, "tagged_with" do
fixtures :posts
before(:each) do
@tag = "ruby"
1.upto(4) {
p = create_post
p.tag_list << @tag
p.published = true
p.save
}
end
it "should have all tagged with #{@tag}" do
Post.tagged_with(@tag).each{ |p| p.tag_list.should include(@tag) }
end
it "should have at least 3 posts tagged with #{@tag}" do
Post.tagged_with(@tag).should have_at_least(3).posts
end
end
Los specs no me convencen mucho así que si alguien tiene alguna forma mejor de hacer que me avise.


