calculated_attributes 0.0.18 → 0.0.19
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/gemfiles/rails3.gemfile.lock +1 -1
- data/gemfiles/rails4_1.gemfile.lock +1 -1
- data/gemfiles/rails4_2.gemfile.lock +1 -1
- data/lib/calculated_attributes.rb +72 -11
- data/lib/calculated_attributes/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2b2dd0e9f52299a368d2b049c1db8ba7d42c1c7d
|
4
|
+
data.tar.gz: ec77da383307dfd1cf345a32bddcc5639f9deeaf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
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.
|
@@ -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
|
-
|
75
|
-
|
76
|
-
|
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
|
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
|
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.
|
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-
|
11
|
+
date: 2015-06-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: appraisal
|