timelines 0.1.3 → 0.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: 5b4ab605225ad136d4ab3388c6b450c3d5018b183eb254e94b06e15c7363b9bd
4
- data.tar.gz: 61b78dcb22d51581a05378b50dd4400afab543a22f25683204994f0f3907adbe
3
+ metadata.gz: b596c9a86e1cb9a8773f9394a6d71ab6f2e4cd521f89eda1f6ac434cbf059e42
4
+ data.tar.gz: 57236b6ed3b91ebe95afabedf0d192c277dd5d6127d810504a2f4b9c6a0db487
5
5
  SHA512:
6
- metadata.gz: c0c5fb0f0c4a5140afc3639b922d8adeec9bd4dc7ead6852c2a9a196586b00d43134cd86539d7a8aed485c2f30f02cd4b4bc7c848a868a6ed8526d72e2830127
7
- data.tar.gz: e0d32533e265a3e66203f5b84b81bb85f40f5b95dded491fa7743c2c0dc254beabb785548a1f59b5cc7fd5e429734bdb4b05753d870b96f6627b7cd8a8183abe
6
+ metadata.gz: 8d02c52990e934303148e6ad8a2bbc021911d3fe21522ba8336ed481334a300e2c4419526dd516b1f1f986c14d5d6c6f6a4d92ece505354d2968d74c5a0b8628
7
+ data.tar.gz: 48248ca65e37a9aed7757f5c129385f25a2047845e252444f5a04394d701f435a439a8487ddf385c840501474287769791c345eba62f90270afc2f02347b516c
data/README.md CHANGED
@@ -10,9 +10,18 @@ include Timelines::Ephemeral
10
10
 
11
11
  This gives you the following Instance methods:
12
12
  ```ruby
13
+ # Returns an AuditTrail object containing the resource and any associated events, in chronological order
14
+ .audit_trail
15
+
16
+ # Returns an AuditTrail object containing the resource and any associated events, in reverse order
17
+ .audit_trail(reverse: true)
18
+
13
19
  # Returns a boolean indicating whether the record is currently active
14
20
  .active?
15
21
 
22
+ # Returns a boolean indicating whether the record was active at a given date
23
+ .active_at?(date)
24
+
16
25
  # Sets the record's `started_at` to the current time to indicate that it has started
17
26
  .start!
18
27
 
@@ -45,6 +54,14 @@ Add this line to your application's Gemfile:
45
54
  gem "timelines"
46
55
  ```
47
56
 
57
+ ## Migrations
58
+ Run this command to create a `timelines_events` table in your project:
59
+ ```ruby
60
+ bin/rails generate timelines:install
61
+ ```
62
+
63
+ Then you can call [whatever method] to get events attached to the record.
64
+
48
65
  ## Contributing
49
66
  Pull requests/issues are welcome on GitHub
50
67
 
@@ -0,0 +1,16 @@
1
+ module Timelines
2
+ module Generators
3
+ class InstallGenerator < ::Rails::Generators::Base
4
+ source_root File.expand_path("templates", __dir__)
5
+
6
+ def copy_application_policy
7
+ [
8
+ "create_timelines_events.rb"
9
+ ].each_with_index do |migration_file, index|
10
+ timestamp = (Time.current + index + 1).strftime("%Y%m%d%H%M%S")
11
+ template migration_file, "db/migrate/#{timestamp}_#{migration_file}.rb"
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,10 @@
1
+ class CreateTimelinesEvents < ActiveRecord::Migration[7.1]
2
+ def change
3
+ create_table :timelines_events, id: :uuid do |t|
4
+ t.references :actor, type: :uuid, polymorphic: true
5
+ t.references :resource, type: :uuid, polymorphic: true
6
+ t.string :event
7
+ t.timestamps
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,15 @@
1
+ module Timelines
2
+ class AuditTrail
3
+ include ActiveModel::Model
4
+ include ActiveModel::Attributes
5
+ include ActiveModel::Validations
6
+
7
+ attribute :resource
8
+ attribute :reverse
9
+ attribute :events
10
+
11
+ def events
12
+ reverse ? resource.events.reverse : resource.events
13
+ end
14
+ end
15
+ end
@@ -7,6 +7,7 @@ module Timelines
7
7
  included do
