Skip to content

Commit

Permalink
Adding per state filtering on articles listing + better display for d…
Browse files Browse the repository at this point in the history
…rafts.

- default: everything but draft.
- published articles
- drafts
- publication pending
- withdrawn
  • Loading branch information
Frédéric de Villamil committed Jan 27, 2012
1 parent 54f82ca commit a59ae1e
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 17 deletions.
1 change: 0 additions & 1 deletion TODO.todo
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ neuro for 6.0.10:
Editor:
- Add the media back into the editor.
– Change the permalink behavior, that one is definitely stupid. RLY.
- Add per state filter in post listing.
- Fix pagination display

Typogarden:
Expand Down
3 changes: 2 additions & 1 deletion app/controllers/admin/content_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ def auto_complete_for_article_keywords

def index
@search = params[:search] ? params[:search] : {}
@articles = Article.search_no_draft_paginate(@search, {:page => params[:page], :per_page => this_blog.admin_display_elements})

@articles = Article.search_with_pagination(@search, {:page => params[:page], :per_page => this_blog.admin_display_elements})

if request.xhr?
render :partial => 'article_list', :locals => { :articles => @articles }
Expand Down
7 changes: 5 additions & 2 deletions app/helpers/admin/base_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,11 @@ def link_to_published(item)
end

def published_or_not(item)
return "<span class='label success'>#{_("Published")}</span>" if item.published
"<span class='label important'>#{_("Unpublished")}</span>"
puts item.state
return "<span class='label success'>#{_("Published")}</span>" if item.state.to_s.downcase == 'published'
return "<span class='label notice'>#{_("Draft")}</span>" if item.state.to_s.downcase == 'draft'
return "<span class='label important'>#{_("Withdrawn")}</span>" if item.state.to_s.downcase == 'withdrawn'
return "<span class='label warning'>#{_("Publication pending")}</span>" if item.state.to_s.downcase == 'publicationpending'
end

def macro_help_popup(macro, text)
Expand Down
10 changes: 8 additions & 2 deletions app/models/article.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ def spam
scope :without_parent, {:conditions => {:parent_id => nil}}
scope :child_of, lambda { |article_id| {:conditions => {:parent_id => article_id}} }
scope :published, lambda { { :conditions => { :published => true, :published_at => Time.at(0)..Time.now }, :order => 'published_at DESC' } }
scope :pending, lambda { { :conditions => ['state = ? and published_at > ?', 'publication_pending', Time.now], :order => 'published_at DESC' } }
scope :withdrawn, lambda { { :conditions => { :state => 'withdrawn' }, :order => 'published_at DESC' } }
scope :published_at, lambda {|time_params| { :conditions => { :published => true, :published_at => Article.time_delta(*time_params) }, :order => 'published_at DESC' } }

setting :password, :string, ''
Expand Down Expand Up @@ -97,8 +99,12 @@ def last_draft(article_id)
article
end

def search_no_draft_paginate(search_hash, paginate_hash)
list_function = ["Article.no_draft"] + function_search_no_draft(search_hash)
def 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'


list_function = ["Article.#{state}"] + function_search_no_draft(search_hash)

if search_hash[:category] and search_hash[:category].to_i > 0
list_function << 'category(search_hash[:category])'
Expand Down
8 changes: 0 additions & 8 deletions app/views/admin/content/_drafts.html.erb

This file was deleted.

15 changes: 12 additions & 3 deletions app/views/admin/content/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,24 @@
:complete => "Element.hide('spinner')" \
do %>

<%#= render 'drafts' unless Article.drafts.empty? %>

<table class="zebra-striped clearfix">
<thead>
<tr class='noborder'>
<td>
<td colspan=5>
<input id="search" type="text" name="search[searchstring]" class='large' />
<%= submit_tag(_("Search"), {:class => 'btn'}) %>
<span id='spinner' style="display:none;"><%= image_tag('spinner.gif') %></span>
</td>
</tr>
<tr class='noborder'>
<td>
<select name="search[state]">
<option value='no_draft'><%= _("All articles") %></option>
<option value='published'><%= _("Published") %></option>
<option value='pending'><%= _("Publication pending") %></option>
<option value='drafts'><%= _("Drafts") %></option>
<option value='withdrawn'><%= _("Withdrawn") %></option>
</select>
</td>
<td><%= collection_select_with_current('search', 'category', Category.all, "id", "name", @search[:category].to_i, true)
%></td>
Expand Down
47 changes: 47 additions & 0 deletions spec/controllers/admin/content_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,53 @@
response.should be_success
end

it 'should restrict to drafts' do
article = Factory(:article, :state => 'draft')
get :index, :search => {:state => 'drafts'}
assigns(:articles).should == [article]
response.should render_template('index')
response.should be_success
end

it 'should restrict to publication pending articles' do
article = Factory(:article, :state => 'publication_pending', :published_at => '2020-01-01')
get :index, :search => {:state => 'pending'}
assigns(:articles).should == [article]
response.should render_template('index')
response.should be_success
end

it 'should restrict to withdrawn articles' do
article = Factory(:article, :state => 'withdrawn', :published_at => '2010-01-01')
get :index, :search => {:state => 'withdrawn'}
assigns(:articles).should == [article]
response.should render_template('index')
response.should be_success
end

it 'should restrict to withdrawn articles' do
article = Factory(:article, :state => 'withdrawn', :published_at => '2010-01-01')
get :index, :search => {:state => 'withdrawn'}
assigns(:articles).should == [article]
response.should render_template('index')
response.should be_success
end

it 'should restrict to published articles' do
article = Factory(:article, :state => 'published', :published_at => '2010-01-01')
get :index, :search => {:state => 'published'}
response.should render_template('index')
response.should be_success
end

it 'should fallback to default behavior' do
article = Factory(:article, :state => 'draft')
get :index, :search => {:state => '3vI1 1337 h4x0r'}
response.should render_template('index')
assigns(:articles).should_not == [article]
response.should be_success
end

end

shared_examples_for 'autosave action' do
Expand Down

0 comments on commit a59ae1e

Please sign in to comment.