presenter-pattern 0.3.3 → 0.3.4
Sign up to get free protection for your applications and to get access to all the features.
- data/Manifest +1 -1
- data/Rakefile +1 -1
- data/lib/presenter-pattern/railtie.rb +1 -1
- data/lib/presenter-pattern.rb +14 -23
- data/presenter-pattern.gemspec +3 -3
- data/test/bad_controller_test.rb +6 -7
- data/test/foos_controller_test.rb +0 -1
- metadata +2 -4
- data/lib/presenter-pattern/test_case.rb +0 -11
data/Manifest
CHANGED
data/Rakefile
CHANGED
@@ -2,7 +2,7 @@ require 'rubygems'
|
|
2
2
|
require 'rake'
|
3
3
|
require 'echoe'
|
4
4
|
|
5
|
-
Echoe.new('presenter-pattern', '0.3.
|
5
|
+
Echoe.new('presenter-pattern', '0.3.4') do |p|
|
6
6
|
p.description = "Enables and enforces the presenter pattern in rails"
|
7
7
|
p.url = "https://github.com/jleven/presenter-pattern"
|
8
8
|
p.author = "Josh Leven"
|
@@ -31,7 +31,7 @@ module PresenterPattern
|
|
31
31
|
|
32
32
|
# if we're coming from a view, let's analyze the situation
|
33
33
|
if !first_view.nil? #and (first_helper.nil? or (caller.index(first_view) < caller.index(first_helper)))
|
34
|
-
raise PresenterPattern::IllegalDatabaseQueryFromView, "
|
34
|
+
raise PresenterPattern::IllegalDatabaseQueryFromView, "DB query from view prohibited, eager-load from a controller instead."
|
35
35
|
else
|
36
36
|
orig_execute *args
|
37
37
|
end
|
data/lib/presenter-pattern.rb
CHANGED
@@ -1,5 +1,4 @@
|
|
1
1
|
require 'presenter-pattern/railtie'
|
2
|
-
require 'presenter-pattern/test_case'
|
3
2
|
|
4
3
|
# PresenterPattern::API enables and enforces very simple api controllers.
|
5
4
|
# Essentially, your controller has the settings:
|
@@ -8,7 +7,7 @@ require 'presenter-pattern/test_case'
|
|
8
7
|
#
|
9
8
|
# and each action ends with an implicit:
|
10
9
|
# responds_with <return_value_from_action>
|
11
|
-
#
|
10
|
+
# unless response created explicitly
|
12
11
|
#
|
13
12
|
# 1. In your controller, include PresenterPattern::API, passing in the formats your api
|
14
13
|
# will support using the bracket operator (defaults to :xml and :json)
|
@@ -16,14 +15,11 @@ require 'presenter-pattern/test_case'
|
|
16
15
|
# e.g. include PresenterPattern::API[:html, :json]
|
17
16
|
#
|
18
17
|
# 2. Each action must return the data you wish for your api to return.
|
19
|
-
# Do NOT call render from within your actions.
|
20
18
|
#
|
21
19
|
|
22
20
|
|
23
21
|
module PresenterPattern
|
24
22
|
module API
|
25
|
-
class NoExplicitRender < RuntimeError; end
|
26
|
-
|
27
23
|
def self.[](*formats)
|
28
24
|
custom_api = self.dup
|
29
25
|
custom_api.class_eval do
|
@@ -54,31 +50,26 @@ module PresenterPattern
|
|
54
50
|
#
|
55
51
|
# each action must return the data meant for their response
|
56
52
|
#
|
57
|
-
# - overwrites ActionController::Metal::ImplicitRender
|
53
|
+
# - this overwrites ActionController::Metal::ImplicitRender
|
58
54
|
def send_action(method_name, *args)
|
59
55
|
#call the action, and store return value
|
60
|
-
|
61
|
-
|
62
|
-
#fail if action calls 'render'
|
63
|
-
raise NoExplicitRender, "Controllers implementing the PresenterPattern::API must not call any render methods" if response_body
|
56
|
+
rval = self.send(method_name, *args)
|
64
57
|
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
58
|
+
unless response_body
|
59
|
+
#set the view variable with the return value
|
60
|
+
name = self.class.instance_variable_get(:@view_var_name)
|
61
|
+
name = name.pluralize if name && method_name.to_s == "index" #pluralize the @view_var_name if it was set at the class and this is GET/index
|
62
|
+
name ||= "data"
|
63
|
+
self.instance_variable_set :"@#{name}", rval
|
69
64
|
|
70
|
-
|
71
|
-
|
65
|
+
#always follow responder pattern passing in the action's return value unless already rendered
|
66
|
+
respond_with rval, (@respond_opts || {})
|
67
|
+
end
|
72
68
|
end
|
73
69
|
|
74
70
|
def respond_opts(options)
|
75
|
-
@
|
76
|
-
@
|
77
|
-
end
|
78
|
-
|
79
|
-
#only the @__rval variable (set in send_action) is passed through to the view
|
80
|
-
def view_assigns
|
81
|
-
{@view_var_name => @__rval}
|
71
|
+
@respond_opts ||= {}
|
72
|
+
@respond_opts.merge! options
|
82
73
|
end
|
83
74
|
end
|
84
75
|
end
|
data/presenter-pattern.gemspec
CHANGED
@@ -2,15 +2,15 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = %q{presenter-pattern}
|
5
|
-
s.version = "0.3.
|
5
|
+
s.version = "0.3.4"
|
6
6
|
|
7
7
|
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
8
|
s.authors = ["Josh Leven"]
|
9
9
|
s.date = %q{2011-05-26}
|
10
10
|
s.description = %q{Enables and enforces the presenter pattern in rails}
|
11
11
|
s.email = %q{josh.leven@gmail.com}
|
12
|
-
s.extra_rdoc_files = ["README.rdoc", "lib/presenter-pattern.rb", "lib/presenter-pattern/railtie.rb"
|
13
|
-
s.files = ["README.rdoc", "Rakefile", "lib/presenter-pattern.rb", "lib/presenter-pattern/railtie.rb", "
|
12
|
+
s.extra_rdoc_files = ["README.rdoc", "lib/presenter-pattern.rb", "lib/presenter-pattern/railtie.rb"]
|
13
|
+
s.files = ["README.rdoc", "Rakefile", "lib/presenter-pattern.rb", "lib/presenter-pattern/railtie.rb", "presenter-pattern.gemspec", "rails/init.rb", "test/bad_controller_test.rb", "test/foos_controller_test.rb", "test/setup_test.rb", "test/test_helper.rb", "Manifest"]
|
14
14
|
s.homepage = %q{https://github.com/jleven/presenter-pattern}
|
15
15
|
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Presenter-pattern", "--main", "README.rdoc"]
|
16
16
|
s.require_paths = ["lib"]
|
data/test/bad_controller_test.rb
CHANGED
@@ -15,9 +15,9 @@ class BadControllerTest < ActionController::TestCase
|
|
15
15
|
assert_equal @name, @response.body
|
16
16
|
end
|
17
17
|
|
18
|
-
def
|
19
|
-
get :
|
20
|
-
assert_equal "", @response.body
|
18
|
+
def test_non_data_variable_in_view
|
19
|
+
get :vars, :id => @foo.id
|
20
|
+
assert_equal "I can see this in the view!", @response.body
|
21
21
|
end
|
22
22
|
|
23
23
|
def test_empty_action
|
@@ -31,9 +31,8 @@ class BadControllerTest < ActionController::TestCase
|
|
31
31
|
end
|
32
32
|
end
|
33
33
|
|
34
|
-
def
|
35
|
-
|
36
|
-
|
37
|
-
end
|
34
|
+
def test_explicit_render
|
35
|
+
get :explicit
|
36
|
+
assert_nil assigns(:data) #shouldn't set @data even though there was a return value since render was called explicitly
|
38
37
|
end
|
39
38
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: presenter-pattern
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -19,20 +19,18 @@ extra_rdoc_files:
|
|
19
19
|
- README.rdoc
|
20
20
|
- lib/presenter-pattern.rb
|
21
21
|
- lib/presenter-pattern/railtie.rb
|
22
|
-
- lib/presenter-pattern/test_case.rb
|
23
22
|
files:
|
24
23
|
- README.rdoc
|
25
24
|
- Rakefile
|
26
25
|
- lib/presenter-pattern.rb
|
27
26
|
- lib/presenter-pattern/railtie.rb
|
28
|
-
-
|
27
|
+
- presenter-pattern.gemspec
|
29
28
|
- rails/init.rb
|
30
29
|
- test/bad_controller_test.rb
|
31
30
|
- test/foos_controller_test.rb
|
32
31
|
- test/setup_test.rb
|
33
32
|
- test/test_helper.rb
|
34
33
|
- Manifest
|
35
|
-
- presenter-pattern.gemspec
|
36
34
|
homepage: https://github.com/jleven/presenter-pattern
|
37
35
|
licenses: []
|
38
36
|
post_install_message:
|
@@ -1,11 +0,0 @@
|
|
1
|
-
module PresenterPattern
|
2
|
-
module TestCase
|
3
|
-
def assigns(key = nil)
|
4
|
-
name = @controller.instance_variable_get :@view_var_name
|
5
|
-
value = @controller.instance_variable_get :@__rval
|
6
|
-
assigns = {}.with_indifferent_access
|
7
|
-
assigns[name] = value
|
8
|
-
key.nil? ? assigns : assigns[key]
|
9
|
-
end
|
10
|
-
end
|
11
|
-
end
|