action_presenter 1.1 → 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -15,3 +15,7 @@
15
15
 
16
16
  * fix `present` helper to return presenter instance if no block is given (test braking change - release new minor version)
17
17
  * RSpec inspired way to define short and consise methods
18
+
19
+ ## Version 1.1.1
20
+
21
+ * allow to scope variable when calling #present helper `present [:admin, @article]`
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- ## ActionPresenter [![Build Status](https://secure.travis-ci.org/zlw/action_presenter.png?branch=master)](http://travis-ci.org/zlw/action_presenter)
1
+ # ActionPresenter [![Build Status](https://secure.travis-ci.org/zlw/action_presenter.png?branch=master)](http://travis-ci.org/zlw/action_presenter)
2
2
 
3
3
  Missing link between models and views.
4
4
  Use presenter pattern in Rails application without changing controllers.
@@ -29,7 +29,7 @@ Gem was tested under Ruby 1.9.2 and 1.9.3, Rails 3.2.1
29
29
 
30
30
  ## Usage
31
31
 
32
- Create presenter class
32
+ ### Creating presenter class
33
33
 
34
34
  ```ruby
35
35
  # /app/presenters/post_presenter.rb
@@ -48,7 +48,7 @@ end
48
48
 
49
49
  You can use all view helpers without any changes. Reference to model by method with the name passed to `presents` class method.
50
50
 
51
- and then use it in view
51
+ ### Calling presenter
52
52
 
53
53
  ```haml
54
54
  # /app/views/posts/index.html.haml
@@ -57,15 +57,33 @@ and then use it in view
57
57
  = p.content
58
58
  ```
59
59
 
60
- If You want to change presenter class pass it as second argument
60
+ It will call (by default) `PostPresenter` (assuming that @post variable is instance of Post class)
61
61
 
62
- ```haml
63
- # /app/views/post/show.html.haml
64
- - present @post, PostShowPresenter do |p|
65
- = p.title
62
+ There are some ways to change default presenter class
63
+
64
+ #### 1. Pass class as second argument
65
+
66
+ ```ruby
67
+ present @post, ArticlePresenter
68
+ ```
69
+
70
+ It will call `ArticlePresenter` class
71
+
72
+ #### 2. Scope variable
73
+
74
+ ```ruby
75
+ present [:admin, @post]
66
76
  ```
67
77
 
68
- All that without any change in any model or controller
78
+ It will call `Admin::PostPresenter` class
79
+
80
+ **Please, keep in mind, that passing presenter class has higher priority than scope:**
81
+
82
+ ```ruby
83
+ present [:admin, @post], ArticlePresenter
84
+ ```
85
+
86
+ **It will also call `ArticlePresenter` class**
69
87
 
70
88
  ## Defaults
71
89
 
@@ -144,6 +162,15 @@ class TestArticlePresenter < ActionView::TestCase
144
162
  end
145
163
  ```
146
164
 
165
+ ## Feature requests, contributing to project
166
+
167
+ Well, I am not sitting all day and thinking about fancy stuff that I could add to action_presenter.
168
+ Everytime I need something I add this to my current project, than extract it to this gem and release a new version.
169
+
170
+ If you need something - please create an issue with feature request or, wchich is more welcome (my time is also limited), send pull request.
171
+
172
+ Also if you something is missing/broken, please do not think `Man, this gem sucks and its author is shitty coder` and no longer use it! Let me know about it - create an issue or send patch.
173
+
147
174
  ## Maintainers
148
175
 
149
176
  * Krzysztof Zalewski (https://github.com/zlw, http://kzalewski.blogspot.com)
@@ -1,3 +1,3 @@
1
1
  module ActionPresenter
2
- VERSION = "1.1"
2
+ VERSION = "1.1.1"
3
3
  end
@@ -6,11 +6,27 @@ module ActionPresenter
6
6
  helper_method :present
7
7
  end
8
8
 
9
- def present(object, klass = "#{object.class}Presenter".constantize)
9
+ def present(object, klass = nil)
10
+ klass ||= presenter_name object
10
11
  presenter = klass.new(object, view_context)
11
12
 
12
13
  return yield presenter if block_given?
13
14
  presenter
14
15
  end
16
+
17
+ private
18
+
19
+ def presenter_name(scope)
20
+ "#{klass_name(scope)}Presenter".constantize
21
+ end
22
+
23
+ def klass_name(scope)
24
+ return scope.class.to_s unless scope.is_a? Array
25
+ return scope.first.class.to_s if scope.length == 1
26
+
27
+ scope.map do |s|
28
+ s.is_a?(Symbol) ? s.capitalize : s.class
29
+ end.join('::')
30
+ end
15
31
  end
16
32
  end
@@ -6,19 +6,79 @@ require 'support/dummy_action_controller'
6
6
  describe ActionPresenter::ViewHelper do
7
7
  include ActionControllerHelper
8
8
 
9
- it 'should call default presenter if presenter class is not given' do
10
- helper.present('foobar') do |p|
11
- p.should be_instance_of StringPresenter
9
+ context "#with block" do
10
+ it 'should call default presenter if presenter class is not given' do
11
+ helper.present('foo') do |p|
12
+ p.should be_instance_of StringPresenter
13
+ end
14
+ end
15
+
16
+ it 'should call given presenter class' do
17
+ klass = FoobarPresenter
18
+ helper.present('foo', klass) do |p|
19
+ p.should be_instance_of klass
20
+ end
21
+ end
22
+
23
+ it 'should call scoped presenter if no class is given' do
24
+ helper.present [:foo, 'foo'] do |p|
25
+ p.should be_instance_of Foo::StringPresenter
26
+ end
27
+ end
28
+
29
+ it 'should call given presenter class even if scope is given' do
30
+ klass = FoobarPresenter
31
+ helper.present [:foo, 'foo'], klass do |p|
32
+ p.should be_instance_of klass
33
+ end
12
34
  end
13
35
  end
14
36
 
15
- it 'should call given presenter class' do
16
- helper.present('foobar', FoobarPresenter) do |p|
17
- p.should be_instance_of FoobarPresenter
37
+ context "#without block" do
38
+ it 'should return presenter instance if no block is given' do
39
+ helper.present('foo').should be_instance_of StringPresenter
40
+ end
41
+
42
+ it 'should return presenter instance if scope is given' do
43
+ helper.present([:foo, 'foo']).should be_instance_of Foo::StringPresenter
44
+ end
45
+
46
+ it "should return given presenter instance even if scope is given" do
47
+ klass = FoobarPresenter
48
+ helper.present([:foo, 'foo'], klass).should be_instance_of klass
18
49
  end
19
50
  end
20
51
 
21
- it 'should return presenter instance if no block is given' do
22
- helper.present('foobar').should be_instance_of StringPresenter
52
+ context "#private methods" do
53
+ context "#presenter_name" do
54
+ it 'should return constant for object' do
55
+ helper.should_receive(:klass_name).with('foo').and_return 'String'
56
+ helper.send(:presenter_name, 'foo').should == StringPresenter
57
+ end
58
+
59
+ it 'should return constant for one element array' do
60
+ helper.should_receive(:klass_name).with(['foo']).and_return 'String'
61
+ helper.send(:presenter_name, ['foo']).should == StringPresenter
62
+ end
63
+
64
+ it 'should return constant for few element array' do
65
+ helper.should_receive(:klass_name).with([:foo, 'foo']).and_return 'Foo::String'
66
+ helper.send(:presenter_name, [:foo, 'foo']).should == Foo::StringPresenter
67
+ end
68
+ end
69
+
70
+ context "#klass_name" do
71
+ it 'should return klass name for object' do
72
+ helper.send(:klass_name, 'foo').should == 'String'
73
+ end
74
+
75
+ it "should return klass name for one element array" do
76
+ helper.send(:klass_name, ['foo']).should == 'String'
77
+ end
78
+
79
+ it "should return klass name for few elements array" do
80
+ helper.send(:klass_name, [:foo, 'foo']).should == 'Foo::String'
81
+ end
82
+ end
23
83
  end
24
84
  end
@@ -8,4 +8,10 @@ class FoobarPresenter < ActionPresenter::Base
8
8
  def foobar
9
9
  'bar'
10
10
  end
11
+ end
12
+
13
+
14
+ module Foo
15
+ class StringPresenter < ActionPresenter::Base
16
+ end
11
17
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: action_presenter
3
3
  version: !ruby/object:Gem::Version
4
- version: '1.1'
4
+ version: 1.1.1
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-02-27 00:00:00.000000000 Z
12
+ date: 2012-03-07 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: actionpack
16
- requirement: &70356359315720 !ruby/object:Gem::Requirement
16
+ requirement: &70294413858660 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70356359315720
24
+ version_requirements: *70294413858660
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: named_accessors
27
- requirement: &70356359300080 !ruby/object:Gem::Requirement
27
+ requirement: &70294413858200 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70356359300080
35
+ version_requirements: *70294413858200
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rake
38
- requirement: &70356359299600 !ruby/object:Gem::Requirement
38
+ requirement: &70294413840020 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70356359299600
46
+ version_requirements: *70294413840020
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rspec
49
- requirement: &70356359299140 !ruby/object:Gem::Requirement
49
+ requirement: &70294413839580 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,7 +54,7 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *70356359299140
57
+ version_requirements: *70294413839580
58
58
  description: Missing link between models and views. Use presenter pattern in Rails
59
59
  application without changing controllers.
60
60
  email:
@@ -100,7 +100,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
100
100
  version: '0'
101
101
  segments:
102
102
  - 0
103
- hash: 3423536328799258601
103
+ hash: 1028900342905026486
104
104
  required_rubygems_version: !ruby/object:Gem::Requirement
105
105
  none: false
106
106
  requirements:
@@ -109,7 +109,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
109
109
  version: '0'
110
110
  segments:
111
111
  - 0
112
- hash: 3423536328799258601
112
+ hash: 1028900342905026486
113
113
  requirements: []
114
114
  rubyforge_project: action_presenter
115
115
  rubygems_version: 1.8.16