activerecord-precounter 0.3.3 → 0.4.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
  SHA256:
3
- metadata.gz: '09078896e68490e75c8beb5645afcd62ad0d5ea91c00e4bce6caf56c78fef5a8'
4
- data.tar.gz: 584556fa31db7232716409333ddc670679846fab59660a66c20432eef6eb95b5
3
+ metadata.gz: a80cb8c2919851956774b0c573d3a3271a9b4f017e33edca6fae411f1c4c189b
4
+ data.tar.gz: 9d479d623ceacf1a93ad4cc64b6ad58ce32f586e07dc8b6335d9e380f82e7d9b
5
5
  SHA512:
6
- metadata.gz: 978b940f2cc5adf4206bb399fbdb0e35f745336095df5a411d9d7fc98e9c3f43b3e0974c905d23b3d96811c9a55cc75d1da1aa3c1f3317b3c33cc20412224d6d
7
- data.tar.gz: 194cd5631328971a328c71ecf853cf80f10f309dff0cf3ff23a9b57275d3bd78b00cfd43ff8e070f6c023d0b5a4183b92fd03e9f2e2edc08e0f18257af38234f
6
+ metadata.gz: ae135551e4619a121e488e62517c072b3692dd8b717867b45114ffe904ee50ad6cec6f3924755f511a48f640510d902986be83e0205b5b7178a054eae6d9b47d
7
+ data.tar.gz: 295507cafb1c6de872f92163d74ebfd37bae295cb2fe4478b3c313b967c0faf657090596fb37153a5a32ac5201be6c48b2384d697ca8901c98f4b0f1fe79da42
@@ -1,3 +1,8 @@
1
+ ## v0.4.0
2
+
3
+ * [breaking] Raise `NotPrecountedError` instead of returning nil if `*_count` is called without precount
4
+ * Add `ActiveRecord::Precountable` module to actively define `*_count` readers and writers by `precounts` DSL on ActiveRecord::Base.
5
+
1
6
  ## v0.3.3
2
7
 
3
8
  * Support passing a single record to `ActiveRecord::Precounter.new` [#8](https://github.com/k0kubun/activerecord-precounter/pull/8)
@@ -0,0 +1,20 @@
1
+ module ActiveRecord
2
+ module Precountable
3
+ class NotPrecountedError < StandardError
4
+ end
5
+
6
+ def precounts(*association_names)
7
+ association_names.each do |association_name|
8
+ var_name = "#{association_name}_count"
9
+ instance_var_name = "@#{var_name}"
10
+
11
+ attr_writer(var_name)
12
+ define_method(var_name) do
13
+ count = instance_variable_get(instance_var_name)
14
+ raise NotPrecountedError.new("`#{association_name}' not precounted") unless count
15
+ count
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -1,4 +1,5 @@
1
1
  require 'active_record/precounter/version'
2
+ require 'active_record/precountable'
2
3
 
3
4
  module ActiveRecord
4
5
  class Precounter
@@ -47,7 +48,7 @@ module ActiveRecord
47
48
  ).count
48
49
  end
49
50
 
50
- writer = define_count_accessor(records.first, association_name)
51
+ writer = define_count_accessor(klass, association_name)
51
52
  records.each do |record|
52
53
  record.public_send(writer, count_by_id.fetch(record.public_send(primary_key), 0))
53
54
  end
@@ -57,15 +58,16 @@ module ActiveRecord
57
58
 
58
59
  private
59
60
 
60
- # @param [ActiveRecord::Base] record
61
+ # @param [Class] record class
61
62
  # @param [String] association_name
62
63
  # @return [String] writer method name
63
- def define_count_accessor(record, association_name)
64
+ def define_count_accessor(klass, association_name)
64
65
  reader_name = "#{association_name}_count"
65
66
  writer_name = "#{reader_name}="
66
67
 
67
- if !record.respond_to?(reader_name) && !record.respond_to?(writer_name)
68
- record.class.send(:attr_accessor, reader_name)
68
+ if !klass.method_defined?(reader_name) && !klass.method_defined?(writer_name)
69
+ klass.extend(ActiveRecord::Precountable)
70
+ klass.public_send(:precounts, association_name)
69
71
  end
70
72
 
71
73
  writer_name
@@ -1,5 +1,5 @@
1
1
  module ActiveRecord
2
2
  class Precounter
3
- VERSION = '0.3.3'
3
+ VERSION = '0.4.0'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-precounter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.3
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Takashi Kokubun
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-10-04 00:00:00.000000000 Z
11
+ date: 2020-05-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -131,6 +131,7 @@ files:
131
131
  - ci/Gemfile.activerecord-5.1.x
132
132
  - ci/Gemfile.activerecord-5.2.x
133
133
  - ci/travis.rb
134
+ - lib/active_record/precountable.rb
134
135
  - lib/active_record/precounter.rb
135
136
  - lib/active_record/precounter/version.rb
136
137
  - lib/activerecord-precounter.rb
@@ -153,7 +154,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
153
154
  - !ruby/object:Gem::Version
154
155
  version: '0'
155
156
  requirements: []
156
- rubygems_version: 3.0.3
157
+ rubygems_version: 3.2.0.pre1
157
158
  signing_key:
158
159
  specification_version: 4
159
160
  summary: Yet Another N+1 COUNT Query Killer for ActiveRecord