8
8
  scope :draft, -> { where(started_at: nil) }
9
9
  scope :active, -> { where(ended_at: nil, started_at: [..Time.current]) }
10
+ scope :active_at, ->(date) { where("started_at <= ? AND (ended_at IS ? OR ended_at >= ?)", date, nil, date) }
10
11
  scope :with_deleted, -> { unscope(where: :ended_at) }
11
12
  scope :ended, -> { where.not(ended_at: nil) }
12
13
  scope :deleted, -> { ended }
@@ -16,6 +17,10 @@ module Timelines
16
17
  started? && !ended?
17
18
  end
18
19
 
20
+ def active_at?(date)
21
+ self.class.active_at(date).where(id: id).exists?
22
+ end
23
+
19
24
  def start!
20
25
  return if started_at.present?
21
26
 
@@ -0,0 +1,11 @@
1
+ require "active_support/concern"
2
+
3
+ module Timelines
4
+ module HasAuditTrail
5
+ extend ActiveSupport::Concern
6
+
7
+ def audit_trail(reverse: false)
8
+ ::Timelines::AuditTrail.new(resource: self, reverse: reverse)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ require "active_support/concern"
2
+
3
+ module Timelines
4
+ module HasEvents
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ has_many :events, class_name: "Timelines::Event", as: :resource, dependent: :destroy
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,8 @@
1
+ module Timelines
2
+ class Event < ActiveRecord::Base
3
+ self.table_name = "timelines_events"
4
+
5
+ belongs_to :actor, polymorphic: true
6
+ belongs_to :resource, polymorphic: true
7
+ end
8
+ end
@@ -1,3 +1,3 @@
1
1
  module Timelines
2
- VERSION = "0.1.3"
2
+ VERSION = "0.3.0"
3
3
  end
data/lib/timelines.rb CHANGED
@@ -1,6 +1,9 @@
1
1
  require "timelines/version"
2
- require "timelines/railtie"
3
- require "timelines/ephemeral"
2
+ require "timelines/models/concerns/ephemeral"
3
+ require "timelines/models/concerns/has_events"
4
+ require "timelines/models/concerns/has_audit_trail"
5
+ require "timelines/models/concerns/audit_trail"
6
+ require "timelines/models/event"
4
7
 
5
8
  module Timelines
6
9
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: timelines
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Craig Gilchrist
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-10-09 00:00:00.000000000 Z
11
+ date: 2024-05-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -35,10 +35,15 @@ extra_rdoc_files: []
35
35
  files:
36
36
  - README.md
37
37
  - Rakefile
38
+ - lib/generators/timelines/install/install_generator.rb
39
+ - lib/generators/timelines/install/templates/create_timelines_events.rb
38
40
  - lib/tasks/timelines_tasks.rake
39
41
  - lib/timelines.rb
40
- - lib/timelines/ephemeral.rb
41
- - lib/timelines/railtie.rb
42
+ - lib/timelines/models/concerns/audit_trail.rb
43
+ - lib/timelines/models/concerns/ephemeral.rb
44
+ - lib/timelines/models/concerns/has_audit_trail.rb
45
+ - lib/timelines/models/concerns/has_events.rb
46
+ - lib/timelines/models/event.rb
42
47
  - lib/timelines/version.rb
43
48
  homepage: https://github.com/Craggar/timelines
44
49
  licenses:
@@ -62,7 +67,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
62
67
  - !ruby/object:Gem::Version
63
68
  version: '0'
64
69
  requirements: []
65
- rubygems_version: 3.4.10
70
+ rubygems_version: 3.5.10
66
71
  signing_key:
67
72
  specification_version: 4
68
73
  summary: Library for managing historical records.
@@ -1,4 +0,0 @@
1
- module Timelines
2
- class Railtie < ::Rails::Railtie
3
- end
4
- end