youthtree-controller-ext 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,27 @@
1
+ Copyright (c) 2010, Youth Tree
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without
5
+ modification, are permitted provided that the following conditions are met:
6
+ 1. Redistributions of source code must retain the above copyright
7
+ notice, this list of conditions and the following disclaimer.
8
+ 2. Redistributions in binary form must reproduce the above copyright
9
+ notice, this list of conditions and the following disclaimer in the
10
+ documentation and/or other materials provided with the distribution.
11
+ 3. All advertising materials mentioning features or use of this software
12
+ must display the following acknowledgement:
13
+ This product includes software developed by the Youth Tree.
14
+ 4. Neither the name of the Youth Tree nor the
15
+ names of its contributors may be used to endorse or promote products
16
+ derived from this software without specific prior written permission.
17
+
18
+ THIS SOFTWARE IS PROVIDED BY YOUTH TREE ''AS IS'' AND ANY
19
+ EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21
+ DISCLAIMED. IN NO EVENT SHALL YOUTH TREE BE LIABLE FOR ANY
22
+ DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23
+ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24
+ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25
+ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
data/README.md ADDED
@@ -0,0 +1,30 @@
1
+ # YouthTree Controller Ext #
2
+
3
+ ## Installation ##
4
+
5
+ 1. Add "gem 'youthtree-controller-ext' to your Gemfile"
6
+ 2. Run bundle install
7
+
8
+ ## Usage ##
9
+
10
+ Use `use_controller_ext` / `use_controller_exts` on a class with one of the named controller extensions.
11
+ By default, it provides:
12
+
13
+ * `:almost_happy` - InheritedResources collection tweak
14
+ * `:authlogic_helpers` - Base authlogic helpers
15
+ * `:pseudocephalopod_resource` - use slugged find in InheritedResource
16
+ * `:ssl_requirement` - Tie in with `youthtree-settings` to make it easier to do ssl in certain environments.
17
+ * `:title_estuary` - adds controller methods for `title_estuary`.
18
+ * `:translation` - adds support for `tu` and `tf` for flash and ui methods.
19
+
20
+ ## Note on Patches/Pull Requests ##
21
+
22
+ 1. Fork the project.
23
+ 2. Make your feature addition or bug fix.
24
+ 3. Add tests for it. This is important so I don't break it in a future version unintentionally.
25
+ 4. Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
26
+ 5. Send me a pull request. Bonus points for topic branches.
27
+
28
+ ## Copyright ##
29
+
30
+ Copyright (c) 2010 Youth Tree. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,56 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ require 'active_support'
5
+ require 'active_support/core_ext'
6
+ require File.expand_path('lib/youth_tree/controller_ext', File.dirname(__FILE__))
7
+
8
+ begin
9
+ require 'jeweler'
10
+ Jeweler::Tasks.new do |gem|
11
+ gem.name = "youthtree-controller-ext"
12
+ gem.summary = "Simple controller mixin support for rails 3. Including some common extensions."
13
+ gem.description = "Provides uses_controller_extension and a set of common mixins."
14
+ gem.email = "sutto@sutto.net"
15
+ gem.homepage = "http://github.com/YouthTree/youthtree-controller-ext"
16
+ gem.authors = ["Darcy Laycock"]
17
+ gem.version = YouthTree::ControllerExt::VERSION
18
+ gem.add_dependency "activesupport", ">= 3.0.0.beta4"
19
+ end
20
+ Jeweler::GemcutterTasks.new
21
+ rescue LoadError
22
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
23
+ end
24
+
25
+ require 'rake/testtask'
26
+ Rake::TestTask.new(:test) do |test|
27
+ test.libs << 'lib' << 'test'
28
+ test.pattern = 'test/**/*_test.rb'
29
+ test.verbose = true
30
+ end
31
+
32
+ begin
33
+ require 'rcov/rcovtask'
34
+ Rcov::RcovTask.new do |test|
35
+ test.libs << 'test'
36
+ test.pattern = 'test/**/*_test.rb'
37
+ test.verbose = true
38
+ end
39
+ rescue LoadError
40
+ task :rcov do
41
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
42
+ end
43
+ end
44
+
45
+ task :test => :check_dependencies
46
+
47
+ task :default => :test
48
+
49
+ require 'rake/rdoctask'
50
+ Rake::RDocTask.new do |rdoc|
51
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
52
+ rdoc.rdoc_dir = 'rdoc'
53
+ rdoc.title = "youthtree-settings #{version}"
54
+ rdoc.rdoc_files.include('README*')
55
+ rdoc.rdoc_files.include('lib/**/*.rb')
56
+ end
@@ -0,0 +1,70 @@
1
+ require 'active_support/concern'
2
+
3
+ module YouthTree
4
+ module ControllerExt
5
+
6
+ VERSION = "0.1.0".freeze
7
+
8
+ mattr_accessor :known_extensions
9
+ self.known_extensions ||= {}
10
+
11
+ def self.load_default!
12
+ dir = File.expand_path("controller_ext", File.dirname(__FILE__))
13
+ Dir[File.join(dir, "*.rb")].each do |f|
14
+ require f
15
+ end
16
+ end
17
+
18
+ # Lookup controller extensions.
19
+ def self.[](name)
20
+ name = name.to_sym
21
+ if known_extensions.has_key?(name)
22
+ return known_extensions[name]
23
+ else
24
+ constant_name = "#{name.to_s.classify}Ext"
25
+ [nil, "YouthTree::ControllerExt", "YouthTree"].each do |namespace|
26
+ full = [namespace, constant_name].compact.join("::")
27
+ begin
28
+ constant = full.constantize
29
+ known_extensions[name] = constant
30
+ return constant
31
+ rescue NameError
32
+ end
33
+ end
34
+ nil
35
+ end
36
+ end
37
+
38
+ def self.for(name, &blk)
39
+ constant_name = "#{name.to_s.classify}Ext"
40
+ const_set(constant_name, Module.new) unless const_defined?(constant_name)
41
+ const_get(constant_name).tap do |m|
42
+ m.module_eval(&blk) if blk.present?
43
+ end
44
+ end
45
+
46
+ module ClassMethods
47
+
48
+ def use_controller_exts(*args)
49
+ args.each do |k|
50
+ mod = YouthTree::ControllerExt[k]
51
+ include mod if mod.present?
52
+ end
53
+ end
54
+ alias uses_controller_exts use_controller_exts
55
+
56
+ end
57
+
58
+ # Include the mixin in action controller.
59
+ ActionController::Base.extend(ClassMethods) if defined?(ActionController::Base)
60
+
61
+ if defined?(Rails::Railtie)
62
+ class Railtie < Rails::Railtie
63
+ initializer "youthtree.controller-ext" do
64
+ YouthTree::ControllerExt.load_default!
65
+ end
66
+ end
67
+ end
68
+
69
+ end
70
+ end
@@ -0,0 +1,9 @@
1
+ YouthTree::ControllerExt.for(:almost_happy) do
2
+
3
+ protected
4
+
5
+ def end_of_association_chain
6
+ super.ordered_for_preview
7
+ end
8
+
9
+ end
@@ -0,0 +1,49 @@
1
+ YouthTree::ControllerExt.for(:authlogic_helpers) do
2
+
3
+ def self.included(parent)
4
+ parent.class_eval do
5
+ helper_method :current_user, :current_user_session, :logged_in?
6
+ end
7
+ end
8
+
9
+ protected
10
+
11
+ def logged_in?
12
+ current_user.present?
13
+ end
14
+
15
+ def current_user_session
16
+ @current_user_session ||= UserSession.find
17
+ end
18
+
19
+ def current_user
20
+ @current_user ||= current_user_session && current_user_session.record
21
+ end
22
+
23
+ def require_user
24
+ unless logged_in?
25
+ store_location
26
+ redirect_to sign_in_path, :notice => "To view this page, you must first log in."
27
+ return false
28
+ end
29
+ end
30
+
31
+ def require_no_user
32
+ if logged_in?
33
+ store_location
34
+ redirect_to users_path(:current), :notice => "You must be logged out to access this page"
35
+ return false
36
+ end
37
+ end
38
+
39
+ def store_location
40
+ session[:return_to] = request.request_uri if request.get?
41
+ end
42
+
43
+ def redirect_back_or_default(default, options = {})
44
+ redirect_to(session[:return_to] || default, options)
45
+ session[:return_to] = nil
46
+ end
47
+
48
+
49
+ end
@@ -0,0 +1,26 @@
1
+ YouthTree::ControllerExt.for(:authorization_helpers) do
2
+
3
+ def self.included(klass)
4
+ klass.class_eval do
5
+ helper_method :can?
6
+ end
7
+ end
8
+
9
+ protected
10
+
11
+ def can?(action, object)
12
+ logged_in? && current_user.can?(action, object)
13
+ end
14
+
15
+ def unauthorized!(message = nil)
16
+ respond_to do |format|
17
+ format.html do
18
+ message = ["I'm sorry, you don't have the correct permissions to do that", message].compact.join(" - ")
19
+ redirect_to :root, :alert => message
20
+ end
21
+ format.json { head :forbidden }
22
+ format.xml { head :forbidden }
23
+ end
24
+ end
25
+
26
+ end
@@ -0,0 +1,9 @@
1
+ YouthTree::ControllerExt.for(:pseudocephalopod_resource) do
2
+
3
+ protected
4
+
5
+ def resource
6
+ get_resource_ivar || set_resource_ivar(end_of_association_chain.find_using_slug!(params[:id]))
7
+ end
8
+
9
+ end
@@ -0,0 +1,25 @@
1
+ YouthTree::ControllerExt.for(:ssl_requirement) do
2
+
3
+ def self.included(parent)
4
+ if defined?(Settings)
5
+ (class << parent; self; end).class_eval do
6
+ include ClassMethods
7
+ alias_method_chain :ssl_required, :env_check
8
+ alias_method_chain :ssl_allowed, :env_check
9
+ end
10
+ end
11
+ end
12
+
13
+ module ClassMethods
14
+
15
+ def ssl_required_with_env_check(*actions)
16
+ ssl_required_without_env_check(*actions) if Settings.ssl?
17
+ end
18
+
19
+ def ssl_allowed_with_env_check(*actions)
20
+ ssl_allowed_without_env_check(*actions) if Settings.ssl?
21
+ end
22
+
23
+ end
24
+
25
+ end
@@ -0,0 +1,33 @@
1
+ YouthTree::ControllerExt.for(:title_estuary) do
2
+
3
+ def self.included(p)
4
+ p.helper_method(:page_title_is, :hide_title?)
5
+ end
6
+
7
+ protected
8
+
9
+ def hide_title?
10
+ instance_variable_defined?(:@hide_title) && @hide_title
11
+ end
12
+
13
+ def hide_title!
14
+ @hide_title = true
15
+ end
16
+
17
+ def show_title!
18
+ @hide_title = false
19
+ end
20
+
21
+ def page_title_is(title)
22
+ self.page_title = title
23
+ end
24
+
25
+ def interpolation_options
26
+ @__interpolation_options ||= {}
27
+ end
28
+
29
+ def add_title_variables!(mapping = {})
30
+ interpolation_options.merge!(mapping)
31
+ end
32
+
33
+ end
@@ -0,0 +1,24 @@
1
+ YouthTree::ControllerExt.for(:translation) do
2
+
3
+ def self.included(parent)
4
+ parent.send :include, InstanceMethods
5
+ parent.send :helper_method ,:tf, :tu
6
+ end
7
+
8
+
9
+ module InstanceMethods
10
+
11
+ protected
12
+
13
+ def tf(key)
14
+ I18n.t(key.to_sym, :scope => :flash)
15
+ end
16
+
17
+ def tu(name, options = {})
18
+ scope = [:ui, options.delete(:scope)].compact.join(".").to_sym
19
+ I18n.t(name, options.merge(:scope => scope))
20
+ end
21
+
22
+ end
23
+
24
+ end
@@ -0,0 +1 @@
1
+ require 'youth_tree/controller_ext'
@@ -0,0 +1,55 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{youthtree-controller-ext}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Darcy Laycock"]
12
+ s.date = %q{2010-07-08}
13
+ s.description = %q{Provides uses_controller_extension and a set of common mixins.}
14
+ s.email = %q{sutto@sutto.net}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.md"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.md",
24
+ "Rakefile",
25
+ "lib/youth_tree/controller_ext.rb",
26
+ "lib/youth_tree/controller_ext/almost_happy_ext.rb",
27
+ "lib/youth_tree/controller_ext/authlogic_helpers_ext.rb",
28
+ "lib/youth_tree/controller_ext/authorization_helpers_ext.rb",
29
+ "lib/youth_tree/controller_ext/pseudocephalopod_resource_ext.rb",
30
+ "lib/youth_tree/controller_ext/ssl_requirement_ext.rb",
31
+ "lib/youth_tree/controller_ext/title_estuary_ext.rb",
32
+ "lib/youth_tree/controller_ext/translation_ext.rb",
33
+ "lib/youthtree-controller-ext.rb",
34
+ "youthtree-controller-ext.gemspec"
35
+ ]
36
+ s.homepage = %q{http://github.com/YouthTree/youthtree-controller-ext}
37
+ s.rdoc_options = ["--charset=UTF-8"]
38
+ s.require_paths = ["lib"]
39
+ s.rubygems_version = %q{1.3.7}
40
+ s.summary = %q{Simple controller mixin support for rails 3. Including some common extensions.}
41
+
42
+ if s.respond_to? :specification_version then
43
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
44
+ s.specification_version = 3
45
+
46
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
47
+ s.add_runtime_dependency(%q<activesupport>, [">= 3.0.0.beta4"])
48
+ else
49
+ s.add_dependency(%q<activesupport>, [">= 3.0.0.beta4"])
50
+ end
51
+ else
52
+ s.add_dependency(%q<activesupport>, [">= 3.0.0.beta4"])
53
+ end
54
+ end
55
+
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: youthtree-controller-ext
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Darcy Laycock
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-07-08 00:00:00 +08:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: activesupport
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: -1848230024
30
+ segments:
31
+ - 3
32
+ - 0
33
+ - 0
34
+ - beta4
35
+ version: 3.0.0.beta4
36
+ type: :runtime
37
+ version_requirements: *id001
38
+ description: Provides uses_controller_extension and a set of common mixins.
39
+ email: sutto@sutto.net
40
+ executables: []
41
+
42
+ extensions: []
43
+
44
+ extra_rdoc_files:
45
+ - LICENSE
46
+ - README.md
47
+ files:
48
+ - .document
49
+ - .gitignore
50
+ - LICENSE
51
+ - README.md
52
+ - Rakefile
53
+ - lib/youth_tree/controller_ext.rb
54
+ - lib/youth_tree/controller_ext/almost_happy_ext.rb
55
+ - lib/youth_tree/controller_ext/authlogic_helpers_ext.rb
56
+ - lib/youth_tree/controller_ext/authorization_helpers_ext.rb
57
+ - lib/youth_tree/controller_ext/pseudocephalopod_resource_ext.rb
58
+ - lib/youth_tree/controller_ext/ssl_requirement_ext.rb
59
+ - lib/youth_tree/controller_ext/title_estuary_ext.rb
60
+ - lib/youth_tree/controller_ext/translation_ext.rb
61
+ - lib/youthtree-controller-ext.rb
62
+ - youthtree-controller-ext.gemspec
63
+ has_rdoc: true
64
+ homepage: http://github.com/YouthTree/youthtree-controller-ext
65
+ licenses: []
66
+
67
+ post_install_message:
68
+ rdoc_options:
69
+ - --charset=UTF-8
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ hash: 3
78
+ segments:
79
+ - 0
80
+ version: "0"
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ hash: 3
87
+ segments:
88
+ - 0
89
+ version: "0"
90
+ requirements: []
91
+
92
+ rubyforge_project:
93
+ rubygems_version: 1.3.7
94
+ signing_key:
95
+ specification_version: 3
96
+ summary: Simple controller mixin support for rails 3. Including some common extensions.
97
+ test_files: []
98
+