iron_trail 0.0.2 → 0.0.4

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: fbae2be821b8553a0ddf746fd9d30b3b7420b27e80e293b0716d78bf60c2c4dd
4
- data.tar.gz: 9525cad670a20bc43414a56a0ea87734e82c908f175a82545cbe0c386f7fec86
3
+ metadata.gz: b26e21161b7a8685eb9522dd6dc8e5856a8573258016a4cb69aa526eab3ca672
4
+ data.tar.gz: f54a66d2bf06b346e7429b8d47ef3018448184a4135b9a6d62e74b2588607d59
5
5
  SHA512:
6
- metadata.gz: b6a32637f82a839336850df3f5b8a090a367b87eb519db903c41681de33578ea3b4d13f9ed15301985a29ce0b82c2bfacd98651da1e266373a639741453e2925
7
- data.tar.gz: 8bb96a201f38d6923d864f65d92b3bec8cab90df9ad30144716d8ea0943d8fad25f46b4433a6e868177454cef3c6f8e7c738d22e7c83354162f897c3e7882795
6
+ metadata.gz: 367db0c530f76d9720689ba997ba0f94e57fa007088130a7b9ce76828d42455f41077433e8312523a7415dd3dc4429b167c4e8029dca2f1843fe1411c9e0da4d
7
+ data.tar.gz: b6179bf73bcf3d03d8957a4dded91a08242f9aca7196cb8ad4223cca35af5ac1f482098694768f5650ce45065bea159afd4240b936444259c5234807a248311b
@@ -19,6 +19,13 @@ module IronTrail
19
19
  scope
20
20
  end
21
21
 
22
+ def reader
23
+ res = super
24
+ res.extend(CollectionProxyMixin)
25
+
26
+ res
27
+ end
28
+
22
29
  def find_target
23
30
  scope.to_a
24
31
  end
@@ -4,6 +4,14 @@ module IronTrail
4
4
  module ChangeModelConcern
5
5
  extend ::ActiveSupport::Concern
6
6
 
7
+ def reify
8
+ Reifier.reify(self)
9
+ end
10
+
11
+ def insert_operation? = (operation == 'i')
12
+ def update_operation? = (operation == 'u')
13
+ def delete_operation? = (operation == 'd')
14
+
7
15
  module ClassMethods
8
16
  def where_object_changes_to(args = {})
9
17
  _where_object_changes(1, args)
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module IronTrail
4
+ module CollectionProxyMixin
5
+ def travel_to(ts)
6
+ arel_table = arel.ast.cores.first.source.left
7
+
8
+ change_record = scope
9
+ .order(arel_table[:created_at] => :desc)
10
+ .where(arel_table[:created_at].lteq(ts))
11
+ .first
12
+
13
+ change_record.reify
14
+ end
15
+ end
16
+ end
@@ -9,6 +9,7 @@ module IronTrail
9
9
  module Model
10
10
  def self.included(mod)
11
11
  mod.include ClassMethods
12
+ mod.attr_reader :irontrail_reified_ghost_attributes
12
13
 
