presenter-pattern 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/Manifest ADDED
@@ -0,0 +1,10 @@
1
+ README.rdoc
2
+ Rakefile
3
+ lib/presenter-pattern.rb
4
+ lib/presenter-pattern/no_querying_views.rb
5
+ rails/init.rb
6
+ test/no_querying_view_test.rb
7
+ test/schema.rb
8
+ test/setup_test.rb
9
+ test/test_helper.rb
10
+ Manifest
data/README.rdoc ADDED
@@ -0,0 +1,12 @@
1
+ = Presenter-Pattern
2
+
3
+ Rails gem for enabling and enforcing the presenter pattern.
4
+
5
+ == Install
6
+
7
+ gem install presenter-pattern
8
+
9
+
10
+ == Usage
11
+
12
+ ... To Be Continued ...
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'echoe'
4
+
5
+ Echoe.new('presenter-pattern', '0.1.1') do |p|
6
+ p.description = "Enables and enforces the presenter pattern in rails"
7
+ p.url = "https://github.com/jleven/presenter-pattern"
8
+ p.author = "Josh Leven"
9
+ p.email = "josh.leven@gmail.com"
10
+ p.ignore_pattern = /^(tmp|pkg|doc|test\/rails_app)|(\.svn|CVS|\.bzr|\.DS|\.git)$/
11
+ p.development_dependencies = []
12
+ end
13
+
14
+ Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
@@ -0,0 +1,9 @@
1
+ require 'presenter-pattern/no_querying_views'
2
+
3
+ module PresenterPattern
4
+ class Base
5
+
6
+ end
7
+ end
8
+
9
+
@@ -0,0 +1,38 @@
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
@@ -0,0 +1,30 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{presenter-pattern}
5
+ s.version = "0.1.1"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Josh Leven"]
9
+ s.date = %q{2011-05-23}
10
+ s.description = %q{Enables and enforces the presenter pattern in rails}
11
+ s.email = %q{josh.leven@gmail.com}
12
+ s.extra_rdoc_files = ["README.rdoc", "lib/presenter-pattern.rb", "lib/presenter-pattern/no_querying_views.rb"]
13
+ s.files = ["README.rdoc", "Rakefile", "lib/presenter-pattern.rb", "lib/presenter-pattern/no_querying_views.rb", "rails/init.rb", "test/no_querying_view_test.rb", "test/schema.rb", "test/setup_test.rb", "test/test_helper.rb", "Manifest", "presenter-pattern.gemspec"]
14
+ s.homepage = %q{https://github.com/jleven/presenter-pattern}
15
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Presenter-pattern", "--main", "README.rdoc"]
16
+ s.require_paths = ["lib"]
17
+ s.rubyforge_project = %q{presenter-pattern}
18
+ s.rubygems_version = %q{1.5.2}
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"]
21
+
22
+ if s.respond_to? :specification_version then
23
+ s.specification_version = 3
24
+
25
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
26
+ else
27
+ end
28
+ else
29
+ end
30
+ end
data/rails/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'presenter-pattern'
@@ -0,0 +1,37 @@
1
+ require 'test_helper'
2
+
3
+ class NoQueryingViewTest < ActionController::TestCase
4
+ tests FoosController
5
+
6
+ def setup
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
+ @foo = Foo.create :name => "name"
19
+ end
20
+
21
+ # def teardown
22
+ # super
23
+ # Foo.delete_all
24
+ # end
25
+
26
+ def test_thin_views_are_successful
27
+ get :show, :id => @foo.id
28
+ assert_response :success
29
+ end
30
+
31
+ def test_thick_views_fail
32
+ assert_raise(ActionView::Template::Error) do
33
+ get :index
34
+ end
35
+ end
36
+
37
+ end
data/test/schema.rb ADDED
@@ -0,0 +1,11 @@
1
+ ActiveRecord::Schema.define(:version => 0) do
2
+ create_table :foos, :force => true do |t|
3
+ t.string :name
4
+ t.string :squawk
5
+ end
6
+ create_table :bars, :force => true do |t|
7
+ t.string :name
8
+ t.string :tweet
9
+ t.integer :foo_id
10
+ end
11
+ end
@@ -0,0 +1,13 @@
1
+ require 'test_helper'
2
+
3
+ require 'foo'
4
+ require 'bar'
5
+
6
+ class SetupTest < Test::Unit::TestCase
7
+
8
+ def test_schema_has_loaded_correctly
9
+ assert_equal [], Foo.all
10
+ assert_equal [], Bar.all
11
+ end
12
+
13
+ end
@@ -0,0 +1,19 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'active_support'
4
+
5
+ ENV['RAILS_ENV'] = 'test'
6
+ ENV['DB'] = 'postgresql'
7
+
8
+ require 'rails_app/config/environment'
9
+ require 'rails/test_help'
10
+
11
+ require 'yaml'
12
+ require 'active_record'
13
+ require 'logger'
14
+
15
+ require File.expand_path('../rails/init', File.dirname(__FILE__))
16
+
17
+ #def app
18
+ # RailsApp::Application
19
+ #end
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: presenter-pattern
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Josh Leven
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-05-23 00:00:00.000000000Z
13
+ dependencies: []
14
+ description: Enables and enforces the presenter pattern in rails
15
+ email: josh.leven@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files:
19
+ - README.rdoc
20
+ - lib/presenter-pattern.rb
21
+ - lib/presenter-pattern/no_querying_views.rb
22
+ files:
23
+ - README.rdoc
24
+ - Rakefile
25
+ - lib/presenter-pattern.rb
26
+ - lib/presenter-pattern/no_querying_views.rb
27
+ - rails/init.rb
28
+ - test/no_querying_view_test.rb
29
+ - test/schema.rb
30
+ - test/setup_test.rb
31
+ - test/test_helper.rb
32
+ - Manifest
33
+ - presenter-pattern.gemspec
34
+ homepage: https://github.com/jleven/presenter-pattern
35
+ licenses: []
36
+ post_install_message:
37
+ rdoc_options:
38
+ - --line-numbers
39
+ - --inline-source
40
+ - --title
41
+ - Presenter-pattern
42
+ - --main
43
+ - README.rdoc
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ none: false
48
+ requirements:
49
+ - - ! '>='
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ! '>='
56
+ - !ruby/object:Gem::Version
57
+ version: '1.2'
58
+ requirements: []
59
+ rubyforge_project: presenter-pattern
60
+ rubygems_version: 1.8.3
61
+ signing_key:
62
+ specification_version: 3
63
+ summary: Enables and enforces the presenter pattern in rails
64
+ test_files:
65
+ - test/no_querying_view_test.rb
66
+ - test/setup_test.rb
67
+ - test/test_helper.rb