effective_logging 4.2.0 → 4.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bd43bfe19f220a08425b7f93f3160ef4d2fa4107e6eb8c7d00d7e92dff2ede90
4
- data.tar.gz: 3d0a977bcff9579e325523aa03472b107d028f2076197e947913b6b4d64df4c7
3
+ metadata.gz: e35081e813792fcd9865ba3eba02cea731a1b51f4a5c011c97af9f3f25e0fc55
4
+ data.tar.gz: a1d4917f498c069796790aa020154f6d2b8b7b6f74a7d4185345277351c00de8
5
5
  SHA512:
6
- metadata.gz: 004220be5d784fce7fb075b9eac6a6f344d4416bfe1c9d64af6edb895b6ed1802e62cca9b52364746a2eba6b9b7b3a4418d3508f912954048d520a0e8106a5a1
7
- data.tar.gz: d02dee405d04dd01b1bb135ecf90b7241e2feae917c911ad5785e8474eb04b5821a02e2478924c64a8b8458984c3691129bbd0265a326fef1f16350487f06ee7
6
+ metadata.gz: 30a6e493e44048f188b01ec46b143f1a95f0bd05b350ed9749f53ce656e50f198f40a093f6d36051efab553f51f4d8f224c24951cf55e5db3b65614d66413080
7
+ data.tar.gz: db74e4b23a36e85c7a7a67e5ff1a48a1c4229db6593b6bf01c725bd8c66e8a79ed2bfa885e8f3945d5f97680587d01d1d539d1fc974d82081d4038ceec0b6eae
@@ -0,0 +1,8 @@
1
+ module Admin
2
+ class TracksController < ApplicationController
3
+ before_action(:authenticate_user!) if defined?(Devise)
4
+ before_action { EffectiveResources.authorize!(self, :admin, :effective_logging) }
5
+
6
+ include Effective::CrudController
7
+ end
8
+ end
@@ -0,0 +1,29 @@
1
+ module Admin
2
+ class EffectiveTracksDatatable < Effective::Datatable
3
+ filters do
4
+ filter_date_range
5
+
6
+ scope :all
7
+ scope :signed_in
8
+ scope :signed_out
9
+ end
10
+
11
+ datatable do
12
+ col :id, visible: false
13
+
14
+ col :created_at
15
+
16
+ col :action
17
+ col :owner, label: 'Resource'
18
+ col :user
19
+
20
+ col :title
21
+ col :details, visible: false
22
+ end
23
+
24
+ collection do
25
+ Effective::Track.deep.all.where(created_at: date_range)
26
+ end
27
+
28
+ end
29
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Just works with effective_resources controller to track views on show action
4
+ # Add acts_as_trackable to your model
5
+ # add_column :things, :tracks_count, :integer, default: 0
6
+ module ActsAsTrackable
7
+ extend ActiveSupport::Concern
8
+
9
+ module Base
10
+ def acts_as_trackable(*options)
11
+ @acts_as_trackable = options || []
12
+ include ::ActsAsTrackable
13
+ end
14
+ end
15
+
16
+ included do
17
+ has_many :tracks, -> { order(:id) }, as: :owner, class_name: 'Effective::Track'
18
+ end
19
+
20
+ module ClassMethods
21
+ def acts_as_trackable?; true; end
22
+ end
23
+
24
+ # Instance Methods
25
+ def track!(action: 'view', user: nil, details: nil)
26
+ tracks.create!(action: action, user: user, details: details)
27
+ end
28
+ end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+ module Effective
3
+ class Track < ActiveRecord::Base
4
+ self.table_name = (EffectiveLogging.tracks_table_name || :tracks).to_s
5
+
6
+ # The owner resource of this tracked thing
7
+ belongs_to :owner, polymorphic: true, counter_cache: true
8
+
9
+ # Present when the user is signed in
10
+ belongs_to :user, polymorphic: true, optional: true
11
+
12
+ ACTIONS = ['view']
13
+
14
+ effective_resource do
15
+ action :string
16
+
17
+ title :string
18
+ details :text
19
+
20
+ timestamps
21
+ end
22
+
23
+ if EffectiveResources.serialize_with_coder?
24
+ serialize :details, type: Hash, coder: YAML
25
+ else
26
+ serialize :details, Hash
27
+ end
28
+
29
+ validates :action, presence: true
30
+ before_save :assign_title # Assign computed title always
31
+
32
+ scope :sorted, -> { order(:id) }
33
+ scope :deep, -> { includes(:owner, :user) }
34
+
35
+ scope :signed_in, -> { where.not(user_id: nil) }
36
+ scope :signed_out, -> { where(user_id: nil) }
37
+
38
+ def to_s
39
+ title.presence || model_name.human
40
+ end
41
+
42
+ private
43
+
44
+ def assign_title
45
+ title = [action, 'of', "#{owner}", ("by #{user || 'guest user'}")].compact.join(' ')
46
+ assign_attributes(title: title)
47
+ end
48
+
49
+ end
50
+ end
data/config/routes.rb CHANGED
@@ -11,6 +11,7 @@ EffectiveLogging::Engine.routes.draw do
11
11
 
