api_presenter 0.3.0 → 0.4.0
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 +4 -4
- data/CHANGELOG.md +4 -0
- data/README.md +20 -12
- data/lib/api_presenter/version.rb +1 -1
- data/lib/generators/api_presenter/config/USAGE +8 -1
- data/lib/generators/api_presenter/config/config_generator.rb +1 -1
- data/lib/generators/api_presenter/config/templates/{api_presenter_config.rb → config.rb} +0 -0
- data/lib/generators/api_presenter/presenter/USAGE +9 -0
- data/lib/generators/api_presenter/presenter/presenter_generator.rb +17 -0
- data/lib/generators/api_presenter/presenter/templates/application_presenter.rb +2 -0
- data/lib/generators/api_presenter/presenter/templates/presenter.rb +13 -0
- metadata +7 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0ac6c35bce2e92fb4729a274a36dd3e8def81451
|
4
|
+
data.tar.gz: ff79c2727b4dd200a6d8346d3362af4f689193fc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 419305783b53de4985e852d0d69a6d8a8f39dfbe9b5255e0b6fef4e6fab9d8832bf6a75b3d0ef575a372c3d6e83e4dce9377adaf05eceabb79e630da9a933269
|
7
|
+
data.tar.gz: a1edcba953b05ebe0d699db1d336dc49c70cdefd54129a25b57589b32498049d4478c9617ff38b2b925c4b7bbce8a4b43a05e44a8218d2584c2ad17df4875dbb
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -52,16 +52,22 @@ class User < ActiveRecord::Base
|
|
52
52
|
end
|
53
53
|
```
|
54
54
|
|
55
|
+
When requesting posts from the API, a client may also want any or all of the posts' categories, sub categories, and users. It may also want the current user's policies for the posts to generate correct UI.
|
56
|
+
|
55
57
|
### 0. Generate config file
|
56
58
|
|
57
59
|
`rails g api_presenter:config`
|
58
60
|
|
59
|
-
Generates a configuration file that allows you to override the default querystring params used by the `presenter` concern. More
|
61
|
+
Generates a configuration file that allows you to override the default querystring params used by the `presenter` concern. More configuration options coming soon.
|
60
62
|
|
61
63
|
### 1. Create your Presenter
|
62
64
|
|
65
|
+
Using the supplied generator, generate a presenter class for your ActiveRecord model. The generator will also generate a convenient `ApplicationApiPresenter` class for centralized methods.
|
66
|
+
|
67
|
+
`rails g api_presenter:presenter post`
|
68
|
+
|
63
69
|
```ruby
|
64
|
-
class PostPresenter <
|
70
|
+
class PostPresenter < ApiApplicationController
|
65
71
|
def associations_map
|
66
72
|
{
|
67
73
|
categories: { associations: { sub_category: :category } },
|
@@ -80,7 +86,7 @@ class PostPresenter < ApiPresenter::Base
|
|
80
86
|
end
|
81
87
|
```
|
82
88
|
|
83
|
-
Presenters
|
89
|
+
Presenters have three opt-in methods:
|
84
90
|
|
85
91
|
* `associations_map` The business-dictated includable resources for the ActiveRecord model (`Post`, in this case). Consists of the model name as key and traversal required to preload/load them. In most cases, the value of `associations` will correspond directly to associations on the primary model.
|
86
92
|
* `policy_methods` A list of Pundit policy methods to resolve for the primary collection if policies are requested.
|
@@ -90,7 +96,7 @@ Presenters can define up to three methods:
|
|
90
96
|
|
91
97
|
Your presentable collection can be an `ActiveRecord::Relation`, an array of records, or even a single record. Just call `present` on it from your controller action. The preloads will be performed, and the included collections/policies will be available in the `@presenter` instance variable.
|
92
98
|
|
93
|
-
The following querystring params are used by the supplied controller concern's `present` method:
|
99
|
+
The following configurable querystring params are used by the supplied controller concern's `present` method:
|
94
100
|
|
95
101
|
* `count [Boolean]` Pass true if you just want a count of the primary collection
|
96
102
|
* `policies [Boolean]` Pass true if you want to resolve policies for the primary collection records
|
@@ -107,6 +113,7 @@ class PostsController < ApplicationController
|
|
107
113
|
# GET /posts?include=categories,subCategories,users&policies=true
|
108
114
|
#
|
109
115
|
def index
|
116
|
+
authorize Post
|
110
117
|
posts = PostQuery.records(current_user, params)
|
111
118
|
present posts
|
112
119
|
end
|
@@ -115,8 +122,9 @@ class PostsController < ApplicationController
|
|
115
122
|
# GET /posts/:id?include=categories,subCategories,users&policies=true
|
116
123
|
#
|
117
124
|
def show
|
118
|
-
post = Post.find(params[:id])
|
119
|
-
|
125
|
+
@post = Post.find(params[:id])
|
126
|
+
authorize @post
|
127
|
+
present @post
|
120
128
|
end
|
121
129
|
end
|
122
130
|
```
|
@@ -125,11 +133,11 @@ end
|
|
125
133
|
|
126
134
|
How you ultimately render the primary collection and the data produced by ApiPresenter is up to you. `@presenter` has the following properties:
|
127
135
|
|
128
|
-
* `collection` The primary collection that was passed into the presenter.
|
129
|
-
* `total_count` When using Kaminari or another pagination method that defines a `total_count` property, returns unpaginated count. If the primary collection is not an `ActiveRecord::Relation`, simply returns the number of records.
|
130
|
-
* `included_collection_names` Convenience method that returns an array of included collecton model names.
|
131
|
-
* `included_collections` A hash of included collections, consisting of the model name and corresponding records.
|
132
|
-
* `policies` An array of resolved policies for the primary collection.
|
136
|
+
* `collection [Array<ActiveRecord::Base>]` The primary collection that was passed into the presenter. Empty if count requested.
|
137
|
+
* `total_count [Integer]` When using Kaminari or another pagination method that defines a `total_count` property, returns unpaginated count. If the primary collection is not an `ActiveRecord::Relation`, simply returns the number of records.
|
138
|
+
* `included_collection_names [Array<Symbol>]` Convenience method that returns an array of included collecton model names.
|
139
|
+
* `included_collections [Hash]` A hash of included collections, consisting of the model name and corresponding records.
|
140
|
+
* `policies [Array<Hash>]` An array of resolved policies for the primary collection.
|
133
141
|
|
134
142
|
Here's an example of how you might render this using JBduiler:
|
135
143
|
|
@@ -318,7 +326,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
318
326
|
|
319
327
|
## Contributing
|
320
328
|
|
321
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
329
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/uberllama/api_presenter.
|
322
330
|
|
323
331
|
|
324
332
|
## License
|
@@ -4,7 +4,7 @@ module ApiPresenter
|
|
4
4
|
source_root File.expand_path('../templates', __FILE__)
|
5
5
|
|
6
6
|
def copy_config_file
|
7
|
-
copy_file
|
7
|
+
copy_file('config.rb', 'config/initializers/api_presenter.rb')
|
8
8
|
end
|
9
9
|
end
|
10
10
|
end
|
File without changes
|
@@ -0,0 +1,9 @@
|
|
1
|
+
Description:
|
2
|
+
Generates an API presenter for an ActiveRecord model with the given name, and a base class ApplicationApiPresenter unless already exists.
|
3
|
+
|
4
|
+
Example:
|
5
|
+
rails generate api_presenter:presenter post
|
6
|
+
|
7
|
+
This will create:
|
8
|
+
app/presenters/application_api_presenter.rb
|
9
|
+
app/presenters/post_presenter.rb
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module ApiPresenter
|
2
|
+
module Generators
|
3
|
+
class PresenterGenerator < ::Rails::Generators::NamedBase
|
4
|
+
source_root File.expand_path('../templates', __FILE__)
|
5
|
+
|
6
|
+
def create_application_presenter
|
7
|
+
unless File.exist?('app/presenters/application_api_presenter.rb')
|
8
|
+
copy_file('application_presenter.rb', 'app/presenters/application_api_presenter.rb')
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def create_presenter
|
13
|
+
template('presenter.rb', File.join('app/presenters', class_path, "#{file_name}_presenter.rb"))
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: api_presenter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yuval Kordov
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: exe
|
11
11
|
cert_chain: []
|
12
|
-
date: 2016-11-
|
12
|
+
date: 2016-11-10 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
@@ -139,7 +139,11 @@ files:
|
|
139
139
|
- lib/api_presenter/version.rb
|
140
140
|
- lib/generators/api_presenter/config/USAGE
|
141
141
|
- lib/generators/api_presenter/config/config_generator.rb
|
142
|
-
- lib/generators/api_presenter/config/templates/
|
142
|
+
- lib/generators/api_presenter/config/templates/config.rb
|
143
|
+
- lib/generators/api_presenter/presenter/USAGE
|
144
|
+
- lib/generators/api_presenter/presenter/presenter_generator.rb
|
145
|
+
- lib/generators/api_presenter/presenter/templates/application_presenter.rb
|
146
|
+
- lib/generators/api_presenter/presenter/templates/presenter.rb
|
143
147
|
homepage: http://github.com/uberllama/api_presenter
|
144
148
|
licenses:
|
145
149
|
- MIT
|