strong_actions 0.0.3 → 0.0.4

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: 33b2e12b49fba4ce6ffdc958dcffb10f3488e7eb
4
- data.tar.gz: 5ae09785037dff9dae2907cb6f35ac0af28ff100
3
+ metadata.gz: 8f33bd237fe0db5884816fedd3d3d8be5c61bc65
4
+ data.tar.gz: d61a5a34fde02ded12c2943e5d22ed0c7382b4f8
5
5
  SHA512:
6
- metadata.gz: d7abda3d916eb50633e65d1a7a66f3bc86332f53e766ae125ec17bdcb2d621e1e2c7456c814b13b54a4024428498e63c1792dd5c60b1559e361b732453631d28
7
- data.tar.gz: 05396c0b7f028c12168adfbb6748294ae129d82db55388b4ec518f86c7b8711babe249922135b25afaad347c2e2e621e9cb9700e14397aae0676959d30dddb5d
6
+ metadata.gz: ea2429d1c839a9b318f7bca17dea13be8cc60448856165c2d996e3baf2913e31aa17d5f4608e0f724bb3b43ecad811d33f3d2ae8f230da2a700ac324190eab2c
7
+ data.tar.gz: 7cf82ccdebb20b21bb570372144f46edd20aa30c71c828f8396ab7c205e7aec92089c56c67ebf576cb20bab605d9d5059834c2df361d8bc72258220fa6a3d431
data/Gemfile CHANGED
@@ -2,3 +2,6 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in strong_actions.gemspec
4
4
  gemspec
5
+
6
+ gem "actionpack", "~> 3.2.0"
7
+ gem "railties", "~> 3.2.0"
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- strong_actions (0.0.3)
4
+ strong_actions (0.0.4)
5
5
  actionpack (>= 3.2.0, < 5.0.0)
6
6
  activesupport (>= 3.2.0, < 5.0.0)
7
7
  railties (>= 3.2.0, < 5.0.0)
@@ -9,44 +9,62 @@ PATH
9
9
  GEM
10
10
  remote: https://rubygems.org/
11
11
  specs:
12
- actionpack (4.1.5)
13
- actionview (= 4.1.5)
14
- activesupport (= 4.1.5)
15
- rack (~> 1.5.2)
16
- rack-test (~> 0.6.2)
17
- actionview (4.1.5)
18
- activesupport (= 4.1.5)
19
- builder (~> 3.1)
12
+ actionpack (3.2.19)
13
+ activemodel (= 3.2.19)
14
+ activesupport (= 3.2.19)
15
+ builder (~> 3.0.0)
20
16
  erubis (~> 2.7.0)
21
- activesupport (4.1.5)
22
- i18n (~> 0.6, >= 0.6.9)
23
- json (~> 1.7, >= 1.7.7)
24
- minitest (~> 5.1)
25
- thread_safe (~> 0.1)
26
- tzinfo (~> 1.1)
27
- builder (3.2.2)
17
+ journey (~> 1.0.4)
18
+ rack (~> 1.4.5)
19
+ rack-cache (~> 1.2)
20
+ rack-test (~> 0.6.1)
21
+ sprockets (~> 2.2.1)
22
+ activemodel (3.2.19)
23
+ activesupport (= 3.2.19)
24
+ builder (~> 3.0.0)
25
+ activesupport (3.2.19)
26
+ i18n (~> 0.6, >= 0.6.4)
27
+ multi_json (~> 1.0)
28
+ builder (3.0.4)
28
29
  erubis (2.7.0)
30
+ hike (1.2.3)
29
31
  i18n (0.6.11)
32
+ journey (1.0.4)
30
33
  json (1.8.1)
31
34
  minitest (5.4.1)
32
- rack (1.5.2)
35
+ multi_json (1.10.1)
36
+ rack (1.4.5)
37
+ rack-cache (1.2)
38
+ rack (>= 0.4)
39
+ rack-ssl (1.3.4)
40
+ rack
33
41
  rack-test (0.6.2)
34
42
  rack (>= 1.0)
35
- railties (4.1.5)
36
- actionpack (= 4.1.5)
37
- activesupport (= 4.1.5)
43
+ railties (3.2.19)
44
+ actionpack (= 3.2.19)
45
+ activesupport (= 3.2.19)
46
+ rack-ssl (~> 1.3.2)
38
47
  rake (>= 0.8.7)
39
- thor (>= 0.18.1, < 2.0)
48
+ rdoc (~> 3.4)
49
+ thor (>= 0.14.6, < 2.0)
40
50
  rake (10.3.2)
51
+ rdoc (3.12.2)
52
+ json (~> 1.4)
53
+ sprockets (2.2.2)
54
+ hike (~> 1.2)
55
+ multi_json (~> 1.0)
56
+ rack (~> 1.0)
57
+ tilt (~> 1.1, != 1.3.0)
41
58
  thor (0.19.1)
42
- thread_safe (0.3.4)
43
- tzinfo (1.2.2)
44
- thread_safe (~> 0.1)
59
+ tilt (1.4.1)
45
60
 
46
61
  PLATFORMS
47
62
  ruby
48
63
 
