add popularity counter to blog posts as well as accessor for recent posts
This commit is contained in:
parent
45cad84f49
commit
4f0b5f8223
4 changed files with 53 additions and 1 deletions
|
@ -23,6 +23,8 @@ module Refinery
|
||||||
@comment = Comment.new
|
@comment = Comment.new
|
||||||
|
|
||||||
@canonical = url_for(:locale => ::Refinery::I18n.default_frontend_locale) if canonical?
|
@canonical = url_for(:locale => ::Refinery::I18n.default_frontend_locale) if canonical?
|
||||||
|
|
||||||
|
@post.increment!(:access_count,1)
|
||||||
|
|
||||||
respond_with (@post) do |format|
|
respond_with (@post) do |format|
|
||||||
format.html { present(@post) }
|
format.html { present(@post) }
|
||||||
|
|
|
@ -78,6 +78,14 @@ module Refinery
|
||||||
def live
|
def live
|
||||||
where( "published_at <= ? and draft = ?", Time.now, false)
|
where( "published_at <= ? and draft = ?", Time.now, false)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def recent(count)
|
||||||
|
where("published_at <= ? and draft = ?", Time.now, false).limit(count)
|
||||||
|
end
|
||||||
|
|
||||||
|
def popular(count)
|
||||||
|
unscoped.order("access_count DESC").limit(count)
|
||||||
|
end
|
||||||
|
|
||||||
def previous(item)
|
def previous(item)
|
||||||
where(["published_at < ? and draft = ?", item.published_at, false]).limit(1)
|
where(["published_at < ? and draft = ?", item.published_at, false]).limit(1)
|
||||||
|
|
8
db/migrate/20120223022021_add_access_count_to_posts.rb
Normal file
8
db/migrate/20120223022021_add_access_count_to_posts.rb
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
class AddAccessCountToPosts < ActiveRecord::Migration
|
||||||
|
def change
|
||||||
|
add_column Refinery::Blog::Post.table_name, :access_count, :integer, :default => 0
|
||||||
|
|
||||||
|
add_index Refinery::Blog::Post.table_name, :access_count
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
|
@ -101,8 +101,42 @@ module Refinery
|
||||||
comment.body.should eq(body)
|
comment.body.should eq(body)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context "post popular" do
|
||||||
|
let(:blog_post) { FactoryGirl.create(:blog_post) }
|
||||||
|
let(:blog_post2) { FactoryGirl.create(:blog_post) }
|
||||||
|
|
||||||
|
before do
|
||||||
|
visit refinery.blog_post_path(blog_post)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should increment access count" do
|
||||||
|
blog_post.reload.access_count.should eq(1)
|
||||||
|
visit refinery.blog_post_path(blog_post)
|
||||||
|
blog_post.reload.access_count.should eq(2)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should be most popular" do
|
||||||
|
Refinery::Blog::Post.popular(2).first.should eq(blog_post)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "post recent" do
|
||||||
|
let(:blog_post) { FactoryGirl.create(:blog_post) }
|
||||||
|
let(:blog_post2) { FactoryGirl.create(:blog_post) }
|
||||||
|
|
||||||
|
before do
|
||||||
|
visit refinery.blog_post_path(blog_post2)
|
||||||
|
visit refinery.blog_post_path(blog_post)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should be the most recent" do
|
||||||
|
Refinery::Blog::Post.recent(2).first.should eq(blog_post2)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "#show draft preview" do
|
describe "#show draft preview" do
|
||||||
let(:blog_post) { FactoryGirl.create(:blog_post_draft) }
|
let(:blog_post) { FactoryGirl.create(:blog_post_draft) }
|
||||||
context "when logged in as admin" do
|
context "when logged in as admin" do
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue