Commit 7e5e9579d73bcb7e1cedc84d60887e2d55271352

Authored by Moises Machado
Committed by Antonio Terceiro
1 parent 534ae2b7

ActionItem908: selected witch articles can display hits

added the hit counter
Added the hits column and the option to display it on the page
app/controllers/public/content_viewer_controller.rb
... ... @@ -49,6 +49,9 @@ class ContentViewerController < ApplicationController
49 49 render :action => 'access_denied', :status => 403, :layout => false
50 50 end
51 51  
  52 + # At this point the page will be showed
  53 + @page.hit
  54 +
52 55 if @page.mime_type != 'text/html'
53 56 headers['Content-Type'] = @page.mime_type
54 57 data = @page.data
... ...
app/helpers/article_helper.rb
... ... @@ -18,8 +18,13 @@ module ArticleHelper
18 18 'div',
19 19 check_box(:article, :notify_comments) +
20 20 content_tag('label', _('I want to receive a notification of each comment written by e-mail'), :for => 'article_notify_comments') +
21   - observe_field(:article_accept_comments, :function => "$('article_notify_comments').disabled = ! $('article_accept_comments').checked")
22   - )
  21 + observe_field(:article_accept_comments, :function => "$('article_notify_comments').disabled = ! $('article_accept_comments').checked")
  22 + ) + (article.can_display_hits? ?
  23 + content_tag(
  24 + 'div',
  25 + check_box(:article, :display_hits) +
  26 + content_tag('label', _('I want this article to display the number of hits it received'), :for => 'article_display_hits')
  27 + ) : '')
23 28 )
24 29 end
25 30  
... ...
app/models/article.rb
... ... @@ -16,6 +16,10 @@ class Article < ActiveRecord::Base
16 16 has_many :article_categorizations, :conditions => [ 'articles_categories.virtual = ?', false ]
17 17 has_many :categories, :through => :article_categorizations
18 18  
  19 + acts_as_having_settings :field => :setting
  20 +
  21 + settings_items :display_hits, :type => :boolean, :default => true
  22 +
19 23 def self.human_attribute_name(attrib)
20 24 case attrib.to_sym
21 25 when :name
... ... @@ -209,13 +213,26 @@ class Article < ActiveRecord::Base
209 213 end
210 214  
211 215 def article_attr_blacklist
212   - ['id', 'profile_id', 'parent_id', 'slug', 'path', 'updated_at', 'created_at', 'last_changed_by_id', 'version', 'lock_version', 'type', 'children_count', 'comments_count']
  216 + ['id', 'profile_id', 'parent_id', 'slug', 'path', 'updated_at', 'created_at', 'last_changed_by_id', 'version', 'lock_version', 'type', 'children_count', 'comments_count', 'hits']
213 217 end
214 218  
215 219 def self.find_by_old_path(old_path)
216 220 find(:first, :include => :versions, :conditions => ['article_versions.path = ?', old_path], :order => 'article_versions.id desc')
217 221 end
218 222  
  223 + def hit
  224 + self.hits += 1
  225 + save!
  226 + end
  227 +
  228 + def can_display_hits?
  229 + true
  230 + end
  231 +
  232 + def display_hits?
  233 + can_display_hits? && display_hits
  234 + end
  235 +
219 236 private
220 237  
221 238 def sanitize_tag_list
... ...
app/models/enterprise_homepage.rb
... ... @@ -26,4 +26,8 @@ class EnterpriseHomepage < Article
26 26 (self.profile.environment.enabled?('disable_products_for_enterprises') ? '' : display_products_list(self.profile, products))
27 27 end
28 28  
  29 + def can_display_hits?
  30 + false
  31 + end
  32 +
29 33 end
... ...
app/models/folder.rb
... ... @@ -29,4 +29,8 @@ class Folder < Article
29 29 true
30 30 end
31 31  
  32 + def can_display_hits?
  33 + false
  34 + end
  35 +
32 36 end
... ...
app/models/rss_feed.rb
... ... @@ -119,4 +119,8 @@ class RssFeed < Article
119 119 'rss-feed'
120 120 end
121 121  
  122 + def can_display_hits?
  123 + false
  124 + end
  125 +
122 126 end
... ...
app/models/uploaded_file.rb
... ... @@ -49,4 +49,8 @@ class UploadedFile < Article
49 49 false
50 50 end
51 51  
  52 + def can_display_hits?
  53 + false
  54 + end
  55 +
52 56 end
... ...
app/views/content_viewer/view_page.rhtml
... ... @@ -61,6 +61,12 @@
61 61 </div>
62 62 <% end %>
63 63  
  64 +<% if @page.display_hits? %>
  65 + <div id="article-hits">
  66 + <%= n_('Viewed one time', 'Viewed %{num} times', @page.hits) % { :num => @page.hits } %>
  67 + </div>
  68 +<% end %>
  69 +
64 70 <% if @page.parent && !@page.parent.path.blank? %>
65 71 <div id="article-parent">
66 72 <%= link_to_document(@page.parent, _('Up')) %>
... ...
db/migrate/058_add_hits_to_articles.rb 0 → 100644
... ... @@ -0,0 +1,11 @@
  1 +class AddHitsToArticles < ActiveRecord::Migration
  2 + def self.up
  3 + add_column :articles, :hits, :integer, :default => 0
  4 + add_column :article_versions, :hits, :integer, :default => 0
  5 + end
  6 +
  7 + def self.down
  8 + remove_column :articles, :hits
  9 + remove_column :article_versions, :hits
  10 + end
  11 +end