49
64
  DEPENDENCIES
65
+ actionpack (~> 3.2.0)
50
66
  bundler (~> 1.6)
67
+ minitest
68
+ railties (~> 3.2.0)
51
69
  rake
52
70
  strong_actions!
data/HISTORY.md CHANGED
@@ -1,10 +1,14 @@
1
1
  # History
2
2
 
3
- ## 0.0.2
3
+ ## 0.0.4
4
+ * test
5
+
6
+ ## 0.0.3
7
+ * refactoring
4
8
 
9
+ ## 0.0.2
5
10
  * support explicit true value
6
11
  * support rails-3.2
7
12
 
8
13
  ## 0.0.1
9
-
10
14
  * First release
data/Rakefile CHANGED
@@ -1,2 +1,12 @@
1
1
  require "bundler/gem_tasks"
2
2
 
3
+ require 'rake/testtask'
4
+
5
+ Rake::TestTask.new(:test) do |t|
6
+ t.libs << 'lib'
7
+ t.libs << 'test'
8
+ t.pattern = 'test/**/*_test.rb'
9
+ t.verbose = false
10
+ end
11
+
12
+ task :default => :test
@@ -1,3 +1,4 @@
1
+ require 'action_controller'
1
2
  require "strong_actions/version"
2
3
  require "strong_actions/config"
3
4
  require "strong_actions/forbidden_action"
@@ -5,8 +5,7 @@ module StrongActions
5
5
  include Singleton
6
6
 
7
7
  def initialize
8
- @config_files = ['config/acl.yml']
9
- load_config_files
8
+ @config_files ||= ['config/acl.yml']
10
9
  end
11
10
 
12
11
  def config_files
@@ -14,31 +13,31 @@ module StrongActions
14
13
  end
15
14
 
16
15
  def config_files=(files)
17
- config_files = files
18
- load_config_files
16
+ @config_files = ([] << files).flatten
19
17
  end
20
18
 
21
19
  def roles
22
- load_config_files if Rails.env.development?
23
- @acl.keys
20
+ definitions.keys
24
21
  end
25
22
 
26
23
  def role_definition(role)
27
- load_config_files if Rails.env.development?
28
- @acl[role]
24
+ definitions[role]
29
25
  end
30
26
 
31
27
  private
32
28
 
33
- def load_config_files
34
- @acl = {}
35
- config_files.each do |config_file|
36
- yml = YAML.load_file(config_file)
37
- yml.each do |role, values|
38
- raise "role #{role} is already defined." if @acl.has_key?(role)
39
- @acl[role] = values
29
+ def definitions
30
+ if @acl.nil? or Rails.env.development?
31
+ @acl = {}
32
+ config_files.each do |config_file|
33
+ yml = YAML.load_file(config_file)
34
+ yml.each do |role, values|
35
+ raise "role #{role} is already defined." if @acl.has_key?(role)
36
+ @acl[role] = values
37
+ end
40
38
  end
41
39
  end
40
+ @acl
42
41
  end
43
42
 
44
43
  end
@@ -1,3 +1,5 @@
1
+ require 'strong_actions/decision'
2
+
1
3
  module StrongActions
2
4
  module ControllerExtensions
3
5
  extend ActiveSupport::Concern
@@ -24,8 +26,6 @@ module StrongActions
24
26
  end
25
27
 
26
28
  def available?(controller_name, action_name = nil, params = {})
27
- action_name ||= 'index'
28
-
29
29
  StrongActions.config.roles.each do |role|
30
30
  return false unless judge(role, controller_name, action_name, params)
31
31
  end
@@ -33,34 +33,9 @@ module StrongActions
33
33
  true
34
34
  end
35
35
 
36
- def judge(role, controller_name, action_name, params)
37
- role_definition = StrongActions.config.role_definition(role)
38
- return true unless role_definition
39
-
40
- begin
41
- role_object = eval(role)
42
- rescue NameError
43
- raise "role #{role} is not defined in controller"
44
- end
45
-
46
- controller_value = role_definition[controller_name]
47
- return true if controller_value.nil?
48
-
49
- if controller_value.is_a?(Hash)
50
- action_value = controller_value[action_name]
51
- else
52
- action_value = controller_value
53
- end
54
- return true if action_value.nil?
55
-
56
- action_value = [action_value] unless action_value.is_a?(Array)
57
- action_value.each do |definition|
58
- next if definition === true
59
- return false unless definition
60
- return false unless role_object.instance_eval(definition)
61
- end
62
-
63
- true
36
+ def judge(role, controller_name, action_name = nil, params = {})
37
+ @decision ||= StrongActions::Decision.new(self)
38
+ @decision.call(role, controller_name, action_name, params)
64
39
  end
65
40
 
66
41
  end
