calculated_attributes 0.1.2 → 0.1.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
  SHA1:
3
- metadata.gz: a15804206f8ef6a215d616b036b8019c4bea1754
4
- data.tar.gz: 36be65afdbe2ca7018547f9d77250b2d516c98d9
3
+ metadata.gz: 114648311fe5389527380b81d89677279f8e9019
4
+ data.tar.gz: 42e5dc334c01643eaf56d97e4eb42aca0cb0113d
5
5
  SHA512:
6
- metadata.gz: 2e86e989c7ff52c20586dfea1135dafd77eb62b2c2aefbd62d46b7d1d5cd99c1bf88957289ffdb33a5e5b8704faef6e3ee7d415352d04d633c052abab8e79f5d
7
- data.tar.gz: 02463a40ef5fc7ecc63fc351a1cbb81702a2566060ff69dc68b9eb54ea51db2e1af4fc87de4811a43bb360e32041092c07e2ecab733c324abf7bbc95e3033247
6
+ metadata.gz: 6635d62772fca4d1da02752555ed19fd55c8b04d9047e95086c233f89ee83822c7e277257ba21607e9bf7bf484b518e2547477333f5da84a1ccd0bcea223cc7d
7
+ data.tar.gz: f5881cb7665e8132f22ee7dbfd932b7695d420cd5fec33fb7e2248a40cdd2975ca093a37fca39ff0375e493e1039fef630c06213935eefa771a13392ebe16772
@@ -27,6 +27,7 @@ GEM
27
27
  astrolabe (1.3.0)
28
28
  parser (>= 2.2.0.pre.3, < 3.0)
29
29
  builder (3.0.4)
30
+ byebug (6.0.2)
30
31
  diff-lcs (1.2.5)
31
32
  i18n (0.7.0)
32
33
  multi_json (1.11.1)
@@ -66,6 +67,7 @@ DEPENDENCIES
66
67
  activerecord (= 3.2.21)
67
68
  appraisal (~> 1.0.3)
68
69
  bundler (~> 1.7)
70
+ byebug
69
71
  calculated_attributes!
70
72
  rake (~> 10.0)
71
73
  rspec (~> 3.1)
@@ -29,6 +29,7 @@ GEM
29
29
  astrolabe (1.3.0)
30
30
  parser (>= 2.2.0.pre.3, < 3.0)
31
31
  builder (3.2.2)
32
+ byebug (6.0.2)
32
33
  diff-lcs (1.2.5)
33
34
  i18n (0.7.0)
34
35
  json (1.8.3)
@@ -71,6 +72,7 @@ DEPENDENCIES
71
72
  activerecord (= 4.1.11)
72
73
  appraisal (~> 1.0.3)
73
74
  bundler (~> 1.7)
75
+ byebug
74
76
  calculated_attributes!
75
77
  rake (~> 10.0)
76
78
  rspec (~> 3.1)
@@ -29,6 +29,7 @@ GEM
29
29
  astrolabe (1.3.0)
30
30
  parser (>= 2.2.0.pre.3, < 3.0)
31
31
  builder (3.2.2)
32
+ byebug (6.0.2)
32
33
  diff-lcs (1.2.5)
33
34
  i18n (0.7.0)
34
35
  json (1.8.3)
@@ -71,6 +72,7 @@ DEPENDENCIES
71
72
  activerecord (= 4.2.2)
72
73
  appraisal (~> 1.0.3)
73
74
  bundler (~> 1.7)
75
+ byebug
74
76
  calculated_attributes!
75
77
  rake (~> 10.0)
76
78
  rspec (~> 3.1)
@@ -1,5 +1,5 @@
1
- Arel::SelectManager.send(:include, Module.new do
1
+ class Arel::SelectManager
2
2
  def projections
3
3
  @ctx.projections
4
4
  end
5
- end)
5
+ end
@@ -17,7 +17,7 @@ module CalculatedAttributes
17
17
  end
18
18
  ActiveRecord::Base.extend CalculatedAttributes
19
19
 
20
- ActiveRecord::Base.send(:include, Module.new do
20
+ class ActiveRecord::Base
21
21
  def calculated(*args)
22
22
  if self.class.respond_to? :scoped
23
23
  self.class.scoped.calculated(*args).find(id)
@@ -62,9 +62,9 @@ ActiveRecord::Base.send(:include, Module.new do
62
62
  end
63
63
  super || (no_sym_in_attr && (self.class.calculated.calculated[method] || self.class.base_class.calculated.calculated[method]))
64
64
  end
65
- end)
65
+ end
66
66
 
67
- ActiveRecord::Relation.send(:include, Module.new do
67
+ class ActiveRecord::Relation
68
68
  def calculated(*args)
69
69
  projections = arel.projections
70
70
  args.each do |arg|
@@ -83,4 +83,4 @@ ActiveRecord::Relation.send(:include, Module.new do
83
83
  end
84
84
  select(projections)
85
85
  end
86
- end)
86
+ end
@@ -44,19 +44,30 @@ module ActiveRecord
44
44
  parents = model_cache[join_root]
45
45
  column_aliases = aliases.column_aliases join_root
46
46
 
47
- message_bus = ActiveSupport::Notifications.instrumenter
48
-
49
- payload = {
50
- record_count: result_set.length,
51
- class_name: join_root.base_klass.name
52
- }
53
-
54
- message_bus.instrument('instantiation.active_record', payload) do
47
+ if ActiveRecord::VERSION::MINOR == 1
48
+ type_caster = result_set.column_type primary_key
49
+
55
50
  result_set.each do |row_hash|
56
- parent = parents[row_hash[primary_key]] ||= join_root.instantiate(row_hash, column_aliases)
51
+ primary_id = type_caster.type_cast row_hash[primary_key]
52
+ parent = parents[primary_id] ||= join_root.instantiate(row_hash, column_aliases)
57
53
  @calculated_columns.each { |column| parent[column.right] = model[column.right] } if @calculated_columns
58
54
  construct(parent, join_root, row_hash, result_set, seen, model_cache, aliases)
59
55
  end
56
+ else
57
+ message_bus = ActiveSupport::Notifications.instrumenter
58
+
59
+ payload = {
60
+ record_count: result_set.length,
61
+ class_name: join_root.base_klass.name
62
+ }
63
+
64
+ message_bus.instrument('instantiation.active_record', payload) do
65
+ result_set.each do |row_hash|
66
+ parent = parents[row_hash[primary_key]] ||= join_root.instantiate(row_hash, column_aliases)
67
+ @calculated_columns.each { |column| parent[column.right] = model[column.right] } if @calculated_columns
68
+ construct(parent, join_root, row_hash, result_set, seen, model_cache, aliases)
69
+ end
70
+ end
60
71
  end
61
72
 
62
73
  parents.values
@@ -1,3 +1,3 @@
1
1
  module CalculatedAttributes
2
- VERSION = '0.1.2'
2
+ VERSION = '0.1.3'
3
3
  end
@@ -104,7 +104,10 @@ describe 'calculated_attributes' do
104
104
 
105
105
  context 'when eager loading models' do
106
106
  it 'includes calculated attributes' do
107
- scope = model_scoped(Post).includes(:comments).references(:comments)
107
+ scope = case ActiveRecord::VERSION::MAJOR
108
+ when 4 then model_scoped(Post).includes(:comments).references(:comments)
109
+ when 3 then model_scoped(Post).eager_load(:comments)
110
+ end
108
111
  expect(scope.first.comments_count).to eq(1)
109
112
  end
110
113
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: calculated_attributes
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Zach Schneider