timelines 0.1.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8cafb7917c5de0767aa5c56cfc8b0888369c8b7269e29b1acdad153466cae36e
4
- data.tar.gz: 6cf81beee062e3860cd0575f88f7ed7bba0252a422cc0e5333db38e4166cd3f5
3
+ metadata.gz: 9e0fcaa71f51ce05f909e1476d3697c19cbce690f581357a4dcc0507154456dd
4
+ data.tar.gz: 8d8f0580bd04c4dca4f7f64d17b889a68eb11d689f006d24aeff8364632aed28
5
5
  SHA512:
6
- metadata.gz: ce8e9743ddda620638237736225e7481045f85252aa784d57ffbdfa9754fb78a8e6a77046cdc965f0379eb8a8c717ed738a8924975c0445067b253422784abce
7
- data.tar.gz: 746ddcaa83b321f511e65236ee3e76f0a2099950a95f322b09bcf124a0c7498c8baeefb7256a8f863010bc42abd48b7bc5c78fe3616683d7e27034c95006dc87
6
+ metadata.gz: 80767881c109435b0f5b2e6c1da160f7cd57dd21d2c0b2ad0a0c6793beff2a930e11b2f159b0327ed813dd28435e461818cc533b5c8c244ead969b877d6c0f88
7
+ data.tar.gz: cbc54257925c045ea37ac59b85bd3f842060f6d28665cee5dd96a70a4b63affb6202a905c108cd74787186688e719a4abc64622998b3a739f850cf740a0a0417
data/README.md CHANGED
@@ -13,6 +13,9 @@ This gives you the following Instance methods:
13
13
  # Returns a boolean indicating whether the record is currently active
14
14
  .active?
15
15
 
16
+ # Returns a boolean indicating whether the record was active at a given date
17
+ .active_at?(date)
18
+
16
19
  # Sets the record's `started_at` to the current time to indicate that it has started
17
20
  .start!
18
21
 
@@ -45,6 +48,14 @@ Add this line to your application's Gemfile:
45
48
  gem "timelines"
46
49
  ```
47
50
 
51
+ ## Migrations
52
+ Run this command to create a `timelines_events` table in your project:
53
+ ```ruby
54
+ bin/rails generate timelines:install
55
+ ```
56
+
57
+ Then you can call [whatever method] to get events attached to the record.
58
+
48
59
  ## Contributing
49
60
  Pull requests/issues are welcome on GitHub
50
61
 
@@ -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
@@ -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 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.2"
2
+ VERSION = "0.2.3"
3
3
  end
data/lib/timelines.rb CHANGED
@@ -1,6 +1,7 @@
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/event"
4
5
 
5
6
  module Timelines
6
7
  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.2
4
+ version: 0.2.3
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-04-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -16,20 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '7'
20
- - - ">="
21
- - !ruby/object:Gem::Version
22
- version: 7.0.0
19
+ version: 7.1.0
23
20
  type: :runtime
24
21
  prerelease: false
25
22
  version_requirements: !ruby/object:Gem::Requirement
26
23
  requirements:
27
24
  - - "~>"
28
25
  - !ruby/object:Gem::Version
29
- version: '7'
30
- - - ">="
31
- - !ruby/object:Gem::Version
32
- version: 7.0.0
26
+ version: 7.1.0
33
27
  description: |2
34
28
  Provides an Ephemeral module that can be included in any ActiveRecord model to
35
29
  provide a simple way to track active/historical/past records.
@@ -41,10 +35,13 @@ extra_rdoc_files: []
41
35
  files:
42
36
  - README.md
43
37
  - Rakefile
38
+ - lib/generators/timelines/install/install_generator.rb
39
+ - lib/generators/timelines/install/templates/create_timelines_events.rb
44
40
  - lib/tasks/timelines_tasks.rake
45
41
  - lib/timelines.rb
46
- - lib/timelines/ephemeral.rb
47
- - lib/timelines/railtie.rb
42
+ - lib/timelines/models/concerns/ephemeral.rb
43
+ - lib/timelines/models/concerns/has_events.rb
44
+ - lib/timelines/models/event.rb
48
45
  - lib/timelines/version.rb
49
46
  homepage: https://github.com/Craggar/timelines
50
47
  licenses:
@@ -68,7 +65,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
68
65
  - !ruby/object:Gem::Version
69
66
  version: '0'
70
67
  requirements: []
71
- rubygems_version: 3.4.10
68
+ rubygems_version: 3.5.4
72
69
  signing_key:
73
70
  specification_version: 4
74
71
  summary: Library for managing historical records.
@@ -1,4 +0,0 @@
1
- module Timelines
2
- class Railtie < ::Rails::Railtie
3
- end
4
- end