Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Move comment queries to scopes

  • Loading branch information...
commit fa14aacb8c86b7d53069f20cff9a73f81a12e12c 1 parent f3765d6
@yaf yaf authored
View
29 app/controllers/admin/dashboard_controller.rb
@@ -6,14 +6,14 @@ class Admin::DashboardController < Admin::BaseController
def index
@newposts_count = Article.published_since(current_user.last_venue).count
@newcomments_count = Feedback.published_since(current_user.last_venue).count
- @comments = Comment.where(published: true).order('created_at DESC').limit(5)
+ @comments = Comment.last_published
@recent_posts = Article.published.limit(5)
@bestof = Article.bestof
@statposts = Article.published.count
@statuserposts = Article.published.count(conditions: {user_id: current_user.id})
- @statcomments = Comment.count(:all, conditions: "state != 'spam'")
- @statspam = Comment.count(:all, conditions: { state: 'spam' })
- @presumedspam = Comment.count(:all, conditions: { state: 'presumed_spam' })
+ @statcomments = Comment.not_spam.count
+ @statspam = Comment.spam.count
+ @presumedspam = Comment.presumed_spam.count
@categories = Category.count
@inbound_links = inbound_links
@typo_links = typo_dev
@@ -30,8 +30,16 @@ def inbound_links
end
def typo_version
- get_typo_version
- version = @typo_version ? @typo_version.split('.') : TYPO_VERSION.to_s.split('.')
+ typo_version = nil
+ version = TYPO_VERSION.to_s.split('.')
+ begin
+ url = "http://blog.typosphere.org/version.txt"
+ open(url) do |http|
+ typo_version = http.read[0..5]
+ version = typo_version.split('.')
+ end
+ rescue
+ end
if version[0].to_i > TYPO_MAJOR.to_i
flash.now[:error] = _("You are late from at least one major version of Typo. You should upgrade immediately. Download and install %s", "<a href='http://typosphere.org/stable.tgz'>#{_("the latest Typo version")}</a>").html_safe
@@ -42,15 +50,6 @@ def typo_version
end
end
- def get_typo_version
- url = "http://blog.typosphere.org/version.txt"
- open(url) do |http|
- @typo_version = http.read[0..5]
- end
- rescue
- @typo_version = nil
- end
-
def typo_dev
url = "http://blog.typosphere.org/articles.rss"
open(url) do |http|
View
9 app/models/comment.rb
@@ -7,9 +7,12 @@ class Comment < Feedback
content_fields :body
validates_presence_of :author, :body
- attr_accessor :user_agent
- attr_accessor :referrer
- attr_accessor :permalink
+ attr_accessor :user_agent, :referrer, :permalink
+
+ scope :spam, lambda { where(state: 'spam') }
+ scope :not_spam, lambda { where("state != 'spam'")}
+ scope :presumed_spam, lambda { where(state: 'presumed_spam')}
+ scope :last_published, lambda { where(published:true).limit(5).order('created_at DESC') }
def notify_user_via_email(user)
if user.notify_via_email?
View
39 spec/models/comment_spec.rb
@@ -219,4 +219,43 @@ def should_be_spam(comment)
end
end
+ describe "spam", integration: true do
+ it "returns only spam comment" do
+ comment = FactoryGirl.create(:comment, state: 'spam')
+ ham_comment = FactoryGirl.create(:comment, state: 'ham')
+ expect(Comment.spam).to eq([comment])
+ end
+ end
+
+ describe "not_spam", integration: true do
+ it "returns all comment that not_spam" do
+ comment = FactoryGirl.create(:comment, state: 'spam')
+ ham_comment = FactoryGirl.create(:comment, state: 'ham')
+ presumed_spam_comment = FactoryGirl.create(:comment, state: 'presumed_spam')
+ expect(Comment.not_spam.sort).to eq([ham_comment, presumed_spam_comment].sort)
+ end
+ end
+
+ describe "presumed_spam", integration: true do
+ it "returns only presumed_spam" do
+ comment = FactoryGirl.create(:comment, state: 'spam')
+ ham_comment = FactoryGirl.create(:comment, state: 'ham')
+ presumed_spam_comment = FactoryGirl.create(:comment, state: 'presumed_spam')
+ expect(Comment.presumed_spam).to eq([presumed_spam_comment])
+ end
+ end
+
+ describe "last_published", integration: true do
+ it "respond only 5 last_published" do
+ date = DateTime.new(2012, 12, 23, 12, 47)
+ comment_1 = FactoryGirl.create(:comment, body: "1", published: true, created_at: date + 1.day)
+ comment_4 = FactoryGirl.create(:comment, body: "4", published: true, created_at: date + 4.day)
+ comment_2 = FactoryGirl.create(:comment, body: "2", published: true, created_at: date + 2.day)
+ comment_6 = FactoryGirl.create(:comment, body: "6", published: true, created_at: date + 6.day)
+ comment_3 = FactoryGirl.create(:comment, body: "3", published: true, created_at: date + 3.day)
+ comment_5 = FactoryGirl.create(:comment, body: "5", published: true, created_at: date + 5.day)
+ expect(Comment.last_published).to eq([comment_6, comment_5, comment_4, comment_3, comment_2])
+ end
+ end
+
end

7 comments on commit fa14aac

@andyw8

This appeared to break two examples in the Article spec. I've create PR #168 to revert it.

@yaf
Owner

Hmm No specs are broken for me. Are you sure specs are not broken due to another changes, more recent ?

@mvz
Owner

@yaf, yes the specs were broken due to the move bestof to Article. I tried to fix this in a3d259b, but I see your comment that that's broken on sqlite3 :-(.

@yaf
Owner

I miss something then, I never push red specs.... What is message and broken spec ? (Perhaps I need to play with git bisect then... )

I put back code into bestof and specs are green... I certainly have a problem with my environment then.

@andyw8

It may have been specific to a particular version of the database driver. You can see the two errors here:

https://travis-ci.org/fdv/typo/builds/5672122

@yaf
Owner

I'm using sqlite3 3.7.13 with gem from typo: sqlite3 1.3.7 ...

@andyw8

I've tried to about the Travis build to cover all 3 databases - see #170.

Please sign in to comment.
Something went wrong with that request. Please try again.