diff --git a/app/controllers/public/content_viewer_controller.rb b/app/controllers/public/content_viewer_controller.rb
index 002de9d..d4bef04 100644
--- a/app/controllers/public/content_viewer_controller.rb
+++ b/app/controllers/public/content_viewer_controller.rb
@@ -49,6 +49,9 @@ class ContentViewerController < ApplicationController
render :action => 'access_denied', :status => 403, :layout => false
end
+ # At this point the page will be showed
+ @page.hit
+
if @page.mime_type != 'text/html'
headers['Content-Type'] = @page.mime_type
data = @page.data
diff --git a/app/helpers/article_helper.rb b/app/helpers/article_helper.rb
index 9dc2823..176e478 100644
--- a/app/helpers/article_helper.rb
+++ b/app/helpers/article_helper.rb
@@ -18,8 +18,13 @@ module ArticleHelper
'div',
check_box(:article, :notify_comments) +
content_tag('label', _('I want to receive a notification of each comment written by e-mail'), :for => 'article_notify_comments') +
- observe_field(:article_accept_comments, :function => "$('article_notify_comments').disabled = ! $('article_accept_comments').checked")
- )
+ observe_field(:article_accept_comments, :function => "$('article_notify_comments').disabled = ! $('article_accept_comments').checked")
+ ) + (article.can_display_hits? ?
+ content_tag(
+ 'div',
+ check_box(:article, :display_hits) +
+ content_tag('label', _('I want this article to display the number of hits it received'), :for => 'article_display_hits')
+ ) : '')
)
end
diff --git a/app/models/article.rb b/app/models/article.rb
index 9bb3245..7ed5278 100644
--- a/app/models/article.rb
+++ b/app/models/article.rb
@@ -16,6 +16,10 @@ class Article < ActiveRecord::Base
has_many :article_categorizations, :conditions => [ 'articles_categories.virtual = ?', false ]
has_many :categories, :through => :article_categorizations
+ acts_as_having_settings :field => :setting
+
+ settings_items :display_hits, :type => :boolean, :default => true
+
def self.human_attribute_name(attrib)
case attrib.to_sym
when :name
@@ -209,13 +213,26 @@ class Article < ActiveRecord::Base
end
def article_attr_blacklist
- ['id', 'profile_id', 'parent_id', 'slug', 'path', 'updated_at', 'created_at', 'last_changed_by_id', 'version', 'lock_version', 'type', 'children_count', 'comments_count']
+ ['id', 'profile_id', 'parent_id', 'slug', 'path', 'updated_at', 'created_at', 'last_changed_by_id', 'version', 'lock_version', 'type', 'children_count', 'comments_count', 'hits']
end
def self.find_by_old_path(old_path)
find(:first, :include => :versions, :conditions => ['article_versions.path = ?', old_path], :order => 'article_versions.id desc')
end
+ def hit
+ self.hits += 1
+ save!
+ end
+
+ def can_display_hits?
+ true
+ end
+
+ def display_hits?
+ can_display_hits? && display_hits
+ end
+
private
def sanitize_tag_list
diff --git a/app/models/enterprise_homepage.rb b/app/models/enterprise_homepage.rb
index 95a7aef..9b68f5e 100644
--- a/app/models/enterprise_homepage.rb
+++ b/app/models/enterprise_homepage.rb
@@ -26,4 +26,8 @@ class EnterpriseHomepage < Article
(self.profile.environment.enabled?('disable_products_for_enterprises') ? '' : display_products_list(self.profile, products))
end
+ def can_display_hits?
+ false
+ end
+
end
diff --git a/app/models/folder.rb b/app/models/folder.rb
index 8eaa74b..3346fa5 100644
--- a/app/models/folder.rb
+++ b/app/models/folder.rb
@@ -29,4 +29,8 @@ class Folder < Article
true
end
+ def can_display_hits?
+ false
+ end
+
end
diff --git a/app/models/rss_feed.rb b/app/models/rss_feed.rb
index 0a50b58..9936786 100644
--- a/app/models/rss_feed.rb
+++ b/app/models/rss_feed.rb
@@ -119,4 +119,8 @@ class RssFeed < Article
'rss-feed'
end
+ def can_display_hits?
+ false
+ end
+
end
diff --git a/app/models/uploaded_file.rb b/app/models/uploaded_file.rb
index a8ac8eb..326b513 100644
--- a/app/models/uploaded_file.rb
+++ b/app/models/uploaded_file.rb
@@ -49,4 +49,8 @@ class UploadedFile < Article
false
end
+ def can_display_hits?
+ false
+ end
+
end
diff --git a/app/views/content_viewer/view_page.rhtml b/app/views/content_viewer/view_page.rhtml
index 61e1070..f970689 100644
--- a/app/views/content_viewer/view_page.rhtml
+++ b/app/views/content_viewer/view_page.rhtml
@@ -61,6 +61,12 @@
<% end %>
+<% if @page.display_hits? %>
+
+ <%= n_('Viewed one time', 'Viewed %{num} times', @page.hits) % { :num => @page.hits } %>
+
+<% end %>
+
<% if @page.parent && !@page.parent.path.blank? %>
<%= link_to_document(@page.parent, _('Up')) %>
diff --git a/db/migrate/058_add_hits_to_articles.rb b/db/migrate/058_add_hits_to_articles.rb
new file mode 100644
index 0000000..f707461
--- /dev/null
+++ b/db/migrate/058_add_hits_to_articles.rb
@@ -0,0 +1,11 @@
+class AddHitsToArticles < ActiveRecord::Migration
+ def self.up
+ add_column :articles, :hits, :integer, :default => 0
+ add_column :article_versions, :hits, :integer, :default => 0
+ end
+
+ def self.down
+ remove_column :articles, :hits
+ remove_column :article_versions, :hits
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
index d0bb515..921d4ed 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -9,7 +9,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 57) do
+ActiveRecord::Schema.define(:version => 58) do
create_table "article_versions", :force => true do |t|
t.integer "article_id"
@@ -41,6 +41,7 @@ ActiveRecord::Schema.define(:version => 57) do
t.integer "reference_article_id"
t.text "setting"
t.boolean "notify_comments", :default => false
+ t.integer "hits", :default => 0
end
create_table "articles", :force => true do |t|
@@ -73,6 +74,7 @@ ActiveRecord::Schema.define(:version => 57) do
t.integer "reference_article_id"
t.text "setting"
t.boolean "notify_comments", :default => false
+ t.integer "hits", :default => 0
end
create_table "articles_categories", :id => false, :force => true do |t|
diff --git a/public/stylesheets/article.css b/public/stylesheets/article.css
index a0f83f4..a229e26 100644
--- a/public/stylesheets/article.css
+++ b/public/stylesheets/article.css
@@ -23,6 +23,11 @@
text-decoration: none;
}
+#article-hits {
+ font-size: 10px;
+ text-align: right;
+}
+
#article-cat {
font-size: 10px;
}
diff --git a/test/functional/content_viewer_controller_test.rb b/test/functional/content_viewer_controller_test.rb
index cec8473..3f4ddc5 100644
--- a/test/functional/content_viewer_controller_test.rb
+++ b/test/functional/content_viewer_controller_test.rb
@@ -644,4 +644,11 @@ class ContentViewerControllerTest < Test::Unit::TestCase
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/ } }
end
+ should 'hit the article when viewed' do
+ a = profile.articles.create!(:name => 'test article')
+ get :view_page, :profile => profile.identifier, :page => [a.path]
+ a.reload
+ assert_equal 1, a.hits
+ end
+
end
diff --git a/test/unit/article_test.rb b/test/unit/article_test.rb
index 9d86e1f..b5b536e 100644
--- a/test/unit/article_test.rb
+++ b/test/unit/article_test.rb
@@ -589,4 +589,30 @@ class ArticleTest < Test::Unit::TestCase
assert !a.notify_comments?
end
+ should 'hold hits count' do
+ a = Article.create!(:name => 'Test article', :profile => profile)
+ a.hits = 10
+ a.save!
+ a.reload
+ assert_equal 10, a.hits
+ end
+
+ should 'increment hit counter when hitted' do
+ a = Article.create!(:name => 'Test article', :profile => profile, :hits => 10)
+ a.hit
+ a.reload
+ assert_equal 11, a.hits
+ end
+
+ should 'have display_hits setting with default true' do
+ a = Article.create!(:name => 'Test article', :profile => profile)
+ assert_respond_to a, :display_hits
+ assert_equal true, a.display_hits
+ end
+
+ should 'can display hits' do
+ a = Article.create!(:name => 'Test article', :profile => profile)
+ assert_respond_to a, :can_display_hits?
+ assert_equal true, a.can_display_hits?
+ end
end
diff --git a/test/unit/enterprise_homepage_test.rb b/test/unit/enterprise_homepage_test.rb
index 4026127..fd7fb32 100644
--- a/test/unit/enterprise_homepage_test.rb
+++ b/test/unit/enterprise_homepage_test.rb
@@ -53,4 +53,10 @@ class EnterpriseHomepageTest < Test::Unit::TestCase
result = a.to_html
assert_match /catalog\/test_enterprise\/#{prod.id}/, result
end
+
+ should 'can display hits' do
+ a = EnterpriseHomepage.new(:name => 'Test article')
+ assert_equal false, a.can_display_hits?
+ end
+
end
diff --git a/test/unit/folder_test.rb b/test/unit/folder_test.rb
index 085fe0b..1414bc6 100644
--- a/test/unit/folder_test.rb
+++ b/test/unit/folder_test.rb
@@ -46,4 +46,9 @@ class FolderTest < ActiveSupport::TestCase
assert Folder.new.folder?, 'folder must identity itself as folder'
end
+ should 'can display hits' do
+ a = Folder.create!(:name => 'Test article', :profile => profile)
+ assert_equal false, a.can_display_hits?
+ end
+
end
diff --git a/test/unit/rss_feed_test.rb b/test/unit/rss_feed_test.rb
index 389c5c1..bf45035 100644
--- a/test/unit/rss_feed_test.rb
+++ b/test/unit/rss_feed_test.rb
@@ -220,4 +220,10 @@ class RssFeedTest < Test::Unit::TestCase
assert !feed.advertise?
end
+ should 'can display hits' do
+ p = create_user('testuser').person
+ a = RssFeed.create!(:name => 'Test article', :profile => p)
+ assert_equal false, a.can_display_hits?
+ end
+
end
diff --git a/test/unit/uploaded_file_test.rb b/test/unit/uploaded_file_test.rb
index 040a7b7..67ec8fe 100644
--- a/test/unit/uploaded_file_test.rb
+++ b/test/unit/uploaded_file_test.rb
@@ -68,4 +68,9 @@ class UploadedFileTest < Test::Unit::TestCase
assert_includes file.attachment_validation_options, :size
end
+ should 'can display hits' do
+ file = UploadedFile.new(:uploaded_data => fixture_file_upload('/files/rails.png', 'image/png'))
+ assert_equal false, file.can_display_hits?
+ end
+
end
--
libgit2 0.21.2