From 97aca38d5317130a589d7c12fde5687887f10618 Mon Sep 17 00:00:00 2001 From: Connor Turland Date: Sun, 8 Feb 2015 20:42:55 -0500 Subject: [PATCH] table is there and page views are being tracked --- app/controllers/maps_controller.rb | 5 +++++ app/helpers/maps_helper.rb | 10 ++++++++++ app/models/map.rb | 1 + app/models/map_view.rb | 4 ++++ app/models/user.rb | 1 + db/migrate/20150209013154_create_map_views.rb | 12 ++++++++++++ db/schema.rb | 12 +++++++++++- test/fixtures/map_views.yml | 11 +++++++++++ test/unit/map_view_test.rb | 7 +++++++ 9 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 app/models/map_view.rb create mode 100644 db/migrate/20150209013154_create_map_views.rb create mode 100644 test/fixtures/map_views.yml create mode 100644 test/unit/map_view_test.rb diff --git a/app/controllers/maps_controller.rb b/app/controllers/maps_controller.rb index 8f0ced9b..c354a58a 100644 --- a/app/controllers/maps_controller.rb +++ b/app/controllers/maps_controller.rb @@ -1,4 +1,5 @@ class MapsController < ApplicationController + include MapsHelper before_filter :require_user, only: [:create, :update, :screenshot, :destroy] @@ -69,6 +70,8 @@ class MapsController < ApplicationController redirect_to root_url, notice: "Access denied. That map is private." and return end + log_page_view(@current, params[:id]) + respond_to do |format| format.html { @allmappers = @map.contributors @@ -99,6 +102,8 @@ class MapsController < ApplicationController redirect_to root_url, notice: "Access denied. That map is private." and return end + log_page_view(@current, params[:id]) + @allmappers = @map.contributors @alltopics = @map.topics.delete_if {|t| t.permission == "private" && (!authenticated? || (authenticated? && @current.id != t.user_id)) } @allsynapses = @map.synapses.delete_if {|s| s.permission == "private" && (!authenticated? || (authenticated? && @current.id != s.user_id)) } diff --git a/app/helpers/maps_helper.rb b/app/helpers/maps_helper.rb index 0c9b3a08..f942d408 100644 --- a/app/helpers/maps_helper.rb +++ b/app/helpers/maps_helper.rb @@ -1,5 +1,15 @@ module MapsHelper + def log_page_view(user, mapId) + if user + MapView.find_or_create_by_user_id_and_map_id(user.id, mapId).touch(:updated_at) + + #limit records of map views for this user to ten + MapView.where(user_id: user.id).order('updated_at desc').offset(10).destroy_all + end + end + + ## this one is for building our custom JSON autocomplete format for typeahead def autocomplete_map_array_json(maps) temp = [] diff --git a/app/models/map.rb b/app/models/map.rb index 3bf5a4a6..9c3a133b 100644 --- a/app/models/map.rb +++ b/app/models/map.rb @@ -1,6 +1,7 @@ class Map < ActiveRecord::Base belongs_to :user + has_many :map_views has_many :topicmappings, :class_name => 'Mapping', :conditions => {:category => 'Topic'} has_many :synapsemappings, :class_name => 'Mapping', :conditions => {:category => 'Synapse'} diff --git a/app/models/map_view.rb b/app/models/map_view.rb new file mode 100644 index 00000000..ccc29c99 --- /dev/null +++ b/app/models/map_view.rb @@ -0,0 +1,4 @@ +class MapView < ActiveRecord::Base + belongs_to :user + belongs_to :map +end diff --git a/app/models/user.rb b/app/models/user.rb index 11062046..36a0e657 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -6,6 +6,7 @@ class User < ActiveRecord::Base has_many :synapses has_many :maps has_many :mappings + has_many :map_views before_create :generate_code diff --git a/db/migrate/20150209013154_create_map_views.rb b/db/migrate/20150209013154_create_map_views.rb new file mode 100644 index 00000000..b0bce196 --- /dev/null +++ b/db/migrate/20150209013154_create_map_views.rb @@ -0,0 +1,12 @@ +class CreateMapViews < ActiveRecord::Migration + def change + create_table :map_views do |t| + t.references :user + t.references :map + + t.timestamps + end + add_index :map_views, :user_id + add_index :map_views, :map_id + end +end diff --git a/db/schema.rb b/db/schema.rb index fc6b7335..a16b0831 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20141121204712) do +ActiveRecord::Schema.define(:version => 20150209003902) do create_table "in_metacode_sets", :force => true do |t| t.integer "metacode_id" @@ -23,6 +23,16 @@ ActiveRecord::Schema.define(:version => 20141121204712) do add_index "in_metacode_sets", ["metacode_id"], :name => "index_in_metacode_sets_on_metacode_id" add_index "in_metacode_sets", ["metacode_set_id"], :name => "index_in_metacode_sets_on_metacode_set_id" + create_table "map_views", :force => true do |t| + t.integer "user_id" + t.integer "map_id" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false + end + + add_index "map_views", ["map_id"], :name => "index_map_views_on_map_id" + add_index "map_views", ["user_id"], :name => "index_map_views_on_user_id" + create_table "mappings", :force => true do |t| t.text "category" t.integer "xloc" diff --git a/test/fixtures/map_views.yml b/test/fixtures/map_views.yml new file mode 100644 index 00000000..c63aac0b --- /dev/null +++ b/test/fixtures/map_views.yml @@ -0,0 +1,11 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html + +# This model initially had no columns defined. If you add columns to the +# model remove the '{}' from the fixture names and add the columns immediately +# below each fixture, per the syntax in the comments below +# +one: {} +# column: value +# +two: {} +# column: value diff --git a/test/unit/map_view_test.rb b/test/unit/map_view_test.rb new file mode 100644 index 00000000..5bbea3e0 --- /dev/null +++ b/test/unit/map_view_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class MapViewTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end