diff --git a/app/controllers/map_controller.rb b/app/controllers/map_controller.rb index 95acd6d086..846a09181c 100644 --- a/app/controllers/map_controller.rb +++ b/app/controllers/map_controller.rb @@ -20,7 +20,7 @@ def show # redirect_old_urls - impressionist(@node.drupal_node_counter) + impressionist(@node) @title = @node.title @tags = @node.tags @tagnames = @tags.collect(&:name) diff --git a/app/controllers/notes_controller.rb b/app/controllers/notes_controller.rb index c7a9f70e68..2567dc7f9f 100644 --- a/app/controllers/notes_controller.rb +++ b/app/controllers/notes_controller.rb @@ -79,7 +79,7 @@ def show alert_and_redirect_moderated - impressionist(@node.drupal_node_counter) + impressionist(@node, 'show', :unique => [:ip_address]) @title = @node.latest.title @tags = @node.tags @tagnames = @tags.collect(&:name) @@ -305,8 +305,7 @@ def popular @notes = DrupalNode.research_notes .limit(20) .where(status: 1) - .order("node_counter.totalcount DESC") - .includes(:drupal_node_counter) + .order("views DESC") @unpaginated = true render :template => 'notes/index' end diff --git a/app/controllers/questions_controller.rb b/app/controllers/questions_controller.rb index e1ead3f21c..fbc4bf6c56 100644 --- a/app/controllers/questions_controller.rb +++ b/app/controllers/questions_controller.rb @@ -30,7 +30,7 @@ def show alert_and_redirect_moderated - impressionist(@node.drupal_node_counter) + impressionist(@node) @title = @node.latest.title @tags = @node.power_tag_objects('question') @tagnames = @tags.collect(&:name) @@ -81,8 +81,7 @@ def popular @title = "Popular Questions" @questions = DrupalNode.questions.where(status: 1) sort_question_by_tags - @questions = @questions.order('node_counter.totalcount DESC') - .includes(:drupal_node_counter) + @questions = @questions.order('views DESC') .limit(20) @wikis = DrupalNode.limit(10) diff --git a/app/controllers/wiki_controller.rb b/app/controllers/wiki_controller.rb index 9834398111..e98e709447 100644 --- a/app/controllers/wiki_controller.rb +++ b/app/controllers/wiki_controller.rb @@ -63,7 +63,7 @@ def show set_sidebar :tags, @tagnames, {:videos => true} @wikis = Tag.find_pages(@node.slug_from_path,30) if @node.has_tag('chapter') || @node.has_tag('tabbed:wikis') - impressionist(@node.drupal_node_counter) + impressionist(@node, 'show', :unique => [:ip_address]) @revision = @node.latest @title = @revision.title end @@ -288,7 +288,7 @@ def index order_string = "node_revisions.timestamp DESC" end - @wikis = DrupalNode.includes(:drupal_node_revision, :drupal_node_counter) + @wikis = DrupalNode.includes(:drupal_node_revision) .group('node_revisions.nid') .order(order_string) .where("node_revisions.status = 1 AND node.status = 1 AND (type = 'page' OR type = 'tool' OR type = 'place')") @@ -300,12 +300,11 @@ def index def popular @title = I18n.t('wiki_controller.popular_wiki_pages') @wikis = DrupalNode.limit(40) - .order("node_counter.totalcount DESC") + .order("views DESC") .joins(:drupal_node_revision) .group('node_revisions.nid') .order("node_revisions.timestamp DESC") .where("node.status = 1 AND node_revisions.status = 1 AND node.nid != 259 AND (type = 'page' OR type = 'tool' OR type = 'place')") - .includes(:drupal_node_counter) render :template => "wiki/index" end diff --git a/app/models/drupal_node.rb b/app/models/drupal_node.rb index ddfd3a02fe..ad84b0875a 100644 --- a/app/models/drupal_node.rb +++ b/app/models/drupal_node.rb @@ -16,7 +16,7 @@ def validate(record) class DrupalNode < ActiveRecord::Base include NodeShared # common methods for node-like models - attr_accessible :title, :uid, :status, :type, :vid, :cached_likes, :comment, :path, :slug + attr_accessible :title, :uid, :status, :type, :vid, :cached_likes, :comment, :path, :slug, :views self.table_name = 'node' self.primary_key = 'nid' @@ -41,7 +41,9 @@ def updated_month updated_at.strftime('%B %Y') end - + # FriendlyId is not being used; see + # https://github.com/publiclab/plots2/pull/687 and + # https://github.com/publiclab/plots2/pull/600 extend FriendlyId friendly_id :friendly_id_string, use: [:slugged, :history] @@ -65,7 +67,6 @@ def friendly_id_string # wasn't working to tie it to .vid, manually defining below # has_one :drupal_main_image, :foreign_key => 'vid', :dependent => :destroy # has_many :drupal_content_field_image_gallery, :foreign_key => 'nid' - has_one :drupal_node_counter, :foreign_key => 'nid', :dependent => :destroy has_many :drupal_upload, :foreign_key => 'nid', :dependent => :destroy has_many :drupal_files, :through => :drupal_upload has_many :drupal_node_community_tag, :foreign_key => 'nid', :dependent => :destroy @@ -167,15 +168,24 @@ def set_changed_and_created self['changed'] = DateTime.now.to_i end - # determines URL ("slug"), initializes the view counter, and sets up a created timestamp + # determines URL ("slug"), and sets up a created timestamp def setup self['created'] = DateTime.now.to_i self.save - DrupalNodeCounter.new({:nid => self.id}).save end public + # the counter_cache does not currently work: views column is not updated for some reason + # https://github.com/publiclab/plots2/issues/1196 + is_impressionable counter_cache: true, column_name: :views + + def totalviews + # disabled as impressionist is not currently updating counter_cache; see above + #self.views + self.legacy_views + self.impressionist_count(:filter => :ip_address) + self.legacy_views + end + def self.weekly_tallies(type = "note", span = 52, time = Time.now) weeks = {} (0..span).each do |week| @@ -473,12 +483,6 @@ def tagnames self.tags.collect(&:name) end - # view count - def totalcount - DrupalNodeCounter.new({:nid => self.id}).save if self.drupal_node_counter.nil? - self.drupal_node_counter.totalcount - end - def edit_path if self.type == "page" || self.type == "tool" || self.type == "place" path = "/wiki/edit/" + self.path.split("/").last diff --git a/app/models/drupal_node_counter.rb b/app/models/drupal_node_counter.rb index 302d709c01..610c418c52 100644 --- a/app/models/drupal_node_counter.rb +++ b/app/models/drupal_node_counter.rb @@ -4,5 +4,4 @@ class DrupalNodeCounter < ActiveRecord::Base belongs_to :drupal_node, :foreign_key => 'nid', :dependent => :destroy - is_impressionable :counter_cache => true, :column_name => :totalcount, :unique => :ip_address end diff --git a/app/models/tag.rb b/app/models/tag.rb index 12ed40f95c..8321c0bde9 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -62,9 +62,9 @@ def belongs_to(current_user, nid) def self.find_top_nodes_by_type(tagname, type = "wiki", limit = 10) DrupalNode.where(type: type) .where('term_data.name = ?', tagname) - .order("node_counter.totalcount DESC") + .order("node.views DESC") .limit(limit) - .include(:drupal_node_counter, :drupal_node_community_tag, :tag) + .include(:drupal_node_community_tag, :tag) end # finds recent nodes - should drop "limit" and allow use of chainable .limit() @@ -109,10 +109,10 @@ def self.find_nodes_by_type_with_all_tags(tagnames, type = "note", limit = 10) def self.find_popular_notes(tagname, views = 20, limit = 10) DrupalNode.where(type: "note") - .where('term_data.name = ? AND node_counter.totalcount > (?)', tagname, views) + .where('term_data.name = ? AND node.views > (?)', tagname, views) .order("node.nid DESC") .limit(limit) - .include(:drupal_node_counter, :drupal_node_community_tag, :tag) + .include(:drupal_node_community_tag, :tag) end def self.exists?(tagname,nid) diff --git a/app/views/dashboard/_node_meta.html.erb b/app/views/dashboard/_node_meta.html.erb index 7e2df1d5c1..5aa4dd01a7 100644 --- a/app/views/dashboard/_node_meta.html.erb +++ b/app/views/dashboard/_node_meta.html.erb @@ -9,7 +9,7 @@ <% else %> | <%= node.comment_count %> <% end %> - + | <%= node.likes %> <% if params[:mod] %>| <% end %> diff --git a/app/views/map/show.html.erb b/app/views/map/show.html.erb index d38f57df5b..fcbb70aab3 100644 --- a/app/views/map/show.html.erb +++ b/app/views/map/show.html.erb @@ -38,7 +38,7 @@

