activekit 0.5.0 → 0.5.1

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: 71b9eb7bd5fc27d2794cd15b8ec08a68e9baae06e37e072188260ddbaaa25473
4
- data.tar.gz: 33f906a96b417c86b8048a2f663dbac5d4a6b0595f9cea23b6a0db92f98982cb
3
+ metadata.gz: 8622adf4eedf9b9fa1b4f31b70e8f9fb588560248d25a2f2dd571bd6a49472e9
4
+ data.tar.gz: cc1adb9159fffe8865e7c907a684c22d991cccd5c3ff2e773e436c779eb8cb76
5
5
  SHA512:
6
- metadata.gz: 5172f9cb6b0aa0623a9e6f9a0263de7a5fadc016a25c6b92514f9916fca13653f8326479f4bc7fa8c54314c77c8d9d75435034b3c0e1e40054f7f002bf969d90
7
- data.tar.gz: 65cb7a16747fb2281042dced7e1781bf0709437c470e8621b7ee232b6b205ab1ad021d8b1287a147abba85ddb96fe4f020e1ebb455b00a532d45488fbdf6d47f
6
+ metadata.gz: 8be02259a5fa52ea71fed586d7843f0e42c9045f818ded4476746ab5e483ec4183132164eb112268dcf1a16d44b783b3a549071ad91acc6c500e45c1644a592e
7
+ data.tar.gz: 224b675066b9134bc3ca74f225e602fcc66eabeda580f07f7affa2538b22e0fc781ede082cccdea9bfa4b97d0088011d86949e5e80d7525dd02622722869d3fc
@@ -50,10 +50,10 @@ module ActiveKit
50
50
 
51
51
  def for(describer_name)
52
52
  describer_name = @describers.keys[0] if describer_name.nil?
53
- raise "Could not find any describer name in #{@current_class.name}." if describer_name.blank?
53
+ raise "Could not find any describer name in '#{@current_class.name}'." if describer_name.blank?
54
54
 
55
55
  describer_name = describer_name.to_sym
56
- raise "Could not find describer '#{describer_name}' in #{@current_class.name}." unless @describers.dig(describer_name)
56
+ raise "Could not find describer '#{describer_name}' in '#{@current_class.name}'." unless @describers.dig(describer_name)
57
57
  componenting = @describers.dig(describer_name, :componenting)
58
58
  return componenting if componenting
59
59
 
@@ -6,7 +6,7 @@ module ActiveKit
6
6
 
7
7
  depends_on = options.delete(:depends_on) || {}
8
8
  describer_names.each do |describer_name|
9
- set_reload_callbacks(depends_on, describer_name)
9
+ set_callbacks(describer_name, depends_on)
10
10
  self.for(describer_name).add_attribute(name: name, options: options.deep_dup)
11
11
  end
12
12
  end
@@ -18,33 +18,58 @@ module ActiveKit
18
18
 
19
19
  private
20
20
 
21
- # Set callbacks for current class and depending classes.
22
- def set_reload_callbacks(depends_on, describer_name)
23
- @current_class.class_eval do
24
- unless searcher.for(describer_name).attributes_present?
25
- after_commit do
26
- self.class.searcher.for(describer_name).reload(record: self)
27
- logger.info "ActiveKit::Search | Indexing from #{self.class.name}: Done."
28
- end
29
- end
21
+ # Set callbacks for depending classes and then current class.
22
+ def set_callbacks(describer_name, depends_on)
23
+ depends_on.each do |depends_on_association, depends_on_inverse|
24
+ klass = @current_class.reflect_on_all_associations.map { |assoc| [assoc.name, assoc.klass.name] }.to_h[depends_on_association]
25
+
26
+ next if klass.constantize.private_method_defined?(after_commit_depends_callback_method_name(describer_name))
27
+ klass.constantize.class_eval <<-CODE, __FILE__, __LINE__ + 1
28
+ after_commit :#{after_commit_depends_callback_method_name(describer_name)}
29
+
30
+ private
30
31
 
31
- unless depends_on.empty?
32
- depends_on.each do |depends_on_association, depends_on_inverse|
33
- klass = self.reflect_on_all_associations.map { |assoc| [assoc.name, assoc.klass.name] }.to_h[depends_on_association]
34
- klass.constantize.class_eval do
35
- after_commit do
36
- inverse_assoc = self.public_send(depends_on_inverse)
37
- if inverse_assoc.respond_to?(:each)
38
- inverse_assoc.each { |instance| instance.class.searcher.for(describer_name).reload(record: instance) }
39
- else
40
- inverse_assoc.class.searcher.for(describer_name).reload(record: inverse_assoc)
32
+ def #{after_commit_depends_callback_method_name(describer_name)}
33
+ inverse_assoc = self.public_send("#{depends_on_inverse}")
34
+ if inverse_assoc.respond_to?(:each)
35
+ inverse_assoc.each do |instance|
36
+ if instance.class.name == "#{@current_class.name}"
37
+ instance.class.searcher.for("#{describer_name}").reload(record: instance)
41
38
  end
42
- logger.info "ActiveKit::Search | Indexing from #{self.class.name}: Done."
39
+ end
40
+ else
41
+ if inverse_assoc.class.name == "#{@current_class.name}"
42
+ inverse_assoc.class.searcher.for("#{describer_name}").reload(record: inverse_assoc)
43
43
  end
44
44
  end
45
+ logger.info "ActiveKit::Search | Indexing Done. (Class: " + self.class.name + " | Reloading: #{@current_class.name} | Describer: #{describer_name})"
45
46
  end
46
- end
47
+ CODE
47
48
  end
49
+
50
+ return if @current_class.private_method_defined?(after_commit_callback_method_name(describer_name))
51
+ @current_class.class_eval <<-CODE, __FILE__, __LINE__ + 1
52
+ after_commit :#{after_commit_callback_method_name(describer_name)}
53
+
54
+ private
55
+
56
+ def #{after_commit_callback_method_name(describer_name)}
57
+ self.class.searcher.for("#{describer_name}").reload(record: self)
58
+ logger.info "ActiveKit::Search | Indexing Done. (Class: " + self.class.name + " | Reloading: #{@current_class.name} | Describer: #{describer_name})"
59
+ end
60
+ CODE
61
+ end
62
+
63
+ def after_commit_callback_method_name(describer_name)
64
+ "#{callback_method_name(describer_name)}_callback"
65
+ end
66
+
67
+ def after_commit_depends_callback_method_name(describer_name)
68
+ "#{callback_method_name(describer_name)}_depends_callback"
69
+ end
70
+
71
+ def callback_method_name(describer_name)
72
+ "activekit_search_#{@current_class.model_name.singular}_#{describer_name}"
48
73
  end
49
74
  end
50
75
  end
@@ -1,3 +1,3 @@
1
1
  module ActiveKit
2
- VERSION = '0.5.0'
2
+ VERSION = '0.5.1'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activekit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - plainsource
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-04-24 00:00:00.000000000 Z
11
+ date: 2024-05-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails