arch_presenter 0.1.5 → 0.1.6

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: a9a1c985b8e80a569e42dda4e1fae1c43267d7a6
4
- data.tar.gz: 7060f5d72bc975bcd3737c78ca63df4a1c4bc42e
3
+ metadata.gz: 3a0abcad3a6fa30f12186903c14b06d2331eeb5e
4
+ data.tar.gz: 78a348ee01770be2cca8976c253ce58d31af7148
5
5
  SHA512:
6
- metadata.gz: 6980a7a33817e83a711fa0cda57e00211057ccab26e359641e571eb08f5967a2a2b265f787519366f035e6a1aac0526183aeec682f98caa9557b4deb9f99bc65
7
- data.tar.gz: 49361a5a0839b46ce0a644b66197de32713bbefef324df211a89eefece86596b497562acb452f08327479178a0c468340c735f120eafdc77a36b534db278e1b2
6
+ metadata.gz: 61703e68f2a16c178f9b9c13bac89efee8b938be218aa2c7bb1c7b5b48b9742988fa881da39dbf22c388b836fe116686aab61edf330d121133fc94dd2577fae9
7
+ data.tar.gz: f0fb0b3ef289c878910616cec1986c5dabf6b72cd5533a41f3d26dcec8919824b7342f0309f9e0e2b1eaa144f37603580d255bb1973a4d160de24b610387f0ed
data/README.md CHANGED
@@ -1,8 +1,8 @@
1
1
  # ArchPresenter
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/arch_presenter`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ ArchPresenter is a simple, lightweight gem that implements the Presenter pattern.
4
4
 
5
- TODO: Delete this and the text above, and describe your gem
5
+ It allows you to slim down your models and controllers by extracting the logic related to class presentation. You can learn more about this pattern [on the Jay Fields' blog](http://blog.jayfields.com/2007/03/rails-presenter-pattern.html).
6
6
 
7
7
  ## Installation
8
8
 
@@ -12,23 +12,171 @@ Add this line to your application's Gemfile:
12
12
  gem 'arch_presenter'
13
13
  ```
14
14
 
15
- And then execute:
15
+ And then run:
16
16
 
17
17
  $ bundle
18
18
 
19
- Or install it yourself as:
20
-
21
- $ gem install arch_presenter
22
19
 
23
20
  ## Usage
24
21
 
25
- TODO: Write usage instructions here
22
+ ### Super-quick example
23
+
24
+ Simple, skinny model:
25
+
26
+ ```ruby
27
+ # app/models/user.rb
28
+ class User
29
+ def first_name
30
+ 'John'
31
+ end
32
+ def last_name
33
+ 'Smith'
34
+ end
35
+ def title
36
+ 'Mr.'
37
+ end
38
+ def name_visible?
39
+ true
40
+ end
41
+ end
42
+ ```
43
+
44
+ Presentation logic moved to the presenter:
45
+
46
+ ```ruby
47
+ # app/presenters/user_presenter.rb
48
+ class UserPresenter < ArchPresenter::Base
49
+ def display_name
50
+ if name_visible?
51
+ [title, first_name, last_name].join(' ')
52
+ else
53
+ '(Name hidden)'
54
+ end
55
+ end
56
+ end
57
+ ```
58
+
59
+ Presenting the object in the controller:
60
+
61
+ ``` ruby
62
+ # app/controllers/users_cotroller.rb
63
+ class UsersController < ApplicationController
64
+ def show
65
+ @user = User.find(params[:id])
66
+ @user = present(@user)
67
+ end
68
+ end
69
+ ```
70
+
71
+ Using the `display_name` function:
72
+
73
+ ```ruby
74
+ # app/views/users/show.html.erb
75
+ <h1><%=@user.display_name %> - User profile</h1>
76
+ ```
77
+
78
+ ### Creating presenter classes
79
+
80
+ First you will need to create a class that will be used to decorate your object.
81
+
82
+ Presenter class files should be placed in `app/presenters/` and should inherit from `ArchPresenter::Base`.
83
+
84
+ ### Autoloading
85
+
86
+ You might wish to add `app/presenters/` to your autoload path. This will allow rails to automatically detect changes made to the presenter files without the need to restart the server.
87
+
88
+ In `config/application.rb` add:
89
+
90
+ ```ruby
91
+ config.autoload_paths << Rails.root.join('app', 'presenters')
92
+ ```
93
+
94
+ ### Decorating
26
95
 
