-
Notifications
You must be signed in to change notification settings - Fork 69
Dashboard Events #348
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
Merged
Merged
Dashboard Events #348
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
ae0d939
Added dashbaord events
mllocs d7b3f60
Fixed typo and hashrocket
mllocs 17a20bc
Added Event model specs
mllocs 780d733
Update and configure shoulda-matchers
enricostano b08f271
Modify events table migration with plain SQL
enricostano 0da31cb
Add more specs to Event model
enricostano 43a0f4f
Use Rails enum, backed by an integer column with CHECK constraint
enricostano 898ee75
Fix and improve specs
enricostano 0eb2536
Use ActiveRecord DSL as much as possible
enricostano 833bf30
Create :updated event on Post update
enricostano dabf7e4
Create :created and :updated event for Member
enricostano b495581
Create :created and :updated event for Transfer
enricostano File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
class Event < ActiveRecord::Base | ||
enum action: { created: 0, updated: 1 } | ||
|
||
belongs_to :post | ||
belongs_to :member | ||
belongs_to :transfer | ||
|
||
validates :action, presence: true | ||
validate :resource_presence | ||
|
||
private | ||
|
||
# Validates that only one resource id is set | ||
# | ||
def resource_presence | ||
return if post_id.present? ^ member_id.present? ^ transfer_id.present? | ||
|
||
errors.add(:base, 'Specify only one resource id: `post_id`, `member_id` or `transfer_id`') | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# This migration does not use Rails ActiveRecord ORM DSL since | ||
# it doesn't provide data integrity (foreign key) for polymorphic models. | ||
# | ||
# The technique applied is called Exclusive Belongs To (AKA Exclusive Arc) | ||
# | ||
# Please read the following article for more details: | ||
# https://hashrocket.com/blog/posts/modeling-polymorphic-associations-in-a-relational-database | ||
# | ||
class CreateEvents < ActiveRecord::Migration | ||
def up | ||
create_table :events do |t| | ||
t.integer :action, null:false | ||
t.integer :post_id | ||
t.integer :member_id | ||
t.integer :transfer_id | ||
t.timestamps | ||
end | ||
|
||
add_foreign_key :events, :posts, name: 'events_post_id_fkey' | ||
add_foreign_key :events, :members, name: 'events_member_id_fkey' | ||
add_foreign_key :events, :transfers, name: 'events_transfer_id_fkey' | ||
|
||
add_index :events, :post_id, unique: true, where: 'post_id IS NOT NULL' | ||
add_index :events, :member_id, unique: true, where: 'member_id IS NOT NULL' | ||
add_index :events, :transfer_id, unique: true, where: 'transfer_id IS NOT NULL' | ||
|
||
execute <<-SQL | ||
ALTER TABLE events | ||
ADD CHECK(action IN (0, 1)), | ||
ADD CHECK( | ||
( | ||
(post_id IS NOT NULL)::integer + | ||
(member_id IS NOT NULL)::integer + | ||
(transfer_id IS NOT NULL)::integer | ||
) = 1 | ||
); | ||
SQL | ||
end | ||
|
||
def down | ||
drop_table :events | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Fabricator(:transfer) do | ||
source { Fabricate(:account) } | ||
destination { Fabricate(:account) } | ||
amount 10 | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
require 'spec_helper' | ||
|
||
describe Event do | ||
describe 'Validations' do | ||
it { is_expected.to validate_presence_of(:action) } | ||
it do | ||
is_expected.to define_enum_for(:action) | ||
.with([:created, :updated]) | ||
end | ||
|
||
describe '#resource_presence validation' do | ||
let(:post) { Fabricate(:post) } | ||
let(:member) { Fabricate(:member) } | ||
let(:event) { Event.new action: 'created' } | ||
|
||
context 'has no resources' do | ||
it { expect(event).to_not be_valid } | ||
end | ||
|
||
context 'has one resource' do | ||
before { event.post = post } | ||
|
||
it { expect(event).to be_valid } | ||
end | ||
|
||
context 'has two resources' do | ||
before { event.post = post; event.member = member } | ||
|
||
it { expect(event).to_not be_valid } | ||
end | ||
end | ||
end | ||
|
||
describe 'Relations' do | ||
it { is_expected.to belong_to(:post) } | ||
it { is_expected.to belong_to(:member) } | ||
it { is_expected.to belong_to(:transfer) } | ||
|
||
it { is_expected.to have_db_column(:post_id) } | ||
it { is_expected.to have_db_column(:member_id) } | ||
it { is_expected.to have_db_column(:transfer_id) } | ||
end | ||
|
||
describe 'Indexes' do | ||
it { is_expected.to have_db_index(:post_id) } | ||
it { is_expected.to have_db_index(:member_id) } | ||
it { is_expected.to have_db_index(:transfer_id) } | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure I fully understand what you mean.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Read the article 😂