relation_to_json 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.github/workflows/main.yml +16 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.rubocop.yml +13 -0
- data/.ruby-version +1 -0
- data/CHANGELOG.md +5 -0
- data/Gemfile +10 -0
- data/Gemfile.lock +87 -0
- data/LICENSE +21 -0
- data/README.md +87 -0
- data/Rakefile +12 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/lib/relation_to_json/base.rb +124 -0
- data/lib/relation_to_json/base_reflection.rb +36 -0
- data/lib/relation_to_json/belongs_to_reflection.rb +33 -0
- data/lib/relation_to_json/has_one_reflection.rb +40 -0
- data/lib/relation_to_json/reflection_builder.rb +38 -0
- data/lib/relation_to_json/version.rb +3 -0
- data/lib/relation_to_json.rb +16 -0
- data/relation_to_json.gemspec +40 -0
- metadata +155 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 1cade304cba822c0b9c2d133c7f09c73ddaf82335de8ab4bde4191eb0cf1d0db
|
4
|
+
data.tar.gz: c3bc952a93ddb46601851b5479a1f16037c11cfa75141d8e45172e443f57db77
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 97274c759640912b25a55bcb2dde4f501eb0a5c2a37bb3d5b5d17ba3f2371fb2ee983826e81ecd7edc85982295084f3502a9a38c55fc22c489acad942e095b67
|
7
|
+
data.tar.gz: 6b169620534867780924d2a85040d5528bb9f257134db64d19cf54d913cba3b16b15b496f99bbdb236522c62c23b2a8b11137ce72b800c15dcf2d01e565f19f8
|
@@ -0,0 +1,16 @@
|
|
1
|
+
name: Ruby
|
2
|
+
|
3
|
+
on: [push,pull_request]
|
4
|
+
|
5
|
+
jobs:
|
6
|
+
build:
|
7
|
+
runs-on: ubuntu-latest
|
8
|
+
steps:
|
9
|
+
- uses: actions/checkout@v2
|
10
|
+
- name: Set up Ruby
|
11
|
+
uses: ruby/setup-ruby@v1
|
12
|
+
with:
|
13
|
+
ruby-version: 3.0.2
|
14
|
+
bundler-cache: true
|
15
|
+
- name: Run the default task
|
16
|
+
run: bundle exec rake
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
3.0.2
|
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
relation_to_json (0.1.0)
|
5
|
+
activesupport (> 5.0)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
activemodel (7.0.0)
|
11
|
+
activesupport (= 7.0.0)
|
12
|
+
activerecord (7.0.0)
|
13
|
+
activemodel (= 7.0.0)
|
14
|
+
activesupport (= 7.0.0)
|
15
|
+
activesupport (7.0.0)
|
16
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
17
|
+
i18n (>= 1.6, < 2)
|
18
|
+
minitest (>= 5.1)
|
19
|
+
tzinfo (~> 2.0)
|
20
|
+
ast (2.4.2)
|
21
|
+
byebug (11.1.3)
|
22
|
+
coderay (1.1.3)
|
23
|
+
concurrent-ruby (1.1.9)
|
24
|
+
diff-lcs (1.5.0)
|
25
|
+
i18n (1.8.11)
|
26
|
+
concurrent-ruby (~> 1.0)
|
27
|
+
method_source (1.0.0)
|
28
|
+
minitest (5.15.0)
|
29
|
+
parallel (1.21.0)
|
30
|
+
parser (3.1.0.0)
|
31
|
+
ast (~> 2.4.1)
|
32
|
+
pry (0.14.1)
|
33
|
+
coderay (~> 1.1)
|
34
|
+
method_source (~> 1.0)
|
35
|
+
pry-byebug (3.8.0)
|
36
|
+
byebug (~> 11.0)
|
37
|
+
pry (~> 0.10)
|
38
|
+
rainbow (3.1.1)
|
39
|
+
rake (13.0.6)
|
40
|
+
regexp_parser (2.2.0)
|
41
|
+
rexml (3.2.5)
|
42
|
+
rspec (3.10.0)
|
43
|
+
rspec-core (~> 3.10.0)
|
44
|
+
rspec-expectations (~> 3.10.0)
|
45
|
+
rspec-mocks (~> 3.10.0)
|
46
|
+
rspec-core (3.10.1)
|
47
|
+
rspec-support (~> 3.10.0)
|
48
|
+
rspec-expectations (3.10.1)
|
49
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
50
|
+
rspec-support (~> 3.10.0)
|
51
|
+
rspec-mocks (3.10.2)
|
52
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
53
|
+
rspec-support (~> 3.10.0)
|
54
|
+
rspec-support (3.10.3)
|
55
|
+
rubocop (1.25.0)
|
56
|
+
parallel (~> 1.10)
|
57
|
+
parser (>= 3.1.0.0)
|
58
|
+
rainbow (>= 2.2.2, < 4.0)
|
59
|
+
regexp_parser (>= 1.8, < 3.0)
|
60
|
+
rexml
|
61
|
+
rubocop-ast (>= 1.15.1, < 2.0)
|
62
|
+
ruby-progressbar (~> 1.7)
|
63
|
+
unicode-display_width (>= 1.4.0, < 3.0)
|
64
|
+
rubocop-ast (1.15.1)
|
65
|
+
parser (>= 3.0.1.1)
|
66
|
+
ruby-progressbar (1.11.0)
|
67
|
+
sqlite3 (1.4.2)
|
68
|
+
tzinfo (2.0.4)
|
69
|
+
concurrent-ruby (~> 1.0)
|
70
|
+
unicode-display_width (2.1.0)
|
71
|
+
|
72
|
+
PLATFORMS
|
73
|
+
x86_64-darwin-21
|
74
|
+
|
75
|
+
DEPENDENCIES
|
76
|
+
activerecord (~> 7.0.0)
|
77
|
+
bundler (~> 2.0)
|
78
|
+
pry
|
79
|
+
pry-byebug
|
80
|
+
rake (~> 13.0)
|
81
|
+
relation_to_json!
|
82
|
+
rspec (~> 3.0)
|
83
|
+
rubocop (~> 1.7)
|
84
|
+
sqlite3 (~> 1.4.2)
|
85
|
+
|
86
|
+
BUNDLED WITH
|
87
|
+
2.2.22
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2022 Derek Y
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
# RelationToJSON
|
2
|
+
|
3
|
+
`RelationToJSON` allows the conversion of `ActiveRecord::Relation` objects into an array of hash-like objects, provided a schema.
|
4
|
+
It allows nesting across different relations, and uses `pluck` to optimize queries over multiple tables.
|
5
|
+
This also acts as a useful interface in a React on Rails application where data needs to be passed to the front end in a simple serializable object, rather than passing a Rails object.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
Add this line to your application's Gemfile
|
9
|
+
```rb
|
10
|
+
gem 'relation_to_json'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle install
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install relation_to_json
|
20
|
+
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
A schema is an array of attributes that you would like your resultant JSON object to have.
|
24
|
+
```rb
|
25
|
+
class User < ApplicationRecord
|
26
|
+
has_one :keyboard
|
27
|
+
validate :first_name, :last_name, presence: true
|
28
|
+
end
|
29
|
+
|
30
|
+
class Keyboard < ApplicationRecord
|
31
|
+
belongs_to :user
|
32
|
+
validate :make, :model, presence: true
|
33
|
+
end
|
34
|
+
```
|
35
|
+
|
36
|
+
You can write the following schema:
|
37
|
+
```rb
|
38
|
+
[
|
39
|
+
:first_name,
|
40
|
+
:last_name,
|
41
|
+
keyboard: [
|
42
|
+
:make,
|
43
|
+
:model,
|
44
|
+
]
|
45
|
+
]
|
46
|
+
```
|
47
|
+
|
48
|
+
Thus with the following `ActiveRecord::Relation`, such as `User.all`, we can write out the following:
|
49
|
+
```rb
|
50
|
+
User.all.to_json_with_schema(schema)
|
51
|
+
```
|
52
|
+
|
53
|
+
Assuming that all of the relations exist, we can expect a response of the format:
|
54
|
+
```rb
|
55
|
+
[
|
56
|
+
{
|
57
|
+
id: 1,
|
58
|
+
first_name: ...,
|
59
|
+
last_name: ...,
|
60
|
+
keyboard: {
|
61
|
+
make: ...,
|
62
|
+
model: ...,
|
63
|
+
}
|
64
|
+
},
|
65
|
+
{
|
66
|
+
id: 2,
|
67
|
+
first_name: ...,
|
68
|
+
last_name: ...,
|
69
|
+
keyboard: {
|
70
|
+
make: ...,
|
71
|
+
model: ...,
|
72
|
+
}
|
73
|
+
}
|
74
|
+
]
|
75
|
+
```
|
76
|
+
|
77
|
+
## Development
|
78
|
+
|
79
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
80
|
+
|
81
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
82
|
+
|
83
|
+
## Contributing
|
84
|
+
Bug reports and pull requests are welcome on Github at https://github.com/DerekYu177/relation_to_json.
|
85
|
+
|
86
|
+
## License
|
87
|
+
This gem is available as open source under the terms of the MIT License.
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require "bundler/setup"
|
5
|
+
require "relation_to_json"
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
+
# require "pry"
|
12
|
+
# Pry.start
|
13
|
+
|
14
|
+
require "irb"
|
15
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,124 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_support/core_ext/hash/indifferent_access"
|
4
|
+
require_relative "reflection_builder"
|
5
|
+
|
6
|
+
module RelationToJSON
|
7
|
+
class Base
|
8
|
+
attr_reader :relation, :schema
|
9
|
+
|
10
|
+
def initialize(relation, schema)
|
11
|
+
@relation = relation
|
12
|
+
@schema = schema
|
13
|
+
end
|
14
|
+
|
15
|
+
def as_json
|
16
|
+
# put everything here, anything else is private
|
17
|
+
attributes, schema_associations = schema
|
18
|
+
.partition { |e| e.is_a?(Symbol) }
|
19
|
+
schema_associations = schema_associations.first.dup || []
|
20
|
+
|
21
|
+
attributes = Set[:id] + attributes
|
22
|
+
|
23
|
+
reflections = RelationToJSON::ReflectionBuilder.build(schema_associations, relation)
|
24
|
+
schema_associations.each do |schema_association, association_attributes|
|
25
|
+
reflection = reflections[schema_association]
|
26
|
+
|
27
|
+
case reflection
|
28
|
+
when RelationToJSON::BelongsToReflection
|
29
|
+
attributes << reflection.foreign_key.to_sym
|
30
|
+
when RelationToJSON::HasOneReflection
|
31
|
+
association_attributes << reflection.foreign_key.to_sym
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
raise_unless_all_attributes_present_on_model!(relation, attributes)
|
36
|
+
|
37
|
+
result = relation
|
38
|
+
.reload # if when the relation isn't loaded, it may have strange ordering
|
39
|
+
.pluck(*attributes)
|
40
|
+
.map { |plucked| attributes.zip(Array.wrap(plucked)).to_h }
|
41
|
+
|
42
|
+
transposed = transpose(result)
|
43
|
+
|
44
|
+
reflections.each do |reflection_name, reflection|
|
45
|
+
foreign_key = reflection.foreign_key
|
46
|
+
primary_key = reflection.primary_key
|
47
|
+
|
48
|
+
# if the current schema still has associations
|
49
|
+
# then we need to recursively find the JSON
|
50
|
+
# representation of that association
|
51
|
+
# Otherwise, we can perform a shallow .pluck
|
52
|
+
# of the association attributes
|
53
|
+
# and map them back onto the transposed hash
|
54
|
+
# this returns an array of hashes that map association attributes to plucked values
|
55
|
+
plucked_values = reflection.pluck_association_columns(transposed)
|
56
|
+
|
57
|
+
case reflection
|
58
|
+
when RelationToJSON::BelongsToReflection
|
59
|
+
# build a temporary mapping of id => assigned_attributes
|
60
|
+
associated_model_primary_key_indexed_plucked_values = plucked_values
|
61
|
+
.to_h { |attrs| [attrs&.fetch(primary_key, nil), attrs] }
|
62
|
+
.compact
|
63
|
+
|
64
|
+
result.each do |record|
|
65
|
+
foreign_key_value = record[foreign_key]
|
66
|
+
plucked_values = associated_model_primary_key_indexed_plucked_values[foreign_key_value]
|
67
|
+
record[reflection_name] = plucked_values
|
68
|
+
record.except!(foreign_key)
|
69
|
+
end
|
70
|
+
when RelationToJSON::HasOneReflection
|
71
|
+
# build a temporary mapping of id => assigned_attributes
|
72
|
+
associated_model_foreign_key_indexed_plucked_values = plucked_values
|
73
|
+
.to_h { |attrs| [attrs&.fetch(foreign_key, nil), attrs] }
|
74
|
+
.compact
|
75
|
+
|
76
|
+
result.each do |record|
|
77
|
+
primary_key_value = record[primary_key]
|
78
|
+
plucked_values = associated_model_foreign_key_indexed_plucked_values[primary_key_value]
|
79
|
+
plucked_values.except!(foreign_key) if plucked_values
|
80
|
+
record[reflection_name] = plucked_values
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
result&.map { |partial| partial.with_indifferent_access }
|
86
|
+
end
|
87
|
+
|
88
|
+
private
|
89
|
+
|
90
|
+
def transpose(values)
|
91
|
+
# values is a list of hashes
|
92
|
+
# each hash should be identical
|
93
|
+
# i.e. [{id: 1, clinical_sender_id: 2}, {id: 2, clinical_sender_id: 3}]
|
94
|
+
# and tranposes into a hash
|
95
|
+
# with identical keys
|
96
|
+
# but values are arrays
|
97
|
+
# i.e. { id: [1, 2], clinical_sender_id: [2, 3] }
|
98
|
+
|
99
|
+
result = {}
|
100
|
+
|
101
|
+
values.each do |value|
|
102
|
+
value.each do |k, v|
|
103
|
+
if result.include?(k)
|
104
|
+
result[k] << v
|
105
|
+
else
|
106
|
+
result[k] = [v]
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
result
|
112
|
+
end
|
113
|
+
|
114
|
+
def raise_unless_all_attributes_present_on_model!(relation, requested)
|
115
|
+
available = Set[*relation.klass.column_names]
|
116
|
+
requested = Set[*requested.map(&:to_s)]
|
117
|
+
return if requested <= available
|
118
|
+
|
119
|
+
missing = requested - available
|
120
|
+
|
121
|
+
raise InvalidSchemaError.new(missing.to_a, relation.klass)
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_support/core_ext/module/delegation"
|
4
|
+
|
5
|
+
module RelationToJSON
|
6
|
+
class BaseReflection
|
7
|
+
attr_reader :reflection, :name
|
8
|
+
attr_accessor :required_columns
|
9
|
+
|
10
|
+
def initialize(reflection, reflection_name, reflection_columns)
|
11
|
+
@reflection = reflection
|
12
|
+
@name = reflection_name
|
13
|
+
@required_columns = reflection_columns
|
14
|
+
end
|
15
|
+
|
16
|
+
delegate :active_record, :polymorphic?, :klass, to: :reflection
|
17
|
+
|
18
|
+
def primary_key
|
19
|
+
reflection.active_record.primary_key.to_sym
|
20
|
+
end
|
21
|
+
|
22
|
+
def foreign_key
|
23
|
+
reflection.foreign_key.to_sym
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def recurse_json_with_schema(transposed)
|
29
|
+
RelationToJSON::Base.new(association_relation(transposed), required_columns).as_json
|
30
|
+
end
|
31
|
+
|
32
|
+
def nested_relations?
|
33
|
+
required_columns.any? { _1.is_a?(Hash) }
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RelationToJSON
|
4
|
+
class BelongsToReflection < BaseReflection
|
5
|
+
def pluck_association_columns(transposed)
|
6
|
+
return recurse_json_with_schema(transposed) if nested_relations?
|
7
|
+
|
8
|
+
required_columns = Set[primary_key, *@required_columns]
|
9
|
+
plucked_attributes = association_relation(transposed)
|
10
|
+
.pluck(*required_columns)
|
11
|
+
.map { |plucked| required_columns.zip(Array.wrap(plucked)).to_h }
|
12
|
+
|
13
|
+
primary_key_indexed_plucked_values = plucked_attributes
|
14
|
+
.to_h { |attributes| [attributes[primary_key], attributes] }
|
15
|
+
|
16
|
+
transposed.fetch(foreign_key, []).map do |record_primary_key|
|
17
|
+
primary_key_indexed_plucked_values[record_primary_key]
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def association_relation(transposed)
|
22
|
+
if polymorphic?
|
23
|
+
active_record.where(
|
24
|
+
primary_key => transposed[foreign_key],
|
25
|
+
)
|
26
|
+
else
|
27
|
+
klass.where(
|
28
|
+
primary_key => transposed[foreign_key],
|
29
|
+
)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RelationToJSON
|
4
|
+
class HasOneReflection < BaseReflection
|
5
|
+
def pluck_association_columns(transposed)
|
6
|
+
return recurse_json_with_schema(transposed) if nested_relations?
|
7
|
+
|
8
|
+
required_columns = Set[primary_key, *@required_columns]
|
9
|
+
plucked_attributes = association_relation(transposed)
|
10
|
+
.pluck(*required_columns)
|
11
|
+
.map { |plucked| required_columns.zip(Array.wrap(plucked)).to_h }
|
12
|
+
|
13
|
+
foreign_key_indexed_plucked_values = plucked_attributes
|
14
|
+
.to_h { |attributes| [attributes[foreign_key], attributes] }
|
15
|
+
|
16
|
+
transposed.fetch(primary_key, []).map do |record_foreign_key|
|
17
|
+
foreign_key_indexed_plucked_values[record_foreign_key]
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def association_relation(transposed)
|
22
|
+
query = { foreign_key => transposed[primary_key] }
|
23
|
+
query[polymorphic_association_key] = foreign_class if polymorphic?
|
24
|
+
klass.where(**query)
|
25
|
+
end
|
26
|
+
|
27
|
+
def polymorphic?
|
28
|
+
reflection.inverse_of.polymorphic?
|
29
|
+
end
|
30
|
+
|
31
|
+
def polymorphic_association_key
|
32
|
+
# *_type
|
33
|
+
reflection.type
|
34
|
+
end
|
35
|
+
|
36
|
+
def foreign_class
|
37
|
+
reflection.active_record.name
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RelationToJSON
|
4
|
+
class ReflectionBuilder
|
5
|
+
class UnsupportedReflectionType < StandardError
|
6
|
+
def initialize(reflection)
|
7
|
+
@reflection = reflection
|
8
|
+
end
|
9
|
+
|
10
|
+
def message
|
11
|
+
"Unrecognized reflection type: #{reflection.class}"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.build(schema_associations, relation)
|
16
|
+
schema_associations.to_h do |reflection_name, reflection_columns|
|
17
|
+
# for each association
|
18
|
+
# we first have to get the relation that the association has
|
19
|
+
# with the active record relation
|
20
|
+
active_record_reflection = relation.model.reflections.fetch(reflection_name.to_s)
|
21
|
+
|
22
|
+
klass = case active_record_reflection
|
23
|
+
when ActiveRecord::Reflection::BelongsToReflection
|
24
|
+
RelationToJSON::BelongsToReflection
|
25
|
+
when ActiveRecord::Reflection::HasOneReflection
|
26
|
+
RelationToJSON::HasOneReflection
|
27
|
+
else
|
28
|
+
raise UnsupportedReflectionType.new(active_record_reflection)
|
29
|
+
end
|
30
|
+
|
31
|
+
[
|
32
|
+
reflection_name,
|
33
|
+
klass.new(active_record_reflection, reflection_name, reflection_columns),
|
34
|
+
]
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require_relative "relation_to_json/base"
|
2
|
+
require_relative "relation_to_json/base_reflection"
|
3
|
+
require_relative "relation_to_json/belongs_to_reflection"
|
4
|
+
require_relative "relation_to_json/has_one_reflection"
|
5
|
+
require_relative "relation_to_json/version"
|
6
|
+
|
7
|
+
class InvalidSchemaError < StandardError
|
8
|
+
def initialize(invalid_attributes, klass)
|
9
|
+
@invalid_attributes = invalid_attributes
|
10
|
+
@klass = klass
|
11
|
+
end
|
12
|
+
|
13
|
+
def message
|
14
|
+
"The attributes: #{@invalid_attributes} do not exist on the model: #{@klass.name}, which has attributes #{@klass.column_names}"
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path('../lib', __FILE__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require 'relation_to_json/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = "relation_to_json"
|
9
|
+
spec.version = RelationToJSON::VERSION
|
10
|
+
spec.authors = ["Derek Yu"]
|
11
|
+
spec.email = ["derek-nis@hotmail.com"]
|
12
|
+
|
13
|
+
spec.summary = %q{RelationToJSON converts a Rails ActiveRecord Relation to JSON, provided a schema}
|
14
|
+
spec.description = <<~DESC
|
15
|
+
RelationToJSON takes in a schema of attributes and associations
|
16
|
+
together with an ActiveRecord::Relation object and produces an array of nested hashes
|
17
|
+
corresponding to a JSON representation of each record.
|
18
|
+
DESC
|
19
|
+
|
20
|
+
spec.homepage = "https://github.com/Derekyu177/relation_to_json"
|
21
|
+
spec.license = "MIT"
|
22
|
+
spec.required_ruby_version = ">= 3.0.2"
|
23
|
+
|
24
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
25
|
+
spec.bindir = "exe"
|
26
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
27
|
+
spec.require_paths = ["lib"]
|
28
|
+
|
29
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
30
|
+
spec.metadata["source_code_uri"] = spec.homepage
|
31
|
+
spec.metadata["changelog_uri"] = "#{spec.homepage}/CHANGELOG.md"
|
32
|
+
|
33
|
+
spec.add_runtime_dependency "activesupport", "> 5.0"
|
34
|
+
|
35
|
+
spec.add_development_dependency "bundler", "~> 2.0"
|
36
|
+
spec.add_development_dependency "pry"
|
37
|
+
spec.add_development_dependency "pry-byebug"
|
38
|
+
spec.add_development_dependency "activerecord", "~> 7.0.0"
|
39
|
+
spec.add_development_dependency "sqlite3", "~> 1.4.2"
|
40
|
+
end
|
metadata
ADDED
@@ -0,0 +1,155 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: relation_to_json
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Derek Yu
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-01-20 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '5.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '5.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '2.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '2.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: pry
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: pry-byebug
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: activerecord
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 7.0.0
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 7.0.0
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: sqlite3
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 1.4.2
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 1.4.2
|
97
|
+
description: |
|
98
|
+
RelationToJSON takes in a schema of attributes and associations
|
99
|
+
together with an ActiveRecord::Relation object and produces an array of nested hashes
|
100
|
+
corresponding to a JSON representation of each record.
|
101
|
+
email:
|
102
|
+
- derek-nis@hotmail.com
|
103
|
+
executables: []
|
104
|
+
extensions: []
|
105
|
+
extra_rdoc_files: []
|
106
|
+
files:
|
107
|
+
- ".github/workflows/main.yml"
|
108
|
+
- ".gitignore"
|
109
|
+
- ".rspec"
|
110
|
+
- ".rubocop.yml"
|
111
|
+
- ".ruby-version"
|
112
|
+
- CHANGELOG.md
|
113
|
+
- Gemfile
|
114
|
+
- Gemfile.lock
|
115
|
+
- LICENSE
|
116
|
+
- README.md
|
117
|
+
- Rakefile
|
118
|
+
- bin/console
|
119
|
+
- bin/setup
|
120
|
+
- lib/relation_to_json.rb
|
121
|
+
- lib/relation_to_json/base.rb
|
122
|
+
- lib/relation_to_json/base_reflection.rb
|
123
|
+
- lib/relation_to_json/belongs_to_reflection.rb
|
124
|
+
- lib/relation_to_json/has_one_reflection.rb
|
125
|
+
- lib/relation_to_json/reflection_builder.rb
|
126
|
+
- lib/relation_to_json/version.rb
|
127
|
+
- relation_to_json.gemspec
|
128
|
+
homepage: https://github.com/Derekyu177/relation_to_json
|
129
|
+
licenses:
|
130
|
+
- MIT
|
131
|
+
metadata:
|
132
|
+
homepage_uri: https://github.com/Derekyu177/relation_to_json
|
133
|
+
source_code_uri: https://github.com/Derekyu177/relation_to_json
|
134
|
+
changelog_uri: https://github.com/Derekyu177/relation_to_json/CHANGELOG.md
|
135
|
+
post_install_message:
|
136
|
+
rdoc_options: []
|
137
|
+
require_paths:
|
138
|
+
- lib
|
139
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
140
|
+
requirements:
|
141
|
+
- - ">="
|
142
|
+
- !ruby/object:Gem::Version
|
143
|
+
version: 3.0.2
|
144
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
145
|
+
requirements:
|
146
|
+
- - ">="
|
147
|
+
- !ruby/object:Gem::Version
|
148
|
+
version: '0'
|
149
|
+
requirements: []
|
150
|
+
rubygems_version: 3.3.5
|
151
|
+
signing_key:
|
152
|
+
specification_version: 4
|
153
|
+
summary: RelationToJSON converts a Rails ActiveRecord Relation to JSON, provided a
|
154
|
+
schema
|
155
|
+
test_files: []
|