proffer 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE.txt ADDED
@@ -0,0 +1,9 @@
1
+ Copyright (c) 2012, James Hunt & Paul Mucur
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
5
+
6
+ Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
7
+ Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
9
+
data/README.md ADDED
@@ -0,0 +1,71 @@
1
+ Proffer [![Build Status](https://secure.travis-ci.org/hudge/proffer.png?branch=master)](http://travis-ci.org/hudge/proffer)
2
+ =======
3
+
4
+ A module to stop Action Controller from copying every instance variable available
5
+ to an action to the view by default. Instead, Proffer provides a way to explicitly
6
+ expose values as local variables within views.
7
+
8
+ Rationale
9
+ ---------
10
+
11
+ By default, Action Controller will make any instance variables present during an
12
+ action available to its views (including partials). This effectively makes them
13
+ global variables and can obscure their origin when maintaining views. Partials
14
+ that exploit this behaviour can be particularly difficult to maintain as they
15
+ may inherit state from their caller implicitly.
16
+
17
+ We want to see if removing this default behaviour changes the way views are
18
+ written. By forcing explicit declaration of dependencies, will better, more
19
+ encapsulated design result?
20
+
21
+ Usage
22
+ -----
23
+
24
+ Add the following to your `Gemfile`:
25
+
26
+ ```ruby
27
+ gem 'proffer'
28
+ ```
29
+
30
+ Include the `Proffer` module into any controllers you wish (include into
31
+ `ApplicationController` to enforce this behaviour throughout your application):
32
+
33
+ ```ruby
34
+ class PostsController < ApplicationController
35
+ include Proffer
36
+
37
+ # This will make a new Post object available as post in the view but
38
+ # @heading will be nil.
39
+ def new
40
+ @heading = "New Post"
41
+ proffer :post => Post.new
42
+ end
43
+ end
44
+ ```
45
+
46
+ Any proffered values will then be available to your views by their key:
47
+
48
+ ```erb
49
+ <%= form_for(post) do |f| %>
50
+ ...
51
+ <% end %>
52
+ ```
53
+
54
+ Compatibility
55
+ -------------
56
+
57
+ As we rely on `ActionController#view_assigns`, this will only work with versions
58
+ of Rails 3.0 or later. It is currently tested against Rails 3.2.
59
+
60
+ Disclaimer
61
+ ----------
62
+
63
+ We have not yet tried this in production so proceed with caution. This gem
64
+ overrides `ActionController#view_assigns` and extends `ActionController#render`,
65
+ so it may be incompatible with other gems that override these methods.
66
+
67
+ License
68
+ -------
69
+
70
+ See LICENSE.txt
71
+
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require 'rspec/core/rake_task'
2
+
3
+ RSpec::Core::RakeTask.new
4
+
5
+ task :test => :spec
6
+ task :default => :spec
7
+
data/lib/proffer.rb ADDED
@@ -0,0 +1,54 @@
1
+ # Public: Stops an Action Controller from automatically passing all instance
2
+ # variables to the view. Instead, variables must be explicitly defined by
3
+ # using proffer.
4
+ #
5
+ # Examples
6
+ #
7
+ # class PostsController < ApplicationController
8
+ # include Proffer
9
+ #
10
+ # # This will make a new Post object available as post in the view but
11
+ # # @heading will be nil.
12
+ # def new
13
+ # @heading = "New Post"
14
+ # proffer :post => Post.new
15
+ # end
16
+ # end
17
+ module Proffer
18
+
19
+ # Public: Make the given values available to the view as local variables.
20
+ #
21
+ # variables - The Hash of values keyed by the local variable name to be used
22
+ # in the view.
23
+ #
24
+ # Examples
25
+ #
26
+ # proffer :model => Model.new
27
+ # # => render ..., :locals => { :model => Model.new }
28
+ #
29
+ # Returns nothing.
30
+ def proffer(variables)
31
+ @_proffered_variables ||= {}
32
+ @_proffered_variables.merge!(variables)
33
+ end
34
+
35
+ # Internal: Override Action Controller's render method to convert proffered
36
+ # variables to locals.
37
+ def render(*args, &blk)
38
+ if @_proffered_variables
39
+ options = args.extract_options!
40
+ options[:locals] ||= {}
41
+ options[:locals] = @_proffered_variables.merge(options[:locals])
42
+ args << options
43
+ end
44
+
45
+ super(*args, &blk)
46
+ end
47
+
48
+ # Internal: Override Action Controller's view_assigns to no longer copy
49
+ # instance variables into the view.
50
+ def view_assigns
51
+ {}
52
+ end
53
+ end
54
+
@@ -0,0 +1,3 @@
1
+ <%= @foo %>
2
+ <%= my_nice_variable %>
3
+
@@ -0,0 +1,5 @@
1
+ xml.root do
2
+ xml.foo @foo
3
+ xml.my_nice_variable my_nice_variable
4
+ end
5
+
@@ -0,0 +1,48 @@
1
+ require 'spec_helper_rails'
2
+
3
+ class ApplicationController < ActionController::Base
4
+ include Proffer
5
+ end
6
+
7
+ class ProfferTestController < ApplicationController
8
+ self.view_paths = [File.expand_path('../fixtures', __FILE__)]
9
+
10
+ def index
11
+ @foo = "Not passed through"
12
+ proffer :my_nice_variable => "Woooo"
13
+ end
14
+ end
15
+
16
+ TestApplication.routes.draw do
17
+ match "/foo" => "proffer_test#index"
18
+ end
19
+
20
+ describe ProfferTestController, :type => :controller do
21
+ render_views
22
+
23
+ describe "#index" do
24
+ describe "as html" do
25
+ it "won't pass instance variables to the view by default" do
26
+ get :index
27
+ response.body.should_not =~ /Not passed through/
28
+ end
29
+
30
+ it "will pass proffered variables to the view" do
31
+ get :index
32
+ response.body.should =~ /Woooo/
33
+ end
34
+ end
35
+
36
+ describe "as xml" do
37
+ it "won't pass instance variables to the view by default" do
38
+ get :index, :format => :xml
39
+ response.body.should_not =~ /Not passed through/
40
+ end
41
+
42
+ it "will pass proffered variables to the view" do
43
+ get :index, :format => :xml
44
+ response.body.should =~ /Woooo/
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,61 @@
1
+ require 'spec_helper'
2
+
3
+ class FakeController < BaseController
4
+ include Proffer
5
+
6
+ def index
7
+ proffer :foo => "bar", :baz => "quux"
8
+ render :index
9
+ end
10
+
11
+ def no_proffering
12
+ render :no_proffering
13
+ end
14
+
15
+ def proffering_with_locals(locals)
16
+ proffer :foo => "bar"
17
+ render :proffering, :locals => locals
18
+ end
19
+
20
+ def multiple_proffer
21
+ proffer :foo => "bar"
22
+ proffer :baz => "quux"
23
+ render :multiple_proffer
24
+ end
25
+ end
26
+
27
+ describe Proffer do
28
+ let(:controller) { FakeController.new }
29
+
30
+ it "sets view_assigns to an empty hash" do
31
+ controller.view_assigns.should == {}
32
+ end
33
+
34
+ describe "#proffer" do
35
+ it "assigns the proffered variables to the view as locals" do
36
+ controller.index
37
+ controller.args_for_render.should == [:index, { :locals => { :foo => "bar", :baz => "quux" } }]
38
+ end
39
+
40
+ it "doesn't add a locals option to the view if nothing is proffered" do
41
+ controller.no_proffering
42
+ controller.args_for_render.should == [:no_proffering]
43
+ end
44
+
45
+ it "doesn't override explicitly provided locals" do
46
+ controller.proffering_with_locals(:baz => "quux")
47
+ controller.args_for_render.should == [:proffering, { :locals => { :baz => "quux", :foo => "bar" } }]
48
+ end
49
+
50
+ it "prefers the explicit locals over the proffered variables if they have the same name" do
51
+ controller.proffering_with_locals(:foo => "new value")
52
+ controller.args_for_render.should == [:proffering, { :locals => { :foo => "new value" } }]
53
+ end
54
+
55
+ it "allows multiple calls to proffer" do
56
+ controller.multiple_proffer
57
+ controller.args_for_render.should == [:multiple_proffer, { :locals => { :foo => "bar", :baz => "quux" } }]
58
+ end
59
+ end
60
+ end
61
+
@@ -0,0 +1,25 @@
1
+ require 'proffer'
2
+
3
+ # To avoid requiring Active Support during testing.
4
+ class Array
5
+ def extract_options!
6
+ if last.is_a?(Hash)
7
+ pop
8
+ else
9
+ {}
10
+ end
11
+ end
12
+ end
13
+
14
+ class BaseController
15
+ attr_reader :args_for_render
16
+
17
+ def render(*args)
18
+ @args_for_render = args
19
+ end
20
+
21
+ def view_assigns
22
+ {:stuff => :not_needed}
23
+ end
24
+ end
25
+
@@ -0,0 +1,12 @@
1
+ require 'proffer'
2
+
3
+ require 'action_controller/railtie'
4
+ require 'rails/test_unit/railtie'
5
+
6
+ class TestApplication < Rails::Application
7
+ config.active_support.deprecation = :stderr
8
+ end
9
+ TestApplication.initialize!
10
+
11
+ require 'rspec/rails'
12
+
metadata ADDED
@@ -0,0 +1,123 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: proffer
3
+ version: !ruby/object:Gem::Version
4
+ hash: 59
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 9
9
+ - 0
10
+ version: 0.9.0
11
+ platform: ruby
12
+ authors:
13
+ - James Hunt
14
+ - Paul Mucur
15
+ autorequire:
16
+ bindir: bin
17
+ cert_chain: []
18
+
19
+ date: 2012-03-31 00:00:00 +01:00
20
+ default_executable:
21
+ dependencies:
22
+ - !ruby/object:Gem::Dependency
23
+ name: rspec-rails
24
+ prerelease: false
25
+ requirement: &id001 !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ">="
29
+ - !ruby/object:Gem::Version
30
+ hash: 3
31
+ segments:
32
+ - 0
33
+ version: "0"
34
+ type: :development
35
+ version_requirements: *id001
36
+ - !ruby/object:Gem::Dependency
37
+ name: rails
38
+ prerelease: false
39
+ requirement: &id002 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ hash: 3
45
+ segments:
46
+ - 0
47
+ version: "0"
48
+ type: :development
49
+ version_requirements: *id002
50
+ - !ruby/object:Gem::Dependency
51
+ name: rake
52
+ prerelease: false
53
+ requirement: &id003 !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ hash: 3
59
+ segments:
60
+ - 0
61
+ version: "0"
62
+ type: :development
63
+ version_requirements: *id003
64
+ description: A module to stop Action Controller from copying every instance variable available to an action to the view by default. Instead, Proffer provides a way to explicitly expose values as local variables within views.
65
+ email:
66
+ executables: []
67
+
68
+ extensions: []
69
+
70
+ extra_rdoc_files: []
71
+
72
+ files:
73
+ - lib/proffer.rb
74
+ - Rakefile
75
+ - README.md
76
+ - LICENSE.txt
77
+ - spec/spec_helper.rb
78
+ - spec/spec_helper_rails.rb
79
+ - spec/proffer_spec.rb
80
+ - spec/proffer_integration_spec.rb
81
+ - spec/fixtures/proffer_test/index.html.erb
82
+ - spec/fixtures/proffer_test/index.xml.builder
83
+ has_rdoc: true
84
+ homepage: http://github.com/hudge/proffer
85
+ licenses: []
86
+
87
+ post_install_message:
88
+ rdoc_options: []
89
+
90
+ require_paths:
91
+ - lib
92
+ required_ruby_version: !ruby/object:Gem::Requirement
93
+ none: false
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ hash: 3
98
+ segments:
99
+ - 0
100
+ version: "0"
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ none: false
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ hash: 3
107
+ segments:
108
+ - 0
109
+ version: "0"
110
+ requirements: []
111
+
112
+ rubyforge_project:
113
+ rubygems_version: 1.6.2
114
+ signing_key:
115
+ specification_version: 3
116
+ summary: An Action Controller module to hide instance variables from views by default.
117
+ test_files:
118
+ - spec/spec_helper.rb
119
+ - spec/spec_helper_rails.rb
120
+ - spec/proffer_spec.rb
121
+ - spec/proffer_integration_spec.rb
122
+ - spec/fixtures/proffer_test/index.html.erb
123
+ - spec/fixtures/proffer_test/index.xml.builder