timelines 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 339ae3e7d0c51c23d89e50623e47968edeea76e05f712450b8239dce582a40d9
4
+ data.tar.gz: 10a248044ed773b787dd9a0520c4198e5742fab92e0635b715dfe13fe5d23dec
5
+ SHA512:
6
+ metadata.gz: 216d2927c9597196a0978234f6745d41ba4b0f9f545b51ad7dd74acdf0fdf0aa49097809f17281969675680e7767b4eb73caa887e16d0f75707739023996cebd
7
+ data.tar.gz: 03f80b16d2d44a06086f0adb3a8c8a26cf10f549297cf4a786d7115045cb4550928f0c778a7de64fb451b219457808f7291c12ba903cb315e35ba83c8d739974
data/README.md ADDED
@@ -0,0 +1,52 @@
1
+ # Timelines
2
+ I use this `Ephemeral` pattern frequently across projects to have a simple pattern for tracking the lifecycle of a record, whether it is currently active/ended/draft, and soft-deleting records to keep an audit trail.
3
+
4
+ ## Usage
5
+ For an ActiveRecord model that has `started_at` and `ended_at` columns (datetime), include `Ephemeral` to give it ephemeral behavior.
6
+
7
+ ```ruby
8
+ include Timelines::Ephemeral
9
+ ```
10
+
11
+ This gives you the following Instance methods:
12
+ ```ruby
13
+ # Returns a boolean indicating whether the record is currently active
14
+ .active?
15
+
16
+ # Sets the record's `started_at` to the current time to indicate that it has started
17
+ .start!
18
+
19
+ # Returns a boolean indicating whether the record has started
20
+ .started?
21
+
22
+ # Sets the record's `ended_at` to the current time to indicate that it has ended
23
+ .end!
24
+
25
+ # Returns a boolean indicating whether the record has ended
26
+ .ended?
27
+
28
+ # Returns a boolean indicating whether the record is currently in a draft state (nil `started_at` and `ended_at`, or `started_at` in the future)
29
+ .draft?
30
+
31
+ # Soft-deletes the record by setting `ended_at` to the current time, removing it from the `.active` scope.
32
+ .destroy
33
+ ```
34
+
35
+ And the following Class methods:
36
+ ```ruby
37
+ # Soft-deletes all records by setting `ended_at` to the current time, removing them from the `.active` scope.
38
+ .self.destroy_all
39
+ ```
40
+
41
+ ## Installation
42
+ Add this line to your application's Gemfile:
43
+
44
+ ```ruby
45
+ gem "timelines"
46
+ ```
47
+
48
+ ## Contributing
49
+ Pull requests/issues are welcome on GitHub
50
+
51
+ ## License
52
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,3 @@
1
+ require "bundler/setup"
2
+
3
+ require "bundler/gem_tasks"
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :timelines do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,52 @@
1
+ require "active_support/concern"
2
+
3
+ module Timelines
4
+ module Ephemeral
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ scope :draft, -> { where(started_at: nil) }
9
+ scope :active, -> { where(ended_at: nil, started_at: [..Time.current]) }
10
+ scope :with_deleted, -> { unscope(where: :ended_at) }
11
+ scope :ended, -> { where.not(ended_at: nil) }
12
+ scope :deleted, -> { ended }
13
+ scope :not_deleted, -> { where(ended_at: nil) }
14
+
15
+ def active?
16
+ started? && !ended?
17
+ end
18
+
19
+ def start!
20
+ return if started_at.present?
21
+
22
+ update(started_at: Time.current)
23
+ end
24
+
25
+ def started?
26
+ !!started_at&.past?
27
+ end
28
+
29
+ def end!
30
+ destroy
31
+ end
32
+
33
+ def ended?
34
+ !!ended_at&.past?
35
+ end
36
+
37
+ def draft?
38
+ !started?
39
+ end
40
+
41
+ def destroy
42
+ return if ended_at.present?
43
+
44
+ update(ended_at: Time.current)
45
+ end
46
+
47
+ def self.destroy_all
48
+ where(ended_at: nil).update_all(ended_at: Time.current)
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,4 @@
1
+ module Timelines
2
+ class Railtie < ::Rails::Railtie
3
+ end
4
+ end
@@ -0,0 +1,3 @@
1
+ module Timelines
2
+ VERSION = "0.1.0"
3
+ end
data/lib/timelines.rb ADDED
@@ -0,0 +1,6 @@
1
+ require "timelines/version"
2
+ require "timelines/railtie"
3
+ require "timelines/ephemeral"
4
+
5
+ module Timelines
6
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: timelines
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Craig Gilchrist
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-10-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 7.1.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 7.1.0
27
+ description: Library for managing historical records.
28
+ email:
29
+ - craig.a.gilchrist@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - README.md
35
+ - Rakefile
36
+ - lib/tasks/timelines_tasks.rake
37
+ - lib/timelines.rb
38
+ - lib/timelines/ephemeral.rb
39
+ - lib/timelines/railtie.rb
40
+ - lib/timelines/version.rb
41
+ homepage: https://github.com/Craggar/timelines
42
+ licenses:
43
+ - MIT
44
+ metadata:
45
+ homepage_uri: https://github.com/Craggar/timelines
46
+ github_repo: git@github.com:Craggar/timelines.git
47
+ source_code_uri: https://github.com/Craggar/timelines
48
+ changelog_uri: https://github.com/Craggar/timelines/CHANGELOG.md
49
+ post_install_message:
50
+ rdoc_options: []
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ requirements: []
64
+ rubygems_version: 3.4.10
65
+ signing_key:
66
+ specification_version: 4
67
+ summary: Library for managing historical records.
68
+ test_files: []