27
- ## Development
96
+ ArchPresenter gem provides a `#present` function that can be invoked in your controller (or in your view if you preffer decorating your classes there).
28
97
 
29
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
98
+ To decorate your class use the `#present` function.
99
+
100
+ In the controller:
101
+
102
+ ```ruby
103
+ user = User.find(1)
104
+ @user = present(user)
105
+ ```
106
+
107
+ or in the view:
108
+
109
+ ```ruby
110
+ <body>
111
+ <% @user = present(@user) %>
112
+ Hello <%= @user.full_name %>!
113
+ </body>
114
+ ```
115
+
116
+ ### Mini-tutorial
117
+
118
+ We have a user model that looks like this:
119
+
120
+ ```ruby
121
+ # app/models/user.rb
122
+ class User < ActiveRecord::Base
123
+ def first_name
124
+ 'John'
125
+ end
126
+ def last_name
127
+ 'Smith'
128
+ end
129
+ def title
130
+ 'Mr.'
131
+ end
132
+ def name_visible?
133
+ true
134
+ end
135
+ def display_name
136
+ if name_visible?
137
+ [title, first_name, last_name].join(' ')
138
+ else
139
+ '(Name hidden)'
140
+ end
141
+ end
142
+ end
143
+ ```
144
+
145
+ where `first_name`, `last_name` and `title` are user table cloumns and `display_name` is only used to show the name while displaying information about the user. `display_name` doesn't really belong to the model and we want to move it to a separate class - the presenter.
146
+
147
+ We create a new file in `app/presenters` and move the `display_name` there:
148
+
149
+ ```ruby
150
+ class UserPresenter < ActiveRecord::Base
151
+ def display_name
152
+ if name_visible?
153
+ [title, first_name, last_name].join(' ')
154
+ else
155
+ '(Name hidden)'
156
+ end
157
+ end
158
+ end
159
+ ```
160
+
161
+ Now in our controller we can use the `present` method:
162
+
163
+ ``` ruby
164
+ # app/controllers/users_cotroller.rb
165
+ class UsersController < ApplicationController
166
+ def show
167
+ @user = User.find(params[:id])
168
+ @user = present(@user)
169
+ end
170
+ end
171
+ ```
172
+
173
+ This will add the `display_name` method to our `@user`. We can now use it in the view:
174
+
175
+ ```ruby
176
+ # app/views/users/show.html.erb
177
+ <h1><%=@user.display_name %> - User profile</h1>
178
+ ```
30
179
 
31
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
180
 
33
181
  ## Contributing
34
182
 
@@ -1,17 +1,4 @@
1
1
  module ArchPresenter
2
2
  class Base < ::SimpleDelegator
3
- def self.human_attribute_name(*args)
4
- original_class.human_attribute_name(*args)
5
- end
6
-
7
- def original_class
8
- presenter_class_name.chomp('Presenter').constantize
9
- end
10
-
11
- private
12
-
13
- def presenter_class_name
14
- self.class.to_s
15
- end
16
3
  end
17
4
  end
@@ -4,6 +4,7 @@ module ArchPresenter
4
4
  presenter_class_name = "#{target_object.class}Presenter"
5
5
  class_constant = presenter_class_name.constantize
6
6
  class_constant.new(target_object)
7
+ # If the presenter class doen't exist resturn the original object
7
8
  rescue NameError
8
9
  target_object
9
10
  end
@@ -1,3 +1,3 @@
1
1
  module ArchPresenter
2
- VERSION = "0.1.5"
2
+ VERSION = "0.1.6"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: arch_presenter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Przemyslaw Krowinski
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-05-16 00:00:00.000000000 Z
11
+ date: 2015-05-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler