snfoil 0.4.3 → 0.6

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
  SHA256:
3
- metadata.gz: 94edbd898ba19e7e2f1b0d37a2b03ffce8dc2018fdc77b5ec46ae6819c0c8dc4
4
- data.tar.gz: 927f494fa9742985745b6ce94d577077f637917c45c89f685433a4bd10e003a7
3
+ metadata.gz: cfab0f49425b4cd1ba02f48f0c3bc114e45fd061118d9673740f2ec05b7655e2
4
+ data.tar.gz: 9c66aac6b0edf869f8243db1c447c5e0ef4df1162734c086dfe26fde86427e74
5
5
  SHA512:
6
- metadata.gz: d7d9a2469b3ab414920670fdf4883a88366e1004c4776d0373eea1d6f973e0fac2e089071e8b6360c6ebc0be4d825f155af5955047df2587dc0b61cc11d651be
7
- data.tar.gz: 1dc91151e2fb75647380079f58b2064377874c7e37c764ac58cf0805e8b23fe82d92947abfae1ed1d2cb8c12eae095f66a36a1ff83cbfad0dbacd5f2a34dd659
6
+ metadata.gz: 239615bc72acaa404521d0771fddaf05a7489cbbf7d9f8b5f1ba8e271c4cb520a2146bdeaf153e933c1c34d83b662a72ebe41f2fd3162825bbd52615fa6b5d8d
7
+ data.tar.gz: b4ac4f458812bfce5002e30103f35a30fc44a8509c3ec3cfb8fa86e612d2fa6f48ada5b34713c29e4910ae321477fbc9bfffdc4e47468121424523f33ba8c3db
@@ -10,6 +10,8 @@ require_relative 'sn_foil/contexts/destroy_context'
10
10
  require_relative 'sn_foil/context'
11
11
  require_relative 'sn_foil/policy'
12
12
  require_relative 'sn_foil/searcher'
13
+ require_relative 'sn_foil/adapters/orms/base_adapter'
14
+ require_relative 'sn_foil/adapters/orms/active_record'
13
15
  require 'active_support/core_ext/module/attribute_accessors'
14
16
  require 'logger'
15
17
 
@@ -1,11 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative 'base_adapter'
4
+
3
5
  module SnFoil
4
6
  module Adapters
5
7
  module ORMs
6
8
  class ActiveRecord < SnFoil::Adapters::ORMs::BaseAdapter
7
- def new(*params)
8
- self.class.new(__getobj__.new(*params))
9
+ def new(**params)
10
+ self.class.new(__getobj__.new(params))
9
11
  end
10
12
 
11
13
  def all
@@ -21,9 +23,13 @@ module SnFoil
21
23
  __getobj__.destroyed?
22
24
  end
23
25
 
24
- def attributes=(*attributes)
26
+ def attributes=(attributes)
25
27
  __getobj__.attributes = attributes
26
28
  end
29
+
30
+ def is_a?(klass)
31
+ __getobj__.class.object_id == klass.object_id
32
+ end
27
33
  end
28
34
  end
29
35
  end
@@ -4,7 +4,7 @@ module SnFoil
4
4
  module Adapters
5
5
  module ORMs
6
6
  class BaseAdapter < SimpleDelegator
7
- def new(*_params)
7
+ def new(**_params)
8
8
  raise NotImplementedError, '#new not implemented in adapter'
9
9
  end
10
10
 
@@ -20,7 +20,7 @@ module SnFoil
20
20
  raise NotImplementedError, '#destroy not implemented in adapter'
21
21
  end
22
22
 
23
- def attributes=(**_attributes)
23
+ def attributes=(_attributes)
24
24
  raise NotImplementedError, '#attributes= not implemented in adapter'
25
25
  end
26
26
  end
@@ -15,8 +15,8 @@ module SnFoil
15
15
  end
16
16
 
17
17
  class_methods do
18
- def build(params:, user: nil, **options)
19
- new(user).build(**options, params: params)
18
+ def build(params:, entity: nil, **options)
19
+ new(entity).build(**options, params: params)
20
20
  end
21
21
  end
22
22
 
@@ -17,8 +17,8 @@ module SnFoil
17
17
  class_methods do
18
18
  attr_reader :i_setup_create_hooks, :i_before_create_hooks, :i_after_create_hooks,
19
19
  :i_after_create_success_hooks, :i_after_create_failure_hooks
20
- def create(params:, user: nil, **options)
21
- new(user).create(**options, params: params)
20
+ def create(params:, entity: nil, **options)
21
+ new(entity).create(**options, params: params)
22
22
  end
23
23
 
24
24
  def setup_create(method = nil, **options, &block)
@@ -70,7 +70,7 @@ module SnFoil
70
70
  options = setup_create_object(**options)
71
71
  authorize(options[:object], :create?, **options)
72
72
  options = create_hooks(**options)
73
- unwrap_object(options[:object])
73
+ options[:object]
74
74
  end
75
75
 
76
76
  def setup_create(**options)
@@ -127,7 +127,9 @@ module SnFoil
127
127
  # This method is private to help protect the order of execution of hooks
128
128
  def create_hooks(options)
129
129
  options = before_create_save(**options)
130
- options = if options[:object].save
130
+ save_successful = options[:object].save
131
+ options.merge!(object: unwrap_object(options[:object]))
132
+ options = if save_successful
131
133
  after_create_save_success(**options)
132
134
  else
133
135
  after_create_save_failure(**options)
@@ -17,8 +17,8 @@ module SnFoil
17
17
  class_methods do
