Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add activity feed #43

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
8 changes: 7 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -270,6 +275,7 @@ DEPENDENCIES
omniauth-github
pg (>= 0.18, < 2.0)
pry-rails
public_activity
puma (~> 3.11)
pundit
rails (~> 5.2.3)
Expand All @@ -286,4 +292,4 @@ RUBY VERSION
ruby 2.6.0p0

BUNDLED WITH
1.17.2
1.17.3
7 changes: 7 additions & 0 deletions app/controllers/activities_controller.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@

class ApplicationController < ActionController::Base
include Pundit
include PublicActivity::StoreController

end
2 changes: 2 additions & 0 deletions app/models/developer.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# frozen_string_literal: true

class Developer < ApplicationRecord
include PublicActivity::Model
tracked owner: proc { |controller, _model| controller.current_user }
end
2 changes: 2 additions & 0 deletions app/models/project.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
5 changes: 3 additions & 2 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions app/views/activities/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h1>Activities:</h1>
<%= render_activities(@activities) %>
4 changes: 4 additions & 0 deletions app/views/layouts/_navbar.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
<li class="nav-item">
<a class="nav-link" href="/projects">Projects</a>
</li>

<li class="nav-item">
<a class="nav-link" href="/activities">Activities</a>
</li>
<li class="nav-item">
<% if user_signed_in? %>
<%= link_to 'Sign Out', destroy_user_session_path, method: :delete, class: "nav-link" %>
Expand Down
1 change: 1 addition & 0 deletions app/views/public_activity/developer/_create.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p><strong><%= a.owner.nickname %></strong> created a new <%= link_to a.trackable_type.downcase, a.trackable %></p>
2 changes: 2 additions & 0 deletions app/views/public_activity/developer/_destroy.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<p><strong><%= a.owner.nickname %></strong> destroyed a <%= link_to a.trackable_type.downcase, a.trackable %></p>

2 changes: 2 additions & 0 deletions app/views/public_activity/developer/_update.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<p><strong><%= a.owner.nickname %></strong> updated a <%= link_to a.trackable_type.downcase, a.trackable %></p>

2 changes: 2 additions & 0 deletions app/views/public_activity/project/_create.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<p><strong><%= a.owner.nickname %></strong> created a new <%= link_to a.trackable_type.downcase, a.trackable %></p>

2 changes: 2 additions & 0 deletions app/views/public_activity/project/_destroy.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<p><strong><%= a.owner.nickname %></strong> destroyed a <%= link_to a.trackable_type.downcase, a.trackable %></p>

2 changes: 2 additions & 0 deletions app/views/public_activity/project/_update.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<p><strong><%= a.owner.nickname %></strong> updated a <%= link_to a.trackable_type.downcase, a.trackable %></p>

1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

get 'juniors', to: 'developers#index'
get 'projects', to: 'projects#index'
get 'activities', to: 'activities#index'

resources :projects

Expand Down
25 changes: 25 additions & 0 deletions db/migrate/20200206135750_create_activities.rb
Original file line number Diff line number Diff line change
@@ -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
21 changes: 20 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down