presenter-pattern 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Manifest +6 -2
- data/Rakefile +1 -1
- data/lib/json-builder/handler.rb +11 -0
- data/lib/json-builder/railtie.rb +12 -0
- data/lib/presenter-pattern/railtie.rb +48 -0
- data/lib/presenter-pattern.rb +5 -3
- data/presenter-pattern.gemspec +6 -6
- data/test/json_responder_test.rb +17 -0
- data/test/json_view_test.rb +17 -0
- data/test/no_querying_view_test.rb +0 -15
- data/test/only_data_in_view_test.rb +23 -0
- data/test/test_helper.rb +1 -4
- metadata +15 -6
- data/lib/presenter-pattern/no_querying_views.rb +0 -38
- data/test/schema.rb +0 -11
data/Manifest
CHANGED
@@ -1,10 +1,14 @@
|
|
1
1
|
README.rdoc
|
2
2
|
Rakefile
|
3
|
+
lib/json-builder/handler.rb
|
4
|
+
lib/json-builder/railtie.rb
|
3
5
|
lib/presenter-pattern.rb
|
4
|
-
lib/presenter-pattern/
|
6
|
+
lib/presenter-pattern/railtie.rb
|
5
7
|
rails/init.rb
|
8
|
+
test/json_responder_test.rb
|
9
|
+
test/json_view_test.rb
|
6
10
|
test/no_querying_view_test.rb
|
7
|
-
test/
|
11
|
+
test/only_data_in_view_test.rb
|
8
12
|
test/setup_test.rb
|
9
13
|
test/test_helper.rb
|
10
14
|
Manifest
|
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.
|
5
|
+
Echoe.new('presenter-pattern', '0.2.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"
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'rails/railtie'
|
2
|
+
|
3
|
+
|
4
|
+
module PresenterPattern
|
5
|
+
class IllegalDatabaseQueryFromView < RuntimeError; end
|
6
|
+
|
7
|
+
class Railtie < Rails::Railtie
|
8
|
+
config.after_initialize do
|
9
|
+
|
10
|
+
if Rails.env.development? || Rails.env.test?
|
11
|
+
|
12
|
+
adapters = begin
|
13
|
+
::ActiveRecord::ConnectionAdapters::AbstractAdapter.subclasses
|
14
|
+
rescue
|
15
|
+
#ActiveRecord not loaded
|
16
|
+
[]
|
17
|
+
end
|
18
|
+
|
19
|
+
if adapters.any?
|
20
|
+
|
21
|
+
puts "\n[WARNING] PresenterPattern is preventing your views from triggering a database query through the following adapters:"
|
22
|
+
adapters.map{|a| a.name.split('::').last}.each {|name| puts " * #{name}"}
|
23
|
+
puts
|
24
|
+
|
25
|
+
adapters.each do |adapter|
|
26
|
+
adapter.class_eval do
|
27
|
+
alias :orig_execute :execute
|
28
|
+
def execute(*args)
|
29
|
+
first_view = caller.grep(/app\/views/).first
|
30
|
+
#first_helper = caller.grep(/app\/helpers/).first
|
31
|
+
|
32
|
+
# if we're coming from a view, let's analyze the situation
|
33
|
+
if !first_view.nil? #and (first_helper.nil? or (caller.index(first_view) < caller.index(first_helper)))
|
34
|
+
raise PresenterPattern::IllegalDatabaseQueryFromView, "No query from view prohibited, eager-load from a controller instead."
|
35
|
+
else
|
36
|
+
orig_execute *args
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
data/lib/presenter-pattern.rb
CHANGED
@@ -1,8 +1,10 @@
|
|
1
|
-
require 'presenter-pattern/
|
1
|
+
require 'presenter-pattern/railtie'
|
2
|
+
require 'json-builder/railtie'
|
2
3
|
|
3
4
|
module PresenterPattern
|
4
|
-
|
5
|
-
|
5
|
+
def protected_instance_variables
|
6
|
+
return instance_variable_names - ["@data"] if respond_to?(:instance_variable_names)
|
7
|
+
super
|
6
8
|
end
|
7
9
|
end
|
8
10
|
|
data/presenter-pattern.gemspec
CHANGED
@@ -2,22 +2,22 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = %q{presenter-pattern}
|
5
|
-
s.version = "0.
|
5
|
+
s.version = "0.2.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"]
|
9
|
-
s.date = %q{2011-05-
|
9
|
+
s.date = %q{2011-05-25}
|
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/
|
13
|
-
s.files = ["README.rdoc", "Rakefile", "lib/presenter-pattern.rb", "lib/presenter-pattern/
|
12
|
+
s.extra_rdoc_files = ["README.rdoc", "lib/json-builder/handler.rb", "lib/json-builder/railtie.rb", "lib/presenter-pattern.rb", "lib/presenter-pattern/railtie.rb"]
|
13
|
+
s.files = ["README.rdoc", "Rakefile", "lib/json-builder/handler.rb", "lib/json-builder/railtie.rb", "lib/presenter-pattern.rb", "lib/presenter-pattern/railtie.rb", "rails/init.rb", "test/json_responder_test.rb", "test/json_view_test.rb", "test/no_querying_view_test.rb", "test/only_data_in_view_test.rb", "test/setup_test.rb", "test/test_helper.rb", "Manifest", "presenter-pattern.gemspec"]
|
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
|
-
s.rubygems_version = %q{1.
|
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/no_querying_view_test.rb", "test/setup_test.rb", "test/test_helper.rb"]
|
20
|
+
s.test_files = ["test/json_responder_test.rb", "test/json_view_test.rb", "test/no_querying_view_test.rb", "test/only_data_in_view_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,17 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class JsonResponderTest < ActionController::TestCase
|
4
|
+
tests BarsController
|
5
|
+
|
6
|
+
def setup
|
7
|
+
super
|
8
|
+
@name = "Hello World!"
|
9
|
+
@bar = Bar.create :name => @name
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_data_variable_in_view
|
13
|
+
get :show, :id => @bar.id, :format => 'json'
|
14
|
+
assert_response :success
|
15
|
+
assert_equal '{"name":"Hello World!","fruit":"banana"}', @response.body
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class JsonViewTest < 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, :format => 'json'
|
14
|
+
assert_response :success
|
15
|
+
assert_equal '{"name":"Hello World!","array":["a","c"],"hash":{"value1":1,"value2":["a","b","c"]}}', @response.body
|
16
|
+
end
|
17
|
+
end
|
@@ -5,24 +5,9 @@ class NoQueryingViewTest < ActionController::TestCase
|
|
5
5
|
|
6
6
|
def setup
|
7
7
|
super
|
8
|
-
#Foo.delete_all
|
9
|
-
|
10
|
-
# @controller = FoosController.new
|
11
|
-
# @request = ActionController::TestRequest.new
|
12
|
-
# @response = ActionController::TestResponse.new
|
13
|
-
|
14
|
-
# PresenterPattern::Application.routes.draw do
|
15
|
-
# resource :foo
|
16
|
-
# end
|
17
|
-
|
18
8
|
@foo = Foo.create :name => "name"
|
19
9
|
end
|
20
10
|
|
21
|
-
# def teardown
|
22
|
-
# super
|
23
|
-
# Foo.delete_all
|
24
|
-
# end
|
25
|
-
|
26
11
|
def test_thin_views_are_successful
|
27
12
|
get :show, :id => @foo.id
|
28
13
|
assert_response :success
|
@@ -0,0 +1,23 @@
|
|
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
|
data/test/test_helper.rb
CHANGED
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.2.0
|
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: 2011-05-
|
12
|
+
date: 2011-05-25 00:00:00.000000000Z
|
13
13
|
dependencies: []
|
14
14
|
description: Enables and enforces the presenter pattern in rails
|
15
15
|
email: josh.leven@gmail.com
|
@@ -17,16 +17,22 @@ executables: []
|
|
17
17
|
extensions: []
|
18
18
|
extra_rdoc_files:
|
19
19
|
- README.rdoc
|
20
|
+
- lib/json-builder/handler.rb
|
21
|
+
- lib/json-builder/railtie.rb
|
20
22
|
- lib/presenter-pattern.rb
|
21
|
-
- lib/presenter-pattern/
|
23
|
+
- lib/presenter-pattern/railtie.rb
|
22
24
|
files:
|
23
25
|
- README.rdoc
|
24
26
|
- Rakefile
|
27
|
+
- lib/json-builder/handler.rb
|
28
|
+
- lib/json-builder/railtie.rb
|
25
29
|
- lib/presenter-pattern.rb
|
26
|
-
- lib/presenter-pattern/
|
30
|
+
- lib/presenter-pattern/railtie.rb
|
27
31
|
- rails/init.rb
|
32
|
+
- test/json_responder_test.rb
|
33
|
+
- test/json_view_test.rb
|
28
34
|
- test/no_querying_view_test.rb
|
29
|
-
- test/
|
35
|
+
- test/only_data_in_view_test.rb
|
30
36
|
- test/setup_test.rb
|
31
37
|
- test/test_helper.rb
|
32
38
|
- Manifest
|
@@ -57,11 +63,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
57
63
|
version: '1.2'
|
58
64
|
requirements: []
|
59
65
|
rubyforge_project: presenter-pattern
|
60
|
-
rubygems_version: 1.
|
66
|
+
rubygems_version: 1.7.2
|
61
67
|
signing_key:
|
62
68
|
specification_version: 3
|
63
69
|
summary: Enables and enforces the presenter pattern in rails
|
64
70
|
test_files:
|
71
|
+
- test/json_responder_test.rb
|
72
|
+
- test/json_view_test.rb
|
65
73
|
- test/no_querying_view_test.rb
|
74
|
+
- test/only_data_in_view_test.rb
|
66
75
|
- test/setup_test.rb
|
67
76
|
- test/test_helper.rb
|
@@ -1,38 +0,0 @@
|
|
1
|
-
no_querying_views = begin
|
2
|
-
Rails.env.development? || Rails.env.test?
|
3
|
-
rescue
|
4
|
-
false
|
5
|
-
end
|
6
|
-
|
7
|
-
if no_querying_views
|
8
|
-
module PresenterPattern
|
9
|
-
class IllegalDatabaseQueryFromView < RuntimeError; end
|
10
|
-
end
|
11
|
-
|
12
|
-
module ::ActiveRecord
|
13
|
-
module ConnectionAdapters
|
14
|
-
%w(PostgreSQLAdapter MysqlAdapter SQLite3Adapter).each do |adapter_name|
|
15
|
-
begin
|
16
|
-
adapter = const_get adapter_name
|
17
|
-
adapter.class_eval do
|
18
|
-
puts "[WARNING] Presenter::Rails is preventing your views from triggering PG, MySQL or SQLite3 database connections."
|
19
|
-
alias :orig_execute :execute
|
20
|
-
def execute(*args)
|
21
|
-
first_view = caller.grep(/app\/views/).first
|
22
|
-
first_helper = caller.grep(/app\/helpers/).first
|
23
|
-
|
24
|
-
# if we're coming from a view, let's analyze the situation
|
25
|
-
if !first_view.nil? and (first_helper.nil? or (caller.index(first_view) < caller.index(first_helper)))
|
26
|
-
raise PresenterPattern::IllegalDatabaseQueryFromView, "No query from view prohibited, eager-load from a controller instead."
|
27
|
-
else
|
28
|
-
orig_execute *args
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
32
|
-
rescue
|
33
|
-
# the adapter has not been instantiated
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
38
|
-
end
|
data/test/schema.rb
DELETED