grant-front 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 975726e6082113ecaa80005e74fd6763b2f8b2e8
4
- data.tar.gz: 673d310c584b1105ab3c84b7e9e2ca9ecb583d30
3
+ metadata.gz: 1ca03d453436703cf2347bb50b4533e377232377
4
+ data.tar.gz: b362f5abbe106f1b64dcfa4e1726d70a13e5f922
5
5
  SHA512:
6
- metadata.gz: 0aade26dfca87ff36b9f18595e965028994b0e71150a01e428b85b0284edeae412747355d2ccf54d0158ab56893ffca5dce44591c06a007ffbc24e969fa6a6f4
7
- data.tar.gz: 097f9ae8ed257a45c278de3563a501cf0d8abab896c6c408e29169c5dded4fa2f6702266f724c1056a052c599f8c2fa908424ac47b0b419a22f33b0dad668663
6
+ metadata.gz: 952db46b0b3581b8e060ed536e6730c882ba187a37b6a0f73b816067710ea1e14adfefb59a740f8590b2b59a510065b7140d24e1b3d7e683077eb28592bf53ea
7
+ data.tar.gz: 94f61b0e99747671044ac3feedef507f10639779401bd96b14e608c84f2d1fc5d930cf02c8e48e702c19124b542736fa2a797b6cd24c6d772e411b8324b8940c
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # GrantFront
2
2
 
