strong_presenter 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +18 -0
- data/Gemfile +4 -0
- data/LICENSE +179 -0
- data/README.md +159 -0
- data/Rakefile +1 -0
- data/lib/strong_presenter/base.rb +217 -0
- data/lib/strong_presenter/helper_proxy.rb +16 -0
- data/lib/strong_presenter/railtie.rb +9 -0
- data/lib/strong_presenter/version.rb +3 -0
- data/lib/strong_presenter.rb +7 -0
- data/strong_presenter.gemspec +19 -0
- metadata +61 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,179 @@
|
|
1
|
+
Copyright (c) 2013 Ronald Ping Man Chan
|
2
|
+
|
3
|
+
The Source Code Form referred to in this LICENSE file applies to all files stored within the same directory as this file, or in a subdirectory of the directory of this file, unless it is explicitly licensed or released otherwise. The official source of this software is found at https://github.com/ronalchn/.
|
4
|
+
|
5
|
+
This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
6
|
+
|
7
|
+
=============================================
|
8
|
+
A copy of the Mozilla Public License follows.
|
9
|
+
=============================================
|
10
|
+
|
11
|
+
Mozilla Public License
|
12
|
+
Version 2.0
|
13
|
+
|
14
|
+
1. Definitions
|
15
|
+
|
16
|
+
1.1. “Contributor”
|
17
|
+
means each individual or legal entity that creates, contributes to the creation of, or owns Covered Software.
|
18
|
+
|
19
|
+
1.2. “Contributor Version”
|
20
|
+
means the combination of the Contributions of others (if any) used by a Contributor and that particular Contributor’s Contribution.
|
21
|
+
|
22
|
+
1.3. “Contribution”
|
23
|
+
means Covered Software of a particular Contributor.
|
24
|
+
|
25
|
+
1.4. “Covered Software”
|
26
|
+
means Source Code Form to which the initial Contributor has attached the notice in Exhibit A, the Executable Form of such Source Code Form, and Modifications of such Source Code Form, in each case including portions thereof.
|
27
|
+
|
28
|
+
1.5. “Incompatible With Secondary Licenses”
|
29
|
+
means
|
30
|
+
|
31
|
+
that the initial Contributor has attached the notice described in Exhibit B to the Covered Software; or
|
32
|
+
|
33
|
+
that the Covered Software was made available under the terms of version 1.1 or earlier of the License, but not also under the terms of a Secondary License.
|
34
|
+
|
35
|
+
1.6. “Executable Form”
|
36
|
+
means any form of the work other than Source Code Form.
|
37
|
+
|
38
|
+
1.7. “Larger Work”
|
39
|
+
means a work that combines Covered Software with other material, in a separate file or files, that is not Covered Software.
|
40
|
+
|
41
|
+
1.8. “License”
|
42
|
+
means this document.
|
43
|
+
|
44
|
+
1.9. “Licensable”
|
45
|
+
means having the right to grant, to the maximum extent possible, whether at the time of the initial grant or subsequently, any and all of the rights conveyed by this License.
|
46
|
+
|
47
|
+
1.10. “Modifications”
|
48
|
+
means any of the following:
|
49
|
+
|
50
|
+
any file in Source Code Form that results from an addition to, deletion from, or modification of the contents of Covered Software; or
|
51
|
+
|
52
|
+
any new file in Source Code Form that contains any Covered Software.
|
53
|
+
|
54
|
+
1.11. “Patent Claims” of a Contributor
|
55
|
+
means any patent claim(s), including without limitation, method, process, and apparatus claims, in any patent Licensable by such Contributor that would be infringed, but for the grant of the License, by the making, using, selling, offering for sale, having made, import, or transfer of either its Contributions or its Contributor Version.
|
56
|
+
|
57
|
+
1.12. “Secondary License”
|
58
|
+
means either the GNU General Public License, Version 2.0, the GNU Lesser General Public License, Version 2.1, the GNU Affero General Public License, Version 3.0, or any later versions of those licenses.
|
59
|
+
|
60
|
+
1.13. “Source Code Form”
|
61
|
+
means the form of the work preferred for making modifications.
|
62
|
+
|
63
|
+
1.14. “You” (or “Your”)
|
64
|
+
means an individual or a legal entity exercising rights under this License. For legal entities, “You” includes any entity that controls, is controlled by, or is under common control with You. For purposes of this definition, “control” means (a) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (b) ownership of more than fifty percent (50%) of the outstanding shares or beneficial ownership of such entity.
|
65
|
+
|
66
|
+
2. License Grants and Conditions
|
67
|
+
|
68
|
+
2.1. Grants
|
69
|
+
|
70
|
+
Each Contributor hereby grants You a world-wide, royalty-free, non-exclusive license:
|
71
|
+
|
72
|
+
under intellectual property rights (other than patent or trademark) Licensable by such Contributor to use, reproduce, make available, modify, display, perform, distribute, and otherwise exploit its Contributions, either on an unmodified basis, with Modifications, or as part of a Larger Work; and
|
73
|
+
|
74
|
+
under Patent Claims of such Contributor to make, use, sell, offer for sale, have made, import, and otherwise transfer either its Contributions or its Contributor Version.
|
75
|
+
|
76
|
+
2.2. Effective Date
|
77
|
+
|
78
|
+
The licenses granted in Section 2.1 with respect to any Contribution become effective for each Contribution on the date the Contributor first distributes such Contribution.
|
79
|
+
|
80
|
+
2.3. Limitations on Grant Scope
|
81
|
+
|
82
|
+
The licenses granted in this Section 2 are the only rights granted under this License. No additional rights or licenses will be implied from the distribution or licensing of Covered Software under this License. Notwithstanding Section 2.1(b) above, no patent license is granted by a Contributor:
|
83
|
+
|
84
|
+
for any code that a Contributor has removed from Covered Software; or
|
85
|
+
|
86
|
+
for infringements caused by: (i) Your and any other third party’s modifications of Covered Software, or (ii) the combination of its Contributions with other software (except as part of its Contributor Version); or
|
87
|
+
|
88
|
+
under Patent Claims infringed by Covered Software in the absence of its Contributions.
|
89
|
+
|
90
|
+
This License does not grant any rights in the trademarks, service marks, or logos of any Contributor (except as may be necessary to comply with the notice requirements in Section 3.4).
|
91
|
+
|
92
|
+
2.4. Subsequent Licenses
|
93
|
+
|
94
|
+
No Contributor makes additional grants as a result of Your choice to distribute the Covered Software under a subsequent version of this License (see Section 10.2) or under the terms of a Secondary License (if permitted under the terms of Section 3.3).
|
95
|
+
|
96
|
+
2.5. Representation
|
97
|
+
|
98
|
+
Each Contributor represents that the Contributor believes its Contributions are its original creation(s) or it has sufficient rights to grant the rights to its Contributions conveyed by this License.
|
99
|
+
|
100
|
+
2.6. Fair Use
|
101
|
+
|
102
|
+
This License is not intended to limit any rights You have under applicable copyright doctrines of fair use, fair dealing, or other equivalents.
|
103
|
+
|
104
|
+
2.7. Conditions
|
105
|
+
|
106
|
+
Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted in Section 2.1.
|
107
|
+
|
108
|
+
3. Responsibilities
|
109
|
+
|
110
|
+
3.1. Distribution of Source Form
|
111
|
+
|
112
|
+
All distribution of Covered Software in Source Code Form, including any Modifications that You create or to which You contribute, must be under the terms of this License. You must inform recipients that the Source Code Form of the Covered Software is governed by the terms of this License, and how they can obtain a copy of this License. You may not attempt to alter or restrict the recipients’ rights in the Source Code Form.
|
113
|
+
|
114
|
+
3.2. Distribution of Executable Form
|
115
|
+
|
116
|
+
If You distribute Covered Software in Executable Form then:
|
117
|
+
|
118
|
+
such Covered Software must also be made available in Source Code Form, as described in Section 3.1, and You must inform recipients of the Executable Form how they can obtain a copy of such Source Code Form by reasonable means in a timely manner, at a charge no more than the cost of distribution to the recipient; and
|
119
|
+
|
120
|
+
You may distribute such Executable Form under the terms of this License, or sublicense it under different terms, provided that the license for the Executable Form does not attempt to limit or alter the recipients’ rights in the Source Code Form under this License.
|
121
|
+
|
122
|
+
3.3. Distribution of a Larger Work
|
123
|
+
|
124
|
+
You may create and distribute a Larger Work under terms of Your choice, provided that You also comply with the requirements of this License for the Covered Software. If the Larger Work is a combination of Covered Software with a work governed by one or more Secondary Licenses, and the Covered Software is not Incompatible With Secondary Licenses, this License permits You to additionally distribute such Covered Software under the terms of such Secondary License(s), so that the recipient of the Larger Work may, at their option, further distribute the Covered Software under the terms of either this License or such Secondary License(s).
|
125
|
+
|
126
|
+
3.4. Notices
|
127
|
+
|
128
|
+
You may not remove or alter the substance of any license notices (including copyright notices, patent notices, disclaimers of warranty, or limitations of liability) contained within the Source Code Form of the Covered Software, except that You may alter any license notices to the extent required to remedy known factual inaccuracies.
|
129
|
+
|
130
|
+
3.5. Application of Additional Terms
|
131
|
+
|
132
|
+
You may choose to offer, and to charge a fee for, warranty, support, indemnity or liability obligations to one or more recipients of Covered Software. However, You may do so only on Your own behalf, and not on behalf of any Contributor. You must make it absolutely clear that any such warranty, support, indemnity, or liability obligation is offered by You alone, and You hereby agree to indemnify every Contributor for any liability incurred by such Contributor as a result of warranty, support, indemnity or liability terms You offer. You may include additional disclaimers of warranty and limitations of liability specific to any jurisdiction.
|
133
|
+
|
134
|
+
4. Inability to Comply Due to Statute or Regulation
|
135
|
+
|
136
|
+
If it is impossible for You to comply with any of the terms of this License with respect to some or all of the Covered Software due to statute, judicial order, or regulation then You must: (a) comply with the terms of this License to the maximum extent possible; and (b) describe the limitations and the code they affect. Such description must be placed in a text file included with all distributions of the Covered Software under this License. Except to the extent prohibited by statute or regulation, such description must be sufficiently detailed for a recipient of ordinary skill to be able to understand it.
|
137
|
+
|
138
|
+
5. Termination
|
139
|
+
|
140
|
+
5.1. The rights granted under this License will terminate automatically if You fail to comply with any of its terms. However, if You become compliant, then the rights granted under this License from a particular Contributor are reinstated (a) provisionally, unless and until such Contributor explicitly and finally terminates Your grants, and (b) on an ongoing basis, if such Contributor fails to notify You of the non-compliance by some reasonable means prior to 60 days after You have come back into compliance. Moreover, Your grants from a particular Contributor are reinstated on an ongoing basis if such Contributor notifies You of the non-compliance by some reasonable means, this is the first time You have received notice of non-compliance with this License from such Contributor, and You become compliant prior to 30 days after Your receipt of the notice.
|
141
|
+
|
142
|
+
5.2. If You initiate litigation against any entity by asserting a patent infringement claim (excluding declaratory judgment actions, counter-claims, and cross-claims) alleging that a Contributor Version directly or indirectly infringes any patent, then the rights granted to You by any and all Contributors for the Covered Software under Section 2.1 of this License shall terminate.
|
143
|
+
|
144
|
+
5.3. In the event of termination under Sections 5.1 or 5.2 above, all end user license agreements (excluding distributors and resellers) which have been validly granted by You or Your distributors under this License prior to termination shall survive termination.
|
145
|
+
|
146
|
+
6. Disclaimer of Warranty
|
147
|
+
|
148
|
+
Covered Software is provided under this License on an “as is” basis, without warranty of any kind, either expressed, implied, or statutory, including, without limitation, warranties that the Covered Software is free of defects, merchantable, fit for a particular purpose or non-infringing. The entire risk as to the quality and performance of the Covered Software is with You. Should any Covered Software prove defective in any respect, You (not any Contributor) assume the cost of any necessary servicing, repair, or correction. This disclaimer of warranty constitutes an essential part of this License. No use of any Covered Software is authorized under this License except under this disclaimer.
|
149
|
+
|
150
|
+
7. Limitation of Liability
|
151
|
+
|
152
|
+
Under no circumstances and under no legal theory, whether tort (including negligence), contract, or otherwise, shall any Contributor, or anyone who distributes Covered Software as permitted above, be liable to You for any direct, indirect, special, incidental, or consequential damages of any character including, without limitation, damages for lost profits, loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses, even if such party shall have been informed of the possibility of such damages. This limitation of liability shall not apply to liability for death or personal injury resulting from such party’s negligence to the extent applicable law prohibits such limitation. Some jurisdictions do not allow the exclusion or limitation of incidental or consequential damages, so this exclusion and limitation may not apply to You.
|
153
|
+
|
154
|
+
8. Litigation
|
155
|
+
|
156
|
+
Any litigation relating to this License may be brought only in the courts of a jurisdiction where the defendant maintains its principal place of business and such litigation shall be governed by laws of that jurisdiction, without reference to its conflict-of-law provisions. Nothing in this Section shall prevent a party’s ability to bring cross-claims or counter-claims.
|
157
|
+
|
158
|
+
9. Miscellaneous
|
159
|
+
|
160
|
+
This License represents the complete agreement concerning the subject matter hereof. If any provision of this License is held to be unenforceable, such provision shall be reformed only to the extent necessary to make it enforceable. Any law or regulation which provides that the language of a contract shall be construed against the drafter shall not be used to construe this License against a Contributor.
|
161
|
+
|
162
|
+
10. Versions of the License
|
163
|
+
|
164
|
+
10.1. New Versions
|
165
|
+
|
166
|
+
Mozilla Foundation is the license steward. Except as provided in Section 10.3, no one other than the license steward has the right to modify or publish new versions of this License. Each version will be given a distinguishing version number.
|
167
|
+
|
168
|
+
10.2. Effect of New Versions
|
169
|
+
|
170
|
+
You may distribute the Covered Software under the terms of the version of the License under which You originally received the Covered Software, or under the terms of any subsequent version published by the license steward.
|
171
|
+
|
172
|
+
10.3. Modified Versions
|
173
|
+
|
174
|
+
If you create software not governed by this License, and you want to create a new license for such software, you may create and use a modified version of this License if you rename the license and remove any references to the name of the license steward (except to note that such modified license differs from this License).
|
175
|
+
|
176
|
+
10.4. Distributing Source Code Form that is Incompatible With Secondary Licenses
|
177
|
+
|
178
|
+
If You choose to distribute Source Code Form that is Incompatible With Secondary Licenses under the terms of this version of the License, the notice described in Exhibit B of this License must be attached.
|
179
|
+
|
data/README.md
ADDED
@@ -0,0 +1,159 @@
|
|
1
|
+
# StrongPresenter
|
2
|
+
|
3
|
+
StrongPresenter lets you add presenters to your application, along with strong_parameters-inspired permit logic to handle mass presentations, where each user may have permision to view different fields.
|
4
|
+
|
5
|
+
Adding an explicit `permit` interface allows authorization logic to be pushed back from the view to the controller where it probably belongs. This allows the view to concentrate on laying out the webpage, rather than deciding whether each of the components should be displayed.
|
6
|
+
|
7
|
+
When displaying a datatable, there is no need to call `can? :read_email_column, @user` in both the table headings, and as each row is displayed. The permission check can be performed once in the controller.
|
8
|
+
|
9
|
+
This gem puts presenters in `app/presenters`, not `app/decorators`, because not all decorators are presenters. It better separates the different class types in your application. Presenters should be providing a read-only interface to a model. Variable setters should not be delegated through the presenter. On the other hand, decorators add features to a model, and can very well delegate setters methods to the model. However, they may be used for other reasons, such as in domain-specific logic. We don't want to mix presenter classes with decorator classes that may be used for domain-specific logic.
|
10
|
+
|
11
|
+
While there exist other presenter gems, we hope to provide a more natural interface to create your presenters. This gem is opinionated - presenters should be in `app/presenters`, read-only interfaces to the underlying models, and not mixed with general-purpose decorators.
|
12
|
+
|
13
|
+
## Installation
|
14
|
+
|
15
|
+
Requires Rails. Rails 3.2+ is supported (probably works on 3.0, 3.1 as well).
|
16
|
+
|
17
|
+
Add this line to your application's Gemfile:
|
18
|
+
|
19
|
+
gem 'strong_presenter'
|
20
|
+
|
21
|
+
And then execute:
|
22
|
+
|
23
|
+
$ bundle
|
24
|
+
|
25
|
+
Or install it yourself as:
|
26
|
+
|
27
|
+
$ gem install strong_presenter
|
28
|
+
|
29
|
+
## Usage
|
30
|
+
|
31
|
+
Create a new presenter:
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
class ApplicationPresenter < StrongPresenter::Base
|
35
|
+
end
|
36
|
+
|
37
|
+
class UserPresenter < ApplicationPresenter
|
38
|
+
presents :user
|
39
|
+
delegate :username, :name, :email, to: :user
|
40
|
+
|
41
|
+
def avatar
|
42
|
+
h.tag :img, :src => user.avatar_url
|
43
|
+
end
|
44
|
+
end
|
45
|
+
```
|
46
|
+
|
47
|
+
Use the presenter on your object in your controller
|
48
|
+
|
49
|
+
```ruby
|
50
|
+
class UserController < ApplicationController
|
51
|
+
def show
|
52
|
+
@user = User.find(params[:id])
|
53
|
+
@user_presenter = UserPresenter.wrap(@user).permit *visible_attributes
|
54
|
+
end
|
55
|
+
|
56
|
+
private
|
57
|
+
def visible_attributes
|
58
|
+
[:username] + (can?(:read_private_details, @user) ? [:name, :email] : [])
|
59
|
+
end
|
60
|
+
end
|
61
|
+
```
|
62
|
+
|
63
|
+
Use the presenter in the view:
|
64
|
+
|
65
|
+
```ruby
|
66
|
+
Avatar: <%= @user_presenter.avatar %>
|
67
|
+
```
|
68
|
+
|
69
|
+
Or use mass presentation to present multiple fields/attributes at once. Notice that if `visible_attributes`
|
70
|
+
does not include name or email, they will not show. The advantage is that authorization logic
|
71
|
+
remains in the controller where it arguably belongs.
|
72
|
+
|
73
|
+
```erb
|
74
|
+
<% @user_presenter.presents :username, :name, :email do |key, value| %>
|
75
|
+
<b><%= UserPresenter.label(key) %>:</b> <%= value %><br>
|
76
|
+
<% end %>
|
77
|
+
```
|
78
|
+
|
79
|
+
The `presents` method will only present those attributes which have been permitted. In contrast, a method
|
80
|
+
such as `avatar` on the presenter will not check if the attribute is permitted. To check this, the `presents`
|
81
|
+
method can be used. For example:
|
82
|
+
|
83
|
+
```erb
|
84
|
+
<% @user_presenter.presents(:avatar).each do |presented_avatar| %>
|
85
|
+
<%= presented_avatar %>
|
86
|
+
<% end %>
|
87
|
+
```
|
88
|
+
|
89
|
+
In this case, since `permit` was not passed `:avatar`, it will not be presented. To remove authorization checks
|
90
|
+
from mass presentations, simply call `permit!` on an instance of a presenter, or on the class (to disable for all instances).
|
91
|
+
|
92
|
+
Fields or attributes can be assigned a label, by calling `label` with a hash.
|
93
|
+
|
94
|
+
```ruby
|
95
|
+
class UserPresenter < ApplicationPresenter
|
96
|
+
...
|
97
|
+
label :email => "E-mail", :name => "Real Name"
|
98
|
+
end
|
99
|
+
```
|
100
|
+
|
101
|
+
Labels can be retrieved by using the field/attribute by calling `label` with the symbol(s):
|
102
|
+
|
103
|
+
```ruby
|
104
|
+
UserPresenter.label [:email, :username] # returns ["E-mail", "Username"]
|
105
|
+
```
|
106
|
+
|
107
|
+
If no label was explicitly assigned, it is simply converted to a string and humanized.
|
108
|
+
|
109
|
+
Labels can be useful when used in a datatable to display collections. The controller can wrap
|
110
|
+
a collection using `wrap_each`:
|
111
|
+
|
112
|
+
```ruby
|
113
|
+
@users_presenter = UserPresenter.wrap_each(@users).permit( *visible_attributes )
|
114
|
+
```
|
115
|
+
|
116
|
+
Then the view can use each presenter, to display only the columns a user is permitted to view.
|
117
|
+
|
118
|
+
```erb
|
119
|
+
<% fields = [:username, :name, :email] %>
|
120
|
+
<table>
|
121
|
+
<tr>
|
122
|
+
<% @users_presenter.filter( *fields ).to_labels.each do |label| %>
|
123
|
+
<th><%= label %></th>
|
124
|
+
<% end %>
|
125
|
+
</tr>
|
126
|
+
<% @users_presenter.each do |user_presenter| %>
|
127
|
+
<tr>
|
128
|
+
<% user_presenter.presents( *fields ).each do |value| %>
|
129
|
+
<%= content_tag :td, value %>
|
130
|
+
<% end %>
|
131
|
+
</tr>
|
132
|
+
<% end %>
|
133
|
+
</table>
|
134
|
+
```
|
135
|
+
|
136
|
+
Here, we use filter to check which of the columns are visible, just like `presents` does. It returns an
|
137
|
+
array of only the visible columns, and `to_labels` is a method added to the array to allow conversion to label form.
|
138
|
+
|
139
|
+
This also allows mass presentation based on GET parameter input, for example:
|
140
|
+
|
141
|
+
```erb
|
142
|
+
<% user_presenter.presents( params[:columns].split(',') ).each do |value| %><%= content_tag :td, value %><% end %>
|
143
|
+
```
|
144
|
+
|
145
|
+
Because of the `permit` checks, there is no danger that private information will be revealed.
|
146
|
+
|
147
|
+
## License
|
148
|
+
|
149
|
+
Mozilla Public License Version 2.0
|
150
|
+
|
151
|
+
Free to use in open source or proprietary applications, with the caveat that any source code changes or additions to files covered under MPL V2 can only be distributed under the MPL V2 or secondary license as described in the full text.
|
152
|
+
|
153
|
+
## Contributing
|
154
|
+
|
155
|
+
1. Fork it
|
156
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
157
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
158
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
159
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,217 @@
|
|
1
|
+
# Copyright (c) 2013 Ronald Ping Man Chan
|
2
|
+
#
|
3
|
+
# This Source Code Form is subject to the terms of the Mozilla Public
|
4
|
+
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
5
|
+
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
6
|
+
|
7
|
+
module StrongPresenter
|
8
|
+
module CollectionHelper
|
9
|
+
# Enumerates collection to call permit on each presenter
|
10
|
+
def permit *fields
|
11
|
+
self.each { |presenter| presenter.permit *fields }
|
12
|
+
self
|
13
|
+
end
|
14
|
+
|
15
|
+
# Enumerates collection to call permit! on each presenter
|
16
|
+
def permit!
|
17
|
+
self.each { |presenter| presenter.permit! }
|
18
|
+
self
|
19
|
+
end
|
20
|
+
|
21
|
+
# Checks if fields are visible on first presenter in collection
|
22
|
+
def filter *args
|
23
|
+
self.first.filter *args
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
class Base
|
28
|
+
# Constructs the presenter, taking 1 argument for the object being wrapped
|
29
|
+
def initialize(object)
|
30
|
+
@object = object
|
31
|
+
end
|
32
|
+
|
33
|
+
# Wraps a model using a new instance of the presenter, and returns the instance. For example:
|
34
|
+
#
|
35
|
+
# user_presenter = UserPresenter.wrap :user
|
36
|
+
#
|
37
|
+
# A block can also be passed to use the presenter. For example:
|
38
|
+
#
|
39
|
+
# <% UserPresenter.wrap @user do |user_presenter| %>
|
40
|
+
# Username: <%= user_presenter.username %>
|
41
|
+
# <% end %>
|
42
|
+
#
|
43
|
+
def self.wrap *objects
|
44
|
+
presenter = self.new *objects
|
45
|
+
yield presenter if block_given?
|
46
|
+
presenter
|
47
|
+
end
|
48
|
+
|
49
|
+
# Wraps each model in a collection with the presenter. A block can also be
|
50
|
+
# passed to use each presenter immediately. For example:
|
51
|
+
#
|
52
|
+
# <ul>
|
53
|
+
# <% UserPresenter.wrap_each @users do |user_presenter| %>
|
54
|
+
# <li><%= user_presenter.username %></li>
|
55
|
+
# <% end %>
|
56
|
+
# </ul>
|
57
|
+
#
|
58
|
+
def self.wrap_each collection
|
59
|
+
collection.map! do |object|
|
60
|
+
self.wrap object do |presenter|
|
61
|
+
yield presenter if block_given?
|
62
|
+
end
|
63
|
+
end
|
64
|
+
collection.send :extend, StrongPresenter::CollectionHelper
|
65
|
+
collection
|
66
|
+
end
|
67
|
+
|
68
|
+
# Performs mass presentation - if it is allowed, subject to `permit`. To permit all without checking, call `permit!` first.
|
69
|
+
#
|
70
|
+
# Presents and returns the result of each field in the argument list. If a block is given, then each result
|
71
|
+
# is passed to the block. Each field is presented by calling the method on the presenter.
|
72
|
+
#
|
73
|
+
# user_presenter.presents :username, :email # returns [user_presenter.username, user_presenter.email]
|
74
|
+
#
|
75
|
+
# Or with two arguments, the name of the field is passed first:
|
76
|
+
#
|
77
|
+
# <ul>
|
78
|
+
# <% user_presenter.presents :username, :email, :address do |field, value| %>
|
79
|
+
# <li><%= field.capitalize %>: <% value %></li>
|
80
|
+
# <% end %>
|
81
|
+
# </ul>
|
82
|
+
#
|
83
|
+
# If only the presented value is desired, use `each`:
|
84
|
+
#
|
85
|
+
# <% user_presenter.presents(:username, :email).each do |value| %>
|
86
|
+
# <td><%= value %></td>
|
87
|
+
# <% end %>
|
88
|
+
#
|
89
|
+
# A field can have arguments in an array:
|
90
|
+
#
|
91
|
+
# user_presenter.presents :username, [:notifications, :unread] # returns [user_presenter.username, user_presenter.notifications(:unread)]
|
92
|
+
#
|
93
|
+
# Notice that this interface allows you to concisely put authorization logic in the controller, with a dumb view layer:
|
94
|
+
#
|
95
|
+
# # app/controllers/users_controller.rb
|
96
|
+
# class UsersController < ApplicationController
|
97
|
+
# def visible_params
|
98
|
+
# @visible_params ||= begin
|
99
|
+
# field = [:username]
|
100
|
+
# field << :email if can? :read_email, @user
|
101
|
+
# field << :edit_link if can? :edit, @user
|
102
|
+
# end
|
103
|
+
# end
|
104
|
+
# def show
|
105
|
+
# @users_presenter = UserPresenter.wrap_each(User.all).permit!
|
106
|
+
# end
|
107
|
+
# end
|
108
|
+
#
|
109
|
+
# # app/views/users/show.html.erb
|
110
|
+
# <table>
|
111
|
+
# <tr>
|
112
|
+
# <% visible_params.each do |field| %>
|
113
|
+
# <th><%= field %></th>
|
114
|
+
# <% end %>
|
115
|
+
# </tr>
|
116
|
+
# <% @users_presenter.each do |user_presenter| %>
|
117
|
+
# <tr>
|
118
|
+
# <% user_presenter.presents(*visible_params).each do |value| %>
|
119
|
+
# <td><%= value %></td>
|
120
|
+
# <% end %>
|
121
|
+
# </tr>
|
122
|
+
# <% end %>
|
123
|
+
# </table>
|
124
|
+
#
|
125
|
+
def presents *fields, &block
|
126
|
+
process_fields(fields).map do |args|
|
127
|
+
value = self.public_send *args
|
128
|
+
yield args[0], value if block_given?
|
129
|
+
value
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
# Checks which fields are visible according to what is permitted. An array is returned.
|
134
|
+
# The array can be converted to labels using `to_labels`
|
135
|
+
#
|
136
|
+
def filter *fields
|
137
|
+
fields = process_fields(fields).map(&:first)
|
138
|
+
presenter_class = self.class
|
139
|
+
(class << fields; self; end).class_eval do
|
140
|
+
define_method :to_labels do
|
141
|
+
presenter_class.label fields
|
142
|
+
end
|
143
|
+
end
|
144
|
+
fields
|
145
|
+
end
|
146
|
+
|
147
|
+
# Pass a hash to set field labels { field => label}
|
148
|
+
# Pass an array to retrieve labels for field symbols. If no label is set, the return
|
149
|
+
# value is the humanized field by default.
|
150
|
+
#
|
151
|
+
def self.label fields
|
152
|
+
@labels ||= {}
|
153
|
+
if fields.class == Hash
|
154
|
+
@labels.merge!(fields)
|
155
|
+
else
|
156
|
+
labels = Array(fields).map { |field| @labels[field] || field.to_s.humanize }
|
157
|
+
return labels if fields.respond_to? :map
|
158
|
+
labels.first
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
# Sets fields which will be permitted. May be invoked multiple times.
|
163
|
+
#
|
164
|
+
def permit *fields
|
165
|
+
self.permitted_attributes.merge fields if permitted_attributes != :all
|
166
|
+
self
|
167
|
+
end
|
168
|
+
|
169
|
+
# Permits all fields
|
170
|
+
#
|
171
|
+
def permit!
|
172
|
+
@permitted_attributes = :all
|
173
|
+
self
|
174
|
+
end
|
175
|
+
|
176
|
+
protected
|
177
|
+
def permitted_attributes
|
178
|
+
@permitted_attributes ||= Set.new
|
179
|
+
end
|
180
|
+
|
181
|
+
private
|
182
|
+
def process_fields fields
|
183
|
+
fields.map! do |field|
|
184
|
+
field = Array(field)
|
185
|
+
field[0] = field[0].to_sym
|
186
|
+
field
|
187
|
+
end
|
188
|
+
fields.select! { |field| permitted_attributes.include? field[0] } if permitted_attributes != :all
|
189
|
+
fields
|
190
|
+
end
|
191
|
+
|
192
|
+
# Sets the model presented by the class
|
193
|
+
#
|
194
|
+
def self.presents name
|
195
|
+
define_method name do
|
196
|
+
@object
|
197
|
+
end
|
198
|
+
end
|
199
|
+
|
200
|
+
# Permits all fields in the presenter for mass presentation
|
201
|
+
#
|
202
|
+
def self.permit!
|
203
|
+
define_method(:permitted_attributes){:all}
|
204
|
+
end
|
205
|
+
|
206
|
+
def object
|
207
|
+
@object
|
208
|
+
end
|
209
|
+
|
210
|
+
# Access to view helpers
|
211
|
+
#
|
212
|
+
def h
|
213
|
+
@helper ||= StrongPresenter::HelperProxy.new
|
214
|
+
end
|
215
|
+
|
216
|
+
end
|
217
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module StrongPresenter
|
2
|
+
class HelperProxy < ActionView::Base
|
3
|
+
include Rails.application.routes.url_helpers
|
4
|
+
|
5
|
+
def method_missing method, *args, &block
|
6
|
+
if ApplicationController.helpers.respond_to? method
|
7
|
+
ApplicationController.helpers.public_send method, *args, &block
|
8
|
+
else
|
9
|
+
super
|
10
|
+
end
|
11
|
+
end
|
12
|
+
def respond_to? method, include_all = false
|
13
|
+
ApplicationController.helpers.respond_to?(method, include_all) or super
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'strong_presenter/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "strong_presenter"
|
8
|
+
gem.version = StrongPresenter::VERSION
|
9
|
+
gem.authors = ["Ronald Chan"]
|
10
|
+
gem.email = ["ronalchn@gmail.com"]
|
11
|
+
gem.description = %q{strong_presenter lets you add presenters to your application, along with strong_parameters-inspired permit logic to handle mass presentations, where each user may have permision to view different fields}
|
12
|
+
gem.summary = %q{strong_presenter lets you add presenters to your application, along with strong_parameters-inspired permit logic to handle mass presentations, where each user may have permision to view different fields}
|
13
|
+
gem.homepage = "https://github.com/ronalchn/strong_presenter"
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
end
|
metadata
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: strong_presenter
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Ronald Chan
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-10-10 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: strong_presenter lets you add presenters to your application, along with
|
15
|
+
strong_parameters-inspired permit logic to handle mass presentations, where each
|
16
|
+
user may have permision to view different fields
|
17
|
+
email:
|
18
|
+
- ronalchn@gmail.com
|
19
|
+
executables: []
|
20
|
+
extensions: []
|
21
|
+
extra_rdoc_files: []
|
22
|
+
files:
|
23
|
+
- .gitignore
|
24
|
+
- Gemfile
|
25
|
+
- LICENSE
|
26
|
+
- README.md
|
27
|
+
- Rakefile
|
28
|
+
- lib/strong_presenter.rb
|
29
|
+
- lib/strong_presenter/base.rb
|
30
|
+
- lib/strong_presenter/helper_proxy.rb
|
31
|
+
- lib/strong_presenter/railtie.rb
|
32
|
+
- lib/strong_presenter/version.rb
|
33
|
+
- strong_presenter.gemspec
|
34
|
+
homepage: https://github.com/ronalchn/strong_presenter
|
35
|
+
licenses: []
|
36
|
+
post_install_message:
|
37
|
+
rdoc_options: []
|
38
|
+
require_paths:
|
39
|
+
- lib
|
40
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
47
|
+
none: false
|
48
|
+
requirements:
|
49
|
+
- - ! '>='
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: '0'
|
52
|
+
requirements: []
|
53
|
+
rubyforge_project:
|
54
|
+
rubygems_version: 1.8.17
|
55
|
+
signing_key:
|
56
|
+
specification_version: 3
|
57
|
+
summary: strong_presenter lets you add presenters to your application, along with
|
58
|
+
strong_parameters-inspired permit logic to handle mass presentations, where each
|
59
|
+
user may have permision to view different fields
|
60
|
+
test_files: []
|
61
|
+
has_rdoc:
|