Published by <%= @node.author.name %>

<% end %>

<%= @node.lat %> N, <%= @node.lon %> E

-

<%= @node.totalcount %> views

+

<%= @node.totalviews %> views

<% if @node.map.field_ground_resolution_value %>

Ground resolution: <%= @node.map.field_ground_resolution_value %> cm/px

<% end %>

Capture date: <%= @node.map.captured_on.to_s %>

Publication date: <%= @node.map.published_on.to_s %>

diff --git a/app/views/notes/_notes.html.erb b/app/views/notes/_notes.html.erb index 81c7be03db..59cf5ef9d8 100644 --- a/app/views/notes/_notes.html.erb +++ b/app/views/notes/_notes.html.erb @@ -31,7 +31,7 @@ <%= t('notes._notes.last_edit_by') %> target="_blank"<% end %> href="/profile/<%= node.latest.author.name %>"><%= node.latest.author.name %> <%= distance_of_time_in_words(Time.at(node.latest.timestamp), Time.current, false, :scope => :'datetime.time_ago_in_words') %> <% end %> - | <%= number_with_delimiter(node.totalcount) %> + | <%= number_with_delimiter(node.totalviews) %> | <%= node.likes %>

diff --git a/app/views/notes/_tagged_notes.html.erb b/app/views/notes/_tagged_notes.html.erb index a74beef777..1e0b028c62 100644 --- a/app/views/notes/_tagged_notes.html.erb +++ b/app/views/notes/_tagged_notes.html.erb @@ -8,7 +8,7 @@

<%= raw t('notes._tagged_notes.by_author_time_ago', :url1 => "/notes/author/"+node.author.name, :author => node.author.name, :time => time_ago_in_words(node.created_at)) %> | <%= node.comment_count %> - | <%= number_with_delimiter(node.totalcount) %> <%= t('notes._tagged_notes.views') %> + | <%= number_with_delimiter(node.totalviews) %> <%= t('notes._tagged_notes.views') %> | <%= node.likes %>

diff --git a/app/views/notes/show.html.erb b/app/views/notes/show.html.erb index d3e2cd580f..2f85233c0f 100644 --- a/app/views/notes/show.html.erb +++ b/app/views/notes/show.html.erb @@ -45,7 +45,7 @@ <%= @node.created_at.to_s(:long) %>