... ...
db/schema.rb
... ... @@ -9,7 +9,7 @@
9 9 #
10 10 # It's strongly recommended to check this file into your version control system.
11 11  
12   -ActiveRecord::Schema.define(:version => 57) do
  12 +ActiveRecord::Schema.define(:version => 58) do
13 13  
14 14 create_table "article_versions", :force => true do |t|
15 15 t.integer "article_id"
... ... @@ -41,6 +41,7 @@ ActiveRecord::Schema.define(:version =&gt; 57) do
41 41 t.integer "reference_article_id"
42 42 t.text "setting"
43 43 t.boolean "notify_comments", :default => false
  44 + t.integer "hits", :default => 0
44 45 end
45 46  
46 47 create_table "articles", :force => true do |t|
... ... @@ -73,6 +74,7 @@ ActiveRecord::Schema.define(:version =&gt; 57) do
73 74 t.integer "reference_article_id"
74 75 t.text "setting"
75 76 t.boolean "notify_comments", :default => false
  77 + t.integer "hits", :default => 0
76 78 end
77 79  
78 80 create_table "articles_categories", :id => false, :force => true do |t|
... ...
public/stylesheets/article.css
... ... @@ -23,6 +23,11 @@
23 23 text-decoration: none;
24 24 }
25 25  
  26 +#article-hits {
  27 + font-size: 10px;
  28 + text-align: right;
  29 +}
  30 +
26 31 #article-cat {
27 32 font-size: 10px;
28 33 }
... ...
test/functional/content_viewer_controller_test.rb
... ... @@ -644,4 +644,11 @@ class ContentViewerControllerTest &lt; Test::Unit::TestCase
644 644 assert_tag :tag => 'div', :attributes => { :id => "post-#{t.id}" }, :descendant => { :tag => 'a', :content => 'No comments yet', :attributes => { :href => /#{profile.identifier}\/blog\/first-post\?form=opened#comment_form/ } }
645 645 end
646 646  
  647 + should 'hit the article when viewed' do
  648 + a = profile.articles.create!(:name => 'test article')
  649 + get :view_page, :profile => profile.identifier, :page => [a.path]
  650 + a.reload
  651 + assert_equal 1, a.hits
  652 + end
  653 +
647 654 end
... ...
test/unit/article_test.rb
... ... @@ -589,4 +589,30 @@ class ArticleTest &lt; Test::Unit::TestCase
589 589 assert !a.notify_comments?
590 590 end
591 591  
  592 + should 'hold hits count' do
  593 + a = Article.create!(:name => 'Test article', :profile => profile)
  594 + a.hits = 10
  595 + a.save!
  596 + a.reload
  597 + assert_equal 10, a.hits
  598 + end
  599 +
  600 + should 'increment hit counter when hitted' do
  601 + a = Article.create!(:name => 'Test article', :profile => profile, :hits => 10)
  602 + a.hit
  603 + a.reload
  604 + assert_equal 11, a.hits
  605 + end
  606 +
  607 + should 'have display_hits setting with default true' do
  608 + a = Article.create!(:name => 'Test article', :profile => profile)
  609 + assert_respond_to a, :display_hits
  610 + assert_equal true, a.display_hits
  611 + end
  612 +
  613 + should 'can display hits' do
  614 + a = Article.create!(:name => 'Test article', :profile => profile)
  615 + assert_respond_to a, :can_display_hits?
  616 + assert_equal true, a.can_display_hits?
  617 + end
592 618 end
... ...
test/unit/enterprise_homepage_test.rb
... ... @@ -53,4 +53,10 @@ class EnterpriseHomepageTest &lt; Test::Unit::TestCase
53 53 result = a.to_html
54 54 assert_match /catalog\/test_enterprise\/#{prod.id}/, result
55 55 end
  56 +
  57 + should 'can display hits' do
  58 + a = EnterpriseHomepage.new(:name => 'Test article')
  59 + assert_equal false, a.can_display_hits?
  60 + end
  61 +
56 62 end
... ...
test/unit/folder_test.rb
... ... @@ -46,4 +46,9 @@ class FolderTest &lt; ActiveSupport::TestCase
46 46 assert Folder.new.folder?, 'folder must identity itself as folder'
47 47 end
48 48  
  49 + should 'can display hits' do
  50 + a = Folder.create!(:name => 'Test article', :profile => profile)
  51 + assert_equal false, a.can_display_hits?
  52 + end
  53 +
49 54 end
... ...
test/unit/rss_feed_test.rb
... ... @@ -220,4 +220,10 @@ class RssFeedTest &lt; Test::Unit::TestCase
220 220 assert !feed.advertise?
221 221 end
222 222  
  223 + should 'can display hits' do
  224 + p = create_user('testuser').person
  225 + a = RssFeed.create!(:name => 'Test article', :profile => p)
  226 + assert_equal false, a.can_display_hits?
  227 + end
  228 +
223 229 end
... ...
test/unit/uploaded_file_test.rb
... ... @@ -68,4 +68,9 @@ class UploadedFileTest &lt; Test::Unit::TestCase
68 68 assert_includes file.attachment_validation_options, :size
69 69 end
70 70  
  71 + should 'can display hits' do
  72 + file = UploadedFile.new(:uploaded_data => fixture_file_upload('/files/rails.png', 'image/png'))
  73 + assert_equal false, file.can_display_hits?
  74 + end
  75 +
71 76 end
... ...