@@ -0,0 +1,40 @@
1
+ module StrongActions
2
+ class Decision
3
+
4
+ def initialize(target)
5
+ @target = target
6
+ end
7
+
8
+ def call(role, controller_name, action_name = nil, params = {})
9
+ role_definition = StrongActions.config.role_definition(role)
10
+ return true unless role_definition
11
+
12
+ begin
13
+ role_object = @target.instance_eval(role)
14
+ rescue NameError
15
+ raise "role #{role} is not defined in controller"
16
+ end
17
+
18
+ controller_value = role_definition[controller_name]
19
+ return true if controller_value.nil?
20
+
21
+ if controller_value.is_a?(Hash)
22
+ action_name ||= 'index'
23
+ action_value = controller_value[action_name]
24
+ else
25
+ action_value = controller_value
26
+ end
27
+ return true if action_value.nil?
28
+
29
+ action_value = [action_value] unless action_value.is_a?(Array)
30
+ action_value.each do |definition|
31
+ next if definition === true
32
+ return false unless definition
33
+ return false unless role_object.instance_eval(definition)
34
+ end
35
+
36
+ true
37
+ end
38
+
39
+ end
40
+ end
@@ -1,3 +1,3 @@
1
1
  module StrongActions
2
- VERSION = '0.0.3'
2
+ VERSION = '0.0.4'
3
3
  end
@@ -22,5 +22,6 @@ Gem::Specification.new do |spec|
22
22
  spec.add_dependency "railties", '>= 3.2.0', '< 5.0.0'
23
23
 
24
24
  spec.add_development_dependency "bundler", "~> 1.6"
25
+ spec.add_development_dependency "minitest"
25
26
  spec.add_development_dependency "rake"
26
27
  end
@@ -0,0 +1,10 @@
1
+ current_user:
2
+ welcome:
3
+ index: true
4
+ destroy: false
5
+ books: true
6
+ end_of_services: false
7
+ stores:
8
+ new: admin?
9
+ sessions:
10
+ index: false
@@ -0,0 +1,56 @@
1
+ require 'test_helper'
2
+
3
+ class DecisionTest < Minitest::Test
4
+
5
+ def setup
6
+ StrongActions.config.config_files = File.join(File.dirname(__FILE__), 'config', 'acl.yml')
7
+ end
8
+
9
+ def decision
10
+ @decision ||= StrongActions::Decision.new(self)
11
+ end
12
+
13
+ def current_user
14
+ @user ||= User.new
15
+ end
16
+
17
+ def test_controller_true
18
+ assert decision.call('current_user', 'books')
19
+ end
20
+
21
+ def test_controller_false
22
+ assert ! decision.call('current_user', 'end_of_services')
23
+ end
24
+
25
+ def test_action_true
26
+ assert decision.call('current_user', 'welcome', 'index')
27
+ end
28
+
29
+ def test_action_false
30
+ assert ! decision.call('current_user', 'welcome', 'destroy')
31
+ end
32
+
33
+ def test_action_default_to_index
34
+ assert decision.call('current_user', 'welcome')
35
+ assert ! decision.call('current_user', 'sessions')
36
+ end
37
+
38
+ def test_admin_for_new
39
+ current_user.admin = false
40
+ assert decision.call('current_user', 'stores')
41
+ assert ! decision.call('current_user', 'stores', 'new')
42
+
43
+ current_user.admin = true
44
+ assert decision.call('current_user', 'stores')
45
+ assert decision.call('current_user', 'stores', 'new')
46
+ end
47
+
48
+ end
49
+
50
+ class User
51
+ attr_accessor :admin
52
+
53
+ def admin?
54
+ admin
55
+ end
56
+ end
@@ -0,0 +1,6 @@
1
+ # Configure Rails Environment
2
+ ENV["RAILS_ENV"] = "test"
3
+
4
+ require 'minitest/autorun'
5
+ require 'rails'
6
+ require 'strong_actions'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: strong_actions
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - ichy
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-09-06 00:00:00.000000000 Z
11
+ date: 2014-09-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -84,6 +84,20 @@ dependencies:
84
84
  - - ~>
85
85
  - !ruby/object:Gem::Version
86
86
  version: '1.6'
87
+ - !ruby/object:Gem::Dependency
88
+ name: minitest
89
+ requirement: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ type: :development
95
+ prerelease: false
96
+ version_requirements: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - '>='
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
87
101
  - !ruby/object:Gem::Dependency
88
102
  name: rake
89
103
  requirement: !ruby/object:Gem::Requirement
@@ -115,10 +129,14 @@ files:
115
129
  - lib/strong_actions.rb
116
130
  - lib/strong_actions/config.rb
117
131
  - lib/strong_actions/controller_extensions.rb
132
+ - lib/strong_actions/decision.rb
118
133
  - lib/strong_actions/forbidden_action.rb
119
134
  - lib/strong_actions/railtie.rb
120
135
  - lib/strong_actions/version.rb
121
136
  - strong_actions.gemspec
137
+ - test/config/acl.yml
138
+ - test/decision_test.rb
139
+ - test/test_helper.rb
122
140
  homepage: https://github.com/hybitz/strong_actions
123
141
  licenses:
124
142
  - MIT
@@ -143,4 +161,7 @@ rubygems_version: 2.2.2
143
161
  signing_key:
144
162
  specification_version: 4
145
163
  summary: access control for rails controller/action
146
- test_files: []
164
+ test_files:
165
+ - test/config/acl.yml
166
+ - test/decision_test.rb
167
+ - test/test_helper.rb