delegated_presenter 1.0.3 → 1.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/Guardfile +24 -0
- data/README.md +0 -13
- data/delegated_presenter.gemspec +4 -2
- data/lib/delegated_presenter/base.rb +72 -72
- data/lib/delegated_presenter/presents_before_rendering.rb +33 -15
- data/lib/delegated_presenter/version.rb +1 -1
- data/spec/dummy/app/controllers/sample_objects_controller.rb +2 -10
- data/spec/dummy/app/presenters/sample_object_presenter.rb +0 -4
- data/spec/lib/delegated_presenter/base_spec.rb +96 -0
- data/spec/lib/delegated_presenter/presents_before_rendering_spec.rb +74 -0
- data/spec/spec_helper.rb +3 -0
- metadata +40 -8
- data/spec/presenter_spec.rb +0 -104
- data/spec/presents_before_rendering_spec.rb +0 -36
data/Guardfile
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# A sample Guardfile
|
2
|
+
# More info at https://github.com/guard/guard#readme
|
3
|
+
|
4
|
+
guard 'rspec' do
|
5
|
+
watch(%r{^spec/.+_spec\.rb$})
|
6
|
+
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
|
7
|
+
watch('spec/spec_helper.rb') { "spec" }
|
8
|
+
|
9
|
+
# Rails example
|
10
|
+
watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
|
11
|
+
watch(%r{^app/(.*)(\.erb|\.haml)$}) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
|
12
|
+
watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
|
13
|
+
watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
|
14
|
+
watch('config/routes.rb') { "spec/routing" }
|
15
|
+
watch('app/controllers/application_controller.rb') { "spec/controllers" }
|
16
|
+
|
17
|
+
# Capybara features specs
|
18
|
+
watch(%r{^app/views/(.+)/.*\.(erb|haml)$}) { |m| "spec/features/#{m[1]}_spec.rb" }
|
19
|
+
|
20
|
+
# Turnip features and steps
|
21
|
+
watch(%r{^spec/acceptance/(.+)\.feature$})
|
22
|
+
watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'spec/acceptance' }
|
23
|
+
end
|
24
|
+
|
data/README.md
CHANGED
@@ -28,7 +28,6 @@ Or install it yourself as:
|
|
28
28
|
|
29
29
|
```ruby
|
30
30
|
class ContactPresenter < DelegatedPresenter::Base
|
31
|
-
|
32
31
|
# By default this presenter will try and present a Contact if it exists.
|
33
32
|
# You can explicitly tell the presenter to present other models using the following syntax:
|
34
33
|
|
@@ -45,7 +44,6 @@ class ContactPresenter < DelegatedPresenter::Base
|
|
45
44
|
def full_name
|
46
45
|
[prefix, first_name, middle_initial, last_name, suffix].compact.join(' ')
|
47
46
|
end
|
48
|
-
|
49
47
|
end
|
50
48
|
```
|
51
49
|
|
@@ -58,9 +56,7 @@ There are two helper methods in the presenter.
|
|
58
56
|
|
59
57
|
```ruby
|
60
58
|
class ContactPresenter < DelegatedPresenter::Base
|
61
|
-
|
62
59
|
hide :id, :crypted_password
|
63
|
-
|
64
60
|
end
|
65
61
|
```
|
66
62
|
|
@@ -68,9 +64,7 @@ or:
|
|
68
64
|
|
69
65
|
```ruby
|
70
66
|
class ContactPresenter < DelegatedPresenter::Base
|
71
|
-
|
72
67
|
expose :first_name, :last_name
|
73
|
-
|
74
68
|
end
|
75
69
|
```
|
76
70
|
|
@@ -82,9 +76,7 @@ Use the following to present a model instance or collection with a presenter, by
|
|
82
76
|
|
83
77
|
```ruby
|
84
78
|
class ContactsController < ApplicationController
|
85
|
-
|
86
79
|
presents :contact
|
87
|
-
|
88
80
|
end
|
89
81
|
```
|
90
82
|
|
@@ -93,9 +85,7 @@ If you for any reason need to explicitly define the presenter you may define a `
|
|
93
85
|
|
94
86
|
```ruby
|
95
87
|
class UsersController < ApplicationController
|
96
|
-
|
97
88
|
presents :user, with: :contact_presenter
|
98
|
-
|
99
89
|
end
|
100
90
|
```
|
101
91
|
|
@@ -104,7 +94,6 @@ end
|
|
104
94
|
|
105
95
|
```ruby
|
106
96
|
class ContactsController < ApplicationController
|
107
|
-
|
108
97
|
def index
|
109
98
|
@contacts = ContactPresenter.new Contact.all
|
110
99
|
end
|
@@ -112,9 +101,7 @@ class ContactsController < ApplicationController
|
|
112
101
|
def show
|
113
102
|
@contact = ContactPresenter.new Contact.find(params[:id])
|
114
103
|
end
|
115
|
-
|
116
104
|
end
|
117
|
-
|
118
105
|
```
|
119
106
|
|
120
107
|
## Contributing
|
data/delegated_presenter.gemspec
CHANGED
@@ -18,12 +18,14 @@ Gem::Specification.new do |gem|
|
|
18
18
|
gem.require_paths = ["lib"]
|
19
19
|
gem.add_dependency "activesupport"
|
20
20
|
|
21
|
-
gem.add_development_dependency "rspec"
|
21
|
+
gem.add_development_dependency "rspec", '~> 2.12.0'
|
22
|
+
gem.add_development_dependency "simplecov"
|
22
23
|
gem.add_development_dependency "sqlite3"
|
23
24
|
gem.add_development_dependency "rspec-rails"
|
24
25
|
gem.add_development_dependency "rails"
|
25
26
|
gem.add_development_dependency "factory_girl"
|
26
27
|
gem.add_development_dependency "database_cleaner"
|
27
|
-
gem.add_development_dependency "
|
28
|
+
gem.add_development_dependency "guard-rspec"
|
29
|
+
gem.add_development_dependency "rb-fsevent"
|
28
30
|
|
29
31
|
end
|
@@ -5,15 +5,77 @@
|
|
5
5
|
# If a method is not exposed.
|
6
6
|
class DelegatedPresenter::Base < SimpleDelegator
|
7
7
|
|
8
|
-
|
9
|
-
HIDDEN_METHODS = {}
|
10
|
-
EXPOSED_METHODS = {}
|
8
|
+
delegate :exposed_methods, :hidden_methods, :presentable, to: :presenter_class
|
11
9
|
|
12
|
-
|
10
|
+
alias :presented_model :__getobj__
|
13
11
|
|
14
|
-
|
12
|
+
class << self
|
15
13
|
|
16
|
-
|
14
|
+
attr_writer :presentable, :hidden_methods, :exposed_methods
|
15
|
+
|
16
|
+
protected
|
17
|
+
|
18
|
+
# Presentable Objects
|
19
|
+
# @api private
|
20
|
+
def presentable
|
21
|
+
@presentable ||= [] unless name == 'DelegatedPresenter::Base'
|
22
|
+
end
|
23
|
+
|
24
|
+
# Methods to hide
|
25
|
+
# @api private
|
26
|
+
def hidden_methods
|
27
|
+
@hidden_methods ||= [] unless name == 'DelegatedPresenter::Base'
|
28
|
+
end
|
29
|
+
|
30
|
+
# Methods to expose
|
31
|
+
# @api private
|
32
|
+
def exposed_methods
|
33
|
+
@exposed_methods ||= [] unless name == 'DelegatedPresenter::Base'
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
# Determine what objects what to present.
|
39
|
+
# @!visibility public
|
40
|
+
# @param :objects One or more models to be presented.
|
41
|
+
# @example Add some presentable objects:
|
42
|
+
# presents :contact, :user, :client
|
43
|
+
def presents(*objects)
|
44
|
+
self.presentable += objects.flatten.collect{ |i| i.to_s.camelize.singularize }
|
45
|
+
end
|
46
|
+
|
47
|
+
# @!visibility public
|
48
|
+
# Hide methods from the presenter.
|
49
|
+
# @param methods one ore more methods from the model to hide from the presenter.
|
50
|
+
# @example Hide methods:
|
51
|
+
# hide :id, :crypted_password, :password_salt
|
52
|
+
def hide(*hidden_methods)
|
53
|
+
self.hidden_methods += hidden_methods
|
54
|
+
end
|
55
|
+
|
56
|
+
# @!visibility public
|
57
|
+
# Exposes methods to the presenter, when defined all others will be hidden.
|
58
|
+
# @param :methods one ore more methods from the model to expose from the presenter.
|
59
|
+
# @example Expose methods:
|
60
|
+
# expose :first_name, :last_name
|
61
|
+
def expose(*exposed_methods)
|
62
|
+
self.exposed_methods += exposed_methods.flatten
|
63
|
+
end
|
64
|
+
|
65
|
+
## Internal Methods: Not Documented
|
66
|
+
|
67
|
+
# @api private
|
68
|
+
def inherited(subclass)
|
69
|
+
subclass.instance_variable_set :@presentable, presentable
|
70
|
+
subclass.instance_variable_set :@hidden_methods, hidden_methods
|
71
|
+
subclass.instance_variable_set :@exposed_methods, exposed_methods
|
72
|
+
return unless subclass.name.present?
|
73
|
+
presentable_class = subclass.name.gsub(/Presenter$/,'')
|
74
|
+
presentable_class.constantize rescue nil
|
75
|
+
subclass.send :presents, presentable_class if Object.const_defined?(presentable_class)
|
76
|
+
end
|
77
|
+
|
78
|
+
end
|
17
79
|
|
18
80
|
# Initializes the presenter with an object.
|
19
81
|
# @param object Can be an collection or instance of a presentable models.
|
@@ -25,13 +87,11 @@ class DelegatedPresenter::Base < SimpleDelegator
|
|
25
87
|
else
|
26
88
|
raise DelegatedPresenter::Error::NotPresentable, "#{self.presenter_class} cannot present a #{object.class}" unless object_is_presentable?(object)
|
27
89
|
__setobj__(object)
|
28
|
-
|
29
|
-
if exposed_methods
|
90
|
+
if exposed_methods.present?
|
30
91
|
expose_methods
|
31
|
-
elsif hidden_methods
|
92
|
+
elsif hidden_methods.present?
|
32
93
|
hide_methods
|
33
94
|
end
|
34
|
-
|
35
95
|
end
|
36
96
|
end
|
37
97
|
|
@@ -45,65 +105,6 @@ class DelegatedPresenter::Base < SimpleDelegator
|
|
45
105
|
|
46
106
|
private
|
47
107
|
|
48
|
-
# Determine what objects what to present.
|
49
|
-
# @!visibility public
|
50
|
-
# @param :objects One or more models to be presented.
|
51
|
-
# @example Add some presentable objects:
|
52
|
-
# presents :contact, :user, :client
|
53
|
-
def self.presents(*objects)
|
54
|
-
PRESENTABLE[name] ||= []
|
55
|
-
PRESENTABLE[name] += objects.flatten.collect{ |i| i.to_s.camelize.singularize }
|
56
|
-
end
|
57
|
-
|
58
|
-
# @!visibility public
|
59
|
-
# Hide methods from the presenter.
|
60
|
-
# @param methods one ore more methods from the model to hide from the presenter.
|
61
|
-
# @example Hide methods:
|
62
|
-
# hide :id, :crypted_password, :password_salt
|
63
|
-
def self.hide(*hidden_methods)
|
64
|
-
HIDDEN_METHODS[name] ||= []
|
65
|
-
HIDDEN_METHODS[name] += hidden_methods
|
66
|
-
end
|
67
|
-
|
68
|
-
# @!visibility public
|
69
|
-
# Exposes methods to the presenter, when defined all others will be hidden.
|
70
|
-
# @param :methods one ore more methods from the model to expose from the presenter.
|
71
|
-
# @example Expose methods:
|
72
|
-
# expose :first_name, :last_name
|
73
|
-
def self.expose(*exposed_methods)
|
74
|
-
EXPOSED_METHODS[name] ||= []
|
75
|
-
EXPOSED_METHODS[name] += exposed_methods.flatten
|
76
|
-
end
|
77
|
-
|
78
|
-
## Internal Methods: Not Documented
|
79
|
-
|
80
|
-
# @api private
|
81
|
-
def self.inherited(subclass)
|
82
|
-
presentable_class = subclass.name.gsub(/Presenter$/,'')
|
83
|
-
presentable_class.constantize rescue nil
|
84
|
-
subclass.presents presentable_class if Object.const_defined?(presentable_class)
|
85
|
-
end
|
86
|
-
|
87
|
-
## Inherited Object Getters
|
88
|
-
|
89
|
-
# Presentable Objects
|
90
|
-
# @api private
|
91
|
-
def self.presentable_objects
|
92
|
-
(PRESENTABLE[name] || [])
|
93
|
-
end
|
94
|
-
|
95
|
-
# Methods to expose
|
96
|
-
# @api private
|
97
|
-
def self.exposed_methods
|
98
|
-
EXPOSED_METHODS[name]
|
99
|
-
end
|
100
|
-
|
101
|
-
# Methods to hide
|
102
|
-
# @api private
|
103
|
-
def self.hidden_methods
|
104
|
-
HIDDEN_METHODS[name]
|
105
|
-
end
|
106
|
-
|
107
108
|
# Raises an error when hidden methods are called.
|
108
109
|
# @api private
|
109
110
|
def hide_methods
|
@@ -127,14 +128,13 @@ class DelegatedPresenter::Base < SimpleDelegator
|
|
127
128
|
# Determines if an object is presentable
|
128
129
|
# @api private
|
129
130
|
def object_is_presentable?(object)
|
130
|
-
|
131
|
+
presentable.include?(object.class.name)
|
131
132
|
end
|
132
133
|
|
133
134
|
# Extracts the the models unique methods from the superclass
|
134
135
|
# @api private
|
135
136
|
def unique_model_methods
|
136
|
-
|
137
|
-
attributes + (presented_model.methods - presented_model.class.superclass.instance_methods)
|
137
|
+
presented_model.methods - presented_model.class.superclass.instance_methods
|
138
138
|
end
|
139
139
|
|
140
140
|
# Maps an array of instances to delegated presented instances
|
@@ -2,30 +2,46 @@ module DelegatedPresenter::PresentsBeforeRendering
|
|
2
2
|
extend ActiveSupport::Concern
|
3
3
|
|
4
4
|
included do
|
5
|
-
|
6
|
-
self.presents_before_rendering = {}
|
5
|
+
alias_method_chain :render, :presentation
|
7
6
|
end
|
8
7
|
|
9
8
|
private
|
10
9
|
|
10
|
+
def presents_before_rendering
|
11
|
+
from_singleton = self.singleton_class.presents_before_rendering
|
12
|
+
from_class = self.class.presents_before_rendering
|
13
|
+
from_singleton.present? ? from_singleton : from_class
|
14
|
+
end
|
15
|
+
|
11
16
|
# Presents specified instance variables before rendering.
|
12
|
-
def
|
13
|
-
presents_before_rendering.each do |var,
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
end
|
20
|
-
end
|
17
|
+
def render_with_presentation(*args, &block)
|
18
|
+
presents_before_rendering.each do |var, options|
|
19
|
+
next if (
|
20
|
+
options.has_key?(:only) && !Array.wrap(options[:only]).include?(action_name.to_sym)
|
21
|
+
) || (
|
22
|
+
options.has_key?(:except) && Array.wrap(options[:except]).include?(action_name.to_sym)
|
23
|
+
) || (ivar = instance_variable_get "@#{var}").blank?
|
21
24
|
|
22
|
-
|
25
|
+
object_class = [ivar].flatten.collect(&:class).first.to_s
|
26
|
+
presenter = options.fetch(:with, "#{object_class}Presenter").to_s.classify.constantize
|
27
|
+
instance_variable_set "@#{var}", presenter.new(ivar)
|
28
|
+
end
|
29
|
+
render_without_presentation(*args, &block)
|
23
30
|
end
|
24
31
|
|
25
32
|
module ClassMethods
|
26
33
|
|
34
|
+
def presents_before_rendering
|
35
|
+
@presents_before_rendering ||= {} unless name == 'ActionController::Base'
|
36
|
+
end
|
37
|
+
|
27
38
|
private
|
28
39
|
|
40
|
+
def inherited_with_presentation(subclass)
|
41
|
+
subclass.instance_variable_set :@presents_before_rendering, presents_before_rendering
|
42
|
+
super
|
43
|
+
end
|
44
|
+
|
29
45
|
# @!visibility public
|
30
46
|
# Sets up a presenter for instance variables. By default it will try to determine the presenter but this can be overridden via the "*with*" option.
|
31
47
|
# @overload presents(instance_var1, instance_var2, [...])
|
@@ -35,10 +51,12 @@ module DelegatedPresenter::PresentsBeforeRendering
|
|
35
51
|
# Specifies which instance variables to present, assumes the presenter has a name of *InstanceClassPresenter*.
|
36
52
|
# @param instance_vars the instance variables to present.
|
37
53
|
# @option options [Symbol] :with The presenter to use.
|
54
|
+
# @option options [Symbol] :only the only action to present on.
|
55
|
+
# @option options [Symbol] :except these actions to present on.
|
38
56
|
def presents(*instance_vars)
|
39
|
-
options
|
40
|
-
instance_vars.
|
41
|
-
self.presents_before_rendering.
|
57
|
+
options = instance_vars.extract_options!
|
58
|
+
instance_vars.each do |var|
|
59
|
+
self.presents_before_rendering[var.to_sym] = options
|
42
60
|
end
|
43
61
|
end
|
44
62
|
|
@@ -1,21 +1,13 @@
|
|
1
1
|
class SampleObjectsController < ApplicationController
|
2
2
|
|
3
|
-
presents :instance, :collection
|
4
|
-
presents :inherited_instance, with: SampleObjectPresenter
|
5
|
-
|
6
3
|
def index
|
7
4
|
@collection = SampleObject.all
|
8
|
-
render text:
|
5
|
+
render text: "Hello World"
|
9
6
|
end
|
10
7
|
|
11
8
|
def show
|
12
9
|
@instance = SampleObject.find(params[:id])
|
13
|
-
render text:
|
14
|
-
end
|
15
|
-
|
16
|
-
def show_inherited
|
17
|
-
@inherited_instance = InheritedSampleObject.find(params[:id])
|
18
|
-
render text: true
|
10
|
+
render text: "Hello World"
|
19
11
|
end
|
20
12
|
|
21
13
|
end
|
@@ -9,8 +9,4 @@ class SampleObjectPresenter < DelegatedPresenter::Base
|
|
9
9
|
# The presenter will always look to the model it is presenting for methods and attributes not defined in the presenter.
|
10
10
|
# If you want to override model method, you can always call `presented_model.{method_name}` to access the original method.
|
11
11
|
|
12
|
-
def full_name
|
13
|
-
[prefix, first_name, middle_name, last_name, suffix].join(' ')
|
14
|
-
end
|
15
|
-
|
16
12
|
end
|
@@ -0,0 +1,96 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe DelegatedPresenter::Base do
|
4
|
+
|
5
|
+
let(:object_class) do
|
6
|
+
object_class = Class.new do
|
7
|
+
|
8
|
+
def foo
|
9
|
+
"foo"
|
10
|
+
end
|
11
|
+
|
12
|
+
def bar
|
13
|
+
"bar"
|
14
|
+
end
|
15
|
+
|
16
|
+
def baz
|
17
|
+
"baz"
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
stub_const 'Foo', object_class
|
23
|
+
end
|
24
|
+
let(:object_instance) { object_class.new }
|
25
|
+
let(:presenter) do
|
26
|
+
object_class
|
27
|
+
presenter = Class.new(DelegatedPresenter::Base) do
|
28
|
+
presents :foo
|
29
|
+
end
|
30
|
+
stub_const 'FooPresenter', presenter
|
31
|
+
end
|
32
|
+
let(:presented_object){ presenter.new(object_instance) }
|
33
|
+
|
34
|
+
describe '.hide' do
|
35
|
+
let(:presenter) do
|
36
|
+
object_class
|
37
|
+
presenter = Class.new(DelegatedPresenter::Base) do
|
38
|
+
presents :foo
|
39
|
+
hide :foo
|
40
|
+
end
|
41
|
+
stub_const 'FooPresenter', presenter
|
42
|
+
end
|
43
|
+
context 'when a hidden method is called' do
|
44
|
+
it 'should raise an error' do
|
45
|
+
expect { presented_object.foo }.to raise_error DelegatedPresenter::Error::MethodHidden
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
context 'when a non hidden method is called' do
|
50
|
+
it 'should raise an error' do
|
51
|
+
expect { presented_object.bar }.to_not raise_error DelegatedPresenter::Error::MethodHidden
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
describe '.expose' do
|
57
|
+
let(:presenter) do
|
58
|
+
object_class
|
59
|
+
presenter = Class.new(DelegatedPresenter::Base) do
|
60
|
+
presents :foo
|
61
|
+
expose :foo
|
62
|
+
end
|
63
|
+
stub_const 'FooPresenter', presenter
|
64
|
+
end
|
65
|
+
context 'when a exposed method is called' do
|
66
|
+
it 'should raise an error' do
|
67
|
+
expect { presented_object.foo }.to_not raise_error DelegatedPresenter::Error::MethodNotExposed
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
context 'when a non exposed method is called' do
|
72
|
+
it 'should raise an error' do
|
73
|
+
expect { presented_object.bar }.to raise_error DelegatedPresenter::Error::MethodNotExposed
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
describe '#class' do
|
79
|
+
it "should be the model class" do
|
80
|
+
presented_object.class.should == object_class
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
describe '#model_instance_method (first_name)' do
|
85
|
+
it 'Calls a method on the presenter' do
|
86
|
+
presented_object.foo.should be_present
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
describe '#model_instance_method (first_name)' do
|
91
|
+
it 'calls an instance in the model' do
|
92
|
+
presented_object.foo.should be_present
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe SampleObjectsController, type: :controller do
|
4
|
+
|
5
|
+
let(:presenter){
|
6
|
+
klass = Class.new(DelegatedPresenter::Base) do
|
7
|
+
presents SampleObject
|
8
|
+
end
|
9
|
+
stub_const("TestPresenter", klass)
|
10
|
+
klass
|
11
|
+
}
|
12
|
+
|
13
|
+
let(:options){ { with: presenter } }
|
14
|
+
|
15
|
+
before do
|
16
|
+
10.times { FactoryGirl.create(:sample_object) }
|
17
|
+
controller.singleton_class.send :presents, :instance, :collection, options
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'presents :collection', focus: true do
|
21
|
+
get :index
|
22
|
+
expect(assigns(:collection).presenter_class).to eq(presenter)
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'presents :instance' do
|
26
|
+
get :show, { id: 1 }
|
27
|
+
expect(assigns(:instance).presenter_class).to eq(presenter)
|
28
|
+
end
|
29
|
+
|
30
|
+
context 'with options' do
|
31
|
+
|
32
|
+
context 'given options[:with]' do
|
33
|
+
|
34
|
+
let(:other_presenter) do
|
35
|
+
klass = Class.new(DelegatedPresenter::Base) do
|
36
|
+
presents SampleObject
|
37
|
+
end
|
38
|
+
stub_const("OtherPresenter", klass)
|
39
|
+
klass
|
40
|
+
end
|
41
|
+
let(:options){ { with: other_presenter } }
|
42
|
+
|
43
|
+
it 'should use the other presenter' do
|
44
|
+
other_presenter.should_receive(:new)
|
45
|
+
get :show, { id: 1 }
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
context 'given options[:only]' do
|
50
|
+
let(:options){ { only: :show } }
|
51
|
+
|
52
|
+
it 'should only present on the specified actions' do
|
53
|
+
SampleObjectPresenter.should_receive(:new).once.and_call_original
|
54
|
+
get :show, { id: 1 }
|
55
|
+
end
|
56
|
+
|
57
|
+
it 'not present on actions not specified' do
|
58
|
+
SampleObjectPresenter.should_receive(:new).never
|
59
|
+
get :index
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
context 'given options[:except]' do
|
64
|
+
let(:options){ { except: :show } }
|
65
|
+
|
66
|
+
it 'should not present on the specified actions' do
|
67
|
+
SampleObjectPresenter.should_receive(:new).never
|
68
|
+
get :show, { id: 1 }
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
end
|
73
|
+
|
74
|
+
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: delegated_presenter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2013-02-15 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
@@ -29,6 +29,22 @@ dependencies:
|
|
29
29
|
version: '0'
|
30
30
|
- !ruby/object:Gem::Dependency
|
31
31
|
name: rspec
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: 2.12.0
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 2.12.0
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: simplecov
|
32
48
|
requirement: !ruby/object:Gem::Requirement
|
33
49
|
none: false
|
34
50
|
requirements:
|
@@ -124,7 +140,23 @@ dependencies:
|
|
124
140
|
- !ruby/object:Gem::Version
|
125
141
|
version: '0'
|
126
142
|
- !ruby/object:Gem::Dependency
|
127
|
-
name:
|
143
|
+
name: guard-rspec
|
144
|
+
requirement: !ruby/object:Gem::Requirement
|
145
|
+
none: false
|
146
|
+
requirements:
|
147
|
+
- - ! '>='
|
148
|
+
- !ruby/object:Gem::Version
|
149
|
+
version: '0'
|
150
|
+
type: :development
|
151
|
+
prerelease: false
|
152
|
+
version_requirements: !ruby/object:Gem::Requirement
|
153
|
+
none: false
|
154
|
+
requirements:
|
155
|
+
- - ! '>='
|
156
|
+
- !ruby/object:Gem::Version
|
157
|
+
version: '0'
|
158
|
+
- !ruby/object:Gem::Dependency
|
159
|
+
name: rb-fsevent
|
128
160
|
requirement: !ruby/object:Gem::Requirement
|
129
161
|
none: false
|
130
162
|
requirements:
|
@@ -151,6 +183,7 @@ files:
|
|
151
183
|
- .rspec
|
152
184
|
- .travis.yml
|
153
185
|
- Gemfile
|
186
|
+
- Guardfile
|
154
187
|
- LICENSE.txt
|
155
188
|
- MIT-LICENSE
|
156
189
|
- README.md
|
@@ -185,8 +218,8 @@ files:
|
|
185
218
|
- spec/dummy/log/.gitkeep
|
186
219
|
- spec/dummy/script/rails
|
187
220
|
- spec/factories/sample_object.rb
|
188
|
-
- spec/
|
189
|
-
- spec/presents_before_rendering_spec.rb
|
221
|
+
- spec/lib/delegated_presenter/base_spec.rb
|
222
|
+
- spec/lib/delegated_presenter/presents_before_rendering_spec.rb
|
190
223
|
- spec/spec_helper.rb
|
191
224
|
homepage: http://github.com/jwaldrip/delegated_presenter
|
192
225
|
licenses: []
|
@@ -233,7 +266,6 @@ test_files:
|
|
233
266
|
- spec/dummy/log/.gitkeep
|
234
267
|
- spec/dummy/script/rails
|
235
268
|
- spec/factories/sample_object.rb
|
236
|
-
- spec/
|
237
|
-
- spec/presents_before_rendering_spec.rb
|
269
|
+
- spec/lib/delegated_presenter/base_spec.rb
|
270
|
+
- spec/lib/delegated_presenter/presents_before_rendering_spec.rb
|
238
271
|
- spec/spec_helper.rb
|
239
|
-
has_rdoc:
|
data/spec/presenter_spec.rb
DELETED
@@ -1,104 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
class SpecObject
|
4
|
-
|
5
|
-
attr_accessor :exposed_attr, :hidden_attr, :model_instance_method
|
6
|
-
|
7
|
-
end
|
8
|
-
|
9
|
-
class SpecObjectPresenter < DelegatedPresenter::Base
|
10
|
-
|
11
|
-
def presenter_method
|
12
|
-
end
|
13
|
-
|
14
|
-
end
|
15
|
-
|
16
|
-
class SpecObjectWithHiddenPresenter < DelegatedPresenter::Base
|
17
|
-
|
18
|
-
presents SpecObject
|
19
|
-
hide :hidden_attr
|
20
|
-
|
21
|
-
end
|
22
|
-
|
23
|
-
class SpecObjectWithExposedPresenter < DelegatedPresenter::Base
|
24
|
-
|
25
|
-
presents SpecObject
|
26
|
-
expose :exposed_attr
|
27
|
-
|
28
|
-
end
|
29
|
-
|
30
|
-
describe SpecObjectPresenter do
|
31
|
-
|
32
|
-
let(:object) { SpecObject.new }
|
33
|
-
subject{ SpecObjectPresenter.new(object) }
|
34
|
-
|
35
|
-
context "presenter method" do
|
36
|
-
it '#presenter_method' do
|
37
|
-
subject.respond_to?(:presenter_method).should == true
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
context "model instance method" do
|
42
|
-
it '#model_instance_method' do
|
43
|
-
subject.respond_to?(:model_instance_method).should == true
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
|
-
end
|
48
|
-
|
49
|
-
describe SpecObjectWithHiddenPresenter do
|
50
|
-
|
51
|
-
describe '.hide' do
|
52
|
-
it "should hide methods" do
|
53
|
-
SpecObjectWithHiddenPresenter.hidden_methods.include?(:hidden_attr).should == true
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
|
-
context 'hides methods' do
|
58
|
-
let(:object) { SpecObject.new }
|
59
|
-
subject { SpecObjectWithHiddenPresenter.new(object) }
|
60
|
-
|
61
|
-
describe '#hidden_attr' do
|
62
|
-
it "should raise an error" do
|
63
|
-
expect{ subject.hidden_attr }.to raise_error(DelegatedPresenter::Error::MethodHidden)
|
64
|
-
end
|
65
|
-
end
|
66
|
-
|
67
|
-
describe '#model_instance_method' do
|
68
|
-
it "should not raise an error" do
|
69
|
-
expect{ subject.model_instance_method }.to_not raise_error(DelegatedPresenter::Error::MethodHidden)
|
70
|
-
end
|
71
|
-
end
|
72
|
-
|
73
|
-
end
|
74
|
-
|
75
|
-
end
|
76
|
-
|
77
|
-
|
78
|
-
describe SpecObjectWithExposedPresenter do
|
79
|
-
|
80
|
-
describe '.expose' do
|
81
|
-
it "should expose methods" do
|
82
|
-
SpecObjectWithExposedPresenter.exposed_methods.include?(:exposed_attr).should == true
|
83
|
-
end
|
84
|
-
end
|
85
|
-
|
86
|
-
context 'exposes methods' do
|
87
|
-
let(:object) { SpecObject.new }
|
88
|
-
subject { SpecObjectWithExposedPresenter.new(object) }
|
89
|
-
|
90
|
-
describe '#exposed_attr' do
|
91
|
-
it "should not raise an error" do
|
92
|
-
expect{ subject.exposed_attr }.to_not raise_error(DelegatedPresenter::Error::MethodNotExposed)
|
93
|
-
end
|
94
|
-
end
|
95
|
-
|
96
|
-
describe '#model_instance_method' do
|
97
|
-
it "should raise an error" do
|
98
|
-
expect{ subject.model_instance_method }.to raise_error(DelegatedPresenter::Error::MethodNotExposed)
|
99
|
-
end
|
100
|
-
end
|
101
|
-
|
102
|
-
end
|
103
|
-
|
104
|
-
end
|
@@ -1,36 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe SampleObjectsController, type: :controller do
|
4
|
-
|
5
|
-
before do
|
6
|
-
10.times { FactoryGirl.create(:sample_object) }
|
7
|
-
10.times { FactoryGirl.create(:inherited_sample_object) }
|
8
|
-
end
|
9
|
-
|
10
|
-
context "presents before rendering" do
|
11
|
-
|
12
|
-
describe "#render (collection)" do
|
13
|
-
it 'presents @collection' do
|
14
|
-
get :index
|
15
|
-
assigns(:collection).presenter_class.should eq(SampleObjectPresenter)
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
describe "#render (instance)" do
|
20
|
-
it 'presents @instance' do
|
21
|
-
get :show, { id: SampleObject.first.id }
|
22
|
-
assigns(:instance).presenter_class.should eq(SampleObjectPresenter)
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
describe "#render (instance with specified presenter)" do
|
27
|
-
it 'presents @with_presenter' do
|
28
|
-
get :show_inherited, { id: InheritedSampleObject.first.id }
|
29
|
-
assigns(:inherited_instance).presenter_class.should eq(SampleObjectPresenter)
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
|
34
|
-
end
|
35
|
-
|
36
|
-
end
|