13
14
  ::ActiveRecord::Reflection.add_reflection(
14
15
  mod,
@@ -4,6 +4,8 @@ module IronTrail
4
4
  class Reflection < ::ActiveRecord::Reflection::AssociationReflection
5
5
  def collection?; true; end
6
6
 
7
+ def macro; :has_iron_trails; end
8
+
7
9
  def association_class
8
10
  ::IronTrail::Association
9
11
  end
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ module IronTrail
4
+ module Reifier
5
+ def self.reify(trail)
6
+ source_attributes = (trail.delete_operation? ? trail.rec_old : trail.rec_new)
7
+ klass = model_from_table_name(trail.rec_table, source_attributes['type'])
8
+
9
+ record = klass.where(id: trail.rec_id).first || klass.new
10
+
11
+ source_attributes.each do |name, value|
12
+ if record.has_attribute?(name)
13
+ record[name.to_sym] = value
14
+ elsif record.respond_to?("#{name}=")
15
+ record.send("#{name}=", value)
16
+ else
17
+ ghost = record.instance_variable_get(:@irontrail_reified_ghost_attributes)
18
+ unless ghost
19
+ ghost = HashWithIndifferentAccess.new
20
+ record.instance_variable_set(:@irontrail_reified_ghost_attributes, ghost)
21
+ end
22
+ ghost[name] = value
23
+ end
24
+ end
25
+
26
+ record
27
+ end
28
+
29
+ def self.model_from_table_name(table_name, sti_type=nil)
30
+ index = ActiveRecord::Base.descendants.reject(&:abstract_class).chunk(&:table_name).to_h do |key, val|
31
+ v = \
32
+ if val.length == 1
33
+ val[0]
34
+ else
35
+ val.to_h { |k| [k.to_s, k] }
36
+ end
37
+
38
+ [key, v]
39
+ end
40
+
41
+ klass = index[table_name]
42
+ raise "Cannot infer model from table named '#{table_name}'" unless klass
43
+
44
+ return klass unless klass.is_a?(Hash)
45
+ klass = klass[sti_type]
46
+
47
+ return klass if klass
48
+
49
+ raise "Cannot infer STI model for table #{table_name} and type '#{sti_type}'"
50
+ end
51
+ end
52
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_literal_string: true
2
2
 
3
3
  module IronTrail
4
- VERSION = '0.0.2'
4
+ VERSION = '0.0.4'
5
5
  end
data/lib/iron_trail.rb CHANGED
@@ -17,6 +17,8 @@ require 'iron_trail/association'
17
17
  require 'iron_trail/reflection'
18
18
  require 'iron_trail/model'
19
19
  require 'iron_trail/change_model_concern'
20
+ require 'iron_trail/collection_proxy_mixin'
21
+ require 'iron_trail/reifier'
20
22
 
21
23
  require 'iron_trail/railtie'
22
24
 
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: iron_trail
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - André Diego Piske
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2024-12-09 00:00:00.000000000 Z
10
+ date: 2024-12-30 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: rails
@@ -136,7 +135,6 @@ dependencies:
136
135
  - - "~>"
137
136
  - !ruby/object:Gem::Version
138
137
  version: '7.2'
139
- description:
140
138
  email: andrepiske@gmail.com
141
139
  executables: []
142
140
  extensions: []
@@ -150,6 +148,7 @@ files:
150
148
  - lib/iron_trail.rb
151
149
  - lib/iron_trail/association.rb
152
150
  - lib/iron_trail/change_model_concern.rb
151
+ - lib/iron_trail/collection_proxy_mixin.rb
153
152
  - lib/iron_trail/config.rb
154
153
  - lib/iron_trail/db_functions.rb
155
154
  - lib/iron_trail/irontrail_log_row_function.sql
@@ -159,6 +158,7 @@ files:
159
158
  - lib/iron_trail/query_transformer.rb
160
159
  - lib/iron_trail/railtie.rb
161
160
  - lib/iron_trail/reflection.rb
161
+ - lib/iron_trail/reifier.rb
162
162
  - lib/iron_trail/sidekiq.rb
163
163
  - lib/iron_trail/sidekiq_middleware.rb
164
164
  - lib/iron_trail/testing/rspec.rb
@@ -168,7 +168,6 @@ homepage: https://github.com/trusted/iron_trail
168
168
  licenses:
169
169
  - MIT
170
170
  metadata: {}
171
- post_install_message:
172
171
  rdoc_options: []
173
172
  require_paths:
174
173
  - lib
@@ -183,8 +182,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
183
182
  - !ruby/object:Gem::Version
184
183
  version: '0'
185
184
  requirements: []
186
- rubygems_version: 3.5.23
187
- signing_key:
185
+ rubygems_version: 3.6.2
188
186
  specification_version: 4
189
187
  summary: Creates a trail strong as iron
190
188
  test_files: []