sevencan 0.1.6 → 0.1.7

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: 606360b04132f4de7de36627b65179277b25f4ab
4
- data.tar.gz: 08ba8054f132143faec82f2b4cabb5a0bb88e562
3
+ metadata.gz: 4de7ff55cfab6b3d85dbfd0709662ba247aed6f4
4
+ data.tar.gz: e284beb2abed20a28ad40b07552d388c68a51039
5
5
  SHA512:
6
- metadata.gz: e3249251be80676b09a6f47bcdc9da176b0f8537fb7374306221b831cabb1b0d3cf554a6ad73e448d2f9e9109268d89a7da8fadb499ad4be914408b9c309e4eb
7
- data.tar.gz: 79f8a63eed2372dc95e5c82366e611b1533af8b26f458eee5716461009d0b37701f3363da388a28e9d5f888e3fa373f294a56329273c1f2f15382c881a61c131
6
+ metadata.gz: 47da39039c9052d7eca7ff6fa8b7bb4fbb7f8d53f3a30abc4c58bb074bc28761cbe8ae278529ac17e86ac00021f18f6c1e2cfa802ac34d76dd7cf22a072b3ec2
7
+ data.tar.gz: 23234e5476b2d13877d4cf8b1da1922b276e4897e6947ea1934abbd3f10ade1c09411428cb9de0d305d1ecc1ebd572e374eff970659fa14f2b94cea6dd176ecd
data/README.md CHANGED
@@ -51,18 +51,22 @@ class MyTopicAbilities
51
51
  end
52
52
 
53
53
  # if [:admin, :editor].include?(current_user.role)
54
- abilities :role, [:admin, :editor] do
54
+ abilities check: :role, in: [:admin, :editor] do
55
55
  can_manager_topic
56
56
  end
57
57
 
58
- abilities :role, [:reviewer] do
58
+ # current_user.role eqlual :reviewer
59
+ abilities check: :role, equal: :reviewer do
59
60
  can :review_topic
60
61
  end
61
62
 
62
- abilities Proc.new { current_user.permissions.include?(:topic_manager) } do
63
+ abilities pass: Proc.new { current_user.permissions.include?(:topic_manager) } do
63
64
  can_manager_topic
64
65
  end
65
66
 
67
+ abilities pass: :editor_filter do
68
+ end
69
+
66
70
 
67
71
  def can_manager_topic
68
72
  can :edit_topic, :destroy_topic
@@ -71,6 +75,10 @@ class MyTopicAbilities
71
75
  def cannot_manager_topic
72
76
  cannot :edit_topic, :destroy_topic
73
77
  end
78
+
79
+ def editor_filter
80
+ current_user.permissions.include?(:topic_editor)
81
+ end
74
82
  end
75
83
 
76
84
  manager.define_rules(Topic, MyTopicAbilities)
@@ -300,7 +308,7 @@ end
300
308
  ```
301
309
 
302
310
 
303
- Manual check, cannot call `ability_check_callback`
311
+ Manual check, not call `ability_check_callback`
304
312
 
305
313
  ```
306
314
  class TopicController < ApplicationController
@@ -50,29 +50,38 @@ module Seven
50
50
  attr_reader :rule_procs
51
51
 
52
52
  # Params:
53
- # field: current_user method
54
- # scope: run rule proc if Array(scope).include?(current_user#{field})
55
- #
56
- # or
57
- #
58
- # proc
59
- def abilities(field = nil, scope = nil, &rule_proc)
60
- checker = case field
61
- when Proc
62
- field
63
- when Symbol, String
64
- raise Seven::ArgsError, 'Scope cannot be nil' if scope.nil?
65
- tmp_scope = scope.is_a?(Array) ? scope : [scope]
66
- Proc.new { tmp_scope.include?(current_user.public_send(field)) }
67
- when nil
68
- nil
53
+ # options:
54
+ # {check: :role, equal: 'admin'} current_user.role == 'admin'
55
+ # {check: :role, in: %w{admin editor}} %w{admin editor}.include?(current_user.role)
56
+ # {pass: :my_filter} call my_filter method
57
+ # {pass: Proc.new { ... }} proc.call
58
+ def abilities(options = nil, &rule_proc)
59
+ filter = build_seven_abilities_filter(options)
60
+ @rule_procs ||= []
61
+ @rule_procs << [filter, rule_proc]
62
+ end
63
+
64
+ def build_seven_abilities_filter(options)
65
+ return if options.nil?
66
+ opts = options.symbolize_keys
67
+
68
+ if val = opts[:pass]
69
+ if val.is_a?(Proc)
70
+ val
71
+ else
72
+ Proc.new { send val }
73
+ end
74
+ elsif attr = opts[:check]
75
+ if list = opts[:in]
76
+ Proc.new { list.include?(current_user.public_send(attr)) }
77
+ elsif val = opts[:equal]
78
+ Proc.new { current_user.public_send(attr) == val }
79
+ else
80
+ raise Seven::ArgsError, 'Invalid check definition'
81
+ end
69
82
  else
70
- raise Seven::ArgsError, "Invalid field '#{field}'" if scope.nil?
83
+ raise Seven::ArgsError, 'Invalid check definition'
71
84
  end
72
-
73
-
74
- @rule_procs ||= []
75
- @rule_procs << [checker, rule_proc]
76
85
  end
77
86
  end
78
87
  end
data/lib/seven/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Seven
2
- VERSION = "0.1.6"
2
+ VERSION = "0.1.7"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sevencan
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - jiangzhi.xie
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-02-24 00:00:00.000000000 Z
11
+ date: 2017-02-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler