jsonapi-realizer 6.0.0.rc1 → 6.0.0.rc2

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
  SHA256:
3
- metadata.gz: b8297880001a46f1294e598d9d83f5dab3059c3af75fcc490279f18401b110c6
4
- data.tar.gz: 07a0d04b94d9ca5d105f191d615fc242d4bf63fbd04f674f646aad267eba7e58
3
+ metadata.gz: d793387ade1272c7e808efdf832b814e5e27c5f8774e8504a77179c330882969
4
+ data.tar.gz: ad543bd542140615082ce32ec252d51e773376040a1a7d8c16df6c93d7bba1ce
5
5
  SHA512:
6
- metadata.gz: a125e92782429f6d6f4fafd9d8fc4cbb082862b4c388d39d6e1b77d653a1ebba33edbff192077ca42df15d18a5ab3f6d246aad4d6f4eac812e25f92ad516d33a
7
- data.tar.gz: cfcc9de3e9fd407613065c7f3328899b52fafd802e95bfc9766b53690911101059f01880a3b9b1308d2698a3976b3d22441b858e6540749b650a954c4772e366
6
+ metadata.gz: daebfcf746fcf038f60777f2edf677798e764f2078556d0218c23dc295230144b7053dcf502e1692c45e514ff2dff09d369f0690b8d1069bc73b388c0c421939
7
+ data.tar.gz: 4e5ce6aa5fab832de40dee2d0d00f12047839decc9ca25090fa87cd059ecec81d30d4472ea8b722d3793dae72d603f97ba822692369e5694bb224b40fce146c0
data/README.md CHANGED
@@ -5,78 +5,84 @@
5
5
  - [![Version](http://img.shields.io/gem/v/jsonapi-realizer.svg?style=flat-square)](https://rubygems.org/gems/jsonapi-realizer)
6
6
 
7
7
 
8
- This library handles incoming [json:api](https://www.jsonapi.org) payloads and turns them, via an adapter system, into data models for your business logic.
8
+ This library handles incoming [json:api](https://www.jsonapi.org) payloads and turns them, via an adapter system, into native data models. While designed with rails in mind, this library doesn't require rails to use.
9
+
9
10
 
10
11
  ## Using
11
12
 
12
13
  In order to use this library you'll want to have some models:
13
14
 
14
- ``` ruby
15
- class Photo < ApplicationRecord
16
- belongs_to :photographer, class_name: "Profile"
17
- end
18
15
 
16
+ ``` ruby
19
17
  class Profile < ApplicationRecord
20
18
  has_many :photos
21
19
  end
22
20
  ```
23
21
 
24
- *Note: They don't have to be ActiveRecord models, but we have built-in support for that library (adapter-based).*
25
-
26
- Second you'll need some realizers:
27
-
28
22
  ``` ruby
29
- class PhotoRealizer
30
- include JSONAPI::Realizer::Resource
31
-
32
- register :photos, class_name: "Photo", adapter: :active_record
23
+ class Photo < ApplicationRecord
24
+ belongs_to :photographer, class_name: "Profile"
25
+ end
26
+ ```
33
27
 
34
- has_one :photographer, as: :profiles
28
+ *Note: They don't have to be ActiveRecord models, but we have built-in support for that library (via an adapter).*
35
29
 
36
- has :title
37
- has :src
38
- end
30
+ Second you'll need some realizers:
39
31
 
32
+ ``` ruby
40
33
  class ProfileRealizer
41
34
  include JSONAPI::Realizer::Resource
42
35
 
43
- register :profiles, class_name: "Profile", adapter: :active_record
36
+ type :profiles, class_name: "Profile", adapter: :active_record
44
37
 
45
- has_many :photos
38
+ has_many :photos, class_name: "PhotoRealizer"
46
39
 
47
40
  has :name
48
41
  end
49
42
  ```
50
43
 
51
- You can define aliases for these properties:
52
-
53
44
  ``` ruby
54
- has_many :doctors, as: :users
45
+ class PhotoRealizer
46
+ include JSONAPI::Realizer::Resource
47
+
48
+ type :photos, class_name: "Photo", adapter: :active_record
55
49
 
56
- has :title, as: :name
50
+ has_one :photographer, as: :profiles, class_name: "ProfileRealizer"
51
+
52
+ has :title
53
+ has :src
54
+ end
57
55
  ```
58
56
 
59
- Once you've designed your resources, we just need to use them! In this example, we'll use controllers from Rails:
57
+ Now that we have these we can invoke them in the controller:
60
58
 
61
59
  ``` ruby
62
60
  class PhotosController < ApplicationController
63
61
  def create
64
- realization = JSONAPI::Realizer.create(params, headers: request.headers)
62
+ realizer = PhotoRealizer.new(
63
+ :intent => :create,
64
+ :parameters => params,
65
+ :headers => request.headers
66
+ )
65
67
 
66
- ProcessPhotosService.new(realization.model)
68
+ realizer.object.save!
67
69
 
68
- render json: JSONAPI::Serializer.serialize(realization.model)
70
+ render json: realizer.object.to_json
69
71
  end
70
72
 
71
73
  def index
72
- realization = JSONAPI::Realizer.index(params, headers: request.headers, type: :photos)
74
+ realizer = PhotoRealizer.new(
75
+ :intent => :index,
76
+ :parameters => params,
77
+ :headers => request.headers
78
+ )
73
79
 
74
- render json: JSONAPI::Serializer.serialize(realization.models, is_collection: true)
80
+ render json: realizer.object.to_json
75
81
  end
76
82
  end
77
83
  ```
78
84
 
79
- Notice that we pass `realization.model` to `ProcessPhotosService`, that's because `jsonapi-realizer` doesn't do the act of saving, creating, or destroying! We just ready up the records for you to handle (including errors).
85
+ Notice that we have to handle creating the model ourselves with `realizer.object.save!`. `jsonapi-realizer` doesn't act on a request, it only prepares you to act on the request.
80
86
 
81
87
 
82
88
  ### Adapters
@@ -88,33 +94,38 @@ There are two core adapters:
88
94
 
89
95
  An adapter must provide the following interfaces:
90
96
 
91
- 0. `find_one`, describes how to find the model
92
- 0. `find_many`, describes how to find many models
93
- 0. `write_attributes`, describes how to write a set of properties
94
- 0. `write_relationships`, describes how to write a set of relationships
95
- 0. `includes_via`, describes how to eager include related models
96
- 0. `sparse_fields`, describes how to only return certain fields
97
+ 0. `find_many(scope)`, describes how to find many records
98
+ 0. `find_one(scope, id)`, describes how to find one record
99
+ 0. `filtering(scope, filters)`, describes how to filter records by a set of properties
100
+ 0. `sorting(scope, sorts)`, describes how to sort records
101
+ 0. `paginate(scope, per, offset)`, describes how to page records
102
+ 0. `write_attributes(model, attributes)`, describes how to write a set of properties
103
+ 0. `write_relationships(model, relationships)`, describes how to write a set of relationships
104
+ 0. `include_relationships(scope, includes)`, describes how to eager include related models
97
105
 
98
- You can also provide custom adapter interfaces like below, which will use `active_record`'s `find_many`, `write_relationships`, `update_via`, `includes_via`, and `sparse_fields`:
106
+ You can also provide custom adapter interfaces like below:
99
107
 
100
108
  ``` ruby
101
- class PhotoRealizer
102
- include JSONAPI::Realizer::Resource
103
-
104
- register :photos, class_name: "Photo", adapter: :active_record
105
-
106
- adapter.find_one do |model_class, id|
107
- model_class.where { id == id or slug == id }.first
108
- end
109
+ JSONAPI::Realizer.configuration do |let|
110
+ let.adapter_mappings = {
111
+ active_record_postgres_pagination: PostgresActiveRecordPaginationAdapter
112
+ }
113
+ end
114
+ ```
109
115
 
110
- adapter.write_attributes do |model, attributes|
111
- model.update_columns(attributes)
116
+ ``` ruby
117
+ module PostgresActiveRecordPaginationAdapter < JSONAPI::Realizer::Adapter::ActiveRecord
118
+ def paginate(scope, per, offset)
119
+ scope.offset(offset).limit(per)
112
120
  end
121
+ end
122
+ ```
113
123
 
114
- has_one :photographer, as: :profiles
124
+ ``` ruby
125
+ class PhotoRealizer
126
+ include JSONAPI::Realizer::Resource
115
127
 
116
- has :title
117
- has :src
128
+ type :photos, class_name: "Photo", adapter: :active_record_postgres_pagination
118
129
  end
119
130
  ```
120
131
 
@@ -33,6 +33,8 @@ module JSONAPI
33
33
  raise(ArgumentError, "need to provide a Adapter#write_relationships interface") unless respond_to?(:write_relationships)
34
34
  raise(ArgumentError, "need to provide a Adapter#include_relationships interface") unless respond_to?(:include_relationships)
35
35
  raise(ArgumentError, "need to provide a Adapter#paginate interface") unless respond_to?(:paginate)
36
+ raise(ArgumentError, "need to provide a Adapter#sorting interface") unless respond_to?(:sorting)
37
+ raise(ArgumentError, "need to provide a Adapter#filtering interface") unless respond_to?(:filtering)
36
38
  end
37
39
  end
38
40
  end
@@ -322,7 +322,7 @@ module JSONAPI
322
322
  end
323
323
 
324
324
  def attribute(name)
325
- configuration.attributes.fetch(name.to_sym){raise(Error::ResourceRelationshipNotFound, name: name, realizer: self)}
325
+ configuration.attributes.fetch(name.to_sym){raise(Error::ResourceAttributeNotFound, name: name, realizer: self)}
326
326
  end
327
327
 
328
328
  def relation(name)
@@ -1,5 +1,5 @@
1
1
  module JSONAPI
2
2
  module Realizer
3
- VERSION = "6.0.0.rc1"
3
+ VERSION = "6.0.0.rc2"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsonapi-realizer
3
3
  version: !ruby/object:Gem::Version
4
- version: 6.0.0.rc1
4
+ version: 6.0.0.rc2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kurtis Rainbolt-Greene
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-12-29 00:00:00.000000000 Z
11
+ date: 2019-02-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rubocop