Skip to content

Commit

Permalink
Remove duplication about perpage from blog
Browse files Browse the repository at this point in the history
  • Loading branch information
Yannick Francois committed Sep 14, 2013
1 parent cf9f952 commit 871e814
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 36 deletions.
13 changes: 4 additions & 9 deletions app/controllers/articles_controller.rb
Expand Up @@ -14,17 +14,12 @@ class ArticlesController < ContentController

def index
conditions = (Blog.default.statuses_in_timeline) ? ["type in (?, ?)", "Article", "Note"] : ["type = ?", "Article"]

respond_to do |format|
format.html { @limit = this_blog.limit_article_display }
format.rss { @limit = this_blog.limit_rss_display }
format.atom { @limit = this_blog.limit_rss_display }
end

limit = this_blog.per_page(params[:format])
unless params[:year].blank?
@articles = Content.published_at(params.values_at(:year, :month, :day)).where(conditions).page(params[:page]).per(@limit)
@articles = Content.published_at(params.values_at(:year, :month, :day)).where(conditions).page(params[:page]).per(limit)
else
@articles = Content.published.where(conditions).page(params[:page]).per(@limit)
@articles = Content.published.where(conditions).page(params[:page]).per(limit)
end

@page_title = this_blog.home_title_template
Expand Down Expand Up @@ -56,7 +51,7 @@ def index
end

def search
@articles = this_blog.articles_matching(params[:q], :page => params[:page], :per_page => @limit)
@articles = this_blog.articles_matching(params[:q], page: params[:page], per_page: this_blog.per_page(params[:format]) )
return error(_("No posts found..."), :status => 200) if @articles.empty?
@page_title = this_blog.search_title_template.to_title(@articles, this_blog, params)
@description = this_blog.search_desc_template.to_title(@articles, this_blog, params)
Expand Down
10 changes: 2 additions & 8 deletions app/controllers/authors_controller.rb
Expand Up @@ -4,14 +4,8 @@ class AuthorsController < ContentController
def show
@author = User.find_by_login(params[:id])
raise ActiveRecord::RecordNotFound unless @author

respond_to do |format|
format.html { @limit = this_blog.limit_article_display }
format.rss { @limit = this_blog.limit_rss_display }
format.atom { @limit = this_blog.limit_rss_display }
end

@articles = @author.articles.published.page(params[:page]).per(@limit)

@articles = @author.articles.published.page(params[:page]).per(this_blog.per_page(params[:format]))
@page_title = this_blog.author_title_template.to_title(@author, this_blog, params)
@keywords = this_blog.meta_keywords
@description = this_blog.author_desc_template.to_title(@author, this_blog, params)
Expand Down
11 changes: 3 additions & 8 deletions app/controllers/feedback_controller.rb
Expand Up @@ -33,8 +33,9 @@ def index
render :text => 'this space left blank'
end
end
format.atom { render_feed 'atom', get_feedback }
format.rss { render_feed 'rss', get_feedback }
feedbacks = Feedback.from(controller_name, params[:article_id]).limit(this_blog.per_page(params[:format]))
format.atom { render_feed 'atom', feedbacks }
format.rss { render_feed 'rss', feedbacks }
end
end

Expand All @@ -46,10 +47,4 @@ def render_feed(format, collection)
render "index_#{format}_feed"
end

private

def get_feedback
@items = Feedback.from(controller_name, params[:article_id]).limit(this_blog.rss_limit_params[:limit])
end

end
9 changes: 7 additions & 2 deletions app/models/blog.rb
Expand Up @@ -205,10 +205,15 @@ def file_url(filename)
end
end

def articles_matching(query, args={})
def articles_matching(query, args={})
Article.search(query, args)
end

def per_page(format)
return limit_article_display if format.nil? || format == 'html'
limit_rss_display
end

def rss_limit_params
limit = limit_rss_display.to_i
return limit.zero? \
Expand Down Expand Up @@ -265,7 +270,7 @@ def split_base_url
raise "Invalid base_url: #{self.base_url}"
end
@split_base_url = { :protocol => $1, :host_with_port => $2,
:root_path => $3.gsub(%r{/$},'') }
:root_path => $3.gsub(%r{/$},'') }
end
@split_base_url
end
Expand Down
26 changes: 17 additions & 9 deletions spec/models/blog_spec.rb
Expand Up @@ -25,7 +25,7 @@

before :each do
@base_url = "http://myblog.net#{sub_url}"
@blog.base_url = @base_url
@blog.base_url = @base_url
end

[true, false].each do |only_path|
Expand Down Expand Up @@ -157,13 +157,13 @@ def set_permalink permalink
blog.urls_to_ping_for(article).map(&:url).should eq ["http://ping.example.com/ping", "http://anotherurl.net/other_line"]
end
end
describe "Blog Twitter configuration" do

describe "Blog Twitter configuration" do
it "A blog without :twitter_consumer_key or twitter_consumer_secret should not have Twitter configured" do
blog = FactoryGirl.build(:blog)
blog.has_twitter_configured?.should == false
end

it "A blog with an empty :twitter_consumer_key and no twitter_consumer_secret should not have Twitter configured" do
blog = FactoryGirl.build(:blog, twitter_consumer_key: "")
blog.has_twitter_configured?.should == false
Expand All @@ -173,30 +173,38 @@ def set_permalink permalink
blog = FactoryGirl.build(:blog, twitter_consumer_key: "", twitter_consumer_secret: "")
blog.has_twitter_configured?.should == false
end

it "A blog with a twitter_consumer_key and no twitter_consumer_secret should not have Twitter configured" do
blog = FactoryGirl.build(:blog, twitter_consumer_key: "12345")
blog.has_twitter_configured?.should == false
end

it "A blog with a twitter_consumer_key and an empty twitter_consumer_secret should not have Twitter configured" do
blog = FactoryGirl.build(:blog, twitter_consumer_key: "12345", twitter_consumer_secret: "")
blog.has_twitter_configured?.should == false
end

it "A blog with a twitter_consumer_secret and no twitter_consumer_key should not have Twitter configured" do
blog = FactoryGirl.build(:blog, twitter_consumer_secret: "67890")
blog.has_twitter_configured?.should == false
end

it "A blog with a twitter_consumer_secret and an empty twitter_consumer_key should not have Twitter configured" do
blog = FactoryGirl.build(:blog, twitter_consumer_secret: "67890", twitter_consumer_key: "")
blog.has_twitter_configured?.should == false
end

it "A blog with a twitter_consumer_key and a twitter_consumer_secret should have Twitter configured" do
blog = FactoryGirl.build(:blog, twitter_consumer_key: "12345", twitter_consumer_secret: "67890")
blog.has_twitter_configured?.should == true
end
end

describe :per_page do
let(:blog) { create(:blog, limit_article_display: 3, limit_rss_display: 4) }
it { expect(blog.per_page(nil)).to eq(3) }
it { expect(blog.per_page('html')).to eq(3) }
it { expect(blog.per_page('rss')).to eq(4) }
it { expect(blog.per_page('atom')).to eq(4) }
end
end

0 comments on commit 871e814

Please sign in to comment.