presenter-rails 0.1.0 → 1.0.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/README.md +17 -45
- data/lib/generators/presenter/templates/model_presenter.template +2 -4
- data/lib/presenter/base.rb +6 -10
- data/lib/presenter/version.rb +1 -1
- metadata +18 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a194fbba0a3a93907791c00696374006422a2e50
|
4
|
+
data.tar.gz: 850c833e23cee57c7f2b80a489d9840c79b7e88e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 402427d29776317abd3578cb6b6877dcba195a5f6e51a62e3f58f99fc13b6c6aee157f9a0b9923a7db61ca00dbf9663cd0eb08341046792601551b6ba478e023
|
7
|
+
data.tar.gz: 26188dcddd364240037d724b95e4236bced96e1db189ad48494b2e9ed7fe38610187d612b60680db8047808c8574397fde421ecd5697407efd3a644f1ddf5905
|
data/README.md
CHANGED
@@ -51,55 +51,43 @@ After you run the install generator, run
|
|
51
51
|
$ rails g presenter User
|
52
52
|
```
|
53
53
|
|
54
|
-
|
54
|
+
this will create a presenter file for one of your models and will inherit from ApplicationPresenter, example shown below:
|
55
55
|
```ruby
|
56
56
|
# app/presenters/user_presenter.rb
|
57
57
|
class UserPresenter < ApplicationPresenter
|
58
58
|
end
|
59
59
|
```
|
60
60
|
|
61
|
-
Note: This
|
62
|
-
|
61
|
+
Note: This provides you with a getter method: #subject.
|
62
|
+
You can use #subject to acces the object you passed in when initializing the presenter. See below on how to do this.
|
63
63
|
```ruby
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
def user # Make sure you use the helper methods in your presenters in place of the instance variable.
|
69
|
-
@user
|
64
|
+
# app/presenters/user_presenter.rb
|
65
|
+
def full_name
|
66
|
+
subject.first_name + ' ' + subject.last_name
|
70
67
|
end
|
71
68
|
```
|
72
69
|
|
73
|
-
|
74
|
-
I'm showing it just so you know what to expect if you try overwrite initialize or the helper without super. If you wish to extend it
|
75
|
-
then remember to super or you will overwrite the default behaviour.
|
76
|
-
|
77
|
-
If you do wish to extend the initialize method / change the instance variable name, call super first. Next, define an instance variable and assign it to the model object using the helper method (named after the model name).
|
70
|
+
If you do wish to override the initialize method, call super as demonstrated below:
|
78
71
|
```ruby
|
79
|
-
def initialize(user)
|
80
|
-
|
81
|
-
|
82
|
-
@another_variable_name = self.user
|
83
|
-
end
|
84
|
-
```
|
72
|
+
def initialize(user, middle_name)
|
73
|
+
@user = user
|
74
|
+
@user.update middle_name: middle_name
|
85
75
|
|
86
|
-
|
87
|
-
|
88
|
-
```ruby
|
89
|
-
class UserPresenter < ApplicationPresenter
|
90
|
-
def name
|
91
|
-
"#{user.first_name} #{user.last_name}"
|
92
|
-
end
|
76
|
+
# Make sure to pass your object to super.
|
77
|
+
super user
|
93
78
|
end
|
94
79
|
```
|
95
80
|
|
96
|
-
|
81
|
+
## Initializing the presenter
|
82
|
+
Now you can initialize a presenter. There are 2 methods for this, either directly initialize the object as usual with:
|
97
83
|
```ruby
|
98
84
|
@user = UserPresenter.new(user)
|
99
85
|
```
|
100
86
|
or use our built in helper to accomplish the same thing:
|
101
87
|
```ruby
|
88
|
+
# The present helper will work with a single model or a collection
|
102
89
|
@user = present(user)
|
90
|
+
@users = present(User.all)
|
103
91
|
```
|
104
92
|
(More on this below)
|
105
93
|
|
@@ -114,7 +102,7 @@ end
|
|
114
102
|
```
|
115
103
|
|
116
104
|
Now the user instance variable has access to all methods belonging to it's presenter and the model being passed in.
|
117
|
-
This allows access to
|
105
|
+
This allows access to:
|
118
106
|
```ruby
|
119
107
|
@user.name
|
120
108
|
# which returns first name and last name like 'Sam Sargent'
|
@@ -146,21 +134,5 @@ This allows access to
|
|
146
134
|
@users = present(users)
|
147
135
|
```
|
148
136
|
|
149
|
-
### #instance getter
|
150
|
-
|
151
|
-
For each presenter you create, a helper method is defined to get the model object being passed in. This replaces the need to throw around
|
152
|
-
the instance variable. It's also faster as it allows the method delegation to not have to parse naming and fetch instance variables every time the inherited #method_missing is called.
|
153
|
-
|
154
|
-
shown using a User model as example
|
155
|
-
```ruby
|
156
|
-
# app/presenters/user_presenter.rb
|
157
|
-
class UserPresenter < ApplicationPresenter
|
158
|
-
# this has the method #user defined automatically to access the user object being passed in to the presenter
|
159
|
-
def name
|
160
|
-
"#{user.first_name} {user.last_name}"
|
161
|
-
end
|
162
|
-
end
|
163
|
-
```
|
164
|
-
|
165
137
|
## License
|
166
138
|
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
@@ -3,10 +3,8 @@ class <%= "#{model.singularize.camelcase}Presenter" %> < ApplicationPresenter
|
|
3
3
|
# Don't worry about the initialize method, it's already handled for you.
|
4
4
|
|
5
5
|
# You can now pass in an instance of <%= model.singularize.camelcase %> when you initialize this presenter
|
6
|
-
# and access it with the helper method
|
7
|
-
# #<%= model.singularize.underscore %>
|
8
|
-
# instead of an instance variable when writing your methods
|
6
|
+
# and access it with the helper method: subject
|
9
7
|
|
10
8
|
# Write methods for this presenter in here using the helper method
|
11
9
|
<%- end -%>
|
12
|
-
end
|
10
|
+
end
|
data/lib/presenter/base.rb
CHANGED
@@ -2,24 +2,20 @@ module Presenter
|
|
2
2
|
class Base
|
3
3
|
include Presenter::Naming
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
5
|
+
attr_reader :subject
|
6
|
+
|
7
|
+
def initialize(subject)
|
8
|
+
@subject = subject
|
8
9
|
end
|
9
10
|
|
10
11
|
protected
|
11
|
-
def generate_model_instance_getter
|
12
|
-
define_singleton_method model_object_name_from_presenter(@model) do
|
13
|
-
@model
|
14
|
-
end
|
15
|
-
end
|
16
12
|
|
17
13
|
def method_missing(method, *args, &block)
|
18
|
-
|
14
|
+
subject.send(method, *args, &block)
|
19
15
|
end
|
20
16
|
|
21
17
|
def respond_to_missing?(method, include_all = false)
|
22
|
-
|
18
|
+
subject.respond_to?(method, include_all)
|
23
19
|
end
|
24
20
|
end
|
25
21
|
end
|
data/lib/presenter/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: presenter-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sam Sargent
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2017-
|
12
|
+
date: 2017-11-25 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
@@ -53,8 +53,21 @@ dependencies:
|
|
53
53
|
- - ">="
|
54
54
|
- !ruby/object:Gem::Version
|
55
55
|
version: '0'
|
56
|
-
|
57
|
-
|
56
|
+
- !ruby/object:Gem::Dependency
|
57
|
+
name: pry
|
58
|
+
requirement: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
type: :development
|
64
|
+
prerelease: false
|
65
|
+
version_requirements: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
description: Presenter design pattern for Rails.
|
58
71
|
email:
|
59
72
|
- samsarge@hotmail.co.uk
|
60
73
|
- finnfrancis123@gmail.com
|
@@ -101,5 +114,5 @@ rubyforge_project:
|
|
101
114
|
rubygems_version: 2.4.5.1
|
102
115
|
signing_key:
|
103
116
|
specification_version: 4
|
104
|
-
summary:
|
117
|
+
summary: Presenter design pattern for Rails.
|
105
118
|
test_files: []
|