classic_presenter 0.0.1 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/README.rdoc +61 -0
- data/lib/classic_presenter/helper.rb +1 -1
- data/lib/classic_presenter/version.rb +1 -1
- data/lib/generators/presenter_generator.rb +10 -0
- data/lib/generators/templates/presenter.rb +2 -0
- metadata +8 -4
data/.gitignore
ADDED
data/README.rdoc
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
== Classic Presenter
|
2
|
+
|
3
|
+
Presenter Pattern for Rails Applications
|
4
|
+
|
5
|
+
== Installation
|
6
|
+
|
7
|
+
gem install classic_presenter
|
8
|
+
|
9
|
+
== Usage
|
10
|
+
|
11
|
+
* In your Gemfile you should set:
|
12
|
+
|
13
|
+
gem 'classic_presenter'
|
14
|
+
|
15
|
+
* Create a simple ActiveRecord class that you want to present:
|
16
|
+
|
17
|
+
class Product < ActiveRecord::Base
|
18
|
+
end
|
19
|
+
|
20
|
+
* Create a presenter class inheriting from ClassicPresenter::Base:
|
21
|
+
|
22
|
+
class ProductPresenter < ClassicPresenter::Base
|
23
|
+
def display_name
|
24
|
+
if user
|
25
|
+
context.content_tag :strong, name
|
26
|
+
else
|
27
|
+
context.link_to name, context.product_path
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def display_description
|
32
|
+
context.render "products/description", :product => self
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
* In your view, show.html.erb for example, you can present your Product object using this syntax:
|
37
|
+
|
38
|
+
<% present ProductPresenter, @product, :user => current_user do |presenter| %>
|
39
|
+
<%= presenter.display_name %>
|
40
|
+
<%= presenter.display_description %>
|
41
|
+
<% end %>
|
42
|
+
|
43
|
+
== Additional details
|
44
|
+
|
45
|
+
* We can pass many objects to the presenter. In this example we are using :user => current_user and current_user is a helper that returns a User that is logged in. But we can use this for example:
|
46
|
+
|
47
|
+
<% present ProductPresenter, @product, :foo => "bar", :bar => "foo" do |presenter| %>
|
48
|
+
|
49
|
+
* Since ClassicPresenter::Base is a Decorator, it implements the “transparent interface” requirements of the Gang of Four’s definition. This means that when we call some method in ProductPresenter, it will be delegated to the decorated object (in this case Product), like this:
|
50
|
+
|
51
|
+
#products_controller.rb
|
52
|
+
@product = ProductPresenter.new(Product.find(params[:id]))
|
53
|
+
|
54
|
+
#show.html.erb
|
55
|
+
<%= @product.name %>
|
56
|
+
<%= @product.display_name %>
|
57
|
+
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
|
@@ -0,0 +1,10 @@
|
|
1
|
+
class PresenterGenerator < Rails::Generators::Base
|
2
|
+
source_root File.expand_path('../templates', __FILE__)
|
3
|
+
argument :presenter_name, :type => :string
|
4
|
+
|
5
|
+
desc "Generates a Presenter Class"
|
6
|
+
def generate_presenter
|
7
|
+
template "presenter.rb", "app/presenters/#{presenter_name.underscore}_presenter.rb"
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: classic_presenter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 1.0.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-06-21 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
16
|
-
requirement: &
|
16
|
+
requirement: &70257448795260 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,7 +21,7 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70257448795260
|
25
25
|
description: A simple Presenter Pattern for Rails Applications
|
26
26
|
email:
|
27
27
|
- rinaldifonseca@gmail.com
|
@@ -29,8 +29,10 @@ executables: []
|
|
29
29
|
extensions: []
|
30
30
|
extra_rdoc_files: []
|
31
31
|
files:
|
32
|
+
- .gitignore
|
32
33
|
- Gemfile
|
33
34
|
- Gemfile.lock
|
35
|
+
- README.rdoc
|
34
36
|
- Rakefile
|
35
37
|
- classic_presenter.gemspec
|
36
38
|
- lib/classic_presenter.rb
|
@@ -38,6 +40,8 @@ files:
|
|
38
40
|
- lib/classic_presenter/helper.rb
|
39
41
|
- lib/classic_presenter/railtie.rb
|
40
42
|
- lib/classic_presenter/version.rb
|
43
|
+
- lib/generators/presenter_generator.rb
|
44
|
+
- lib/generators/templates/presenter.rb
|
41
45
|
- spec/classic_presenter_base_spec.rb
|
42
46
|
- spec/classic_presenter_helper_spec.rb
|
43
47
|
- spec/spec_helper.rb
|