Commit 745bd5056dd98a333b26d0f37ac91fb7e0c1abd9
1 parent
04962ef3
Exists in
master
and in
29 other branches
Named scope to filter article by array of types
Showing
2 changed files
with
14 additions
and
0 deletions
Show diff stats
app/models/article.rb
| @@ -444,6 +444,7 @@ class Article < ActiveRecord::Base | @@ -444,6 +444,7 @@ class Article < ActiveRecord::Base | ||
| 444 | named_scope :galleries, :conditions => { :type => 'Gallery' } | 444 | named_scope :galleries, :conditions => { :type => 'Gallery' } |
| 445 | named_scope :images, :conditions => { :is_image => true } | 445 | named_scope :images, :conditions => { :is_image => true } |
| 446 | named_scope :text_articles, :conditions => [ 'articles.type IN (?)', text_article_types ] | 446 | named_scope :text_articles, :conditions => [ 'articles.type IN (?)', text_article_types ] |
| 447 | + named_scope :with_types, lambda { |types| { :conditions => [ 'articles.type IN (?)', types ] } } | ||
| 447 | 448 | ||
| 448 | named_scope :more_popular, :order => 'hits DESC' | 449 | named_scope :more_popular, :order => 'hits DESC' |
| 449 | named_scope :more_comments, :order => "comments_count DESC" | 450 | named_scope :more_comments, :order => "comments_count DESC" |
test/unit/article_test.rb
| @@ -1744,4 +1744,17 @@ class ArticleTest < ActiveSupport::TestCase | @@ -1744,4 +1744,17 @@ class ArticleTest < ActiveSupport::TestCase | ||
| 1744 | assert_nil article.author_id | 1744 | assert_nil article.author_id |
| 1745 | end | 1745 | end |
| 1746 | 1746 | ||
| 1747 | + should 'return articles with specific types' do | ||
| 1748 | + Article.delete_all | ||
| 1749 | + | ||
| 1750 | + c1 = fast_create(TinyMceArticle, :name => 'Testing article 1', :body => 'Article body 1', :profile_id => profile.id) | ||
| 1751 | + c2 = fast_create(TextArticle, :name => 'Testing article 2', :body => 'Article body 2', :profile_id => profile.id) | ||
| 1752 | + c3 = fast_create(Event, :name => 'Testing article 3', :body => 'Article body 3', :profile_id => profile.id) | ||
| 1753 | + c4 = fast_create(RssFeed, :name => 'Testing article 4', :body => 'Article body 4', :profile_id => profile.id) | ||
| 1754 | + c5 = fast_create(TextileArticle, :name => 'Testing article 5', :body => 'Article body 5', :profile_id => profile.id) | ||
| 1755 | + | ||
| 1756 | + assert_equivalent [c1,c2], Article.with_types(['TinyMceArticle', 'TextArticle']) | ||
| 1757 | + assert_equivalent [c3], Article.with_types(['Event']) | ||
| 1758 | + end | ||
| 1759 | + | ||
| 1747 | end | 1760 | end |