3
- [![Gem Version](https://badge.fury.io/rb/grant-front.png)](https://rubygems.org/gems/grant-front) [![Build Status](https://travis-ci.org/ogom/grant-front.png?branch=master)](https://travis-ci.org/ogom/grant-front)
3
+ [![Gem Version](https://badge.fury.io/rb/grant-front.svg)](http://badge.fury.io/rb/grant-front) [![Build Status](https://travis-ci.org/ogom/grant-front.png?branch=master)](https://travis-ci.org/ogom/grant-front)
4
4
 
5
5
  Authorization Grant Front on Rails.
6
6
 
@@ -26,17 +26,30 @@ $ gem install grant-front
26
26
 
27
27
  ## Usage
28
28
 
29
+ ```
30
+ class ApplicationPolicy
31
+ include GrantFront
32
+ attr_reader :user, :record
33
+
34
+ def initialize(user, record)
35
+ @user = user
36
+ @record = record
37
+ end
38
+ end
39
+ ```
40
+
29
41
  ```
30
42
  class UserPolicy < ApplicationPolicy
31
43
  def create?
32
44
  grant :foo, :bar, :baz
33
45
  end
34
46
 
35
- def grant(*roles)
36
- roles.each do |role|
37
- return true if user.roles.include? role
38
- end
39
- return false, roles
47
+ def update?
48
+ grant :foo, :bar
49
+ end
50
+
51
+ def destroy?
52
+ grant :bar, :baz
40
53
  end
41
54
  end
42
55
  ```
@@ -48,7 +61,8 @@ $ rake grant_front:draw
48
61
  ||foo|bar|baz|
49
62
  |:-:|:-:|:-:|:-:|
50
63
  |create|o|o|o|
51
- |new|o|o|o|
64
+ |update|o|o||
65
+ |destroy||o|o|
52
66
 
53
67
 
54
68
  ## License
@@ -1,9 +1,35 @@
1
1
  require 'grant-front/version'
2
2
  require 'grant-front/grant'
3
3
  require 'grant-front/policy'
4
- require 'grant-front/rails' if defined? Rails::Railtie
5
4
 
6
5
  module GrantFront
7
- class << self
6
+ def self.included(klass)
7
+ klass.extend(ClassMethods)
8
+ end
9
+
10
+ module ClassMethods
11
+ def mock!
12
+ alias_method :keep_grant, :grant
13
+ alias_method :grant, :mock_grant
14
+ end
15
+
16
+ def unmock!
17
+ return unless method_defined? :mock_grant
18
+ alias_method :grant, :keep_grant
19
+ end
8
20
  end
21
+
22
+ private
23
+ def grant(*roles)
24
+ roles.each do |role|
25
+ return true if user.roles.include? role
26
+ end
27
+ return false
28
+ end
29
+
30
+ def mock_grant(*roles)
31
+ roles
32
+ end
9
33
  end
34
+
35
+ require 'grant-front/rails' if defined? Rails::Railtie
@@ -19,13 +19,14 @@ module GrantFront
19
19
  raw = {methods: {}, roles: []}
20
20
  reg = Regexp.new(/\?$/)
21
21
  user = Struct.new(:id, :roles).new(1, [])
22
+ ApplicationPolicy.mock!
22
23
 
23
24
  policy = klass.new(user, user)
24
25
  policy.methods.each do |name|
25
26
  if name =~ reg
26
27
  owner = policy.method(name).owner
27
28
  if owner == klass or owner == ApplicationPolicy
28
- allow, roles = policy.send(name)
29
+ roles = policy.send(name)
29
30
  roles ||= []
30
31
  raw[:methods][name.to_s.gsub(reg, '').to_sym] = roles
31
32
  raw[:roles] += roles
@@ -33,6 +34,7 @@ module GrantFront
33
34
  end
34
35
  end
35
36
  end
37
+ ApplicationPolicy.unmock!
36
38
 
37
39
  raw
38
40
  end
@@ -1,3 +1,3 @@
1
1
  module GrantFront
2
- VERSION = '0.0.1'.freeze
2
+ VERSION = '0.0.2'.freeze
3
3
  end
@@ -0,0 +1,21 @@
1
+ require 'spec_helper'
2
+
3
+ describe GrantFront::Policy do
4
+ describe ".all" do
5
+ it "returns a policy" do
6
+ expect(GrantFront::Policy.all.count).to eq(1)
7
+ end
8
+ end
9
+
10
+ describe ".find" do
11
+ let(:policy) { GrantFront::Policy.all.first }
12
+
13
+ it "returns some methods" do
14
+ expect(GrantFront::Policy.find(policy)[:methods].keys).to eq([:create, :update, :destroy])
15
+ end
16
+
17
+ it "returns some roles" do
18
+ expect(GrantFront::Policy.find(policy)[:roles]).to eq([:foo, :bar, :baz])
19
+ end
20
+ end
21
+ end
@@ -1,8 +1,8 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe GrantFront do
3
+ describe "GrantFront::VERSION" do
4
4
  describe "VERSION" do
5
- it "GrantFront::VERSION" do
5
+ it "returns #{GrantFront::VERSION} version" do
6
6
  expect(GrantFront::VERSION).to eq(GrantFront::VERSION)
7
7
  end
8
8
  end
@@ -1 +1,25 @@
1
1
  require 'grant-front'
2
+
3
+ class ApplicationPolicy
4
+ include GrantFront
5
+ attr_reader :user, :record
6
+
7
+ def initialize(user, record)
8
+ @user = user
9
+ @record = record
10
+ end
11
+ end
12
+
13
+ class UserPolicy < ApplicationPolicy
14
+ def create?
15
+ grant :foo, :bar, :baz
16
+ end
17
+
18
+ def update?
19
+ grant :foo, :bar
20
+ end
21
+
22
+ def destroy?
23
+ grant :bar, :baz
24
+ end
25
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: grant-front
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - ogom
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-02 00:00:00.000000000 Z
11
+ date: 2014-12-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -59,6 +59,7 @@ files:
59
59
  - lib/grant-front/version.rb
60
60
  - lib/tasks/grant-front.rake
61
61
  - spec/grant-front_spec.rb
62
+ - spec/lib/policy_spec.rb
62
63
  - spec/lib/version_spec.rb
63
64
  - spec/spec_helper.rb
64
65
  homepage: http://ogom.github.io/grant-front
@@ -87,5 +88,6 @@ specification_version: 4
87
88
  summary: Authorization Grant Front.
88
89
  test_files:
89
90
  - spec/grant-front_spec.rb
91
+ - spec/lib/policy_spec.rb
90
92
  - spec/lib/version_spec.rb
91
93
  - spec/spec_helper.rb