Skip to content

Commit

Permalink
Merge pull request #101 from randomecho/issue-87-show-all-articles
Browse files Browse the repository at this point in the history
Show all articles, including drafts, when using default admin filter
  • Loading branch information
yaf committed Dec 14, 2012
2 parents 6f3ab89 + 60b3603 commit 0a984d9
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 14 deletions.
8 changes: 6 additions & 2 deletions app/models/article.rb
Expand Up @@ -105,9 +105,13 @@ def self.last_draft(article_id)
end end


def self.search_with_pagination(search_hash, paginate_hash) def self.search_with_pagination(search_hash, paginate_hash)
state = (search_hash[:state] and ["no_draft", "drafts", "published", "withdrawn", "pending"].include? search_hash[:state]) ? search_hash[:state] : 'no_draft' state = (search_hash[:state] and ["no_draft", "drafts", "published", "withdrawn", "pending"].include? search_hash[:state]) ? search_hash[:state] : nil


list_function = ["Article.#{state}"] + function_search_no_draft(search_hash) if state.nil?
list_function = function_search_all_posts(search_hash)
elsif
list_function = ["Article.#{state}"] + function_search_all_posts(search_hash)
end


if search_hash[:category] && search_hash[:category].to_i > 0 if search_hash[:category] && search_hash[:category].to_i > 0
list_function << 'category(search_hash[:category])' list_function << 'category(search_hash[:category])'
Expand Down
4 changes: 2 additions & 2 deletions app/models/content.rb
Expand Up @@ -54,7 +54,7 @@ def self.find_already_published(limit)
where('published_at < ?', Time.now).limit(1000).order('created_at DESC') where('published_at < ?', Time.now).limit(1000).order('created_at DESC')
end end


# Use only for self.function_search_no_draft method # Use only for self.function_search_all_posts method
scope :published_at_like, lambda {|date_at| {:conditions => { scope :published_at_like, lambda {|date_at| {:conditions => {
:published_at => ( :published_at => (
if date_at =~ /\d{4}-\d{2}-\d{2}/ if date_at =~ /\d{4}-\d{2}-\d{2}/
Expand All @@ -70,7 +70,7 @@ def self.find_already_published(limit)
} }
} }


def self.function_search_no_draft(search_hash) def self.function_search_all_posts(search_hash)
list_function = [] list_function = []
search_hash ||= {} search_hash ||= {}


Expand Down
2 changes: 1 addition & 1 deletion app/views/admin/content/index.html.erb
Expand Up @@ -21,7 +21,7 @@
<tr class='noborder'> <tr class='noborder'>
<td> <td>
<select name="search[state]"> <select name="search[state]">
<option value='no_draft'><%= _("All articles") %></option> <option value=''><%= _("All articles") %></option>
<option value='published'><%= _("Published") %></option> <option value='published'><%= _("Published") %></option>
<option value='pending'><%= _("Publication pending") %></option> <option value='pending'><%= _("Publication pending") %></option>
<option value='drafts'><%= _("Drafts") %></option> <option value='drafts'><%= _("Drafts") %></option>
Expand Down
5 changes: 3 additions & 2 deletions spec/models/article_spec.rb
Expand Up @@ -751,10 +751,11 @@ def assert_sets_trigger(art)
Article.search_with_pagination({}, {page: nil, per_page: 1}).should eq([article]) Article.search_with_pagination({}, {page: nil, per_page: 1}).should eq([article])
end end


it "returns no draft article by default" do it "returns both published and draft articles by default" do
article = FactoryGirl.create(:article, state: 'published') article = FactoryGirl.create(:article, state: 'published')
draft_article = FactoryGirl.create(:article, state: 'draft') draft_article = FactoryGirl.create(:article, state: 'draft')
Article.search_with_pagination({}, {page: nil, per_page: 12}).should eq([article]) result = Article.search_with_pagination({}, {page: nil, per_page: 12})
result.count.should eq 2
end end


it "returns article of search categorie" do it "returns article of search categorie" do
Expand Down
14 changes: 7 additions & 7 deletions spec/models/content_spec.rb
Expand Up @@ -59,34 +59,34 @@
end end
end end


describe "#function_search_no_draft" do describe "#function_search_all_posts" do
it "returns empty array when nil given" do it "returns empty array when nil given" do
Content.function_search_no_draft(nil).should be_empty Content.function_search_all_posts(nil).should be_empty
end end


it "returns article that match with searchstring" do it "returns article that match with searchstring" do
expected_function = ['searchstring(search_hash[:searchstring])'] expected_function = ['searchstring(search_hash[:searchstring])']
Content.function_search_no_draft({searchstring: 'something'}).should eq expected_function Content.function_search_all_posts({searchstring: 'something'}).should eq expected_function
end end


it "returns article that match with published_at" do it "returns article that match with published_at" do
expected_function = ['published_at_like(search_hash[:published_at])'] expected_function = ['published_at_like(search_hash[:published_at])']
Content.function_search_no_draft({published_at: '2012-02'}).should eq expected_function Content.function_search_all_posts({published_at: '2012-02'}).should eq expected_function
end end


it "returns article that match with user_id" do it "returns article that match with user_id" do
expected_function = ['user_id(search_hash[:user_id])'] expected_function = ['user_id(search_hash[:user_id])']
Content.function_search_no_draft({user_id: '1'}).should eq expected_function Content.function_search_all_posts({user_id: '1'}).should eq expected_function
end end


it "returns article that match with not published" do it "returns article that match with not published" do
expected_function = ['not_published'] expected_function = ['not_published']
Content.function_search_no_draft({published: '0'}).should eq expected_function Content.function_search_all_posts({published: '0'}).should eq expected_function
end end


it "returns article that match with published" do it "returns article that match with published" do
expected_function = ['published'] expected_function = ['published']
Content.function_search_no_draft({published: '1'}).should eq expected_function Content.function_search_all_posts({published: '1'}).should eq expected_function
end end


end end
Expand Down

0 comments on commit 0a984d9

Please sign in to comment.