From 35aa787084b19e66e64450fe2fdb2690dc640b90 Mon Sep 17 00:00:00 2001 From: Filip Stybel Date: Thu, 6 Feb 2020 16:33:02 +0100 Subject: [PATCH 1/3] Install public_activity gem for create activity feed --- Gemfile | 1 + Gemfile.lock | 8 +++++- .../20200206135750_create_activities.rb | 25 +++++++++++++++++++ db/schema.rb | 21 +++++++++++++++- 4 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 db/migrate/20200206135750_create_activities.rb diff --git a/Gemfile b/Gemfile index ef96c09..befb232 100644 --- a/Gemfile +++ b/Gemfile @@ -23,6 +23,7 @@ gem 'devise' gem 'omniauth-github' gem "octokit", "~> 4.0" gem "pundit" +gem 'public_activity' group :development, :test do gem 'pry-rails' diff --git a/Gemfile.lock b/Gemfile.lock index 3952646..0da5764 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -147,6 +147,11 @@ GEM method_source (~> 0.9.0) pry-rails (0.3.9) pry (>= 0.10.4) + public_activity (1.6.4) + actionpack (>= 3.0.0) + activerecord (>= 3.0) + i18n (>= 0.5.0) + railties (>= 3.0.0) public_suffix (4.0.1) puma (3.12.1) pundit (2.1.0) @@ -270,6 +275,7 @@ DEPENDENCIES omniauth-github pg (>= 0.18, < 2.0) pry-rails + public_activity puma (~> 3.11) pundit rails (~> 5.2.3) @@ -286,4 +292,4 @@ RUBY VERSION ruby 2.6.0p0 BUNDLED WITH - 1.17.2 + 1.17.3 diff --git a/db/migrate/20200206135750_create_activities.rb b/db/migrate/20200206135750_create_activities.rb new file mode 100644 index 0000000..a2b0dec --- /dev/null +++ b/db/migrate/20200206135750_create_activities.rb @@ -0,0 +1,25 @@ +# frozen_string_literal: true + +# Migration responsible for creating a table with activities +class CreateActivities < (ActiveRecord.version.release() < Gem::Version.new('5.2.0') ? ActiveRecord::Migration : ActiveRecord::Migration[5.2]) + # Create table + def self.up + create_table :activities do |t| + t.belongs_to :trackable, :polymorphic => true + t.belongs_to :owner, :polymorphic => true + t.string :key + t.text :parameters + t.belongs_to :recipient, :polymorphic => true + + t.timestamps + end + + add_index :activities, [:trackable_id, :trackable_type] + add_index :activities, [:owner_id, :owner_type] + add_index :activities, [:recipient_id, :recipient_type] + end + # Drop table + def self.down + drop_table :activities + end +end diff --git a/db/schema.rb b/db/schema.rb index 0624e12..728057d 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,11 +10,30 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2019_10_05_194627) do +ActiveRecord::Schema.define(version: 2020_02_06_135750) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" + create_table "activities", force: :cascade do |t| + t.string "trackable_type" + t.bigint "trackable_id" + t.string "owner_type" + t.bigint "owner_id" + t.string "key" + t.text "parameters" + t.string "recipient_type" + t.bigint "recipient_id" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["owner_id", "owner_type"], name: "index_activities_on_owner_id_and_owner_type" + t.index ["owner_type", "owner_id"], name: "index_activities_on_owner_type_and_owner_id" + t.index ["recipient_id", "recipient_type"], name: "index_activities_on_recipient_id_and_recipient_type" + t.index ["recipient_type", "recipient_id"], name: "index_activities_on_recipient_type_and_recipient_id" + t.index ["trackable_id", "trackable_type"], name: "index_activities_on_trackable_id_and_trackable_type" + t.index ["trackable_type", "trackable_id"], name: "index_activities_on_trackable_type_and_trackable_id" + end + create_table "developers", force: :cascade do |t| t.string "name" t.datetime "created_at", null: false From c3937019371bf6fb922fcfdabee3a5ad7348725e Mon Sep 17 00:00:00 2001 From: Filip Stybel Date: Thu, 6 Feb 2020 21:50:10 +0100 Subject: [PATCH 2/3] Track activity of models - add tracking in Project model - add tracking in Developer model - add tracking owner of activity --- app/controllers/application_controller.rb | 2 ++ app/models/developer.rb | 2 ++ app/models/project.rb | 2 ++ app/models/user.rb | 5 +++-- 4 files changed, 9 insertions(+), 2 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 5dde212..b9708eb 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -2,4 +2,6 @@ class ApplicationController < ActionController::Base include Pundit + include PublicActivity::StoreController + end diff --git a/app/models/developer.rb b/app/models/developer.rb index 5467101..7b2334e 100644 --- a/app/models/developer.rb +++ b/app/models/developer.rb @@ -1,4 +1,6 @@ # frozen_string_literal: true class Developer < ApplicationRecord + include PublicActivity::Model + tracked owner: proc { |controller, _model| controller.current_user } end diff --git a/app/models/project.rb b/app/models/project.rb index 1ef799c..2c1ba79 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -1,6 +1,8 @@ # frozen_string_literal: true class Project < ApplicationRecord + include PublicActivity::Model + tracked owner: proc { |controller, _model| controller.current_user } validates :name, presence: true belongs_to :user diff --git a/app/models/user.rb b/app/models/user.rb index 659f19a..45f960b 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -6,9 +6,10 @@ class User < ApplicationRecord devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable, :omniauthable - + has_many :projects - + has_many :activities, as: :owner, class_name: 'PublicActivity::Activity' + validates :role, presence: true, inclusion: { in: ROLES } enum role: ROLES From 6562f3d9dfdc1f79edebe2636e3fc92755381228 Mon Sep 17 00:00:00 2001 From: Filip Stybel Date: Thu, 6 Feb 2020 21:54:07 +0100 Subject: [PATCH 3/3] Add views for recorded user activity --- app/controllers/activities_controller.rb | 7 +++++++ app/views/activities/index.html.erb | 2 ++ app/views/layouts/_navbar.html.erb | 4 ++++ app/views/public_activity/developer/_create.html.erb | 1 + app/views/public_activity/developer/_destroy.html.erb | 2 ++ app/views/public_activity/developer/_update.html.erb | 2 ++ app/views/public_activity/project/_create.html.erb | 2 ++ app/views/public_activity/project/_destroy.html.erb | 2 ++ app/views/public_activity/project/_update.html.erb | 2 ++ config/routes.rb | 1 + 10 files changed, 25 insertions(+) create mode 100644 app/controllers/activities_controller.rb create mode 100644 app/views/activities/index.html.erb create mode 100644 app/views/public_activity/developer/_create.html.erb create mode 100644 app/views/public_activity/developer/_destroy.html.erb create mode 100644 app/views/public_activity/developer/_update.html.erb create mode 100644 app/views/public_activity/project/_create.html.erb create mode 100644 app/views/public_activity/project/_destroy.html.erb create mode 100644 app/views/public_activity/project/_update.html.erb diff --git a/app/controllers/activities_controller.rb b/app/controllers/activities_controller.rb new file mode 100644 index 0000000..b0081d4 --- /dev/null +++ b/app/controllers/activities_controller.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class ActivitiesController < ApplicationController + def index + @activities = PublicActivity::Activity.includes(:owner).order(created_at: :desc).limit(20) + end +end \ No newline at end of file diff --git a/app/views/activities/index.html.erb b/app/views/activities/index.html.erb new file mode 100644 index 0000000..9ba0ecc --- /dev/null +++ b/app/views/activities/index.html.erb @@ -0,0 +1,2 @@ +

Activities:

+<%= render_activities(@activities) %> diff --git a/app/views/layouts/_navbar.html.erb b/app/views/layouts/_navbar.html.erb index 32a6a4d..75fba9a 100644 --- a/app/views/layouts/_navbar.html.erb +++ b/app/views/layouts/_navbar.html.erb @@ -14,6 +14,10 @@ + +