cm-action-trail 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +33 -0
- data/app/models/action_trail.rb +16 -0
- data/db/migrate/20220302120216_create_action_trails.rb +14 -0
- data/lib/cm-action-trail/core.rb +40 -0
- data/lib/cm-action-trail/engine.rb +5 -0
- data/lib/cm-action-trail/version.rb +3 -0
- data/lib/cm-action-trail.rb +13 -0
- metadata +51 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: cf963726bc6cc26dcf6c632b42def11bc2fa174047019dfb450d7253a302342b
|
4
|
+
data.tar.gz: 71838d3c802ec1223651e187422d09866795929bd8487e350d574a68f4bb5f04
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a973c09b6e3e542c8d8726e1ccdaaee503b7b4796982fe90e3e0fac2bed0305df1d7f51780e9bf6a05ae39bbe5367eaa597260036ac0a5e03f856bfe33ad9335
|
7
|
+
data.tar.gz: f3030df9dee7411025ea5a3a795d48a2f591b7460794a4fb3a7faa575901054e372aed887a413d32324748d96635470185b30b037ef6ceda2c94afc71f0721e8
|
data/README.md
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# CmAdmin
|
2
|
+
|
3
|
+
An action logger gem for Ruby on Rails application.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'cm-action-trail'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle install
|
16
|
+
|
17
|
+
## Usage
|
18
|
+
|
19
|
+
Copy the migration
|
20
|
+
|
21
|
+
$ rails cm_action_trail:install:migrations
|
22
|
+
|
23
|
+
Include inside the model
|
24
|
+
|
25
|
+
has_action_trail
|
26
|
+
|
27
|
+
In application.rb
|
28
|
+
|
29
|
+
require 'cm-action-trail'
|
30
|
+
|
31
|
+
## Contributing
|
32
|
+
|
33
|
+
Bug reports and pull requests are welcome on [GitHub](https://github.com/commutatus/cm-action-trail).
|
@@ -0,0 +1,16 @@
|
|
1
|
+
class ActionTrail < ApplicationRecord
|
2
|
+
|
3
|
+
belongs_to :entity, polymorphic: true
|
4
|
+
belongs_to :actor, foreign_key: 'actor_id', class_name: 'User', optional: true
|
5
|
+
|
6
|
+
enum trail_type: { created: 0, updated: 1, deleted: 2 }
|
7
|
+
|
8
|
+
def create_notification(notify_content, receiver)
|
9
|
+
notifications.create(
|
10
|
+
notification_content: notify_content,
|
11
|
+
sender_id: self.actor_id,
|
12
|
+
notifiable: self.entity,
|
13
|
+
receiver: receiver
|
14
|
+
)
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
class CreateActionTrails < ActiveRecord::Migration[6.1]
|
2
|
+
def change
|
3
|
+
create_table :action_trails do |t|
|
4
|
+
t.string :entity_type
|
5
|
+
t.integer :entity_id
|
6
|
+
t.integer :trail_type
|
7
|
+
t.string :event_name, null: false
|
8
|
+
t.integer :actor_id
|
9
|
+
t.jsonb :entity_changes
|
10
|
+
|
11
|
+
t.timestamps
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module CmActionTrail::Core
|
2
|
+
extend ActiveSupport::Concern
|
3
|
+
|
4
|
+
included do
|
5
|
+
attr_accessor :current_action_name
|
6
|
+
|
7
|
+
after_commit :register_event
|
8
|
+
|
9
|
+
before_save {|record|
|
10
|
+
if record.current_action_name.nil?
|
11
|
+
record.current_action_name = record.new_record? ? 'created' : 'updated'
|
12
|
+
end
|
13
|
+
}
|
14
|
+
before_destroy {|record| record.current_action_name = 'destroyed'}
|
15
|
+
|
16
|
+
has_many :action_trails, as: :entity
|
17
|
+
|
18
|
+
end
|
19
|
+
|
20
|
+
def check_trail_type
|
21
|
+
if self.new_record?
|
22
|
+
:created
|
23
|
+
elsif self.destroyed?
|
24
|
+
:deleted
|
25
|
+
else
|
26
|
+
:updated
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def register_event
|
31
|
+
trail_type = check_trail_type
|
32
|
+
::ActionTrail.create(
|
33
|
+
entity: self,
|
34
|
+
trail_type: trail_type,
|
35
|
+
event_name: self.current_action_name,
|
36
|
+
actor_id: Current.user&.id,
|
37
|
+
entity_changes: saved_changes.except(:updated_at)
|
38
|
+
)
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'cm-action-trail/version'
|
2
|
+
require 'cm-action-trail/engine'
|
3
|
+
require 'cm-action-trail/core'
|
4
|
+
|
5
|
+
module CmActionTrail
|
6
|
+
def has_action_trail
|
7
|
+
include CmActionTrail::Core
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
ActiveSupport.on_load(:active_record) do
|
12
|
+
ActiveRecord::Base.extend CmActionTrail
|
13
|
+
end
|
metadata
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cm-action-trail
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- anbublacky
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-04-06 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Description of Cm::ActionTrail.
|
14
|
+
email:
|
15
|
+
- anbublacky@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- README.md
|
21
|
+
- app/models/action_trail.rb
|
22
|
+
- db/migrate/20220302120216_create_action_trails.rb
|
23
|
+
- lib/cm-action-trail.rb
|
24
|
+
- lib/cm-action-trail/core.rb
|
25
|
+
- lib/cm-action-trail/engine.rb
|
26
|
+
- lib/cm-action-trail/version.rb
|
27
|
+
homepage: https://github.com/commutatus/cm-action-trail
|
28
|
+
licenses:
|
29
|
+
- MIT
|
30
|
+
metadata:
|
31
|
+
homepage_uri: https://github.com/commutatus/cm-action-trail
|
32
|
+
post_install_message:
|
33
|
+
rdoc_options: []
|
34
|
+
require_paths:
|
35
|
+
- lib
|
36
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
requirements: []
|
47
|
+
rubygems_version: 3.2.3
|
48
|
+
signing_key:
|
49
|
+
specification_version: 4
|
50
|
+
summary: Summary of Cm::ActionTrail.
|
51
|
+
test_files: []
|