activerecord-redundancy 0.2.2 → 0.3.0

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
  SHA1:
3
- metadata.gz: 504c28b1fdbd3e01948e48a5823d95148023ce59
4
- data.tar.gz: fbd3a059de3d014b8b20bbc1da47a53e08ecc2e8
3
+ metadata.gz: dbed46abce552a4b6c0bb975fbdeb45acd10c638
4
+ data.tar.gz: ec046b565f92b938eb81e5b62c710a2878be44fe
5
5
  SHA512:
6
- metadata.gz: 60c35703583a72dc60dc140ad45ccb346192cdd92116ab9a88e5a09ba6e33275b4cd1dd36fcbe4e4ff349ee00ec8de2e99fd7419b26bd2433b41af88fc63c94f
7
- data.tar.gz: ac6f62a775f5db3fe3d19dfdd1658db2e5c188a13522fab4d9670ac933579603638a70019d12fb4a67328b7acb28407266d8c2e291f82a0a7502f55539b54939
6
+ metadata.gz: 6c5dee5844b4784e2608e2fc1cbb07c1e87a0e3024db1f04a48e4d88d55ac0117809768824068930a74ca0dce57ef7d3abe042e276dda735c3a332dca2d49cb9
7
+ data.tar.gz: c3983f7a81384676b34a5bc01684f96fc8fb476a272ca06ca611a168496c346564093e16302812237739fcaaea4f48f6f7b9ec56d1616a69b204e55755c77fd5
@@ -15,7 +15,7 @@ module Redundancy
15
15
  def after_save record
16
16
  return unless target
17
17
 
18
- get_value_from_target record
18
+ get_value_from_source record
19
19
  force_update_target record
20
20
  end
21
21
 
@@ -55,24 +55,26 @@ module Redundancy
55
55
  local_klass = klass
56
56
 
57
57
  reflection = get_reflection local_klass, association
58
- raise ArgumentError, "BelongsTo reflection required" unless
59
- [:belongs_to].include? reflection.macro
58
+ raise ArgumentError, "HasOne or HasMany reflection required" unless
59
+ [:has_one, :has_many].include? reflection.macro
60
60
 
61
61
  foreign_key = reflection.foreign_key
62
62
  remote_klass = reflection.klass
63
63
 
64
+ inverse_association = get_inverse_association local_klass, remote_klass, options
65
+
64
66
  cache_method = options[:cache_method] || :"raw_#{attribute}"
65
67
 
66
- local_klass.redundacies << UpdateMethod.new(
68
+ remote_klass.redundacies << UpdateMethod.new(
67
69
  source: { attribute: cache_method },
68
- dest: { association: association, attribute: attribute },
69
- change_if: options[:change_if], klass: local_klass
70
+ dest: { association: inverse_association, attribute: attribute },
71
+ change_if: options[:change_if], klass: remote_klass
70
72
  )
71
73
 
72
- local_klass.redundacies << UpdatePrevMethod.new(
74
+ remote_klass.redundacies << UpdatePrevMethod.new(
73
75
  source: { attribute: cache_method },
74
- dest: { klass: remote_klass, prev_id: foreign_key, attribute: attribute },
75
- change_if: foreign_key, klass: local_klass
76
+ dest: { klass: local_klass, prev_id: foreign_key, attribute: attribute },
77
+ change_if: foreign_key, klass: remote_klass
76
78
  )
77
79
 
78
80
  end
@@ -1,3 +1,3 @@
1
1
  module Redundancy
2
- VERSION = "0.2.2"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -3,7 +3,4 @@ class Post < ActiveRecord::Base
3
3
 
4
4
  cache_column :user, :name
5
5
  cache_column :user, :name, cache_column: :username
6
-
7
- cache_method :user, :posts_count
8
- cache_method :user, :posts_star
9
6
  end
@@ -4,6 +4,9 @@ class User < ActiveRecord::Base
4
4
 
5
5
  cache_column :account, :email
6
6
 
7
+ cache_method :posts, :posts_count
8
+ cache_method :posts, :posts_star
9
+
7
10
  def raw_posts_count
8
11
  posts.count
9
12
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-redundancy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Theo