resource_defaults 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +22 -0
- data/.rspec +1 -0
- data/Gemfile +4 -0
- data/LICENSE +20 -0
- data/README.rdoc +19 -0
- data/Rakefile +44 -0
- data/VERSION +1 -0
- data/lib/resource_defaults.rb +52 -0
- data/spec/resource_defaults_spec.rb +98 -0
- data/spec/spec_helper.rb +12 -0
- metadata +114 -0
data/.document
ADDED
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 Andrew Bloomgarden
|
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.rdoc
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
= resource_defaults
|
2
|
+
|
3
|
+
If you have resources accessible from multiple routes, it's useful to be able to set default actions. This gem makes that possible.
|
4
|
+
|
5
|
+
Works with Rails 3.
|
6
|
+
|
7
|
+
== Note on Patches/Pull Requests
|
8
|
+
|
9
|
+
* Fork the project.
|
10
|
+
* Make your feature addition or bug fix.
|
11
|
+
* Add tests for it. This is important so I don't break it in a
|
12
|
+
future version unintentionally.
|
13
|
+
* Commit, do not mess with rakefile, version, or history.
|
14
|
+
(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)
|
15
|
+
* Send me a pull request. Bonus points for topic branches.
|
16
|
+
|
17
|
+
== Copyright
|
18
|
+
|
19
|
+
Copyright (c) 2010 Andrew Bloomgarden. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'bundler'
|
4
|
+
Bundler.setup
|
5
|
+
Bundler.require
|
6
|
+
|
7
|
+
begin
|
8
|
+
require 'jeweler'
|
9
|
+
Jeweler::Tasks.new do |gem|
|
10
|
+
gem.name = "resource_defaults"
|
11
|
+
gem.summary = %Q{Allows setting defaults for resources in Rails routing}
|
12
|
+
gem.description = %Q{If you have resources accessible from multiple routes, it's useful to be able to set default actions. This gem makes that possible.}
|
13
|
+
gem.email = "stalkingtiger@gmail.com"
|
14
|
+
gem.homepage = "http://github.com/aughr/resource_defaults"
|
15
|
+
gem.authors = ["Andrew Bloomgarden"]
|
16
|
+
gem.add_development_dependency "rspec-rails", ">= 2.0.0.beta.9.1"
|
17
|
+
gem.add_runtime_dependency "rails", ">= 3.0.0.beta3"
|
18
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
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 'rspec/core/rake_task'
|
26
|
+
RSpec::Core::RakeTask.new(:spec)
|
27
|
+
|
28
|
+
RSpec::Core::RakeTask.new(:rcov) do |spec|
|
29
|
+
spec.rcov = true
|
30
|
+
end
|
31
|
+
|
32
|
+
task :spec => :check_dependencies
|
33
|
+
|
34
|
+
task :default => :spec
|
35
|
+
|
36
|
+
require 'rake/rdoctask'
|
37
|
+
Rake::RDocTask.new do |rdoc|
|
38
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
39
|
+
|
40
|
+
rdoc.rdoc_dir = 'rdoc'
|
41
|
+
rdoc.title = "resource_defaults #{version}"
|
42
|
+
rdoc.rdoc_files.include('README*')
|
43
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
44
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.0.0
|
@@ -0,0 +1,52 @@
|
|
1
|
+
module ResourceDefaults
|
2
|
+
class Railtie < Rails::Railtie
|
3
|
+
initializer 'resource_defaults.load', :before => 'action_dispatch.prepare_dispatcher' do
|
4
|
+
ActionDispatch::Routing::Mapper.send :include, ResourceDefaults
|
5
|
+
end
|
6
|
+
end
|
7
|
+
|
8
|
+
def initialize(*args)
|
9
|
+
super
|
10
|
+
@scope[:resource_defaults] = {}
|
11
|
+
@scope[:resource_defaults_in_progress] = []
|
12
|
+
end
|
13
|
+
|
14
|
+
def resource_defaults(*args, &block)
|
15
|
+
options = args.extract_options!
|
16
|
+
target = args.first
|
17
|
+
raise ArgumentError, 'must provide a resource to apply default actions' if target.blank?
|
18
|
+
@scope[:resource_defaults][target] = options.merge(:block => block)
|
19
|
+
end
|
20
|
+
|
21
|
+
def scope(*args)
|
22
|
+
old_resource_defaults = @scope[:resource_defaults].dup
|
23
|
+
super
|
24
|
+
ensure
|
25
|
+
@scope[:resource_defaults] = old_resource_defaults
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def apply_common_behavior_for(method, resources, options, &block)
|
31
|
+
return true if super
|
32
|
+
|
33
|
+
resource = resources.first
|
34
|
+
if !@scope[:resource_defaults_in_progress].include?(resource) && @scope[:resource_defaults][resource]
|
35
|
+
defaults = @scope[:resource_defaults][resource].dup
|
36
|
+
scope :resource_defaults_in_progress => resource do
|
37
|
+
default_block = defaults.delete(:block)
|
38
|
+
send method, resource, options do
|
39
|
+
instance_exec &default_block if default_block
|
40
|
+
instance_exec &block if block
|
41
|
+
end
|
42
|
+
end
|
43
|
+
return true
|
44
|
+
end
|
45
|
+
|
46
|
+
false
|
47
|
+
end
|
48
|
+
|
49
|
+
def merge_resource_defaults_in_progress_scope(parent, child)
|
50
|
+
(parent || []) + [child]
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,98 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe "ResourceDefaults" do
|
4
|
+
before(:all) do
|
5
|
+
ActionDispatch::Routing::Mapper.send :include, ResourceDefaults
|
6
|
+
end
|
7
|
+
|
8
|
+
before(:each) do
|
9
|
+
@routes = ActionDispatch::Routing::RouteSet.new
|
10
|
+
end
|
11
|
+
|
12
|
+
describe "standard resources" do
|
13
|
+
before(:each) do
|
14
|
+
@routes.draw do
|
15
|
+
resources :articles do
|
16
|
+
put :publish, :on => :member
|
17
|
+
get :search, :on => :collection
|
18
|
+
|
19
|
+
resources :comments do
|
20
|
+
put :approve, :on => :member
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
resources :comments do
|
25
|
+
put :approve, :on => :member
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should have 25 routes" do
|
31
|
+
# 3*7 defaults + publish + search + 2*approve
|
32
|
+
routes_for(@routes).length.should == 25
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
describe "defaulted resources" do
|
37
|
+
before(:each) do
|
38
|
+
@routes.draw do
|
39
|
+
resource_defaults :comments do
|
40
|
+
put :approve, :on => :member
|
41
|
+
end
|
42
|
+
|
43
|
+
resources :articles do
|
44
|
+
put :publish, :on => :member
|
45
|
+
get :search, :on => :collection
|
46
|
+
|
47
|
+
resources :comments
|
48
|
+
end
|
49
|
+
|
50
|
+
resources :comments
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
it "should have 25 routes" do
|
55
|
+
# 3*7 defaults + publish + search + 2*approve
|
56
|
+
routes_for(@routes).length.should == 25
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
describe "scoped defaulted resources" do
|
61
|
+
before(:each) do
|
62
|
+
@routes.draw do
|
63
|
+
namespace :admin do
|
64
|
+
resource_defaults :comments do
|
65
|
+
put :approve, :on => :member
|
66
|
+
end
|
67
|
+
|
68
|
+
resources :articles do
|
69
|
+
put :publish, :on => :member
|
70
|
+
get :search, :on => :collection
|
71
|
+
|
72
|
+
resources :comments
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
resources :comments
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
it "should have 24 routes" do
|
81
|
+
# 3*7 defaults + publish + search + approve
|
82
|
+
routes_for(@routes).length.should == 24
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
def routes_for(routes)
|
87
|
+
# taken from Railties's routes.rake
|
88
|
+
routes.routes.collect do |route|
|
89
|
+
# TODO: The :index method is deprecated in 1.9 in favor of :key
|
90
|
+
# but we don't have :key in 1.8.7. We can remove this check when
|
91
|
+
# stop supporting 1.8.x
|
92
|
+
key_method = Hash.method_defined?('key') ? 'key' : 'index'
|
93
|
+
name = routes.named_routes.routes.send(key_method, route).to_s
|
94
|
+
reqs = route.requirements.empty? ? "" : route.requirements.inspect
|
95
|
+
{:name => name, :verb => route.verb.to_s, :path => route.path, :reqs => reqs}
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
2
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
3
|
+
|
4
|
+
require 'rubygems'
|
5
|
+
# Set up gems listed in the Gemfile.
|
6
|
+
require 'bundler'
|
7
|
+
Bundler.setup
|
8
|
+
Bundler.require
|
9
|
+
|
10
|
+
require 'resource_defaults'
|
11
|
+
require 'rspec'
|
12
|
+
require 'rspec/autorun'
|
metadata
ADDED
@@ -0,0 +1,114 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: resource_defaults
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 23
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
- 0
|
10
|
+
version: 1.0.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Andrew Bloomgarden
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-06-01 00:00:00 -07:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
23
|
+
none: false
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
hash: 124392737
|
28
|
+
segments:
|
29
|
+
- 2
|
30
|
+
- 0
|
31
|
+
- 0
|
32
|
+
- beta
|
33
|
+
- 9
|
34
|
+
- 1
|
35
|
+
version: 2.0.0.beta.9.1
|
36
|
+
type: :development
|
37
|
+
name: rspec-rails
|
38
|
+
prerelease: false
|
39
|
+
version_requirements: *id001
|
40
|
+
- !ruby/object:Gem::Dependency
|
41
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
hash: -1848230021
|
47
|
+
segments:
|
48
|
+
- 3
|
49
|
+
- 0
|
50
|
+
- 0
|
51
|
+
- beta3
|
52
|
+
version: 3.0.0.beta3
|
53
|
+
type: :runtime
|
54
|
+
name: rails
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: *id002
|
57
|
+
description: If you have resources accessible from multiple routes, it's useful to be able to set default actions. This gem makes that possible.
|
58
|
+
email: stalkingtiger@gmail.com
|
59
|
+
executables: []
|
60
|
+
|
61
|
+
extensions: []
|
62
|
+
|
63
|
+
extra_rdoc_files:
|
64
|
+
- LICENSE
|
65
|
+
- README.rdoc
|
66
|
+
files:
|
67
|
+
- .document
|
68
|
+
- .gitignore
|
69
|
+
- .rspec
|
70
|
+
- Gemfile
|
71
|
+
- LICENSE
|
72
|
+
- README.rdoc
|
73
|
+
- Rakefile
|
74
|
+
- VERSION
|
75
|
+
- lib/resource_defaults.rb
|
76
|
+
- spec/resource_defaults_spec.rb
|
77
|
+
- spec/spec_helper.rb
|
78
|
+
has_rdoc: true
|
79
|
+
homepage: http://github.com/aughr/resource_defaults
|
80
|
+
licenses: []
|
81
|
+
|
82
|
+
post_install_message:
|
83
|
+
rdoc_options:
|
84
|
+
- --charset=UTF-8
|
85
|
+
require_paths:
|
86
|
+
- lib
|
87
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
88
|
+
none: false
|
89
|
+
requirements:
|
90
|
+
- - ">="
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
hash: 3
|
93
|
+
segments:
|
94
|
+
- 0
|
95
|
+
version: "0"
|
96
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ">="
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
hash: 3
|
102
|
+
segments:
|
103
|
+
- 0
|
104
|
+
version: "0"
|
105
|
+
requirements: []
|
106
|
+
|
107
|
+
rubyforge_project:
|
108
|
+
rubygems_version: 1.3.7
|
109
|
+
signing_key:
|
110
|
+
specification_version: 3
|
111
|
+
summary: Allows setting defaults for resources in Rails routing
|
112
|
+
test_files:
|
113
|
+
- spec/resource_defaults_spec.rb
|
114
|
+
- spec/spec_helper.rb
|