presenter-pattern 0.2.1 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Manifest +2 -2
- data/Rakefile +2 -2
- data/lib/presenter-pattern.rb +64 -3
- data/presenter-pattern.gemspec +3 -3
- data/test/bad_controller_test.rb +39 -0
- data/test/foos_controller_test.rb +61 -0
- metadata +5 -5
- data/test/no_querying_view_test.rb +0 -22
- data/test/only_data_in_view_test.rb +0 -23
data/Manifest
CHANGED
@@ -4,8 +4,8 @@ lib/presenter-pattern.rb
|
|
4
4
|
lib/presenter-pattern/railtie.rb
|
5
5
|
presenter-pattern.gemspec
|
6
6
|
rails/init.rb
|
7
|
-
test/
|
8
|
-
test/
|
7
|
+
test/bad_controller_test.rb
|
8
|
+
test/foos_controller_test.rb
|
9
9
|
test/setup_test.rb
|
10
10
|
test/test_helper.rb
|
11
11
|
Manifest
|
data/Rakefile
CHANGED
@@ -2,12 +2,12 @@ require 'rubygems'
|
|
2
2
|
require 'rake'
|
3
3
|
require 'echoe'
|
4
4
|
|
5
|
-
Echoe.new('presenter-pattern', '0.
|
5
|
+
Echoe.new('presenter-pattern', '0.3.0') 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"
|
9
9
|
p.email = "josh.leven@gmail.com"
|
10
|
-
p.ignore_pattern = /^(tmp|pkg|doc|test\/rails_app)|(\.svn|CVS|\.bzr|\.DS|\.git)$/
|
10
|
+
p.ignore_pattern = /^(tmp|pkg|doc|test\/rails_app|Gemfile)|(\.svn|CVS|\.bzr|\.DS|\.git)$/
|
11
11
|
p.development_dependencies = []
|
12
12
|
end
|
13
13
|
|
data/lib/presenter-pattern.rb
CHANGED
@@ -1,9 +1,70 @@
|
|
1
1
|
require 'presenter-pattern/railtie'
|
2
2
|
|
3
|
+
|
4
|
+
# PresenterPattern::API enables and enforces very simple api controllers.
|
5
|
+
# Essentially, your controller has the settings:
|
6
|
+
# layout nil
|
7
|
+
# responds_to *formats <--- formats is specified in step 1 below
|
8
|
+
#
|
9
|
+
# and each action ends with an implicit:
|
10
|
+
# responds_with <return_value_from_action>
|
11
|
+
#
|
12
|
+
#
|
13
|
+
# 1. In your controller, include PresenterPattern::API, passing in the formats your api
|
14
|
+
# will support using the bracket operator (defaults to :xml and :json)
|
15
|
+
#
|
16
|
+
# e.g. include PresenterPattern::API[:html, :json]
|
17
|
+
#
|
18
|
+
# 2. Each action must return the data you wish for your api to return.
|
19
|
+
# Do NOT call render from within your actions.
|
20
|
+
#
|
21
|
+
|
22
|
+
|
3
23
|
module PresenterPattern
|
4
|
-
|
5
|
-
|
6
|
-
|
24
|
+
module API
|
25
|
+
class NoExplicitRender < RuntimeError; end
|
26
|
+
|
27
|
+
def self.[](*formats)
|
28
|
+
custom_api = self.dup
|
29
|
+
custom_api.class_eval do
|
30
|
+
define_singleton_method :included do |host_class|
|
31
|
+
host_class.layout nil
|
32
|
+
host_class.respond_to *formats
|
33
|
+
end
|
34
|
+
end
|
35
|
+
custom_api
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.included(host_class)
|
39
|
+
host_class.layout nil
|
40
|
+
host_class.respond_to :xml, :json
|
41
|
+
end
|
42
|
+
|
43
|
+
# enforces simple api response
|
44
|
+
#
|
45
|
+
# each action must return the data meant for their response
|
46
|
+
#
|
47
|
+
# - overwrites ActionController::Metal::ImplicitRender
|
48
|
+
def send_action(method_name, *args)
|
49
|
+
#call the action, and store return value
|
50
|
+
@__rval = self.send(method_name, *args)
|
51
|
+
|
52
|
+
#fail if action calls 'render'
|
53
|
+
raise NoExplicitRender, "Controllers implementing the PresenterPattern::API must not call any render methods" if response_body
|
54
|
+
|
55
|
+
#always follow responder pattern passing in the action's return value
|
56
|
+
respond_with @__rval, (@respond_with_opts || {})
|
57
|
+
end
|
58
|
+
|
59
|
+
def respond_opts(options)
|
60
|
+
@respond_with_opts ||= {}
|
61
|
+
@respond_with_opts.merge! options
|
62
|
+
end
|
63
|
+
|
64
|
+
#only the @__rval variable (set in send_action) is passed through to the view
|
65
|
+
def view_assigns
|
66
|
+
{"data" => @__rval}
|
67
|
+
end
|
7
68
|
end
|
8
69
|
end
|
9
70
|
|
data/presenter-pattern.gemspec
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = %q{presenter-pattern}
|
5
|
-
s.version = "0.
|
5
|
+
s.version = "0.3.0"
|
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"]
|
@@ -10,14 +10,14 @@ Gem::Specification.new do |s|
|
|
10
10
|
s.description = %q{Enables and enforces the presenter pattern in rails}
|
11
11
|
s.email = %q{josh.leven@gmail.com}
|
12
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/
|
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"]
|
17
17
|
s.rubyforge_project = %q{presenter-pattern}
|
18
18
|
s.rubygems_version = %q{1.7.2}
|
19
19
|
s.summary = %q{Enables and enforces the presenter pattern in rails}
|
20
|
-
s.test_files = ["test/
|
20
|
+
s.test_files = ["test/bad_controller_test.rb", "test/foos_controller_test.rb", "test/setup_test.rb", "test/test_helper.rb"]
|
21
21
|
|
22
22
|
if s.respond_to? :specification_version then
|
23
23
|
s.specification_version = 3
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class BadControllerTest < ActionController::TestCase
|
4
|
+
tests BadController
|
5
|
+
|
6
|
+
def setup
|
7
|
+
super
|
8
|
+
@name = "Hello World!"
|
9
|
+
@foo = Foo.create :name => @name
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_data_variable_in_view
|
13
|
+
get :good, :id => @foo.id
|
14
|
+
assert_response :success
|
15
|
+
assert_equal @name, @response.body
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_only_data_variable_in_view
|
19
|
+
get :no_vars, :id => @foo.id
|
20
|
+
assert_equal "", @response.body
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_empty_action
|
24
|
+
get :empty
|
25
|
+
assert_response :success
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_query_in_view_fails
|
29
|
+
assert_raise(ActionView::Template::Error) do
|
30
|
+
get :query
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_no_explicit_render
|
35
|
+
assert_raise(PresenterPattern::API::NoExplicitRender) do
|
36
|
+
get :explicit
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class FoosControllerTest < ActionController::TestCase
|
4
|
+
tests FoosController
|
5
|
+
|
6
|
+
def setup
|
7
|
+
super
|
8
|
+
@name = "Name"
|
9
|
+
@foo = Foo.create :name => @name
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_index
|
13
|
+
get :index
|
14
|
+
assert_response :success
|
15
|
+
assert_equal @name, @response.body
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_show
|
19
|
+
get :show, :id => @foo.id
|
20
|
+
assert_response :success
|
21
|
+
assert_equal @name, @response.body
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_new
|
25
|
+
get :new
|
26
|
+
assert_response :success
|
27
|
+
assert_equal "", @response.body
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_create_success
|
31
|
+
post :create, :foo => @foo.attributes, :format => :json
|
32
|
+
assert_response :success
|
33
|
+
assert_equal @foo.to_json, @response.body
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_create_failure
|
37
|
+
post :create, :foo => {}, :format => :json
|
38
|
+
assert_response :unprocessable_entity
|
39
|
+
assert_equal "{\"name\":[\"can't be blank\"]}", @response.body
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_create_failure_ajax
|
43
|
+
xhr :post, :create, :foo => {}, :format => :json
|
44
|
+
assert_response :unprocessable_entity
|
45
|
+
assert_equal "{\"name\":[\"can't be blank\"]}", @response.body
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_respond_opts
|
49
|
+
get :edit, :id => @foo.id, :format => :json
|
50
|
+
assert_response 206
|
51
|
+
assert_equal @foo.to_json, @response.body
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_destroy
|
55
|
+
assert_difference('Foo.count', -1) do
|
56
|
+
delete :destroy, :id => @foo.id, :format => :json
|
57
|
+
end
|
58
|
+
assert_response :success
|
59
|
+
assert_equal "{}", @response.body
|
60
|
+
end
|
61
|
+
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.
|
4
|
+
version: 0.3.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -26,8 +26,8 @@ files:
|
|
26
26
|
- lib/presenter-pattern/railtie.rb
|
27
27
|
- presenter-pattern.gemspec
|
28
28
|
- rails/init.rb
|
29
|
-
- test/
|
30
|
-
- test/
|
29
|
+
- test/bad_controller_test.rb
|
30
|
+
- test/foos_controller_test.rb
|
31
31
|
- test/setup_test.rb
|
32
32
|
- test/test_helper.rb
|
33
33
|
- Manifest
|
@@ -62,7 +62,7 @@ signing_key:
|
|
62
62
|
specification_version: 3
|
63
63
|
summary: Enables and enforces the presenter pattern in rails
|
64
64
|
test_files:
|
65
|
-
- test/
|
66
|
-
- test/
|
65
|
+
- test/bad_controller_test.rb
|
66
|
+
- test/foos_controller_test.rb
|
67
67
|
- test/setup_test.rb
|
68
68
|
- test/test_helper.rb
|
@@ -1,22 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class NoQueryingViewTest < ActionController::TestCase
|
4
|
-
tests FoosController
|
5
|
-
|
6
|
-
def setup
|
7
|
-
super
|
8
|
-
@foo = Foo.create :name => "name"
|
9
|
-
end
|
10
|
-
|
11
|
-
def test_thin_views_are_successful
|
12
|
-
get :show, :id => @foo.id
|
13
|
-
assert_response :success
|
14
|
-
end
|
15
|
-
|
16
|
-
def test_thick_views_fail
|
17
|
-
assert_raise(ActionView::Template::Error) do
|
18
|
-
get :index
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
end
|
@@ -1,23 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class OnlyDataInViewTest < ActionController::TestCase
|
4
|
-
tests FoosController
|
5
|
-
|
6
|
-
def setup
|
7
|
-
super
|
8
|
-
@name = "Hello World!"
|
9
|
-
@foo = Foo.create :name => @name
|
10
|
-
end
|
11
|
-
|
12
|
-
def test_data_variable_in_view
|
13
|
-
get :show, :id => @foo.id
|
14
|
-
assert_response :success
|
15
|
-
assert_equal @name, @response.body
|
16
|
-
end
|
17
|
-
|
18
|
-
def test_only_data_variable_in_view
|
19
|
-
get :edit, :id => @foo.id
|
20
|
-
assert_equal "", @response.body
|
21
|
-
end
|
22
|
-
|
23
|
-
end
|