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/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/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 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 @@ +
<%= a.owner.nickname %> created a new <%= link_to a.trackable_type.downcase, a.trackable %>
diff --git a/app/views/public_activity/developer/_destroy.html.erb b/app/views/public_activity/developer/_destroy.html.erb new file mode 100644 index 0000000..58a6afd --- /dev/null +++ b/app/views/public_activity/developer/_destroy.html.erb @@ -0,0 +1,2 @@ +<%= a.owner.nickname %> destroyed a <%= link_to a.trackable_type.downcase, a.trackable %>
+ diff --git a/app/views/public_activity/developer/_update.html.erb b/app/views/public_activity/developer/_update.html.erb new file mode 100644 index 0000000..a3d052c --- /dev/null +++ b/app/views/public_activity/developer/_update.html.erb @@ -0,0 +1,2 @@ +<%= a.owner.nickname %> updated a <%= link_to a.trackable_type.downcase, a.trackable %>
+ diff --git a/app/views/public_activity/project/_create.html.erb b/app/views/public_activity/project/_create.html.erb new file mode 100644 index 0000000..5594374 --- /dev/null +++ b/app/views/public_activity/project/_create.html.erb @@ -0,0 +1,2 @@ +<%= a.owner.nickname %> created a new <%= link_to a.trackable_type.downcase, a.trackable %>
+ diff --git a/app/views/public_activity/project/_destroy.html.erb b/app/views/public_activity/project/_destroy.html.erb new file mode 100644 index 0000000..58a6afd --- /dev/null +++ b/app/views/public_activity/project/_destroy.html.erb @@ -0,0 +1,2 @@ +<%= a.owner.nickname %> destroyed a <%= link_to a.trackable_type.downcase, a.trackable %>
+ diff --git a/app/views/public_activity/project/_update.html.erb b/app/views/public_activity/project/_update.html.erb new file mode 100644 index 0000000..a3d052c --- /dev/null +++ b/app/views/public_activity/project/_update.html.erb @@ -0,0 +1,2 @@ +<%= a.owner.nickname %> updated a <%= link_to a.trackable_type.downcase, a.trackable %>
+ diff --git a/config/routes.rb b/config/routes.rb index 36a7a4a..fcc2c5f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -5,6 +5,7 @@ get 'juniors', to: 'developers#index' get 'projects', to: 'projects#index' + get 'activities', to: 'activities#index' resources :projects 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