18
18
  attr_reader :i_setup_destroy_hooks, :i_before_destroy_hooks, :i_after_destroy_hooks,
19
19
  :i_after_destroy_success_hooks, :i_after_destroy_failure_hooks
20
- def destroy(id:, user: nil, **options)
21
- new(user).destroy(**options, id: id)
20
+ def destroy(id:, entity: nil, **options)
21
+ new(entity).destroy(**options, id: id)
22
22
  end
23
23
 
24
24
  def setup_destroy(method = nil, **options, &block)
@@ -121,7 +121,9 @@ module SnFoil
121
121
  # This method is private to help protect the order of execution of hooks
122
122
  def destroy_hooks(options)
123
123
  options = before_destroy_save(options)
124
- options = if options[:object].destroy
124
+ destroy_successful = options[:object].destroy
125
+ options.merge!(object: unwrap_object(options[:object]))
126
+ options = if destroy_successful
125
127
  after_destroy_save_success(options)
126
128
  else
127
129
  after_destroy_save_failure(options)
@@ -15,8 +15,8 @@ module SnFoil
15
15
  class_methods do
16
16
  attr_reader :i_searcher, :i_setup_index_hooks
17
17
 
18
- def index(params: {}, user: nil, **options)
19
- new(user).index(**options, params: params)
18
+ def index(params: {}, entity: nil, **options)
19
+ new(entity).index(**options, params: params)
20
20
  end
21
21
 
22
22
  def searcher(klass = nil)
@@ -42,13 +42,13 @@ module SnFoil
42
42
  self.class.i_setup_hooks || []
43
43
  end
44
44
 
45
- attr_reader :user
46
- def initialize(user = nil)
47
- @user = user
45
+ attr_reader :entity
46
+ def initialize(entity = nil)
47
+ @entity = entity
48
48
  end
49
49
 
50
50
  def authorize(object, action, **options)
51
- return unless user # Add logging
51
+ return unless entity # Add logging
52
52
 
53
53
  policy = lookup_policy(object, options)
54
54
  raise Pundit::NotAuthorizedError, query: action, record: object, policy: policy unless policy.public_send(action)
@@ -59,7 +59,7 @@ module SnFoil
59
59
  def scope(object_class = nil, **options)
60
60
  object_class ||= model
61
61
  policy_name = lookup_policy(object_class, options).class.name
62
- "#{policy_name}::Scope".safe_constantize.new(wrap_object(object_class), user)
62
+ "#{policy_name}::Scope".safe_constantize.new(wrap_object(object_class), entity)
63
63
  end
64
64
 
65
65
  def wrap_object(object)
@@ -103,11 +103,11 @@ module SnFoil
103
103
 
104
104
  def lookup_policy(object, options)
105
105
  lookup = if options[:policy]
106
- options[:policy].new(user, object)
106
+ options[:policy].new(entity, object)
107
107
  elsif policy
108
- policy.new(user, object)
108
+ policy.new(entity, object)
109
109
  else
110
- Pundit.policy!(user, object)
110
+ Pundit.policy!(entity, object)
111
111
  end
112
112
 
113
113
  lookup.options = options if lookup.respond_to? :options=
@@ -15,8 +15,8 @@ module SnFoil
15
15
  class_methods do
16
16
  attr_reader :i_setup_show_hooks
17
17
 
18
- def show(id:, user: nil, **options)
19
- new(user).show(**options, id: id)
18
+ def show(id:, entity: nil, **options)
19
+ new(entity).show(**options, id: id)
20
20
  end
21
21
 
22
22
  def setup_show(method = nil, **options, &block)
@@ -17,8 +17,8 @@ module SnFoil
17
17
  class_methods do
18
18
  attr_reader :i_setup_update_hooks, :i_before_update_hooks, :i_after_update_hooks,
19
19
  :i_after_update_success_hooks, :i_after_update_failure_hooks
20
- def update(id:, params:, user: nil, **options)
21
- new(user).update(**options, id: id, params: params)
20
+ def update(id:, params:, entity: nil, **options)
21
+ new(entity).update(**options, id: id, params: params)
22
22
  end
23
23
 
24
24
  def setup_update(method = nil, **options, &block)
@@ -124,7 +124,9 @@ module SnFoil
124
124
  # This method is private to help protect the order of execution of hooks
125
125
  def update_hooks(options)
126
126
  options = before_update_save(options)
127
- options = if options[:object].save
127
+ update_successful = options[:object].save
128
+ options.merge!(object: unwrap_object(options[:object]))
129
+ options = if update_successful
128
130
  after_update_save_success(options)
129
131
  else
130
132
  after_update_save_failure(options)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SnFoil
4
- VERSION = '0.4.3'
4
+ VERSION = '0.6'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: snfoil
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.3
4
+ version: '0.6'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matthew Howes
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2020-03-12 00:00:00.000000000 Z
12
+ date: 2020-07-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -17,14 +17,14 @@ dependencies:
17
17
  requirements:
18
18
  - - ">="
19
19
  - !ruby/object:Gem::Version
20
- version: '5.1'
20
+ version: 5.2.4.3
21
21
  type: :runtime
22
22
  prerelease: false
23
23
  version_requirements: !ruby/object:Gem::Requirement
24
24
  requirements:
25
25
  - - ">="
26
26
  - !ruby/object:Gem::Version
27
- version: '5.1'
27
+ version: 5.2.4.3
28
28
  - !ruby/object:Gem::Dependency
29
29
  name: logger
30
30
  requirement: !ruby/object:Gem::Requirement