relation_to_struct 1.2.2 → 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/Appraisals +2 -2
- data/gemfiles/rails_5_0.gemfile +1 -1
- data/gemfiles/rails_5_1.gemfile +1 -1
- data/lib/relation_to_struct.rb +0 -1
- data/lib/relation_to_struct/active_record_relation_extension.rb +5 -2
- data/lib/relation_to_struct/version.rb +1 -1
- data/relation_to_struct.gemspec +2 -2
- data/spec/active_record_relation_spec.rb +15 -0
- metadata +7 -10
- data/gemfiles/rails_4_1.gemfile +0 -7
- data/gemfiles/rails_4_2.gemfile +0 -7
- data/lib/relation_to_struct/active_record_result_41_extension.rb +0 -26
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f3c97c2553a7d275a6830b61306a9980db46f98c
|
4
|
+
data.tar.gz: d466c23b0d22e28fd1892314f35bc547d913425d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4885ef40deeb19798257eca384395bf7469f8e5363b05019e2fc64b99e711d6988755198b55a1c6f9da279f0dc7499e8569d0eccdbaf15d676ff66fcbaad6234
|
7
|
+
data.tar.gz: 96f902d4a3259653b5c6f9288655728f588202cffb0b8c941172797b063395abedd9e6d97283a4705d5f70b3e63fd901127ba6902574787ee8ab08d3d5478bb4
|
data/.gitignore
CHANGED
data/Appraisals
CHANGED
data/gemfiles/rails_5_0.gemfile
CHANGED
data/gemfiles/rails_5_1.gemfile
CHANGED
data/lib/relation_to_struct.rb
CHANGED
@@ -5,7 +5,10 @@ module RelationToStruct::ActiveRecordRelationExtension
|
|
5
5
|
raise ArgumentError, 'Expected select_values to be present' unless self.select_values.present?
|
6
6
|
|
7
7
|
relation = spawn
|
8
|
-
|
8
|
+
|
9
|
+
# See the definition of #pluck in:
|
10
|
+
# activerecord/lib/active_record/relation/calculations.rb
|
11
|
+
result = klass.connection.select_all(relation.arel, nil, bound_attributes)
|
9
12
|
|
10
13
|
if result.columns.size != struct_class.members.size
|
11
14
|
raise ArgumentError, 'Expected struct fields and columns lengths to be equal'
|
@@ -15,7 +18,7 @@ module RelationToStruct::ActiveRecordRelationExtension
|
|
15
18
|
raise ArgumentError, 'Expected column names to be unique'
|
16
19
|
end
|
17
20
|
|
18
|
-
values_after_casting =
|
21
|
+
values_after_casting = result.cast_values()
|
19
22
|
|
20
23
|
if result.columns.size == 1
|
21
24
|
values_after_casting.map do |tuple|
|
data/relation_to_struct.gemspec
CHANGED
@@ -26,6 +26,6 @@ Gem::Specification.new do |spec|
|
|
26
26
|
spec.add_development_dependency "pry-byebug"
|
27
27
|
spec.add_development_dependency "pg"
|
28
28
|
|
29
|
-
spec.add_dependency "activerecord", ">=
|
30
|
-
spec.add_dependency "activesupport", ">=
|
29
|
+
spec.add_dependency "activerecord", ">= 5.0", "< 5.2"
|
30
|
+
spec.add_dependency "activesupport", ">= 5.0", "< 5.2"
|
31
31
|
end
|
@@ -39,6 +39,21 @@ describe ActiveRecord::Relation do
|
|
39
39
|
).to eq([test_struct.new(hayek.name, austrian.name)])
|
40
40
|
end
|
41
41
|
|
42
|
+
it 'incorporates both integer and text where bind parameters' do
|
43
|
+
hayek = Economist.create!(name: 'F.A. Hayek')
|
44
|
+
test_struct = Struct.new(:id, :name)
|
45
|
+
|
46
|
+
structs = Economist
|
47
|
+
.where(
|
48
|
+
:id => [hayek.id],
|
49
|
+
:name => hayek.name,
|
50
|
+
)
|
51
|
+
.select(:id, :name)
|
52
|
+
.to_structs(test_struct)
|
53
|
+
|
54
|
+
expect(structs).to eq([test_struct.new(hayek.id, hayek.name)])
|
55
|
+
end
|
56
|
+
|
42
57
|
it 'properly casts values from arbitrary calculated columns' do
|
43
58
|
hayek = Economist.create!(name: 'F.A. Hayek')
|
44
59
|
scope = Economist.all
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: relation_to_struct
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- James Coleman
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-12-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: appraisal
|
@@ -114,7 +114,7 @@ dependencies:
|
|
114
114
|
requirements:
|
115
115
|
- - ">="
|
116
116
|
- !ruby/object:Gem::Version
|
117
|
-
version: '
|
117
|
+
version: '5.0'
|
118
118
|
- - "<"
|
119
119
|
- !ruby/object:Gem::Version
|
120
120
|
version: '5.2'
|
@@ -124,7 +124,7 @@ dependencies:
|
|
124
124
|
requirements:
|
125
125
|
- - ">="
|
126
126
|
- !ruby/object:Gem::Version
|
127
|
-
version: '
|
127
|
+
version: '5.0'
|
128
128
|
- - "<"
|
129
129
|
- !ruby/object:Gem::Version
|
130
130
|
version: '5.2'
|
@@ -134,7 +134,7 @@ dependencies:
|
|
134
134
|
requirements:
|
135
135
|
- - ">="
|
136
136
|
- !ruby/object:Gem::Version
|
137
|
-
version: '
|
137
|
+
version: '5.0'
|
138
138
|
- - "<"
|
139
139
|
- !ruby/object:Gem::Version
|
140
140
|
version: '5.2'
|
@@ -144,7 +144,7 @@ dependencies:
|
|
144
144
|
requirements:
|
145
145
|
- - ">="
|
146
146
|
- !ruby/object:Gem::Version
|
147
|
-
version: '
|
147
|
+
version: '5.0'
|
148
148
|
- - "<"
|
149
149
|
- !ruby/object:Gem::Version
|
150
150
|
version: '5.2'
|
@@ -162,14 +162,11 @@ files:
|
|
162
162
|
- LICENSE.txt
|
163
163
|
- README.md
|
164
164
|
- Rakefile
|
165
|
-
- gemfiles/rails_4_1.gemfile
|
166
|
-
- gemfiles/rails_4_2.gemfile
|
167
165
|
- gemfiles/rails_5_0.gemfile
|
168
166
|
- gemfiles/rails_5_1.gemfile
|
169
167
|
- lib/relation_to_struct.rb
|
170
168
|
- lib/relation_to_struct/active_record_base_extension.rb
|
171
169
|
- lib/relation_to_struct/active_record_relation_extension.rb
|
172
|
-
- lib/relation_to_struct/active_record_result_41_extension.rb
|
173
170
|
- lib/relation_to_struct/version.rb
|
174
171
|
- relation_to_struct.gemspec
|
175
172
|
- spec/active_record_base_spec.rb
|
@@ -200,7 +197,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
200
197
|
version: '0'
|
201
198
|
requirements: []
|
202
199
|
rubyforge_project:
|
203
|
-
rubygems_version: 2.
|
200
|
+
rubygems_version: 2.6.14
|
204
201
|
signing_key:
|
205
202
|
specification_version: 4
|
206
203
|
summary: Return struct results from ActiveRecord relation queries
|
data/gemfiles/rails_4_1.gemfile
DELETED
data/gemfiles/rails_4_2.gemfile
DELETED
@@ -1,26 +0,0 @@
|
|
1
|
-
module RelationToStruct::ActiveRecord41ResultExtension
|
2
|
-
extend ::ActiveSupport::Concern
|
3
|
-
|
4
|
-
included do
|
5
|
-
alias_method_chain :column_type, :ar_42_semantics
|
6
|
-
end
|
7
|
-
|
8
|
-
def cast_values(type_overrides = {}) # :nodoc:
|
9
|
-
types = columns.map { |name| column_type(name, type_overrides) }
|
10
|
-
result = rows.map do |values|
|
11
|
-
types.zip(values).map { |type, value| type.type_cast(value) }
|
12
|
-
end
|
13
|
-
|
14
|
-
columns.one? ? result.map!(&:first) : result
|
15
|
-
end
|
16
|
-
|
17
|
-
def column_type_with_ar_42_semantics(name, type_overrides = {})
|
18
|
-
type_overrides.fetch(name) do
|
19
|
-
column_types.fetch(name, ::ActiveRecord::Result::IDENTITY_TYPE)
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
if ActiveRecord.version < Gem::Version.new("4.2.0")
|
25
|
-
::ActiveRecord::Result.send(:include, RelationToStruct::ActiveRecord41ResultExtension)
|
26
|
-
end
|