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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5ef5a2774a142651a244f17d207b850cf66d330b
4
- data.tar.gz: b5c11f5d72a1f2a6845724ed1d6dbf9b1dea1b08
3
+ metadata.gz: a194fbba0a3a93907791c00696374006422a2e50
4
+ data.tar.gz: 850c833e23cee57c7f2b80a489d9840c79b7e88e
5
5
  SHA512:
6
- metadata.gz: 11bf5be25532c008fe7affe61c637ce32a3a24ef5c41149bcc8880e8833d44d96a7cf6ebfd0e6fe7147e87689ea54845d7e38a75f25ff010055dc66090b4d37a
7
- data.tar.gz: 1586385a58f932d1adf22d1d97fcc8d18e5ab949d225b193bd2923bd525ee5d09525dd51b3004be2f6a7e2c364e686e9a2b1ca7e20f7202d5ab4cda6a5c9711c
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
- to create a presenter file for one of your models. This will inherit from ApplicationPresenter, example shown below:
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 automatically inherits a dynamic initialize method which sets an instance variable. You also inherit a helper method dynamically named after the model too.
62
- So you DO NOT have to define this yourself:
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
- def initialize(user)
65
- @user = user
66
- end
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
- This also comes with with some error handling and some other code. So don't worry about this.
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
- super
81
- # assigning your variable to the user method gives it access to the inherited code.
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
- This means you can define methods in your presenter using the instance variable named after your model.
87
- Let's imagine our User model has a first_name and last_name field and we want to define #name. All we would have to do is:
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
- now you can initialize a presenter. There are 2 methods for this, either directly initialize the object as usual with:
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
@@ -2,24 +2,20 @@ module Presenter
2
2
  class Base
3
3
  include Presenter::Naming
4
4
 
5
- def initialize(model)
6
- @model = model
7
- generate_model_instance_getter
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
- @model.send(method, *args, &block)
14
+ subject.send(method, *args, &block)
19
15
  end
20
16
 
21
17
  def respond_to_missing?(method, include_all = false)
22
- @model.respond_to?(method, include_all)
18
+ subject.respond_to?(method, include_all)
23
19
  end
24
20
  end
25
21
  end
@@ -1,3 +1,3 @@
1
1
  module Presenter
2
- VERSION = '0.1.0'
2
+ VERSION = '1.0.0'
3
3
  end
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: 0.1.0
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-07-30 00:00:00.000000000 Z
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
- description: Keep code neat and abstract view & presenter logic. Adding a simple separation
57
- of concerns when creating your models with an easy API.
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: Easily implement the presenter design pattern in rails projects.
117
+ summary: Presenter design pattern for Rails.
105
118
  test_files: []