calculated_attributes 0.0.18 → 0.0.19

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b751dd8a58b1f4de0e48f23776273daa460cdf81
4
- data.tar.gz: 0caef728cf0c2314840f443ee08fd57d50eef14e
3
+ metadata.gz: 2b2dd0e9f52299a368d2b049c1db8ba7d42c1c7d
4
+ data.tar.gz: ec77da383307dfd1cf345a32bddcc5639f9deeaf
5
5
  SHA512:
6
- metadata.gz: 82e7bc56070d8594c59b590a70fbdd7f71445e2fabee1599382e2776c099ced798fce0035bd134ceb0b742a0378804638554555ea55d28ec25df85a2a318abcd
7
- data.tar.gz: 02e510a66e63e67f636f1a41a2073a2c952f321974c2ace9e0b4f6eaed6327b45ed23d8d8b243188d927c77bf0dc0529669230aacba99b3801bc878bf9e2710d
6
+ metadata.gz: 5dd5735365b4d0f7ecc94ae7667c918ec3fe452f5eb537ab1613905aa20ee0d954f820c800f42ce5df42221ea52e621479308a2fc9733c195761504993f116a0
7
+ data.tar.gz: 1f05b7e90a8a33e2c021f12ac9fe77d67aa042cb56b145e7f94d2d1ad620f9cca77c5a7e26e37beee1df008888fab863a217a05838d439670a406fde5ed679b2
data/README.md CHANGED
@@ -91,7 +91,7 @@ end
91
91
  In Rails 4.x, you cannot call `count` on a relation with calculated attributes, e.g.
92
92
 
93
93
  ```ruby
94
- `Post.scoped.calculated(:comments_count).count`
94
+ Post.scoped.calculated(:comments_count).count
95
95
  ```
96
96
 
97
97
  will error. This is because of an [ActiveRecord issue](https://github.com/rails/rails/blob/master/activerecord/lib/active_record/relation/calculations.rb#L368-L375) that does not permit Arel nodes in the count method.
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ../
3
3
  specs:
4
- calculated_attributes (0.0.18)
4
+ calculated_attributes (0.0.19)
5
5
  activerecord (>= 3.2.20)
6
6
 
7
7
  GEM
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ../
3
3
  specs:
4
- calculated_attributes (0.0.18)
4
+ calculated_attributes (0.0.19)
5
5
  activerecord (>= 3.2.20)
6
6
 
7
7
  GEM
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ../
3
3
  specs:
4
- calculated_attributes (0.0.18)
4
+ calculated_attributes (0.0.19)
5
5
  activerecord (>= 3.2.20)
6
6
 
7
7
  GEM
@@ -71,15 +71,9 @@ ActiveRecord::Relation.send(:include, Module.new do
71
71
  args.each do |arg|
72
72
  lam = klass.calculated.calculated[arg] || klass.base_class.calculated.calculated[arg]
73
73
  sql = lam.call
74
- if sql.is_a? String
75
- new_projection = Arel.sql("(#{sql})").as(arg.to_s)
76
- new_projection.calculated_attr!
77
- projections.push new_projection
78
- else
79
- new_projection = sql.as(arg.to_s)
80
- new_projection.calculated_attr!
81
- projections.push new_projection
82
- end
74
+ new_projection = sql.is_a?(String) ? Arel.sql("(#{sql})").as(arg.to_s) : sql.as(arg.to_s)
75
+ new_projection.calculated_attr!
76
+ projections.push new_projection
83
77
  end
84
78
  select(projections)
85
79
  end
@@ -100,9 +94,76 @@ module ActiveRecord
100
94
  apply_join_dependency(relation, join_dependency)
101
95
  end
102
96
  end
103
- end
104
97
 
105
- module ActiveRecord
98
+ module AttributeMethods
99
+ module ClassMethods
100
+ # Generates all the attribute related methods for columns in the database
101
+ # accessors, mutators and query methods.
102
+ def define_attribute_methods
103
+ case ActiveRecord::VERSION::MAJOR
104
+ when 3
105
+ unless defined?(@attribute_methods_mutex)
106
+ msg = "It looks like something (probably a gem/plugin) is overriding the " \
107
+ "ActiveRecord::Base.inherited method. It is important that this hook executes so " \
108
+ "that your models are set up correctly. A workaround has been added to stop this " \
109
+ "causing an error in 3.2, but future versions will simply not work if the hook is " \
110
+ "overridden. If you are using Kaminari, please upgrade as it is known to have had " \
111
+ "this problem.\n\n"
112
+ msg << "The following may help track down the problem:"
113
+
114
+ meth = method(:inherited)
115
+ if meth.respond_to?(:source_location)
116
+ msg << " #{meth.source_location.inspect}"
117
+ else
118
+ msg << " #{meth.inspect}"
119
+ end
120
+ msg << "\n\n"
121
+
122
+ ActiveSupport::Deprecation.warn(msg)
123
+
124
+ @attribute_methods_mutex = Mutex.new
125
+ end
126
+
127
+ # Use a mutex; we don't want two thread simaltaneously trying to define
128
+ # attribute methods.
129
+ @attribute_methods_mutex.synchronize do
130
+ return if attribute_methods_generated?
131
+ superclass.define_attribute_methods unless self == base_class
132
+ columns_to_define =
133
+ if defined?(calculated) && calculated.instance_variable_get('@calculations')
134
+ calculated_keys = calculated.instance_variable_get('@calculations').keys
135
+ column_names.reject { |c| calculated_keys.include? c.intern }
136
+ else
137
+ column_names
138
+ end
139
+ super(columns_to_define)
140
+ columns_to_define.each { |name| define_external_attribute_method(name) }
141
+ @attribute_methods_generated = true
142
+ end
143
+
144
+ when 4
145
+ return false if @attribute_methods_generated
146
+ # Use a mutex; we don't want two threads simultaneously trying to define
147
+ # attribute methods.
148
+ generated_attribute_methods.synchronize do
149
+ return false if @attribute_methods_generated
150
+ superclass.define_attribute_methods unless self == base_class
151
+ columns_to_define =
152
+ if defined?(calculated) && calculated.instance_variable_get('@calculations')
153
+ calculated_keys = calculated.instance_variable_get('@calculations').keys
154
+ column_names.reject { |c| calculated_keys.include? c.intern }
155
+ else
156
+ column_names
157
+ end
158
+ super(columns_to_define)
159
+ @attribute_methods_generated = true
160
+ end
161
+ true
162
+ end
163
+ end
164
+ end
165
+ end
166
+
106
167
  module Associations
107
168
  class JoinDependency
108
169
  attr_writer :calculated_columns
@@ -1,3 +1,3 @@
1
1
  module CalculatedAttributes
2
- VERSION = '0.0.18'
2
+ VERSION = '0.0.19'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: calculated_attributes
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.18
4
+ version: 0.0.19
5
5
  platform: ruby
6
6
  authors:
7
7
  - Zach Schneider
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-06-18 00:00:00.000000000 Z
11
+ date: 2015-06-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: appraisal