12
12
  namespace :admin do
13
13
  resources :logs, only: [:index, :show]
14
+ resources :tracks, only: :index
14
15
  end
15
16
 
16
17
  end
@@ -30,5 +30,22 @@ class CreateEffectiveLogging < ActiveRecord::Migration[6.0]
30
30
  enable_extension('pg_trgm')
31
31
  add_index :logs, :message, using: :gin, opclass: :gin_trgm_ops
32
32
  add_index :logs, :details, using: :gin, opclass: :gin_trgm_ops
33
+
34
+ create_table :tracks do |t|
35
+ t.string :owner_type
36
+ t.integer :owner_id
37
+
38
+ t.string :user_type
39
+ t.integer :user_id
40
+
41
+ t.string :action
42
+ t.string :title
43
+ t.text :details
44
+
45
+ t.timestamps
46
+ end
47
+
48
+ add_index :tracks, :owner_id
49
+ add_index :tracks, :user_id
33
50
  end
34
51
  end
@@ -28,6 +28,7 @@ module EffectiveLogging
28
28
  app.config.to_prepare do
29
29
  ActiveSupport.on_load :active_record do
30
30
  ActiveRecord::Base.extend(ActsAsLoggable::Base)
31
+ ActiveRecord::Base.extend(ActsAsTrackable::Base)
31
32
  ActiveRecord::Base.extend(EffectiveLoggingLog::Base)
32
33
  end
33
34
  end
@@ -1,3 +1,3 @@
1
1
  module EffectiveLogging
2
- VERSION = '4.2.0'.freeze
2
+ VERSION = '4.3.0'.freeze
3
3
  end
@@ -5,7 +5,7 @@ require 'effective_logging/version'
5
5
  module EffectiveLogging
6
6
  def self.config_keys
7
7
  [
8
- :logs_table_name, :layout, :additional_statuses,
8
+ :logs_table_name, :tracks_table_name, :layout, :additional_statuses,
9
9
  :log_class_name,
10
10
  :active_storage_enabled, :email_enabled, :sign_in_enabled, :sign_out_enabled
11
11
  ]
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: effective_logging
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.2.0
4
+ version: 4.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Code and Effect
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-02-13 00:00:00.000000000 Z
11
+ date: 2024-06-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -177,14 +177,18 @@ files:
177
177
  - app/assets/javascripts/effective_logging.js
178
178
  - app/assets/javascripts/effective_logging/effective_logger.js.coffee.erb
179
179
  - app/controllers/admin/logs_controller.rb
180
+ - app/controllers/admin/tracks_controller.rb
180
181
  - app/controllers/effective/logs_controller.rb
181
182
  - app/datatables/admin/effective_logs_datatable.rb
183
+ - app/datatables/admin/effective_tracks_datatable.rb
182
184
  - app/datatables/effective_log_changes_datatable.rb
183
185
  - app/datatables/effective_logs_datatable.rb
184
186
  - app/helpers/effective_logging_helper.rb
185
187
  - app/models/concerns/acts_as_loggable.rb
188
+ - app/models/concerns/acts_as_trackable.rb
186
189
  - app/models/concerns/effective_logging_log.rb
187
190
  - app/models/effective/log.rb
191
+ - app/models/effective/track.rb
188
192
  - app/models/effective_logger.rb
189
193
  - app/views/admin/logs/_layout.html.haml
190
194
  - app/views/admin/logs/index.html.haml