diff --git a/app/models/node.rb b/app/models/node.rb index 9affb5b62d..6d69cc9aed 100644 --- a/app/models/node.rb +++ b/app/models/node.rb @@ -166,12 +166,10 @@ def setup public - is_impressionable counter_cache: true, column_name: :views + is_impressionable counter_cache: true, column_name: :views, unique: :ip_address def totalviews - # this doesn't filter out duplicate ip addresses as the line below does: - # self.views + self.legacy_views - impressionist_count(filter: :ip_address) + legacy_views + views + legacy_views end def self.weekly_tallies(type = 'note', span = 52, time = Time.now) diff --git a/db/migrate/20190401093400_update_node_view_count.rb b/db/migrate/20190401093400_update_node_view_count.rb new file mode 100644 index 0000000000..8d28ff4c9b --- /dev/null +++ b/db/migrate/20190401093400_update_node_view_count.rb @@ -0,0 +1,11 @@ +class UpdateNodeViewCount < ActiveRecord::Migration[5.2] + def up + Node.ids.each do |id| + node = Node.find(id) + node.update_columns(views: node.impressionist_count(filter: :ip_address)) + end + end + + def down + end +end diff --git a/db/schema.rb.example b/db/schema.rb.example index 448d91721e..84867acb7d 100644 --- a/db/schema.rb.example +++ b/db/schema.rb.example @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2019_03_30_043340) do +ActiveRecord::Schema.define(version: 2019_04_01_093400) do create_table "answer_selections", force: true do |t| t.integer "user_id" diff --git a/test/functional/notes_controller_test.rb b/test/functional/notes_controller_test.rb index 68e0467585..0e0898e22d 100644 --- a/test/functional/notes_controller_test.rb +++ b/test/functional/notes_controller_test.rb @@ -81,7 +81,7 @@ def teardown assert_equal '0.0.0.0', Impression.last.ip_address Impression.last.update_attribute('ip_address', '0.0.0.1') - assert_difference 'note.totalviews', 1 do + assert_difference 'note.reload.totalviews', 1 do get :show, params: { author: note.author.name, @@ -90,10 +90,10 @@ def teardown } end - assert_equal 2, note.totalviews + assert_equal 2, note.reload.totalviews # same IP won't add to views twice - assert_difference 'note.totalviews', 0 do + assert_difference 'note.reload.totalviews', 0 do get :show, params: { author: note.author.name, diff --git a/test/integration/node_unique_views_test.rb b/test/integration/node_unique_views_test.rb index 92805f866e..d7d32673de 100644 --- a/test/integration/node_unique_views_test.rb +++ b/test/integration/node_unique_views_test.rb @@ -12,7 +12,7 @@ class NodeInsertExtrasTest < ActionDispatch::IntegrationTest assert_response :success end - assert_difference 'nodes(:about).totalviews', 0 do + assert_difference 'nodes(:about).reload.totalviews', 0 do assert_difference 'Impression.count', 0 do get "/wiki/#{nodes(:about).slug}" assert_response :success @@ -22,7 +22,7 @@ class NodeInsertExtrasTest < ActionDispatch::IntegrationTest assert_equal '127.0.0.1', Impression.last.ip_address assert Impression.last.update_attributes(ip_address: '0.0.0.0') - assert_difference 'nodes(:about).totalviews', 1 do + assert_difference 'nodes(:about).reload.totalviews', 1 do assert_difference 'Impression.count', 1 do get "/wiki/#{nodes(:about).slug}" assert_response :success