controller_support 0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ .idea
2
+ .DS_Store
3
+ *.gem
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm use 1.8.7-p352@controller_support --create
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.2
4
+ - 1.9.3
5
+ - 1.8.7
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,30 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ Controller Support (0.0.1)
5
+ active_support
6
+
7
+ GEM
8
+ remote: http://rubygems.org/
9
+ specs:
10
+ active_support (3.0.0)
11
+ activesupport (= 3.0.0)
12
+ activesupport (3.0.0)
13
+ diff-lcs (1.1.3)
14
+ rake (10.0.2)
15
+ rspec (2.12.0)
16
+ rspec-core (~> 2.12.0)
17
+ rspec-expectations (~> 2.12.0)
18
+ rspec-mocks (~> 2.12.0)
19
+ rspec-core (2.12.1)
20
+ rspec-expectations (2.12.0)
21
+ diff-lcs (~> 1.1.3)
22
+ rspec-mocks (2.12.0)
23
+
24
+ PLATFORMS
25
+ ruby
26
+
27
+ DEPENDENCIES
28
+ Controller Support!
29
+ rake
30
+ rspec
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2012 YOURNAME
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,85 @@
1
+ # ControllerSupport [![Build Status](https://travis-ci.org/yonbergman/controller_support.png?branch=master)](https://travis-ci.org/yonbergman/controller_support)
2
+
3
+ Controller Supports are controller oriented mixins (modules) for Rails that can help you extract behaviour from controllers into shared resources.
4
+
5
+ ## Why?
6
+ Controller Supports lets you extract common behaviour or non-core controller knowledge into it's seperate resource which makes it easier to test and maintain.
7
+ Some example of ControllerSupports:
8
+
9
+ * **UserSupport** - Authentication and current_user
10
+ * **ErrorSupport** - Global error methods and a before filter to catch uncaught exceptions
11
+ * **EventSupport** - Used to load the event resource based on the `params[:id]`
12
+ * **LocalizationSupport** - Helper methods to build hashes that will be passed to I18n
13
+ * **MobileSupport** - Check if in a mobile session and change the layout
14
+
15
+ ControllerSupports are just an extension of `ActiveSupport::Concern` so they supply all of the behaviours that you'd get from `ActiveSupport::Concern`.
16
+ What Controller Supports adds on top of ActiveSupport::Concern is an easier way to define methods as `helper_methods` and `before_filters`. It does so in such a way that will enable you to `include` the support in controllers but also into other objects like test stubs or Mailers.
17
+
18
+ If you don't know `ActiveSupport::Concern` there are several links to reading material in the bottom of this README.
19
+
20
+
21
+ ## Installation
22
+ Just add the `controller_support` gem to your Gemfile
23
+
24
+ ```ruby
25
+ gem 'controller_support'
26
+ ```
27
+
28
+ ## Usage
29
+
30
+ To define a module as a controller support you just need to extend it with `ControllerSupport::Base` it can then act as a mixin which you can `include` into Controllers or any other object.
31
+
32
+ ```ruby
33
+ module UserSupport
34
+ extend ControllerSupport::Base
35
+
36
+ helper_method :current_user, :user_signed_in
37
+ before_filter :must_be_signed_in
38
+ # ControllerSupport also support after_filter and around_filter
39
+
40
+ def current_user
41
+ User.find(session[:user_id])
42
+ end
43
+
44
+ def user_signed_in?
45
+ current_user.present?
46
+ end
47
+
48
+ def must_be_signed_in
49
+ redirect_to sign_up_path unless user_signed_in?
50
+ end
51
+
52
+ end
53
+ ```
54
+
55
+ To use the support just `include` it in the controller of your choice
56
+
57
+ ```ruby
58
+ class ItemController < ApplicationController
59
+ include UserSupport
60
+
61
+ skip_before_filter :must_be_signed_in, :only => :new
62
+
63
+ def new
64
+ render :inline => "this is just an example"
65
+ end
66
+
67
+ def show
68
+ current_user.item.find(params[:id])
69
+ end
70
+ end
71
+ ```
72
+ ## ActiveSupport::Concern reading material
73
+
74
+ If you don't know about ActiveSupport::Concern and why it's an awesome thing you should read these stuff
75
+
76
+ * [The offical documentation](http://api.rubyonrails.org/classes/ActiveSupport/Concern.html)
77
+ * [Concerning yourself with ActiveSupport::Concern](http://www.fakingfantastic.com/2010/09/20/concerning-yourself-with-active-support-concern/) - This blog post covers everything you need to know
78
+ * [Concerning ActiveSupport::Concern](http://opensoul.org/blog/archives/2011/02/07/concerning-activesupportconcern/)
79
+ * [Extending ActiveModel via ActiveSupport::Concern](http://chris-schmitz.com/extending-activemodel-via-activesupportconcern/)
80
+ * [Trimming the fat from your controllers](http://notninjas.com/2012/12/03/trimming-the-fat-from-your-controllers/) - A blog post I wrote that led to the creation of this gem.
81
+
82
+
83
+ ## License
84
+
85
+ This project rocks and uses MIT-LICENSE.
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+
8
+ require 'rspec/core/rake_task'
9
+ RSpec::Core::RakeTask.new('spec')
10
+ task :default => :spec
@@ -0,0 +1,23 @@
1
+ $:.push File.expand_path("../lib", __FILE__)
2
+
3
+ # Maintain your gem's version:
4
+ require "controller_support/version"
5
+
6
+ # Describe your gem and declare its dependencies:
7
+ Gem::Specification.new do |s|
8
+ s.name = "controller_support"
9
+ s.version = ControllerSupport::VERSION
10
+ s.authors = ["Yonatan Bergman"]
11
+ s.email = ["yonbergman@gmail.com"]
12
+ s.homepage = "https://github.com/yonbergman/controller_support"
13
+ s.summary = "An extension of ActiveSupport::Concern to create smart and beautiful controller mixins"
14
+ s.description = "An extension of ActiveSupport::Concern to create smart and beautiful controller mixins"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.require_path = "lib"
18
+ s.test_files = Dir.glob('spec/lib/*_spec.rb')
19
+
20
+ s.add_dependency "active_support"
21
+ s.add_development_dependency 'rake'
22
+ s.add_development_dependency 'rspec'
23
+ end
@@ -0,0 +1,31 @@
1
+ require 'active_support/concern'
2
+
3
+ module ControllerSupport
4
+ module Base
5
+
6
+ include ActiveSupport::Concern
7
+
8
+ def self.extended(base)
9
+ base.instance_variable_set("@_dependencies", [])
10
+ end
11
+
12
+ def append_features(base)
13
+ super(base)
14
+ unless base.instance_variable_defined?("@_dependencies")
15
+ if instance_variable_defined?("@_custom_blocks")
16
+ @_custom_blocks.each do |block|
17
+ base.class_eval(&block)
18
+ end
19
+ end
20
+ end
21
+ end
22
+
23
+ [:before_filter, :after_filter, :around_filter, :helper_method].each do |method_name|
24
+ define_method method_name do |*names|
25
+ @_custom_blocks ||= []
26
+ @_custom_blocks << Proc.new { send(method_name,*names) if respond_to? method_name }
27
+ end
28
+ end
29
+
30
+ end
31
+ end
@@ -0,0 +1,3 @@
1
+ module ControllerSupport
2
+ VERSION = "0.2"
3
+ end
@@ -0,0 +1,4 @@
1
+ require "controller_support/base"
2
+
3
+ module ControllerSupport
4
+ end
@@ -0,0 +1,58 @@
1
+ require 'spec_helper'
2
+ require 'spec_helpers/dummy_classes'
3
+ require 'spec_helpers/example_controller_supports'
4
+ require 'spec_helpers/mixed_helpers'
5
+
6
+ describe ControllerSupport::Base do
7
+
8
+ subject { create_mixed_instance(DummyMailer, FooSupport) }
9
+
10
+ describe "Basic ActiveSupport::Concern behaviours" do
11
+ it "should mix in methods into class" do
12
+ should respond_to :foo
13
+ end
14
+
15
+ it "should run the included block" do
16
+ should respond_to :dynamic_foo
17
+ end
18
+
19
+ it "should support dependencies" do
20
+ foobar_instance = create_mixed_instance(DummyMailer, FoobarSupport)
21
+ foobar_instance.should respond_to :foobar
22
+ foobar_instance.should respond_to :bar
23
+ end
24
+
25
+ it "should support the ClassMethod submodule" do
26
+ subject.class.should respond_to :class_foo
27
+ end
28
+
29
+ end
30
+
31
+ describe "extended Controller Support behaviour" do
32
+ let(:foo_controller) { create_mixed_class(DummyController, FooSupport) }
33
+ let(:wrapped_foo_controller) { create_mixed_class(DummyController, WrapFooSupport) }
34
+
35
+ it "can set before_filters" do
36
+ foo_controller.before_filters.should eq([:foo])
37
+ end
38
+
39
+ it "can set helper_methods" do
40
+ foo_controller.helper_methods.should eq([:dynamic_foo])
41
+ end
42
+
43
+ it "can set before_filters even via inheritance" do
44
+ wrapped_foo_controller.before_filters.should eq([:foo])
45
+ end
46
+
47
+ it "doesn't override any after_filters already set on the controller" do
48
+
49
+ class ControllerWithAfterFilter < DummyController
50
+ after_filter :original
51
+ include FooSupport
52
+ end
53
+
54
+ ControllerWithAfterFilter.after_filters.should eq([:original, :extended])
55
+ end
56
+ end
57
+
58
+ end
@@ -0,0 +1,12 @@
1
+ # Configure Rails Envinronment
2
+ ENV["RAILS_ENV"] = "test"
3
+
4
+ require 'rubygems'
5
+ require 'bundler/setup'
6
+ require 'controller_support'
7
+
8
+ RSpec.configure do |config|
9
+ config.treat_symbols_as_metadata_keys_with_true_values = true
10
+ config.run_all_when_everything_filtered = true
11
+ config.filter_run :focus
12
+ end
@@ -0,0 +1,34 @@
1
+ #
2
+ # A dummy controller - has the following class methods
3
+ # * before_filter
4
+ # * around_filter
5
+ # * after_filter
6
+ # * helper_method
7
+ #
8
+ # for each of the methods it also has matching getter method
9
+ # * before_filters
10
+ # * around_filters
11
+ # * after_filters
12
+ # * helper_methods
13
+ #
14
+ class DummyController
15
+
16
+ [:before_filter, :after_filter, :around_filter, :helper_method].each do |controller_func|
17
+ getter = "#{controller_func}s"
18
+
19
+ self.class.send :define_method, getter do
20
+ @_symbols ||= {}
21
+ @_symbols[controller_func] ||= []
22
+ end
23
+
24
+ self.class.send :define_method, controller_func do |*symbols|
25
+ send(getter).concat symbols
26
+ end
27
+ end
28
+
29
+ end
30
+
31
+ # A DummyMailer - does nothing :)
32
+ class DummyMailer
33
+
34
+ end
@@ -0,0 +1,50 @@
1
+ module FooSupport # a complex support
2
+ extend ControllerSupport::Base
3
+
4
+ before_filter :foo
5
+ helper_method :dynamic_foo
6
+ after_filter :extended
7
+
8
+ included do
9
+ define_method :dynamic_foo do
10
+ "dynamic_foo"
11
+ end
12
+ end
13
+
14
+ def foo
15
+ "foo"
16
+ end
17
+
18
+ module ClassMethods
19
+ def class_foo
20
+ "class_foo"
21
+ end
22
+ end
23
+
24
+ end
25
+
26
+
27
+ module BarSupport # simple support
28
+ extend ControllerSupport::Base
29
+
30
+ def bar
31
+ "bar"
32
+ end
33
+ end
34
+
35
+
36
+ module FoobarSupport # example for inheritance of simple support
37
+ extend ControllerSupport::Base
38
+
39
+ include BarSupport
40
+
41
+ def foobar
42
+ "foobar"
43
+ end
44
+ end
45
+
46
+ module WrapFooSupport # example for inheritance of complex support
47
+ extend ControllerSupport::Base
48
+
49
+ include FooSupport
50
+ end
@@ -0,0 +1,12 @@
1
+ # A bunch of helpers to help mix in modules for test cases
2
+ # to make sure each test is contained
3
+
4
+ def create_mixed_class(base_class, support)
5
+ klass = Class.new(base_class)
6
+ klass.send :include, support
7
+ klass
8
+ end
9
+
10
+ def create_mixed_instance(base_class, support)
11
+ create_mixed_class(base_class, support).new
12
+ end
metadata ADDED
@@ -0,0 +1,123 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: controller_support
3
+ version: !ruby/object:Gem::Version
4
+ hash: 15
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 2
9
+ version: "0.2"
10
+ platform: ruby
11
+ authors:
12
+ - Yonatan Bergman
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2012-12-12 00:00:00 Z
18
+ dependencies:
19
+ - !ruby/object:Gem::Dependency
20
+ name: active_support
21
+ prerelease: false
22
+ requirement: &id001 !ruby/object:Gem::Requirement
23
+ none: false
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ hash: 3
28
+ segments:
29
+ - 0
30
+ version: "0"
31
+ type: :runtime
32
+ version_requirements: *id001
33
+ - !ruby/object:Gem::Dependency
34
+ name: rake
35
+ prerelease: false
36
+ requirement: &id002 !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ hash: 3
42
+ segments:
43
+ - 0
44
+ version: "0"
45
+ type: :development
46
+ version_requirements: *id002
47
+ - !ruby/object:Gem::Dependency
48
+ name: rspec
49
+ prerelease: false
50
+ requirement: &id003 !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ hash: 3
56
+ segments:
57
+ - 0
58
+ version: "0"
59
+ type: :development
60
+ version_requirements: *id003
61
+ description: An extension of ActiveSupport::Concern to create smart and beautiful controller mixins
62
+ email:
63
+ - yonbergman@gmail.com
64
+ executables: []
65
+
66
+ extensions: []
67
+
68
+ extra_rdoc_files: []
69
+
70
+ files:
71
+ - .gitignore
72
+ - .rspec
73
+ - .rvmrc
74
+ - .travis.yml
75
+ - Gemfile
76
+ - Gemfile.lock
77
+ - MIT-LICENSE
78
+ - README.md
79
+ - Rakefile
80
+ - controller_support.gemspec
81
+ - lib/controller_support.rb
82
+ - lib/controller_support/base.rb
83
+ - lib/controller_support/version.rb
84
+ - spec/lib/support_spec.rb
85
+ - spec/spec_helper.rb
86
+ - spec/spec_helpers/dummy_classes.rb
87
+ - spec/spec_helpers/example_controller_supports.rb
88
+ - spec/spec_helpers/mixed_helpers.rb
89
+ homepage: https://github.com/yonbergman/controller_support
90
+ licenses: []
91
+
92
+ post_install_message:
93
+ rdoc_options: []
94
+
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ none: false
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ hash: 3
103
+ segments:
104
+ - 0
105
+ version: "0"
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
+ none: false
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ hash: 3
112
+ segments:
113
+ - 0
114
+ version: "0"
115
+ requirements: []
116
+
117
+ rubyforge_project:
118
+ rubygems_version: 1.8.24
119
+ signing_key:
120
+ specification_version: 3
121
+ summary: An extension of ActiveSupport::Concern to create smart and beautiful controller mixins
122
+ test_files:
123
+ - spec/lib/support_spec.rb