power-types 0.3.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1 @@
1
+ 2.5
@@ -1,8 +1,8 @@
1
1
  sudo: false
2
2
  language: ruby
3
3
  rvm:
4
- - 2.3.1
5
- before_install: gem install bundler -v 1.12.4
4
+ - 2.5.0
5
+ before_install: gem install bundler -v 2.1.4
6
6
  deploy:
7
7
  provider: rubygems
8
8
  api_key:
data/README.md CHANGED
@@ -8,7 +8,7 @@ Rails pattern enforcing types used by the Platanus team.
8
8
  In Rails projects, Platanus encourages to use classes beyond models and controllers to hold the app's logic.
9
9
  These powerful types proposed are Services, Commands, Observers, Utils and Values.
10
10
 
11
- For a deeper understanding about the usage of these patterns, feel welcome to read the [related post in Platanus Blog](https://blog.platan.us/services-commands-y-otros-poderosos-patrones-en-rails) (in spanish).
11
+ For a deeper understanding about the usage of these patterns, feel welcome to read the [related post in Platanus Blog](https://blog.platan.us/services-commands-y-otros-poderosos-patrones-en-rails-27c2d3aa7c2e) (in spanish).
12
12
 
13
13
  The goal aimed with this gem is to go further, and not just apply this patterns over POROs (plain simple ruby classes). The gem provides an special structure and syntax to create and run services, commands and more, with ease.
14
14
 
@@ -229,7 +229,7 @@ Wizard.create!(name: "Gandalf", villian: "Sauron") #=> This action will trigger
229
229
 
230
230
  ### Values and Utils
231
231
 
232
- This two types do not have generators.
232
+ These two types don't have generators.
233
233
 
234
234
  Values are just simple Ruby classes, but watch out to keep them in the Values directory!
235
235
 
@@ -259,6 +259,112 @@ Example of calling a Util function:
259
259
  MagicTricks.dissapear(rabbit)
260
260
  ```
261
261
 
262
+ ### Presenters
263
+
264
+ For generating presenters we use:
265
+
266
+ ```
267
+ $ rails generate presenter users_show
268
+ ```
269
+
270
+ This will create the `UsersShowPresenter` class, inheriting from a base class:
271
+
272
+ ```ruby
273
+ class UsersShowPresenter < PowerTypes::PresenterBase
274
+ end
275
+ ```
276
+
277
+ And its corresponding rspec file:
278
+
279
+ ```ruby
280
+ require 'rails_helper'
281
+
282
+ describe UsersShowPresenter do
283
+ pending "add some examples to (or delete) #{__FILE__}"
284
+ end
285
+ ```
286
+
287
+ To initialize a presenter inside your controller action you should execute the `present_with` method with valid params:
288
+
289
+ ```ruby
290
+ class UsersController < InheritedResources::Base
291
+ def show
292
+ presenter_params = { param1: 1, param2: 2 }
293
+ @presenter = present_with(:users_show, presenter_params)
294
+ end
295
+ end
296
+ ```
297
+
298
+ You can access view helper methods through the `h` method:
299
+
300
+ ```ruby
301
+ class UsersShowPresenter < PowerTypes::PresenterBase
302
+ def platanus_link
303
+ h.link_to "Hi Platanus!", "https://platan.us"
304
+ end
305
+ end
306
+ ```
307
+
308
+ You can access `presenter_params` inside the presenter as an `attr_reader`
309
+
310
+ ```ruby
311
+ class UsersController < InheritedResources::Base
312
+ def show
313
+ presenter_params = { platanus_url: "https://platan.us" }
314
+ @presenter = present_with(:users_show, presenter_params)
315
+ end
316
+ end
317
+ ```
318
+
319
+ ```ruby
320
+ class UsersShowPresenter < PowerTypes::PresenterBase
321
+ def platanus_link
322
+ h.link_to "Hi Platanus!", platanus_url
323
+ end
324
+ end
325
+ ```
326
+
327
+ If the presenter param has a [decorator](https://github.com/drapergem/draper), the `attr_reader` will be decorated.
328
+
329
+ ```ruby
330
+ class UsersController < InheritedResources::Base
331
+ def show
332
+ presenter_params = { user: user }
333
+ @presenter = present_with(:users_show, presenter_params)
334
+ end
335
+
336
+ private
337
+
338
+ def user
339
+ @user ||= User.find!(params[:id])
340
+ end
341
+ end
342
+ ```
343
+
344
+ ```ruby
345
+ class UserDecorator < Draper::Decorator
346
+ delegate_all
347
+
348
+ def cool_view_name
349
+ "~º#{name}º~"
350
+ end
351
+ end
352
+ ```
353
+
354
+ ```ruby
355
+ class UsersShowPresenter < PowerTypes::PresenterBase
356
+ def platanus_link
357
+ h.link_to "Hi #{user.cool_view_name}!", platanus_url
358
+ end
359
+ end
360
+ ```
361
+
362
+ In the view, you can use it like this:
363
+
364
+ ```
365
+ <div><%= @presenter.platanus_link %></div>
366
+ ```
367
+
262
368
  ## Contributing
263
369
 
264
370
  1. Fork it
@@ -1,12 +1,22 @@
1
1
  module PowerTypes
2
2
  class InitGenerator < Rails::Generators::Base
3
3
  desc "This generator creates the folder structure for the power-types gem"
4
+
4
5
  def create_folders
5
6
  empty_directory "app/commands/"
6
7
  empty_directory "app/services/"
7
8
  empty_directory "app/observers/"
9
+ empty_directory "app/presenters/"
8
10
  empty_directory "app/utils/"
9
11
  empty_directory "app/values/"
10
12
  end
13
+
14
+ def config_presenters
15
+ insert_into_file(
16
+ "app/controllers/application_controller.rb",
17
+ "\n include PowerTypes::Presentable",
18
+ after: "ActionController::Base"
19
+ )
20
+ end
11
21
  end
12
22
  end
@@ -0,0 +1,12 @@
1
+ module Rails
2
+ class PresenterGenerator < Rails::Generators::NamedBase
3
+ source_root File.expand_path("../templates", __FILE__)
4
+
5
+ desc "This generator creates a new presenter at app/presenters"
6
+
7
+ def create_presenter
8
+ template('presenter.rb', "app/presenters/#{file_name.underscore}_presenter.rb")
9
+ template('presenter_spec.rb', "spec/presenters/#{file_name.underscore}_presenter_spec.rb")
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,2 @@
1
+ class <%= class_name %>Presenter < PowerTypes::BasePresenter
2
+ end
@@ -0,0 +1,5 @@
1
+ require 'rails_helper'
2
+
3
+ describe <%= class_name %>Presenter do
4
+ pending "add some examples to (or delete) #{__FILE__}"
5
+ end
@@ -1,10 +1,15 @@
1
+ require "active_support/all"
2
+
1
3
  require "power_types/version"
2
4
  require "power_types/util"
5
+ require "power_types/errors"
3
6
  require "power_types/patterns/service"
4
7
  require "power_types/patterns/command"
5
8
  require "power_types/patterns/observer/observable"
6
9
  require "power_types/patterns/observer/observer"
7
10
  require "power_types/patterns/observer/trigger"
11
+ require "power_types/patterns/presenter/base_presenter"
12
+ require "power_types/patterns/presenter/presentable"
8
13
 
9
14
  module PowerTypes
10
15
  end
@@ -0,0 +1,2 @@
1
+ class PowerTypes::Error < RuntimeError; end
2
+ class PowerTypes::PresenterError < PowerTypes::Error; end
@@ -0,0 +1,28 @@
1
+ module PowerTypes
2
+ class BasePresenter
3
+ def initialize(view, params = {})
4
+ @h = view
5
+
6
+ params.each_pair do |attribute, value|
7
+ if respond_to?(attribute, true)
8
+ raise PowerTypes::PresenterError.new(
9
+ "attribute #{attribute} already defined in presenter"
10
+ )
11
+ end
12
+
13
+ singleton_class.send(:attr_accessor, attribute)
14
+ instance_variable_set("@#{attribute}", decorated_value(value))
15
+ end
16
+ end
17
+
18
+ private
19
+
20
+ attr_reader :h
21
+
22
+ def decorated_value(value)
23
+ return value unless value.respond_to?(:decorate)
24
+
25
+ value.decorate
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,20 @@
1
+ module PowerTypes
2
+ module Presentable
3
+ def present_with(presenter_name, data = {})
4
+ presenter_class_by_name(presenter_name).new(view_context, data)
5
+ end
6
+
7
+ def presenter_class_by_name(presenter_name)
8
+ class_name = presenter_name.to_s.classify
9
+ class_constant = class_name.safe_constantize
10
+
11
+ if class_constant.blank?
12
+ raise PowerTypes::PresenterError.new(
13
+ "missing #{class_name} presenter class"
14
+ )
15
+ end
16
+
17
+ class_constant
18
+ end
19
+ end
20
+ end
@@ -1,3 +1,3 @@
1
1
  module PowerTypes
2
- VERSION = '0.3.1'
2
+ VERSION = '0.4.0'
3
3
  end
@@ -19,16 +19,19 @@ Gem::Specification.new do |spec|
19
19
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
20
  spec.require_paths = ["lib"]
21
21
 
22
- spec.add_development_dependency "bundler", "~> 1.6"
23
- spec.add_development_dependency "rake", "~> 10.4"
24
- spec.add_development_dependency "rspec", "~> 3.1"
25
- spec.add_development_dependency "rspec-nc", "~> 0.2"
22
+ spec.add_dependency "activesupport"
23
+ spec.add_development_dependency "bundler", "~> 2.1.4"
24
+ spec.add_development_dependency "coveralls"
26
25
  spec.add_development_dependency "guard", "~> 2.11"
27
26
  spec.add_development_dependency "guard-rspec", "~> 4.5"
28
- spec.add_development_dependency "terminal-notifier-guard", "~> 1.6", ">= 1.6.1"
29
27
  spec.add_development_dependency "pry", "~> 0.10"
30
- spec.add_development_dependency "pry-remote", "~> 0.1"
31
28
  spec.add_development_dependency "pry-byebug", "~> 3.2"
32
29
  spec.add_development_dependency "pry-nav", "~> 0.2"
33
- spec.add_development_dependency "coveralls"
30
+ spec.add_development_dependency "pry-remote", "~> 0.1"
31
+ spec.add_development_dependency "rake", "~> 10.4"
32
+ spec.add_development_dependency "rspec", "~> 3.1"
33
+ spec.add_development_dependency "rspec-nc", "~> 0.2"
34
+ spec.add_development_dependency "rubocop", "0.66"
35
+ spec.add_development_dependency "rubocop-rspec"
36
+ spec.add_development_dependency "terminal-notifier-guard", "~> 1.6", ">= 1.6.1"
34
37
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: power-types
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ignacio Baixas
@@ -9,156 +9,164 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2018-07-31 00:00:00.000000000 Z
12
+ date: 2020-10-19 00:00:00.000000000 Z
13
13
  dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: '0'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: '0'
14
28
  - !ruby/object:Gem::Dependency
15
29
  name: bundler
16
30
  requirement: !ruby/object:Gem::Requirement
17
31
  requirements:
18
32
  - - "~>"
19
33
  - !ruby/object:Gem::Version
20
- version: '1.6'
34
+ version: 2.1.4
21
35
  type: :development
22
36
  prerelease: false
23
37
  version_requirements: !ruby/object:Gem::Requirement
24
38
  requirements:
25
39
  - - "~>"
26
40
  - !ruby/object:Gem::Version
27
- version: '1.6'
41
+ version: 2.1.4
28
42
  - !ruby/object:Gem::Dependency
29
- name: rake
43
+ name: coveralls
30
44
  requirement: !ruby/object:Gem::Requirement
31
45
  requirements:
32
- - - "~>"
46
+ - - ">="
33
47
  - !ruby/object:Gem::Version
34
- version: '10.4'
48
+ version: '0'
35
49
  type: :development
36
50
  prerelease: false
37
51
  version_requirements: !ruby/object:Gem::Requirement
38
52
  requirements:
39
- - - "~>"
53
+ - - ">="
40
54
  - !ruby/object:Gem::Version
41
- version: '10.4'
55
+ version: '0'
42
56
  - !ruby/object:Gem::Dependency
43
- name: rspec
57
+ name: guard
44
58
  requirement: !ruby/object:Gem::Requirement
45
59
  requirements:
46
60
  - - "~>"
47
61
  - !ruby/object:Gem::Version
48
- version: '3.1'
62
+ version: '2.11'
49
63
  type: :development
50
64
  prerelease: false
51
65
  version_requirements: !ruby/object:Gem::Requirement
52
66
  requirements:
53
67
  - - "~>"
54
68
  - !ruby/object:Gem::Version
55
- version: '3.1'
69
+ version: '2.11'
56
70
  - !ruby/object:Gem::Dependency
57
- name: rspec-nc
71
+ name: guard-rspec
58
72
  requirement: !ruby/object:Gem::Requirement
59
73
  requirements:
60
74
  - - "~>"
61
75
  - !ruby/object:Gem::Version
62
- version: '0.2'
76
+ version: '4.5'
63
77
  type: :development
64
78
  prerelease: false
65
79
  version_requirements: !ruby/object:Gem::Requirement
66
80
  requirements:
67
81
  - - "~>"
68
82
  - !ruby/object:Gem::Version
69
- version: '0.2'
83
+ version: '4.5'
70
84
  - !ruby/object:Gem::Dependency
71
- name: guard
85
+ name: pry
72
86
  requirement: !ruby/object:Gem::Requirement
73
87
  requirements:
74
88
  - - "~>"
75
89
  - !ruby/object:Gem::Version
76
- version: '2.11'
90
+ version: '0.10'
77
91
  type: :development
78
92
  prerelease: false
79
93
  version_requirements: !ruby/object:Gem::Requirement
80
94
  requirements:
81
95
  - - "~>"
82
96
  - !ruby/object:Gem::Version
83
- version: '2.11'
97
+ version: '0.10'
84
98
  - !ruby/object:Gem::Dependency
85
- name: guard-rspec
99
+ name: pry-byebug
86
100
  requirement: !ruby/object:Gem::Requirement
87
101
  requirements:
88
102
  - - "~>"
89
103
  - !ruby/object:Gem::Version
90
- version: '4.5'
104
+ version: '3.2'
91
105
  type: :development
92
106
  prerelease: false
93
107
  version_requirements: !ruby/object:Gem::Requirement
94
108
  requirements:
95
109
  - - "~>"
96
110
  - !ruby/object:Gem::Version
97
- version: '4.5'
111
+ version: '3.2'
98
112
  - !ruby/object:Gem::Dependency
99
- name: terminal-notifier-guard
113
+ name: pry-nav
100
114
  requirement: !ruby/object:Gem::Requirement
101
115
  requirements:
102
116
  - - "~>"
103
117
  - !ruby/object:Gem::Version
104
- version: '1.6'
105
- - - ">="
106
- - !ruby/object:Gem::Version
107
- version: 1.6.1
118
+ version: '0.2'
108
119
  type: :development
109
120
  prerelease: false
110
121
  version_requirements: !ruby/object:Gem::Requirement
111
122
  requirements:
112
123
  - - "~>"
113
124
  - !ruby/object:Gem::Version
114
- version: '1.6'
115
- - - ">="
116
- - !ruby/object:Gem::Version
117
- version: 1.6.1
125
+ version: '0.2'
118
126
  - !ruby/object:Gem::Dependency
119
- name: pry
127
+ name: pry-remote
120
128
  requirement: !ruby/object:Gem::Requirement
121
129
  requirements:
122
130
  - - "~>"
123
131
  - !ruby/object:Gem::Version
124
- version: '0.10'
132
+ version: '0.1'
125
133
  type: :development
126
134
  prerelease: false
127
135
  version_requirements: !ruby/object:Gem::Requirement
128
136
  requirements:
129
137
  - - "~>"
130
138
  - !ruby/object:Gem::Version
131
- version: '0.10'
139
+ version: '0.1'
132
140
  - !ruby/object:Gem::Dependency
133
- name: pry-remote
141
+ name: rake
134
142
  requirement: !ruby/object:Gem::Requirement
135
143
  requirements:
136
144
  - - "~>"
137
145
  - !ruby/object:Gem::Version
138
- version: '0.1'
146
+ version: '10.4'
139
147
  type: :development
140
148
  prerelease: false
141
149
  version_requirements: !ruby/object:Gem::Requirement
142
150
  requirements:
143
151
  - - "~>"
144
152
  - !ruby/object:Gem::Version
145
- version: '0.1'
153
+ version: '10.4'
146
154
  - !ruby/object:Gem::Dependency
147
- name: pry-byebug
155
+ name: rspec
148
156
  requirement: !ruby/object:Gem::Requirement
149
157
  requirements:
150
158
  - - "~>"
151
159
  - !ruby/object:Gem::Version
152
- version: '3.2'
160
+ version: '3.1'
153
161
  type: :development
154
162
  prerelease: false
155
163
  version_requirements: !ruby/object:Gem::Requirement
156
164
  requirements:
157
165
  - - "~>"
158
166
  - !ruby/object:Gem::Version
159
- version: '3.2'
167
+ version: '3.1'
160
168
  - !ruby/object:Gem::Dependency
161
- name: pry-nav
169
+ name: rspec-nc
162
170
  requirement: !ruby/object:Gem::Requirement
163
171
  requirements:
164
172
  - - "~>"
@@ -172,7 +180,21 @@ dependencies:
172
180
  - !ruby/object:Gem::Version
173
181
  version: '0.2'
174
182
  - !ruby/object:Gem::Dependency
175
- name: coveralls
183
+ name: rubocop
184
+ requirement: !ruby/object:Gem::Requirement
185
+ requirements:
186
+ - - '='
187
+ - !ruby/object:Gem::Version
188
+ version: '0.66'
189
+ type: :development
190
+ prerelease: false
191
+ version_requirements: !ruby/object:Gem::Requirement
192
+ requirements:
193
+ - - '='
194
+ - !ruby/object:Gem::Version
195
+ version: '0.66'
196
+ - !ruby/object:Gem::Dependency
197
+ name: rubocop-rspec
176
198
  requirement: !ruby/object:Gem::Requirement
177
199
  requirements:
178
200
  - - ">="
@@ -185,6 +207,26 @@ dependencies:
185
207
  - - ">="
186
208
  - !ruby/object:Gem::Version
187
209
  version: '0'
210
+ - !ruby/object:Gem::Dependency
211
+ name: terminal-notifier-guard
212
+ requirement: !ruby/object:Gem::Requirement
213
+ requirements:
214
+ - - "~>"
215
+ - !ruby/object:Gem::Version
216
+ version: '1.6'
217
+ - - ">="
218
+ - !ruby/object:Gem::Version
219
+ version: 1.6.1
220
+ type: :development
221
+ prerelease: false
222
+ version_requirements: !ruby/object:Gem::Requirement
223
+ requirements:
224
+ - - "~>"
225
+ - !ruby/object:Gem::Version
226
+ version: '1.6'
227
+ - - ">="
228
+ - !ruby/object:Gem::Version
229
+ version: 1.6.1
188
230
  description: Power Types for Rails by Platanus
189
231
  email:
190
232
  - ignacio@platan.us
@@ -198,6 +240,7 @@ files:
198
240
  - ".hound.yml"
199
241
  - ".rspec"
200
242
  - ".rubocop.yml"
243
+ - ".ruby-version"
201
244
  - ".travis.yml"
202
245
  - CODE_OF_CONDUCT.md
203
246
  - Gemfile
@@ -208,19 +251,25 @@ files:
208
251
  - lib/generators/power_types/init_generator.rb
209
252
  - lib/generators/rails/command_generator.rb
210
253
  - lib/generators/rails/observer_generator.rb
254
+ - lib/generators/rails/presenter_generator.rb
211
255
  - lib/generators/rails/service_generator.rb
212
256
  - lib/generators/rails/templates/command.rb
213
257
  - lib/generators/rails/templates/command_spec.rb
214
258
  - lib/generators/rails/templates/observer.rb
215
259
  - lib/generators/rails/templates/observer_spec.rb
260
+ - lib/generators/rails/templates/presenter.rb
261
+ - lib/generators/rails/templates/presenter_spec.rb
216
262
  - lib/generators/rails/templates/service.rb
217
263
  - lib/generators/rails/templates/service_spec.rb
218
264
  - lib/power-types.rb
219
265
  - lib/power_types.rb
266
+ - lib/power_types/errors.rb
220
267
  - lib/power_types/patterns/command.rb
221
268
  - lib/power_types/patterns/observer/observable.rb
222
269
  - lib/power_types/patterns/observer/observer.rb
223
270
  - lib/power_types/patterns/observer/trigger.rb
271
+ - lib/power_types/patterns/presenter/base_presenter.rb
272
+ - lib/power_types/patterns/presenter/presentable.rb
224
273
  - lib/power_types/patterns/service.rb
225
274
  - lib/power_types/util.rb
226
275
  - lib/power_types/version.rb
@@ -244,8 +293,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
244
293
  - !ruby/object:Gem::Version
245
294
  version: '0'
246
295
  requirements: []
247
- rubyforge_project:
248
- rubygems_version: 2.7.7
296
+ rubygems_version: 3.0.8
249
297
  signing_key:
250
298
  specification_version: 4
251
299
  summary: Power Types for Rails by Platanus