dude_policy 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
+ SHA256:
3
+ metadata.gz: c1b4e1e0aa981141b42efcca848389438987fb99a890d4dfcb2375b9567cf2de
4
+ data.tar.gz: e65b4dfdce1fbfb939b5e73caaae218c72c1b6e895daecdbd9bb5351ce8fe4ce
5
+ SHA512:
6
+ metadata.gz: 4a1d56704ffafee188c45bf81937b2663cf5d638ff4441bd11bd8aa780f1ffbc1a0f451f611e728fda2aaf7d40963c9505309e5371e8717a27881a48d8907116
7
+ data.tar.gz: 281ae48292156284ee965d9554969b4476923d53921f5fcdcd91214756386676bef6c5aceeff392849b8f34a867e634a1b7e01cfca863238fba257c54b5d4145
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.6.3
6
+ before_install: gem install bundler -v 2.1.1
data/CHANGELOG.md ADDED
@@ -0,0 +1 @@
1
+ No changes sofar
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at equivalent@eq8.eu. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [https://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: https://contributor-covenant.org
74
+ [version]: https://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in dude_policy.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "rspec", "~> 3.0"
data/Gemfile.lock ADDED
@@ -0,0 +1,49 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ dude_policy (0.1.0)
5
+ activesupport (> 4.2)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activesupport (6.0.2.2)
11
+ concurrent-ruby (~> 1.0, >= 1.0.2)
12
+ i18n (>= 0.7, < 2)
13
+ minitest (~> 5.1)
14
+ tzinfo (~> 1.1)
15
+ zeitwerk (~> 2.2)
16
+ concurrent-ruby (1.1.6)
17
+ diff-lcs (1.3)
18
+ i18n (1.8.2)
19
+ concurrent-ruby (~> 1.0)
20
+ minitest (5.14.0)
21
+ rake (12.3.3)
22
+ rspec (3.9.0)
23
+ rspec-core (~> 3.9.0)
24
+ rspec-expectations (~> 3.9.0)
25
+ rspec-mocks (~> 3.9.0)
26
+ rspec-core (3.9.1)
27
+ rspec-support (~> 3.9.1)
28
+ rspec-expectations (3.9.1)
29
+ diff-lcs (>= 1.2.0, < 2.0)
30
+ rspec-support (~> 3.9.0)
31
+ rspec-mocks (3.9.1)
32
+ diff-lcs (>= 1.2.0, < 2.0)
33
+ rspec-support (~> 3.9.0)
34
+ rspec-support (3.9.2)
35
+ thread_safe (0.3.6)
36
+ tzinfo (1.2.7)
37
+ thread_safe (~> 0.1)
38
+ zeitwerk (2.3.0)
39
+
40
+ PLATFORMS
41
+ ruby
42
+
43
+ DEPENDENCIES
44
+ dude_policy!
45
+ rake (~> 12.0)
46
+ rspec (~> 3.0)
47
+
48
+ BUNDLED WITH
49
+ 2.1.1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Tomas Valent
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,111 @@
1
+ # Dude Policy
2
+
3
+ This gem provides a way how to do Ruby on Rails [Policy Objects](https://blog.eq8.eu/article/policy-object.html)
4
+ from point of view of current_user/current_account (the **dude**)
5
+
6
+ ![](https://triblive.com/wp-content/uploads/2019/01/673808_web1_gtr-liv-goldenglobes-121718.jpg)
7
+
8
+ Here are some examples what we mean:
9
+
10
+
11
+ ```ruby
12
+ # irb
13
+ article = Article.find(123)
14
+ review = Review.find(123)
15
+ current_user = User.find(432) # e.g. Devise on any authentication solution
16
+
17
+ current_user.dude.able_to_edit_article?(article)
18
+ # => true
19
+
20
+ current_user.dude.able_to_add_article_review?(article)
21
+ # => true
22
+
23
+ current_user.dude.able_to_delete_review?(review)
24
+ # => false
25
+ ```
26
+
27
+ [RSpec](https://rspec.info/) examples:
28
+
29
+ ```ruby
30
+ # spec/any_file_spec.rb
31
+ RSpec.describe 'short demo' do
32
+ let(:author_user) { User.create }
33
+ let(:article) { Article.create(author: author_user) }
34
+ let(:different_user) { User.create }
35
+
36
+ # you write tests like this:
37
+ it { expect(author_user.able_to_edit_article?(article)).to be_truthy }
38
+
39
+ # or you can take advantage of native `be_` RSpec matcher that converts any questionmark ending method to matcher
40
+ it { expect(author_user).to be_able_to_edit_article(article) }
41
+ it { expect(different_user).not_to be_able_to_edit_article(article) }
42
+ it { expect(author_user).not_to be_able_to_add_article_review(article) }
43
+ it { expect(different_user).to be_able_to_add_article_review(article) }
44
+ it { expect(author_user).not_to be_able_to_delete_review(article) }
45
+ it { expect(different_user).to be_able_to_add_article_review(article) }
46
+ end
47
+ ```
48
+
49
+ Policy objects:
50
+
51
+ ```ruby
52
+ # app/policy/article_policy.rb
53
+ class ArticlePolicy < DudePolicy::BasePolicy
54
+ def able_to_update_article?(dude:)
55
+ return true if article.author == dude
56
+ false
57
+ end
58
+
59
+ def able_to_add_article_review?(dude:)
60
+ return true if article.author != dude
61
+ false
62
+ end
63
+
64
+ private
65
+
66
+ def article
67
+ resource # delegation defined in DudePolicy::BasePolicy
68
+ end
69
+ end
70
+ ```
71
+
72
+ Full example in example app
73
+
74
+ ## Installation
75
+
76
+ Add this line to your application's Gemfile:
77
+
78
+ ```ruby
79
+ gem 'dude_policy'
80
+ ```
81
+
82
+ And then execute:
83
+
84
+ $ bundle install
85
+
86
+ Or install it yourself as:
87
+
88
+ $ gem install dude_policy
89
+
90
+ ## Usage
91
+
92
+ ### Gem is responsible for Authorization
93
+
94
+ Gem will provide a way how to do `Authorization` (whan logged in user can/cannot do)
95
+
96
+ For `Authentication` (is User logged in ?) you will need a different solution / gem (e.g. [Devise](https://github.com/heartcombo/devise), custom login solution, ...)
97
+
98
+
99
+
100
+ ## Contributing
101
+
102
+ Bug reports and pull requests are welcome on GitHub at https://github.com/equivalent/dude_policy This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/dude_policy/blob/master/CODE_OF_CONDUCT.md).
103
+
104
+
105
+ ## License
106
+
107
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
108
+
109
+ ## Code of Conduct
110
+
111
+ Everyone interacting in the DudePolicy project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/equivalent/dude_policy/blob/master/CODE_OF_CONDUCT.md).
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 "dude_policy"
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(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,31 @@
1
+ require_relative 'lib/dude_policy/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "dude_policy"
5
+ spec.version = DudePolicy::VERSION
6
+ spec.authors = ["Tomas Valent"]
7
+ spec.email = ["equivalent@eq8.eu"]
8
+
9
+ spec.summary = %q{Policy objects for Ruby on Rails from perspectvie of current account}
10
+ spec.description = %q{current user (current account) oriented Plain Ruby Object Policy for Ruby on Rails}
11
+ spec.homepage = "https://github.com/equivalent/dude_policy"
12
+ spec.license = "MIT"
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
14
+
15
+ spec.metadata["allowed_push_host"] = "https://rubygems.org/"
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["source_code_uri"] = spec.homepage
19
+ spec.metadata["changelog_uri"] = "https://github.com/equivalent/dude_policy/blob/master/CHANGELOG.md"
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
24
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ end
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_dependency "activesupport", '> 4.2' # ensures compatibility for ruby 2.0.0+ to head
31
+ end
@@ -0,0 +1,13 @@
1
+ module DudePolicy
2
+ class BasePolicy
3
+ attr_reader :resource
4
+
5
+ def initialize(resource)
6
+ @resource = resource
7
+ end
8
+
9
+ def inspect
10
+ "<#{self.class.name}##{object_id} resource:#{resource.class.name}>"
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,25 @@
1
+ module DudePolicy
2
+ class Dude
3
+ attr_reader :dude
4
+
5
+ def initialize(current_dude)
6
+ @dude = current_dude
7
+ end
8
+
9
+ def inspect
10
+ id = "##{dude.id}" if dude.respond_to?(:id)
11
+ "<#DudePolicy::Dude##{object_id} #{dude.class.name}#{id}>"
12
+ end
13
+
14
+ def method_missing(name, resource)
15
+ resource.policy.send(name, dude: dude)
16
+ end
17
+
18
+ # RSpec mocks are checking if object responds to calls
19
+ # Due to fact that we use method_missing is delegating responsibility
20
+ # Dude class responds to all calls
21
+ def respond_to?(*)
22
+ true
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,11 @@
1
+ module DudePolicy
2
+ module HasPolicy
3
+ def policy
4
+ @policy ||= policy_name.constantize.new(self)
5
+ end
6
+
7
+ def policy_name
8
+ "#{self.class.name}Policy"
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,7 @@
1
+ module DudePolicy
2
+ module IsADude
3
+ def dude
4
+ @dude ||= DudePolicy::Dude.new(self)
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,13 @@
1
+ module DudePolicy
2
+ class NilDudePolicy
3
+ include Singleton
4
+
5
+ def method_missing(*)
6
+ false
7
+ end
8
+
9
+ def inspect
10
+ "<#DudePolicy##{object_id} on nil>"
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,7 @@
1
+ module DudePolicy
2
+ module NilExtesion
3
+ def dude
4
+ DudePolicy::NilDudePolicy.instance
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ module DudePolicy
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,24 @@
1
+ require 'active_support/inflector'
2
+
3
+ require 'singleton' # standard Ruby lib
4
+ require "dude_policy/version"
5
+
6
+ # nil stuff
7
+ require "dude_policy/nil_extension"
8
+ require "dude_policy/nil_dude_policy"
9
+
10
+ # current account stuff
11
+ require "dude_policy/dude"
12
+ require "dude_policy/base_policy"
13
+
14
+ # extend app
15
+ require "dude_policy/is_a_dude"
16
+ require "dude_policy/has_policy"
17
+
18
+
19
+ # core of `dude_policy` is extending `nil` to include `nil.dude` method
20
+ NilClass.send(:include, DudePolicy::NilExtesion)
21
+
22
+ module DudePolicy
23
+ NotAuthorized = Class.new(StandardError)
24
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dude_policy
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Tomas Valent
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-04-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.2'
27
+ description: current user (current account) oriented Plain Ruby Object Policy for
28
+ Ruby on Rails
29
+ email:
30
+ - equivalent@eq8.eu
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - ".gitignore"
36
+ - ".rspec"
37
+ - ".travis.yml"
38
+ - CHANGELOG.md
39
+ - CODE_OF_CONDUCT.md
40
+ - Gemfile
41
+ - Gemfile.lock
42
+ - LICENSE.txt
43
+ - README.md
44
+ - Rakefile
45
+ - bin/console
46
+ - bin/setup
47
+ - dude_policy.gemspec
48
+ - lib/dude_policy.rb
49
+ - lib/dude_policy/base_policy.rb
50
+ - lib/dude_policy/dude.rb
51
+ - lib/dude_policy/has_policy.rb
52
+ - lib/dude_policy/is_a_dude.rb
53
+ - lib/dude_policy/nil_dude_policy.rb
54
+ - lib/dude_policy/nil_extension.rb
55
+ - lib/dude_policy/version.rb
56
+ homepage: https://github.com/equivalent/dude_policy
57
+ licenses:
58
+ - MIT
59
+ metadata:
60
+ allowed_push_host: https://rubygems.org/
61
+ homepage_uri: https://github.com/equivalent/dude_policy
62
+ source_code_uri: https://github.com/equivalent/dude_policy
63
+ changelog_uri: https://github.com/equivalent/dude_policy/blob/master/CHANGELOG.md
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: 2.3.0
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ requirements: []
79
+ rubygems_version: 3.0.3
80
+ signing_key:
81
+ specification_version: 4
82
+ summary: Policy objects for Ruby on Rails from perspectvie of current account
83
+ test_files: []