activemodel-can_validator 0.0.1

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: 2fcfa2c7aaae1fd3314717694a5492d8387611eb
4
+ data.tar.gz: 2879eef1672d0e5e2b86b032eb4bcdfe89f7e41f
5
+ SHA512:
6
+ metadata.gz: 29aa6cdd18c654637163b6a1a2911e584bbefbe04d42e5b516cfe2f7ab462b1cc137c7e79bb96f2c5dce4f0cf600aafc67ce7ebf455bad47bd83c6c34fd35b67
7
+ data.tar.gz: 904c7f34df160a3204dc0a7b1206d826841c68ac98be90adbc349c2785e03f8e201e41f7ab95d9098415e7594925a1457d7ca7853b44f003bfea7c84953344f3
data/.coverrails.yml ADDED
@@ -0,0 +1 @@
1
+ service_name: travis-ci
data/.gitignore ADDED
@@ -0,0 +1,34 @@
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
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in activemodel-cancan_validator.gemspec
4
+ gemspec
5
+
6
+ group :test do
7
+ gem 'coveralls', require: false
8
+ end
data/README.md ADDED
@@ -0,0 +1,67 @@
1
+ # activemodel-can_validator
2
+
3
+ [![Build Status](https://travis-ci.org/yuku-t/activemodel-can_validator.svg?branch=master)](https://travis-ci.org/yuku-t/activemodel-can_validator) [![Code Climate](https://codeclimate.com/github/yuku-t/activemodel-can_validator/badges/gpa.svg)](https://codeclimate.com/github/yuku-t/activemodel-can_validator) [![Coverage Status](https://coveralls.io/repos/yuku-t/activemodel-can_validator/badge.svg)](https://coveralls.io/r/yuku-t/activemodel-can_validator) [![Dependency Status](https://gemnasium.com/yuku-t/activemodel-can_validator.svg)](https://gemnasium.com/yuku-t/activemodel-can_validator)
4
+
5
+ Validate user permissions via [CanCan](https://github.com/CanCanCommunity/cancancan) API.
6
+
7
+ ## Usage
8
+
9
+ Add to your Gemfile:
10
+
11
+ ```rb
12
+ gem 'activemodel-can_validator'
13
+ ```
14
+
15
+ Run:
16
+
17
+ ```
18
+ bundle install
19
+ ```
20
+
21
+ Then add the followng to your model which belongs to a user:
22
+
23
+ ```rb
24
+ validates :user, can: { create: true }, on: :create
25
+ ```
26
+
27
+ ### Sample
28
+
29
+ A user can retweet other users' tweets unless she is blocked by the user.
30
+
31
+ ```rb
32
+ class User < ActiveRecord::Base
33
+ has_many :retweets
34
+
35
+ def block?(user)
36
+ blocking_users.include?(user)
37
+ end
38
+
39
+ delegate :can?, :cannot?, to: :ability
40
+
41
+ def ability
42
+ @ability ||= Ability.new(self)
43
+ end
44
+
45
+ class Ability
46
+ include CanCan::Ability
47
+
48
+ def initialize(user)
49
+ user ||= User.new
50
+
51
+ can :create, Retweet do |retweet|
52
+ !retweet.tweet.user.block?(user)
53
+ end
54
+ end
55
+ end
56
+ end
57
+
58
+ class Tweet < ActiveRecord::Base
59
+ belongs_to :user
60
+ end
61
+
62
+ class Retweet < ActiveRecord::Base
63
+ belongs_to :user
64
+ belongs_to :tweet
65
+ validates :user, presence: true, can: { create: true }, on: :create
66
+ end
67
+ ```
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new('spec')
5
+ task default: :spec
@@ -0,0 +1,22 @@
1
+ file = File.open(File.expand_path('../lib/can_validator/version.rb', __FILE__))
2
+ version = file.read.scan(/\d+\.\d+\.\d+/).first
3
+ file.close
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'activemodel-can_validator'
7
+ spec.version = version
8
+ spec.authors = ['Yuku Takahashi']
9
+ spec.email = ['yuku@qiita.com']
10
+ spec.summary = 'A Rails validator based on CanCan.'
11
+ spec.homepage = 'https://github.com/yuku-t/activemodel-can_validator'
12
+ spec.license = 'MIT'
13
+
14
+ spec.files = `git ls-files -z`.split("\x0")
15
+ spec.require_paths = ['lib']
16
+
17
+ spec.add_dependency 'activemodel'
18
+
19
+ spec.add_development_dependency 'bundler', '~> 1.7'
20
+ spec.add_development_dependency 'rake', '~> 10.0'
21
+ spec.add_development_dependency 'rspec'
22
+ end
@@ -0,0 +1,4 @@
1
+ en:
2
+ errors:
3
+ messages:
4
+ permission_denied: doesn't have permission.
@@ -0,0 +1,4 @@
1
+ ja:
2
+ errors:
3
+ messages:
4
+ permission_denied: は権限がありません。
@@ -0,0 +1,3 @@
1
+ require 'can_validator'
2
+ require 'can_validator/version'
3
+ require 'can_validator/engine'
@@ -0,0 +1,18 @@
1
+ class CanValidator < ActiveModel::EachValidator
2
+ RESERVED_OPTIONS = [:allow_nil, :allow_blank, :message, :on, :if, :unless]
3
+
4
+ def validate_each(record, attribute, value)
5
+ if !value.respond_to?(:can?) || !has_permission?(record, value)
6
+ record.errors.add(attribute, options[:message] || :permission_denied)
7
+ end
8
+ end
9
+
10
+ private
11
+
12
+ def has_permission?(record, value)
13
+ actions = options.keys - RESERVED_OPTIONS
14
+ actions.all? do |action|
15
+ options[action] ? value.can?(action, record) : value.cannot?(action, record)
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,6 @@
1
+ require 'rails'
2
+
3
+ class CanValidator
4
+ class Engine < ::Rails::Engine
5
+ end
6
+ end
@@ -0,0 +1,3 @@
1
+ class CanValidator
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,55 @@
1
+ require 'spec_helper'
2
+
3
+ describe CanValidator do
4
+ describe 'validation' do
5
+ subject do
6
+ model_class.new(attr: value)
7
+ end
8
+
9
+ let(:model_class) do
10
+ Class.new(TestModel) do
11
+ validates :attr, can: { positive_action: true, negative_action: false }
12
+ end
13
+ end
14
+
15
+ context 'when the value object has CanCan interface' do
16
+ let(:value) do
17
+ Class.new do
18
+ def can?(*args)
19
+ false
20
+ end
21
+
22
+ def cannot?(*args)
23
+ !can?(*args)
24
+ end
25
+ end.new
26
+ end
27
+
28
+ context 'and it can do positive_action and cannot do negative_action against the resource' do
29
+ before do
30
+ allow(value).to receive(:can?).with(:positive_action, a_kind_of(model_class)).and_return(true)
31
+ allow(value).to receive(:can?).with(:negative_action, a_kind_of(model_class)).and_return(false)
32
+ end
33
+
34
+ it { should be_valid }
35
+ end
36
+
37
+ context "and it doesn't satisfy at least one required permission" do
38
+ before do
39
+ allow(value).to receive(:can?).with(:positive_action, a_kind_of(model_class)).and_return(true)
40
+ allow(value).to receive(:can?).with(:negative_action, a_kind_of(model_class)).and_return(true)
41
+ end
42
+
43
+ it { should be_invalid }
44
+ end
45
+ end
46
+
47
+ context 'when the value object does not have CanCan interface' do
48
+ let(:value) do
49
+ Object.new
50
+ end
51
+
52
+ it { should be_invalid }
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,26 @@
1
+ require 'coveralls'
2
+ Coveralls.wear!
3
+
4
+ require 'active_model'
5
+ require 'can_validator'
6
+
7
+ RSpec.configure do |config|
8
+ config.color = true
9
+ config.run_all_when_everything_filtered = true
10
+ end
11
+
12
+ class TestModel
13
+ include ActiveModel::Validations
14
+
15
+ def self.name
16
+ 'TestModel'
17
+ end
18
+
19
+ def initialize(attributes = {})
20
+ @attributes = attributes
21
+ end
22
+
23
+ def read_attribute_for_validation(key)
24
+ @attributes[key]
25
+ end
26
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activemodel-can_validator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Yuku Takahashi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-03-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activemodel
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '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.7'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.7'
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
+ description:
70
+ email:
71
+ - yuku@qiita.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".coverrails.yml"
77
+ - ".gitignore"
78
+ - ".travis.yml"
79
+ - Gemfile
80
+ - README.md
81
+ - Rakefile
82
+ - activemodel-can_validator.gemspec
83
+ - config/locales/en.yml
84
+ - config/locales/ja.yml
85
+ - lib/activemodel-cancan_validator.rb
86
+ - lib/can_validator.rb
87
+ - lib/can_validator/engine.rb
88
+ - lib/can_validator/version.rb
89
+ - spec/can_validator_spec.rb
90
+ - spec/spec_helper.rb
91
+ homepage: https://github.com/yuku-t/activemodel-can_validator
92
+ licenses:
93
+ - MIT
94
+ metadata: {}
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 2.2.2
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: A Rails validator based on CanCan.
115
+ test_files: []