iron_trail 0.0.2 → 0.0.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: fbae2be821b8553a0ddf746fd9d30b3b7420b27e80e293b0716d78bf60c2c4dd
4
- data.tar.gz: 9525cad670a20bc43414a56a0ea87734e82c908f175a82545cbe0c386f7fec86
3
+ metadata.gz: cd13db21df61365c3749688097fbe2cecc39e13fdffeec89b82952306105ed30
4
+ data.tar.gz: fb18dbcf2ec368a3412bcbc96b67a25aa31285992335303878a051c6b2ece806
5
5
  SHA512:
6
- metadata.gz: b6a32637f82a839336850df3f5b8a090a367b87eb519db903c41681de33578ea3b4d13f9ed15301985a29ce0b82c2bfacd98651da1e266373a639741453e2925
7
- data.tar.gz: 8bb96a201f38d6923d864f65d92b3bec8cab90df9ad30144716d8ea0943d8fad25f46b4433a6e868177454cef3c6f8e7c738d22e7c83354162f897c3e7882795
6
+ metadata.gz: f24d6c2460879b591d703fdf711a80e08ba9bda0a8353c832877790b58f586f2b72db69873cbebe33363953ae9d06a40cb2d059bbac564a11ec8fab3a55e390d
7
+ data.tar.gz: a0f434e388b67352d61d45ac5667d93f1a4f048e5dbc332437b205b6a23d9f4b272eb558d43fdffaf58dc1b15dee93aa8b1a9ac47746d5caf1a4fb50a3e7733f
@@ -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,
@@ -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.3'
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,14 @@
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.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - André Diego Piske
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-12-09 00:00:00.000000000 Z
11
+ date: 2024-12-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -150,6 +150,7 @@ files:
150
150
  - lib/iron_trail.rb
151
151
  - lib/iron_trail/association.rb
152
152
  - lib/iron_trail/change_model_concern.rb
153
+ - lib/iron_trail/collection_proxy_mixin.rb
153
154
  - lib/iron_trail/config.rb
154
155
  - lib/iron_trail/db_functions.rb
155
156
  - lib/iron_trail/irontrail_log_row_function.sql
@@ -159,6 +160,7 @@ files:
159
160
  - lib/iron_trail/query_transformer.rb
160
161
  - lib/iron_trail/railtie.rb
161
162
  - lib/iron_trail/reflection.rb
163
+ - lib/iron_trail/reifier.rb
162
164
  - lib/iron_trail/sidekiq.rb
163
165
  - lib/iron_trail/sidekiq_middleware.rb
164
166
  - lib/iron_trail/testing/rspec.rb
@@ -183,7 +185,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
183
185
  - !ruby/object:Gem::Version
184
186
  version: '0'
185
187
  requirements: []
186
- rubygems_version: 3.5.23
188
+ rubygems_version: 3.5.21
187
189
  signing_key:
188
190
  specification_version: 4
189
191
  summary: Creates a trail strong as iron