pundit_namespaces 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c9ddc6c580f50af6151214bb04a1cbf14b5816ef
4
+ data.tar.gz: 6bacd091829b3210fe407658f33684b603138b01
5
+ SHA512:
6
+ metadata.gz: f7132b1a41aa03f331ca086765cc505970f90b39a3183eaa0a8dcb34da473a76726205feabf9222a803137cf11449a214977e2f88275eaa2610869defee86360
7
+ data.tar.gz: b5e531855a91ad36d739e96325b8bb27992601e90957a8a3b1bc012858219d6e7aa3629191ef959ae20d0f17a5e9e80abe03897ea40a51147b2ce5928e766185
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ pundit_namespaces
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.2
data/.travis.yml ADDED
@@ -0,0 +1,10 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9
4
+ - 2.0
5
+ - 2.1
6
+ - 2.2
7
+ before_install: gem install bundler -v 1.10.6
8
+ addons:
9
+ code_climate:
10
+ repo_token: b981faeecbf981dabe7d229fe278683766592bc553f1f4025e629f8c4270a32e
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in pundit_namespaces.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,70 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ ## Uncomment and set this to only include directories you want to watch
5
+ # directories %w(app lib config test spec features) \
6
+ # .select{|d| Dir.exists?(d) ? d : UI.warning("Directory #{d} does not exist")}
7
+
8
+ ## Note: if you are using the `directories` clause above and you are not
9
+ ## watching the project directory ('.'), then you will want to move
10
+ ## the Guardfile to a watched dir and symlink it back, e.g.
11
+ #
12
+ # $ mkdir config
13
+ # $ mv Guardfile config/
14
+ # $ ln -s config/Guardfile .
15
+ #
16
+ # and, you'll have to watch "config/Guardfile" instead of "Guardfile"
17
+
18
+ # Note: The cmd option is now required due to the increasing number of ways
19
+ # rspec may be run, below are examples of the most common uses.
20
+ # * bundler: 'bundle exec rspec'
21
+ # * bundler binstubs: 'bin/rspec'
22
+ # * spring: 'bin/rspec' (This will use spring if running and you have
23
+ # installed the spring binstubs per the docs)
24
+ # * zeus: 'zeus rspec' (requires the server to be started separately)
25
+ # * 'just' rspec: 'rspec'
26
+
27
+ guard :rspec, cmd: "bundle exec rspec" do
28
+ require "guard/rspec/dsl"
29
+ dsl = Guard::RSpec::Dsl.new(self)
30
+
31
+ # Feel free to open issues for suggestions and improvements
32
+
33
+ # RSpec files
34
+ rspec = dsl.rspec
35
+ watch(rspec.spec_helper) { rspec.spec_dir }
36
+ watch(rspec.spec_support) { rspec.spec_dir }
37
+ watch(rspec.spec_files)
38
+
39
+ # Ruby files
40
+ ruby = dsl.ruby
41
+ dsl.watch_spec_files_for(ruby.lib_files)
42
+
43
+ # Rails files
44
+ rails = dsl.rails(view_extensions: %w(erb haml slim))
45
+ dsl.watch_spec_files_for(rails.app_files)
46
+ dsl.watch_spec_files_for(rails.views)
47
+
48
+ watch(rails.controllers) do |m|
49
+ [
50
+ rspec.spec.("routing/#{m[1]}_routing"),
51
+ rspec.spec.("controllers/#{m[1]}_controller"),
52
+ rspec.spec.("acceptance/#{m[1]}")
53
+ ]
54
+ end
55
+
56
+ # Rails config changes
57
+ watch(rails.spec_helper) { rspec.spec_dir }
58
+ watch(rails.routes) { "#{rspec.spec_dir}/routing" }
59
+ watch(rails.app_controller) { "#{rspec.spec_dir}/controllers" }
60
+
61
+ # Capybara features specs
62
+ watch(rails.view_dirs) { |m| rspec.spec.("features/#{m[1]}") }
63
+ watch(rails.layouts) { |m| rspec.spec.("features/#{m[1]}") }
64
+
65
+ # Turnip features and steps
66
+ watch(%r{^spec/acceptance/(.+)\.feature$})
67
+ watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) do |m|
68
+ Dir[File.join("**/#{m[1]}.feature")][0] || "spec/acceptance"
69
+ end
70
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Paul Spieker
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,69 @@
1
+ [![Build Status](https://travis-ci.org/spieker/pundit_namespaces.svg?branch=master)](https://travis-ci.org/spieker/pundit_namespaces)
2
+ [![Code Climate](https://codeclimate.com/github/spieker/pundit_namespaces/badges/gpa.svg)](https://codeclimate.com/github/spieker/pundit_namespaces)
3
+ [![Test Coverage](https://codeclimate.com/github/spieker/pundit_namespaces/badges/coverage.svg)](https://codeclimate.com/github/spieker/pundit_namespaces/coverage)
4
+
5
+ # PunditNamespaces
6
+
7
+ Namespaces for Pundit policies.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'pundit'
15
+ gem 'pundit_namespaces'
16
+ ```
17
+
18
+ Install pundit and then include PunditNamespaces in your application controller
19
+ and override the `pundit_namespace` method returning the namespace to be used.
20
+
21
+ ```ruby
22
+ class ApplicationController < ActionController::Base
23
+ include Pundit
24
+ include PunditNamespaces
25
+
26
+ def pundit_namespace
27
+ current_user.class.name
28
+ end
29
+ end
30
+ ```
31
+
32
+ ## Usage
33
+
34
+ As described in the installation part, the `pundit_namespace` method has to
35
+ return the namespace to be used. The policy has then to be namespaced within the
36
+ module returned by pundit_namespace.
37
+
38
+ ```ruby
39
+ # app/policies/admin/post_policy.rb
40
+ module Admin
41
+ class PostPolicy < AdminPolicy
42
+ class Scope < AdminPolicy::Scope
43
+ end
44
+ end
45
+ end
46
+
47
+ # app/policies/user/post_policy.rb
48
+ module User
49
+ class PostPolicy < UserPolicy
50
+ class Scope < UserPolicy::Scope
51
+ end
52
+ end
53
+ end
54
+ ```
55
+
56
+ ## Development
57
+
58
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
59
+
60
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
61
+
62
+ ## Contributing
63
+
64
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/pundit_namespaces. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
65
+
66
+
67
+ ## License
68
+
69
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "pundit_namespaces"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,68 @@
1
+ require 'pundit'
2
+
3
+ module PunditNamespaces
4
+ class NamespacedPolicyFinder < Pundit::PolicyFinder
5
+ attr_reader :namespace
6
+
7
+ def initialize(object, namespace = nil)
8
+ super(object)
9
+ @namespace = namespace
10
+ end
11
+
12
+ private
13
+ def base_policy
14
+ return nil if object.nil?
15
+ klass = name_from_method(object)
16
+ return klass if klass
17
+ klass = name_from_model_name(object) ||
18
+ name_from_class(object) ||
19
+ name_from_symbol(object) ||
20
+ name_from_array(object) ||
21
+ name_from_object(object)
22
+ "#{klass}#{Pundit::SUFFIX}"
23
+ end
24
+
25
+ def name_from_method(object)
26
+ if object.respond_to?(:policy_class)
27
+ object.policy_class
28
+ elsif object.class.respond_to?(:policy_class)
29
+ object.class.policy_class
30
+ else
31
+ nil
32
+ end
33
+ end
34
+
35
+ def name_from_model_name(object)
36
+ if object.respond_to?(:model_name)
37
+ object.model_name
38
+ elsif object.class.respond_to?(:model_name)
39
+ object.class.model_name
40
+ else
41
+ nil
42
+ end
43
+ end
44
+
45
+ def name_from_class(object)
46
+ return nil unless object.is_a?(Class)
47
+ object.name
48
+ end
49
+
50
+ def name_from_symbol(object)
51
+ return nil unless object.is_a?(Symbol)
52
+ object.to_s.camelize
53
+ end
54
+
55
+ def name_from_array(object)
56
+ return nil unless object.is_a?(Array)
57
+ object.join('/').camelize
58
+ end
59
+
60
+ def name_from_object(object)
61
+ name_from_class(object.class)
62
+ end
63
+
64
+ def find
65
+ [namespace, base_policy].compact.flatten.join('::')
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,3 @@
1
+ module PunditNamespaces
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,53 @@
1
+ require "pundit_namespaces/version"
2
+ require "pundit_namespaces/namespaced_policy_finder"
3
+
4
+ module PunditNamespaces
5
+ class <<self
6
+ def policy_scope(user, scope, namespace = nil)
7
+ policy_scope = NamespacedPolicyFinder.new(scope, namespace).scope
8
+ policy_scope.new(user, scope).resolve if policy_scope
9
+ end
10
+
11
+ def policy_scope!(user, scope, namespace = nil)
12
+ policy_scope = NamespacedPolicyFinder.new(scope, namespace).scope!
13
+ policy_scope.new(user, scope).resolve
14
+ end
15
+
16
+ def policy(user, record, namespace = nil)
17
+ policy = NamespacedPolicyFinder.new(record, namespace).policy
18
+ policy.new(user, record) if policy
19
+ end
20
+
21
+ def policy!(user, record, namespace = nil)
22
+ NamespacedPolicyFinder.new(record, namespace).policy!.new(user, record)
23
+ end
24
+ end
25
+
26
+ def pundit_namespace
27
+ end
28
+
29
+ def policy(record)
30
+ user = pundit_user
31
+ namespace = pundit_namespace
32
+ policies[record] ||= PunditNamespaces.policy!(user, record, namespace)
33
+ end
34
+
35
+ def policies
36
+ @_pundit_policies ||= {}
37
+ @_pundit_policies[pundit_namespace] ||= {}
38
+ end
39
+
40
+ def policy_scopes
41
+ @_pundit_policy_scopes ||= {}
42
+ @_pundit_policy_scopes[pundit_namespace] ||= {}
43
+ end
44
+
45
+ private
46
+
47
+ def pundit_policy_scope(scope)
48
+ user = pundit_user
49
+ namespace = pundit_namespace
50
+ scopes = policy_scopes
51
+ scopes[scope] ||= PunditNamespaces.policy_scope!(user, scope, namespace)
52
+ end
53
+ end
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'pundit_namespaces/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "pundit_namespaces"
8
+ spec.version = PunditNamespaces::VERSION
9
+ spec.authors = ["Paul Spieker"]
10
+ spec.email = ["p.spieker@duenos.de"]
11
+
12
+ spec.summary = %q{Add namespacing support to Pundit}
13
+ spec.homepage = "https://github.com/spieker/pundit_namespaces"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "pundit", "~> 1.0"
22
+ spec.add_development_dependency "bundler", "~> 1.10"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec"
25
+ spec.add_development_dependency "guard"
26
+ spec.add_development_dependency "guard-rspec"
27
+ spec.add_development_dependency "codeclimate-test-reporter"
28
+ end
metadata ADDED
@@ -0,0 +1,159 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pundit_namespaces
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Paul Spieker
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-11-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: pundit
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
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.10'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.10'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.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: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: guard
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: guard-rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: codeclimate-test-reporter
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description:
112
+ email:
113
+ - p.spieker@duenos.de
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".rspec"
120
+ - ".ruby-gemset"
121
+ - ".ruby-version"
122
+ - ".travis.yml"
123
+ - CODE_OF_CONDUCT.md
124
+ - Gemfile
125
+ - Guardfile
126
+ - LICENSE.txt
127
+ - README.md
128
+ - Rakefile
129
+ - bin/console
130
+ - bin/setup
131
+ - lib/pundit_namespaces.rb
132
+ - lib/pundit_namespaces/namespaced_policy_finder.rb
133
+ - lib/pundit_namespaces/version.rb
134
+ - pundit_namespaces.gemspec
135
+ homepage: https://github.com/spieker/pundit_namespaces
136
+ licenses:
137
+ - MIT
138
+ metadata: {}
139
+ post_install_message:
140
+ rdoc_options: []
141
+ require_paths:
142
+ - lib
143
+ required_ruby_version: !ruby/object:Gem::Requirement
144
+ requirements:
145
+ - - ">="
146
+ - !ruby/object:Gem::Version
147
+ version: '0'
148
+ required_rubygems_version: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ requirements: []
154
+ rubyforge_project:
155
+ rubygems_version: 2.4.8
156
+ signing_key:
157
+ specification_version: 4
158
+ summary: Add namespacing support to Pundit
159
+ test_files: []