doodoo 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +41 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +64 -0
- data/Rakefile +10 -0
- data/doodoo.gemspec +31 -0
- data/lib/doodoo.rb +7 -0
- data/lib/doodoo/nested_load_and_authorize.rb +91 -0
- data/lib/doodoo/version.rb +3 -0
- data/spec/lib/nested_load_and_authorize_spec.rb +47 -0
- data/spec/spec_helper.rb +24 -0
- metadata +118 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 1319c292b242343da75d9e62ed590111d475553e
|
4
|
+
data.tar.gz: fce9490b875dc6e900834312b0b4475ec4cf0f47
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 451de26fbd52f97d54179e18c99faec95d7a02dac23b3c60f5a2d3ceadff7dfb2f3ca3e10481c9c8e9272d061923e9fada11109af1f79f67a1e01e7df568797f
|
7
|
+
data.tar.gz: b7e0f144843e070956c7da7043d84c683895e16ad9b607e27d3db46aff37207f3595d166d355b800956dc3f7f4268e9de282d674cfac549dc2a09ce1bc968b15
|
data/.gitignore
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
/.config
|
4
|
+
/coverage/
|
5
|
+
/InstalledFiles
|
6
|
+
/pkg/
|
7
|
+
/spec/reports/
|
8
|
+
/test/tmp/
|
9
|
+
/test/version_tmp/
|
10
|
+
/tmp/
|
11
|
+
|
12
|
+
## Specific to RubyMotion:
|
13
|
+
.dat*
|
14
|
+
.repl_history
|
15
|
+
build/
|
16
|
+
|
17
|
+
## Documentation cache and generated files:
|
18
|
+
/.yardoc/
|
19
|
+
/_yardoc/
|
20
|
+
/doc/
|
21
|
+
/rdoc/
|
22
|
+
|
23
|
+
## Environment normalisation:
|
24
|
+
/.bundle/
|
25
|
+
/lib/bundler/man/
|
26
|
+
|
27
|
+
# for a library or gem, you might want to ignore these files since the code is
|
28
|
+
# intended to run in multiple environments; otherwise, check them in:
|
29
|
+
Gemfile.lock
|
30
|
+
.ruby-version
|
31
|
+
.ruby-gemset
|
32
|
+
|
33
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
34
|
+
.rvmrc
|
35
|
+
|
36
|
+
# RubyMine IDE:
|
37
|
+
.idea/
|
38
|
+
|
39
|
+
# Mac:
|
40
|
+
.DS_Store
|
41
|
+
|
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Matt Connolly
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
# Doodoo
|
2
|
+
|
3
|
+
A set of tools to help with CanCan. Doodoo provides a module for use with Rails controllers that assists with
|
4
|
+
authorisation, especially with nested routes.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
gem 'doodoo'
|
11
|
+
|
12
|
+
And then execute:
|
13
|
+
|
14
|
+
$ bundle
|
15
|
+
|
16
|
+
Or install it yourself as:
|
17
|
+
|
18
|
+
$ gem install doodoo
|
19
|
+
|
20
|
+
## Usage
|
21
|
+
|
22
|
+
Add to ApplicationController:
|
23
|
+
|
24
|
+
include Doodoo::NestedLoadAndAuthorize
|
25
|
+
|
26
|
+
And then in controllers (For example a TasksController, nested under ProjectsController):
|
27
|
+
|
28
|
+
before_action do
|
29
|
+
# load task nested under project, only if params[:project_id] exists:
|
30
|
+
load_and_authorize_if_present :project do
|
31
|
+
load_and_authorize :task
|
32
|
+
end
|
33
|
+
|
34
|
+
# enforce nested load:
|
35
|
+
load_and_authorize :project do
|
36
|
+
load_and_authorize :task
|
37
|
+
end
|
38
|
+
|
39
|
+
# and for a shallow route:
|
40
|
+
load_and_authorize :task
|
41
|
+
|
42
|
+
# load without authorisation:
|
43
|
+
load_resource :task
|
44
|
+
|
45
|
+
# only specific actions
|
46
|
+
load_and_authorize :task, :only => [:show]
|
47
|
+
|
48
|
+
# supply a condition in should_load_task? method (can use a proc here too)
|
49
|
+
load_and_authorize :task, :if => :should_load_task?
|
50
|
+
|
51
|
+
# specific id parameter for another action:
|
52
|
+
load_and_authorize :task, :id_param => :task_id, :only => [:other_action]
|
53
|
+
|
54
|
+
# only load if parameter present in menu:
|
55
|
+
end
|
56
|
+
|
57
|
+
|
58
|
+
## Contributing
|
59
|
+
|
60
|
+
1. Fork it ( http://github.com/<my-github-username>/doodoo/fork )
|
61
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
62
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
63
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
64
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/doodoo.gemspec
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'doodoo/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "doodoo"
|
8
|
+
spec.version = Doodoo::VERSION
|
9
|
+
spec.authors = ["Matt Connolly"]
|
10
|
+
spec.email = ["matt.connolly@tworedkites.com"]
|
11
|
+
spec.summary = %q{A set of tools to help with CanCan.}
|
12
|
+
spec.description = <<EOF
|
13
|
+
A set of tools to help with CanCan.
|
14
|
+
|
15
|
+
Doodoo provides a module for use with Rails controllers that assists with authorisation,
|
16
|
+
especially with nested routes.
|
17
|
+
EOF
|
18
|
+
spec.homepage = ""
|
19
|
+
spec.license = "MIT"
|
20
|
+
|
21
|
+
spec.files = `git ls-files -z`.split("\x0")
|
22
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
23
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
24
|
+
spec.require_paths = ["lib"]
|
25
|
+
|
26
|
+
spec.add_dependency 'rails', '>= 3.2'
|
27
|
+
|
28
|
+
spec.add_development_dependency "bundler", "~> 1.5"
|
29
|
+
spec.add_development_dependency "rake"
|
30
|
+
spec.add_development_dependency "rspec", "~> 2.14"
|
31
|
+
end
|
data/lib/doodoo.rb
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
require 'active_support/concern'
|
2
|
+
require 'active_record'
|
3
|
+
|
4
|
+
module Doodoo
|
5
|
+
module NestedLoadAndAuthorize
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
included do
|
9
|
+
# make the loaded_resources array accessible in views.
|
10
|
+
helper_method :loaded_resources
|
11
|
+
end
|
12
|
+
|
13
|
+
# An array of the resources loaded by +load_and_authorize+. This may be useful for constructing
|
14
|
+
# breadcrumbs or for views to determine what context they are being rendered in.
|
15
|
+
def loaded_resources
|
16
|
+
@_loaded_resources ||= []
|
17
|
+
end
|
18
|
+
|
19
|
+
# load and authorise the resource with the same parameters as the Can Can controller additions class macro
|
20
|
+
# load_and_authorize_resource.
|
21
|
+
#
|
22
|
+
# This differs in that it is an instance method, so you must call it in a before_filter (or from another instance
|
23
|
+
# method).
|
24
|
+
#
|
25
|
+
# This method has no effect if the resource is already loaded.
|
26
|
+
#
|
27
|
+
# Any block passed to this method will be executed if the resource was successfully loaded by this call. Nested
|
28
|
+
# calls automatically supply the +:through+ parameter. To bypass this, set :through => nil.
|
29
|
+
def load_and_authorize(name, options={})
|
30
|
+
@_through_stack ||= []
|
31
|
+
|
32
|
+
# only touch can can if the instance variable is nil
|
33
|
+
resource = instance_variable_get("@#{name}") || instance_variable_get("@#{name.to_s.pluralize}")
|
34
|
+
|
35
|
+
if resource.nil?
|
36
|
+
# apply if, only and except behaviours just is if this was done by before_filter
|
37
|
+
proceed = true
|
38
|
+
proceed &&= [*options[:only]].include?(action_name.to_sym) if options[:only]
|
39
|
+
proceed &&= ![*options[:except]].include?(action_name.to_sym) if options[:except]
|
40
|
+
proceed &&= case options[:if]
|
41
|
+
when Symbol
|
42
|
+
send(options[:if])
|
43
|
+
when Proc
|
44
|
+
options[:if].call
|
45
|
+
when nil
|
46
|
+
true
|
47
|
+
end
|
48
|
+
|
49
|
+
if proceed
|
50
|
+
# automatically load this resource through a nested one unless manually specified
|
51
|
+
options[:through] = @_through_stack.last unless @_through_stack.empty? || options.include?(:through)
|
52
|
+
# create the can can resource class
|
53
|
+
cancan = self.class.cancan_resource_class.new(self, name, options.except(:if, :only, :except, :param))
|
54
|
+
resource = cancan.load_resource
|
55
|
+
cancan.authorize_resource unless options[:skip_authorize]
|
56
|
+
|
57
|
+
if resource
|
58
|
+
# If the resource was a scope (query/collection-proxy) simply add the result class into the loaded_resources
|
59
|
+
loaded_resources << ((resource.is_a? ActiveRecord::Relation) ? resource.klass : resource)
|
60
|
+
end
|
61
|
+
|
62
|
+
if resource && block_given?
|
63
|
+
# only call block if we got an instance variable set
|
64
|
+
begin
|
65
|
+
@_through_stack.push(name)
|
66
|
+
yield
|
67
|
+
ensure
|
68
|
+
@_through_stack.pop
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
resource
|
74
|
+
end
|
75
|
+
|
76
|
+
# syntactic sugar for adding the :skip_authorize option
|
77
|
+
def load_resource(name, options={}, &block)
|
78
|
+
options[:skip_authorize] = true
|
79
|
+
load_and_authorize(name, options, &block)
|
80
|
+
end
|
81
|
+
|
82
|
+
# load the resource only if its request parameter is present. This allows for optional nesting to
|
83
|
+
# work like shallow routes with the before_filter way of doing things.
|
84
|
+
def load_and_authorize_if_present(name, options={}, &block)
|
85
|
+
key = options[:id_param] || "#{name}_id"
|
86
|
+
if params[key]
|
87
|
+
load_and_authorize(name, options, &block)
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'action_controller'
|
3
|
+
|
4
|
+
describe Doodoo::NestedLoadAndAuthorize do
|
5
|
+
|
6
|
+
class ChildController < ActionController::Base
|
7
|
+
include Doodoo::NestedLoadAndAuthorize
|
8
|
+
end
|
9
|
+
|
10
|
+
class ToyController < ActionController::Base
|
11
|
+
include Doodoo::NestedLoadAndAuthorize
|
12
|
+
end
|
13
|
+
|
14
|
+
context "loading a model" do
|
15
|
+
let(:can_can_class) { double("CanCan class") }
|
16
|
+
let(:child_controller) { ChildController.new }
|
17
|
+
let(:toy_controller) { ToyController.new }
|
18
|
+
let(:child) { double("Amelia") }
|
19
|
+
let(:toy) { double("Peppa Pig doll") }
|
20
|
+
before { ActionController::Base.stub(:cancan_resource_class => can_can_class) }
|
21
|
+
|
22
|
+
it "loads a model" do
|
23
|
+
can_can_resource = double("can can resource")
|
24
|
+
can_can_class.should_receive(:new).with(child_controller, :child, {}).and_return(can_can_resource)
|
25
|
+
can_can_resource.should_receive(:load_resource).and_return(child)
|
26
|
+
can_can_resource.should_receive(:authorize_resource)
|
27
|
+
child_controller.load_and_authorize :child
|
28
|
+
expect(child_controller.loaded_resources).to match_array([child])
|
29
|
+
end
|
30
|
+
|
31
|
+
it "loads a nested model" do
|
32
|
+
can_can_resource = double("can can resource")
|
33
|
+
can_can_resource_2 = double("can can resource")
|
34
|
+
can_can_class.should_receive(:new).with(toy_controller, :child, {}).and_return(can_can_resource)
|
35
|
+
can_can_class.should_receive(:new).with(toy_controller, :toy, {:through => :child}).and_return(can_can_resource_2)
|
36
|
+
can_can_resource.should_receive(:load_resource).and_return(child)
|
37
|
+
can_can_resource.should_receive(:authorize_resource)
|
38
|
+
can_can_resource_2.should_receive(:load_resource).and_return(toy)
|
39
|
+
can_can_resource_2.should_receive(:authorize_resource)
|
40
|
+
|
41
|
+
toy_controller.load_and_authorize :child do
|
42
|
+
toy_controller.load_and_authorize :toy
|
43
|
+
end
|
44
|
+
expect(toy_controller.loaded_resources).to match_array([child, toy])
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
+
# Require this file using `require "spec_helper"` to ensure that it is only
|
4
|
+
# loaded once.
|
5
|
+
#
|
6
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
7
|
+
|
8
|
+
require 'bundler/setup'
|
9
|
+
|
10
|
+
$:.unshift File.expand_path("../../lib", __FILE__)
|
11
|
+
|
12
|
+
require 'doodoo'
|
13
|
+
|
14
|
+
RSpec.configure do |config|
|
15
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
16
|
+
config.run_all_when_everything_filtered = true
|
17
|
+
config.filter_run :focus
|
18
|
+
|
19
|
+
# Run specs in random order to surface order dependencies. If you find an
|
20
|
+
# order dependency and want to debug it, you can fix the order by providing
|
21
|
+
# the seed, which is printed after each run.
|
22
|
+
# --seed 1234
|
23
|
+
config.order = 'random'
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,118 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: doodoo
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Matt Connolly
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-05-22 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.2'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.2'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.5'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.5'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '2.14'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '2.14'
|
69
|
+
description: |
|
70
|
+
A set of tools to help with CanCan.
|
71
|
+
|
72
|
+
Doodoo provides a module for use with Rails controllers that assists with authorisation,
|
73
|
+
especially with nested routes.
|
74
|
+
email:
|
75
|
+
- matt.connolly@tworedkites.com
|
76
|
+
executables: []
|
77
|
+
extensions: []
|
78
|
+
extra_rdoc_files: []
|
79
|
+
files:
|
80
|
+
- ".gitignore"
|
81
|
+
- ".rspec"
|
82
|
+
- Gemfile
|
83
|
+
- LICENSE.txt
|
84
|
+
- README.md
|
85
|
+
- Rakefile
|
86
|
+
- doodoo.gemspec
|
87
|
+
- lib/doodoo.rb
|
88
|
+
- lib/doodoo/nested_load_and_authorize.rb
|
89
|
+
- lib/doodoo/version.rb
|
90
|
+
- spec/lib/nested_load_and_authorize_spec.rb
|
91
|
+
- spec/spec_helper.rb
|
92
|
+
homepage: ''
|
93
|
+
licenses:
|
94
|
+
- MIT
|
95
|
+
metadata: {}
|
96
|
+
post_install_message:
|
97
|
+
rdoc_options: []
|
98
|
+
require_paths:
|
99
|
+
- lib
|
100
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - ">="
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - ">="
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
requirements: []
|
111
|
+
rubyforge_project:
|
112
|
+
rubygems_version: 2.2.2
|
113
|
+
signing_key:
|
114
|
+
specification_version: 4
|
115
|
+
summary: A set of tools to help with CanCan.
|
116
|
+
test_files:
|
117
|
+
- spec/lib/nested_load_and_authorize_spec.rb
|
118
|
+
- spec/spec_helper.rb
|