subroutine 0.8.0 → 0.8.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7a1e060b5a23f95d926f8df63e2eb9fc12745a93
4
- data.tar.gz: 1205b94faff4d024aa9670cbd0cee003ea3bff5f
3
+ metadata.gz: 2703769996395de8478c8c16b7eb8f2826c903b3
4
+ data.tar.gz: 42bf18f9555f5977ffcb93508468d399fe62d993
5
5
  SHA512:
6
- metadata.gz: c3313881107db6835f530a41b17f0702f407a2c83413cd7705c00ef4b02017b54566037ad464231651e04948a8416ad4fc02c6939e36d2ac814383fb542829ba
7
- data.tar.gz: 75a5d2fe57b634ff5d4e84e105621c769679f065bc1f72c49b835b988972c39fe2d041fbda7f8c8ff99e3c51d79df51ab01ce63299a3b46d88e2a39096d73f2f
6
+ metadata.gz: d7be70968a4c5a9bbc85feabdcef6a1ded8fd04b797acd62266bdab558a51d64cafb93229d220222e5515978c187f328c24f337c0572c135b5b6f6f20e7e9c16
7
+ data.tar.gz: 6c04f8721942a5ff271d06065a4b9ebce38f830b5727d0c3b31d7734da379ba613ecab9ee6a319794442fa641ed5c5d7383cb7a07ba5b6814c5c54e5a26f06be
@@ -8,6 +8,9 @@ module Subroutine
8
8
  alias_method :field_without_associations, :field
9
9
  alias_method :field, :field_with_associations
10
10
  end
11
+
12
+ base.send(:alias_method, :setup_fields_without_association, :setup_fields)
13
+ base.send(:alias_method, :setup_fields, :setup_fields_with_association)
11
14
  end
12
15
 
13
16
  module ClassMethods
@@ -99,8 +102,8 @@ module Subroutine
99
102
  end
100
103
  end
101
104
 
102
- def initialize(*args)
103
- super(*args)
105
+ def setup_fields_with_association(*args)
106
+ setup_fields_without_association(*args)
104
107
 
105
108
  _fields.each_pair do |field, config|
106
109
  next unless config[:association]
@@ -24,12 +24,21 @@ module Subroutine
24
24
  base.instance_eval do
25
25
  extend ::Subroutine::Auth::ClassMethods
26
26
 
27
- class_attribute :authorization_declared
27
+ class_attribute :authorization_declared, instance_writer: false
28
28
  self.authorization_declared = false
29
+
30
+ class_attribute :user_class_name, instance_writer: false
31
+ self.user_class_name = "User"
29
32
  end
30
33
  end
31
34
 
32
35
  module ClassMethods
36
+
37
+ def supported_user_class_names
38
+ [user_class_name, "Integer", "NilClass"].compact
39
+ end
40
+
41
+
33
42
  def authorize(validation_name)
34
43
  validate validation_name, unless: :skip_auth_checks?
35
44
  end
@@ -95,6 +104,10 @@ module Subroutine
95
104
  super(args.extract_options!)
96
105
  @skip_auth_checks = false
97
106
  @current_user = args.shift
107
+
108
+ unless self.class.supported_user_class_names.include?(@current_user.class.name)
109
+ raise ArgumentError, "current_user must be one of the following types {#{self.class.supported_user_class_names.join(",")}} but was #{@current_user.class.name}"
110
+ end
98
111
  end
99
112
 
100
113
  def skip_auth_checks!
@@ -107,7 +120,7 @@ module Subroutine
107
120
  end
108
121
 
109
122
  def current_user
110
- @current_user = ::User.find(@current_user) if ::Integer === @current_user
123
+ @current_user = self.user_class_name.constantize.find(@current_user) if ::Integer === @current_user
111
124
  @current_user
112
125
  end
113
126
 
@@ -3,7 +3,7 @@
3
3
  module Subroutine
4
4
  MAJOR = 0
5
5
  MINOR = 8
6
- PATCH = 0
6
+ PATCH = 1
7
7
  PRE = nil
8
8
 
9
9
  VERSION = [MAJOR, MINOR, PATCH, PRE].compact.join('.')
@@ -4,6 +4,7 @@ require 'test_helper'
4
4
 
5
5
  module Subroutine
6
6
  class AuthTest < TestCase
7
+
7
8
  def user
8
9
  @user ||= ::User.new(email_address: 'doug@example.com')
9
10
  end
@@ -50,6 +51,25 @@ module Subroutine
50
51
  end
51
52
  end
52
53
 
54
+ def test_the_current_user_can_be_defined_by_an_id
55
+ user = CustomAuthorizeOp.new(1).current_user
56
+ assert_equal 1, user.id
57
+ assert_equal true, user.is_a?(::User)
58
+ assert_equal false, user.is_a?(::AdminUser)
59
+ end
60
+
61
+ def test_the_user_class_can_be_overridden
62
+ user = DifferentUserClassOp.new(1).current_user
63
+ assert_equal 1, user.id
64
+ assert_equal true, user.is_a?(::AdminUser)
65
+ end
66
+
67
+ def test_another_class_cant_be_used_as_the_user
68
+ assert_raises "current_user must be one of the following types {AdminUser,Integer,NilClass} but was String" do
69
+ DifferentUserClassOp.new("doug")
70
+ end
71
+ end
72
+
53
73
  def test_it_does_not_run_authorizations_if_explicitly_bypassed
54
74
  op = CustomAuthorizeOp.new User.new(email_address: 'foo@bar.com')
55
75
  op.skip_auth_checks!
data/test/support/ops.rb CHANGED
@@ -13,6 +13,10 @@ class User
13
13
  attr_accessor :password
14
14
 
15
15
  validates :email_address, presence: true
16
+
17
+ def self.find(id)
18
+ new(id: id)
19
+ end
16
20
  end
17
21
 
18
22
  class AdminUser < ::User
@@ -137,6 +141,11 @@ class NoUserRequirementsOp < OpWithAuth
137
141
  no_user_requirements!
138
142
  end
139
143
 
144
+ class DifferentUserClassOp < OpWithAuth
145
+ self.user_class_name = "AdminUser"
146
+ require_user!
147
+ end
148
+
140
149
  class CustomAuthorizeOp < OpWithAuth
141
150
  require_user!
142
151
  authorize :authorize_user_is_correct
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: subroutine
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.0
4
+ version: 0.8.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Nelson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-05-14 00:00:00.000000000 Z
11
+ date: 2019-06-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel