snfoil 0.3.0 → 0.4.0
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 +4 -4
- data/CODE_OF_CONDUCT.md +74 -0
- data/README.md +43 -0
- data/Rakefile +4 -0
- data/lib/sn_foil/contexts/change_context.rb +14 -18
- data/lib/sn_foil/contexts/create_context.rb +23 -3
- data/lib/sn_foil/contexts/destroy_context.rb +24 -4
- data/lib/sn_foil/contexts/index_context.rb +22 -3
- data/lib/sn_foil/contexts/setup_context.rb +41 -5
- data/lib/sn_foil/contexts/show_context.rb +26 -0
- data/lib/sn_foil/contexts/update_context.rb +23 -3
- data/lib/sn_foil/policy.rb +3 -2
- data/lib/sn_foil/version.rb +5 -0
- data/lib/sn_foil.rb +47 -0
- metadata +22 -17
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: '08550dd2ea79c81a206bd765375d240a28ff5fadc734c279cbace455ae617650'
|
4
|
+
data.tar.gz: 934a30ee35d2c6e07f739e0e01c219efcbb6e9bb782b341477a70b06f51a60fd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 96ac100420b4276442ccc955cfb5b35f93ea60bd11af4fdb4725b0e6a0a307e67391016dbab97cdeffc3af0532d419526138cb3770d76c1e8ed4388813fbc66a
|
7
|
+
data.tar.gz: f8d3cb25b57174aeea76be073d9a37b6fb12862cd0e6b5986468d33051038e84412315221db13a26d5a5f1b9e5b61d4143afcd10620045f69bb8a00420ad30b9
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at howeszy@gmail.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/README.md
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
# Sn::Foil
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/sn_foil`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'snfoil'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install snfoil
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/snfoil. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
36
|
+
|
37
|
+
## License
|
38
|
+
|
39
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
40
|
+
|
41
|
+
## Code of Conduct
|
42
|
+
|
43
|
+
Everyone interacting in the Sn::Foil project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/snfoil/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
@@ -12,15 +12,22 @@ module SnFoil
|
|
12
12
|
include SetupContext
|
13
13
|
end
|
14
14
|
|
15
|
-
class_methods do
|
16
|
-
attr_reader :i_params, :
|
15
|
+
class_methods do # rubocop:disable Metrics/BlockLength
|
16
|
+
attr_reader :i_params, :i_setup_change_hooks, :i_before_change_hooks, :i_after_change_hooks,
|
17
|
+
:i_after_change_success_hooks, :i_after_change_failure_hooks
|
17
18
|
def params(*whitelisted_params)
|
18
19
|
@i_params ||= []
|
19
20
|
@i_params |= whitelisted_params
|
20
21
|
end
|
21
22
|
|
23
|
+
def setup_change(method = nil, **options, &block)
|
24
|
+
raise ArgumentError, '#setup_change requires either a method name or a block' if method.nil? && block.nil?
|
25
|
+
|
26
|
+
(@i_setup_change_hooks ||= []) << { method: method, block: block, if: options[:if], unless: options[:unless] }
|
27
|
+
end
|
28
|
+
|
22
29
|
def before_change(method = nil, **options, &block)
|
23
|
-
raise ArgumentError, '#
|
30
|
+
raise ArgumentError, '#before_change requires either a method name or a block' if method.nil? && block.nil?
|
24
31
|
|
25
32
|
(@i_before_change_hooks ||= []) << { method: method, block: block, if: options[:if], unless: options[:unless] }
|
26
33
|
end
|
@@ -69,6 +76,10 @@ module SnFoil
|
|
69
76
|
options
|
70
77
|
end
|
71
78
|
|
79
|
+
def setup_change_hooks
|
80
|
+
self.class.i_setup_change_hooks || []
|
81
|
+
end
|
82
|
+
|
72
83
|
def before_change_hooks
|
73
84
|
self.class.i_before_change_hooks || []
|
74
85
|
end
|
@@ -84,21 +95,6 @@ module SnFoil
|
|
84
95
|
def after_change_failure_hooks
|
85
96
|
self.class.i_after_change_failure_hooks || []
|
86
97
|
end
|
87
|
-
|
88
|
-
def run_hook(hook, **options)
|
89
|
-
return options unless hook_valid?(hook, **options)
|
90
|
-
|
91
|
-
return send(hook[:method], **options) if hook[:method]
|
92
|
-
|
93
|
-
instance_exec options, &hook[:block]
|
94
|
-
end
|
95
|
-
|
96
|
-
def hook_valid?(hook, **options)
|
97
|
-
return false if !hook[:if].nil? && hook[:if].call(options) == false
|
98
|
-
return false if !hook[:unless].nil? && hook[:unless].call(options) == true
|
99
|
-
|
100
|
-
true
|
101
|
-
end
|
102
98
|
end
|
103
99
|
end
|
104
100
|
end
|
@@ -15,13 +15,20 @@ module SnFoil
|
|
15
15
|
end
|
16
16
|
|
17
17
|
class_methods do
|
18
|
-
attr_reader :
|
18
|
+
attr_reader :i_setup_create_hooks, :i_before_create_hooks, :i_after_create_hooks,
|
19
|
+
:i_after_create_success_hooks, :i_after_create_failure_hooks
|
19
20
|
def create(params:, user: nil, **options)
|
20
21
|
new(user).create(**options, params: params)
|
21
22
|
end
|
22
23
|
|
24
|
+
def setup_create(method = nil, **options, &block)
|
25
|
+
raise ArgumentError, '#setup_create requires either a method name or a block' if method.nil? && block.nil?
|
26
|
+
|
27
|
+
(@i_setup_create_hooks ||= []) << { method: method, block: block, if: options[:if], unless: options[:unless] }
|
28
|
+
end
|
29
|
+
|
23
30
|
def before_create(method = nil, **options, &block)
|
24
|
-
raise ArgumentError, '#
|
31
|
+
raise ArgumentError, '#before_create requires either a method name or a block' if method.nil? && block.nil?
|
25
32
|
|
26
33
|
(@i_before_create_hooks ||= []) << { method: method, block: block, if: options[:if], unless: options[:unless] }
|
27
34
|
end
|
@@ -59,7 +66,7 @@ module SnFoil
|
|
59
66
|
|
60
67
|
def create(**options)
|
61
68
|
options[:action] = :create
|
62
|
-
options =
|
69
|
+
options = before_setup_create_object(**options)
|
63
70
|
options = setup_create_object(**options)
|
64
71
|
authorize(options[:object], :create?, **options)
|
65
72
|
options = create_hooks(**options)
|
@@ -86,6 +93,10 @@ module SnFoil
|
|
86
93
|
options
|
87
94
|
end
|
88
95
|
|
96
|
+
def setup_create_hooks
|
97
|
+
self.class.i_setup_create_hooks || []
|
98
|
+
end
|
99
|
+
|
89
100
|
def before_create_hooks
|
90
101
|
self.class.i_before_create_hooks || []
|
91
102
|
end
|
@@ -104,6 +115,15 @@ module SnFoil
|
|
104
115
|
|
105
116
|
private
|
106
117
|
|
118
|
+
def before_setup_create_object(**options)
|
119
|
+
options = setup_create(**options)
|
120
|
+
options = setup_create_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
|
121
|
+
options = setup_change(**options)
|
122
|
+
options = setup_change_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
|
123
|
+
options = setup(**options)
|
124
|
+
setup_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
|
125
|
+
end
|
126
|
+
|
107
127
|
# This method is private to help protect the order of execution of hooks
|
108
128
|
def create_hooks(options)
|
109
129
|
options = before_create_save(**options)
|
@@ -6,7 +6,7 @@ require_relative './change_context'
|
|
6
6
|
|
7
7
|
module SnFoil
|
8
8
|
module Contexts
|
9
|
-
module DestroyContext
|
9
|
+
module DestroyContext # rubocop:disable Metrics/ModuleLength
|
10
10
|
extend ActiveSupport::Concern
|
11
11
|
|
12
12
|
included do
|
@@ -15,13 +15,20 @@ module SnFoil
|
|
15
15
|
end
|
16
16
|
|
17
17
|
class_methods do
|
18
|
-
attr_reader :
|
18
|
+
attr_reader :i_setup_destroy_hooks, :i_before_destroy_hooks, :i_after_destroy_hooks,
|
19
|
+
:i_after_destroy_success_hooks, :i_after_destroy_failure_hooks
|
19
20
|
def destroy(id:, user: nil, **options)
|
20
21
|
new(user).destroy(**options, id: id)
|
21
22
|
end
|
22
23
|
|
24
|
+
def setup_destroy(method = nil, **options, &block)
|
25
|
+
raise ArgumentError, '#setup_destroy requires either a method name or a block' if method.nil? && block.nil?
|
26
|
+
|
27
|
+
(@i_setup_destroy_hooks ||= []) << { method: method, block: block, if: options[:if], unless: options[:unless] }
|
28
|
+
end
|
29
|
+
|
23
30
|
def before_destroy(method = nil, **options, &block)
|
24
|
-
raise ArgumentError, '#
|
31
|
+
raise ArgumentError, '#before_destroy requires either a method name or a block' if method.nil? && block.nil?
|
25
32
|
|
26
33
|
(@i_before_destroy_hooks ||= []) << { method: method, block: block, if: options[:if], unless: options[:unless] }
|
27
34
|
end
|
@@ -53,7 +60,7 @@ module SnFoil
|
|
53
60
|
|
54
61
|
def destroy(**options)
|
55
62
|
options[:action] = :destroy
|
56
|
-
options =
|
63
|
+
options = before_setup_destroy_object(**options)
|
57
64
|
options = setup_destroy_object(**options)
|
58
65
|
authorize(options[:object], :destroy?, **options)
|
59
66
|
options = destroy_hooks(**options)
|
@@ -80,6 +87,10 @@ module SnFoil
|
|
80
87
|
options
|
81
88
|
end
|
82
89
|
|
90
|
+
def setup_destroy_hooks
|
91
|
+
self.class.i_setup_destroy_hooks || []
|
92
|
+
end
|
93
|
+
|
83
94
|
def before_destroy_hooks
|
84
95
|
self.class.i_before_destroy_hooks || []
|
85
96
|
end
|
@@ -98,6 +109,15 @@ module SnFoil
|
|
98
109
|
|
99
110
|
private
|
100
111
|
|
112
|
+
def before_setup_destroy_object(**options)
|
113
|
+
options = setup_destroy(**options)
|
114
|
+
options = setup_destroy_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
|
115
|
+
options = setup_change(**options)
|
116
|
+
options = setup_change_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
|
117
|
+
options = setup(**options)
|
118
|
+
setup_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
|
119
|
+
end
|
120
|
+
|
101
121
|
# This method is private to help protect the order of execution of hooks
|
102
122
|
def destroy_hooks(options)
|
103
123
|
options = before_destroy_save(options)
|
@@ -13,7 +13,7 @@ module SnFoil
|
|
13
13
|
end
|
14
14
|
|
15
15
|
class_methods do
|
16
|
-
attr_reader :i_searcher
|
16
|
+
attr_reader :i_searcher, :i_setup_index_hooks
|
17
17
|
|
18
18
|
def index(params: {}, user: nil, **options)
|
19
19
|
new(user).index(**options, params: params)
|
@@ -22,24 +22,43 @@ module SnFoil
|
|
22
22
|
def searcher(klass = nil)
|
23
23
|
@i_searcher = klass
|
24
24
|
end
|
25
|
+
|
26
|
+
def setup_index(method = nil, **options, &block)
|
27
|
+
raise ArgumentError, '#setup_index requires either a method name or a block' if method.nil? && block.nil?
|
28
|
+
|
29
|
+
(@i_setup_index_hooks ||= []) << { method: method, block: block, if: options[:if], unless: options[:unless] }
|
30
|
+
end
|
25
31
|
end
|
26
32
|
|
27
33
|
def searcher
|
28
34
|
self.class.i_searcher
|
29
35
|
end
|
30
36
|
|
37
|
+
def setup_index_hooks
|
38
|
+
self.class.i_setup_index_hooks || []
|
39
|
+
end
|
40
|
+
|
31
41
|
def index(params:, **options)
|
32
42
|
options[:action] = :index
|
33
|
-
options =
|
43
|
+
options = before_setup_index(**options)
|
44
|
+
authorize(nil, :index?, **options)
|
34
45
|
options.fetch(:searcher) { searcher }
|
35
46
|
.new(scope: scope.resolve)
|
36
47
|
.search(params: params)
|
37
48
|
end
|
38
49
|
|
39
|
-
# Param manipulation based on User should be done here
|
40
50
|
def setup_index(**options)
|
41
51
|
options
|
42
52
|
end
|
53
|
+
|
54
|
+
private
|
55
|
+
|
56
|
+
def before_setup_index(**options)
|
57
|
+
options = setup_index(**options)
|
58
|
+
options = setup_index_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
|
59
|
+
options = setup(**options)
|
60
|
+
setup_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
|
61
|
+
end
|
43
62
|
end
|
44
63
|
end
|
45
64
|
end
|
@@ -9,7 +9,7 @@ module SnFoil
|
|
9
9
|
extend ActiveSupport::Concern
|
10
10
|
|
11
11
|
class_methods do
|
12
|
-
attr_reader :i_model, :i_policy
|
12
|
+
attr_reader :i_model, :i_policy, :i_setup_hooks
|
13
13
|
|
14
14
|
def model(klass = nil)
|
15
15
|
@i_model = klass
|
@@ -18,6 +18,12 @@ module SnFoil
|
|
18
18
|
def policy(klass = nil)
|
19
19
|
@i_policy = klass
|
20
20
|
end
|
21
|
+
|
22
|
+
def setup(method = nil, **options, &block)
|
23
|
+
raise ArgumentError, '#setup requires either a method name or a block' if method.nil? && block.nil?
|
24
|
+
|
25
|
+
(@i_setup_hooks ||= []) << { method: method, block: block, if: options[:if], unless: options[:unless] }
|
26
|
+
end
|
21
27
|
end
|
22
28
|
|
23
29
|
def model
|
@@ -28,6 +34,14 @@ module SnFoil
|
|
28
34
|
self.class.i_policy
|
29
35
|
end
|
30
36
|
|
37
|
+
def setup(**options)
|
38
|
+
options
|
39
|
+
end
|
40
|
+
|
41
|
+
def setup_hooks
|
42
|
+
self.class.i_setup_hooks || []
|
43
|
+
end
|
44
|
+
|
31
45
|
attr_reader :user
|
32
46
|
def initialize(user = nil)
|
33
47
|
@user = user
|
@@ -38,6 +52,7 @@ module SnFoil
|
|
38
52
|
|
39
53
|
policy = lookup_policy(object, options)
|
40
54
|
raise Pundit::NotAuthorizedError, query: action, record: object, policy: policy unless policy.public_send(action)
|
55
|
+
|
41
56
|
true
|
42
57
|
end
|
43
58
|
|
@@ -69,13 +84,34 @@ module SnFoil
|
|
69
84
|
@adapter ||= SnFoil.adapter
|
70
85
|
end
|
71
86
|
|
87
|
+
def run_hook(hook, **options)
|
88
|
+
return options unless hook_valid?(hook, **options)
|
89
|
+
|
90
|
+
return send(hook[:method], **options) if hook[:method]
|
91
|
+
|
92
|
+
instance_exec options, &hook[:block]
|
93
|
+
end
|
94
|
+
|
95
|
+
def hook_valid?(hook, **options)
|
96
|
+
return false if !hook[:if].nil? && hook[:if].call(options) == false
|
97
|
+
return false if !hook[:unless].nil? && hook[:unless].call(options) == true
|
98
|
+
|
99
|
+
true
|
100
|
+
end
|
101
|
+
|
72
102
|
private
|
73
103
|
|
74
104
|
def lookup_policy(object, options)
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
105
|
+
lookup = if options[:policy]
|
106
|
+
options[:policy].new(user, object)
|
107
|
+
elsif policy
|
108
|
+
policy.new(user, object)
|
109
|
+
else
|
110
|
+
Pundit.policy!(user, object)
|
111
|
+
end
|
112
|
+
|
113
|
+
lookup.options = options if lookup.respond_to? :options=
|
114
|
+
lookup
|
79
115
|
end
|
80
116
|
end
|
81
117
|
end
|
@@ -13,9 +13,17 @@ module SnFoil
|
|
13
13
|
end
|
14
14
|
|
15
15
|
class_methods do
|
16
|
+
attr_reader :i_setup_show_hooks
|
17
|
+
|
16
18
|
def show(id:, user: nil, **options)
|
17
19
|
new(user).show(**options, id: id)
|
18
20
|
end
|
21
|
+
|
22
|
+
def setup_show(method = nil, **options, &block)
|
23
|
+
raise ArgumentError, '#setup_show requires either a method name or a block' if method.nil? && block.nil?
|
24
|
+
|
25
|
+
(@i_setup_show_hooks ||= []) << { method: method, block: block, if: options[:if], unless: options[:unless] }
|
26
|
+
end
|
19
27
|
end
|
20
28
|
|
21
29
|
def setup_show_object(id: nil, object: nil, **options)
|
@@ -24,12 +32,30 @@ module SnFoil
|
|
24
32
|
options.merge! object: wrap_object(object || scope.resolve.find(id))
|
25
33
|
end
|
26
34
|
|
35
|
+
def setup_show_hooks
|
36
|
+
self.class.i_setup_show_hooks || []
|
37
|
+
end
|
38
|
+
|
27
39
|
def show(**options)
|
28
40
|
options[:action] = :show
|
41
|
+
options = before_setup_show(**options)
|
29
42
|
options = setup_show_object(**options)
|
30
43
|
authorize(options[:object], :show?, **options)
|
31
44
|
unwrap_object options[:object]
|
32
45
|
end
|
46
|
+
|
47
|
+
def setup_show(**options)
|
48
|
+
options
|
49
|
+
end
|
50
|
+
|
51
|
+
private
|
52
|
+
|
53
|
+
def before_setup_show(**options)
|
54
|
+
options = setup_show(**options)
|
55
|
+
options = setup_show_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
|
56
|
+
options = setup(**options)
|
57
|
+
setup_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
|
58
|
+
end
|
33
59
|
end
|
34
60
|
end
|
35
61
|
end
|
@@ -15,13 +15,20 @@ module SnFoil
|
|
15
15
|
end
|
16
16
|
|
17
17
|
class_methods do
|
18
|
-
attr_reader :
|
18
|
+
attr_reader :i_setup_update_hooks, :i_before_update_hooks, :i_after_update_hooks,
|
19
|
+
:i_after_update_success_hooks, :i_after_update_failure_hooks
|
19
20
|
def update(id:, params:, user: nil, **options)
|
20
21
|
new(user).update(**options, id: id, params: params)
|
21
22
|
end
|
22
23
|
|
24
|
+
def setup_update(method = nil, **options, &block)
|
25
|
+
raise ArgumentError, '#setup_update requires either a method name or a block' if method.nil? && block.nil?
|
26
|
+
|
27
|
+
(@i_setup_update_hooks ||= []) << { method: method, block: block, if: options[:if], unless: options[:unless] }
|
28
|
+
end
|
29
|
+
|
23
30
|
def before_update(method = nil, **options, &block)
|
24
|
-
raise ArgumentError, '#
|
31
|
+
raise ArgumentError, '#before_update requires either a method name or a block' if method.nil? && block.nil?
|
25
32
|
|
26
33
|
(@i_before_update_hooks ||= []) << { method: method, block: block, if: options[:if], unless: options[:unless] }
|
27
34
|
end
|
@@ -56,7 +63,7 @@ module SnFoil
|
|
56
63
|
|
57
64
|
def update(**options)
|
58
65
|
options[:action] = :update
|
59
|
-
options =
|
66
|
+
options = before_setup_update_object(**options)
|
60
67
|
options = setup_update_object(**options)
|
61
68
|
authorize(options[:object], :update?, **options)
|
62
69
|
options = update_hooks(**options)
|
@@ -83,6 +90,10 @@ module SnFoil
|
|
83
90
|
options
|
84
91
|
end
|
85
92
|
|
93
|
+
def setup_update_hooks
|
94
|
+
self.class.i_setup_update_hooks || []
|
95
|
+
end
|
96
|
+
|
86
97
|
def before_update_hooks
|
87
98
|
self.class.i_before_update_hooks || []
|
88
99
|
end
|
@@ -101,6 +112,15 @@ module SnFoil
|
|
101
112
|
|
102
113
|
private
|
103
114
|
|
115
|
+
def before_setup_update_object(**options)
|
116
|
+
options = setup_update(**options)
|
117
|
+
options = setup_update_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
|
118
|
+
options = setup_change(**options)
|
119
|
+
options = setup_change_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
|
120
|
+
options = setup(**options)
|
121
|
+
setup_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
|
122
|
+
end
|
123
|
+
|
104
124
|
# This method is private to help protect the order of execution of hooks
|
105
125
|
def update_hooks(options)
|
106
126
|
options = before_update_save(options)
|
data/lib/sn_foil/policy.rb
CHANGED
@@ -6,8 +6,9 @@ module SnFoil
|
|
6
6
|
module Policy
|
7
7
|
extend ActiveSupport::Concern
|
8
8
|
|
9
|
-
attr_reader :record, :entity
|
10
|
-
|
9
|
+
attr_reader :record, :entity
|
10
|
+
attr_accessor :options
|
11
|
+
def initialize(entity, record, options = {})
|
11
12
|
@record = record
|
12
13
|
@entity = entity
|
13
14
|
@options = options
|
data/lib/sn_foil.rb
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'sn_foil/version'
|
4
|
+
require_relative 'sn_foil/contexts/build_context'
|
5
|
+
require_relative 'sn_foil/contexts/index_context'
|
6
|
+
require_relative 'sn_foil/contexts/show_context'
|
7
|
+
require_relative 'sn_foil/contexts/create_context'
|
8
|
+
require_relative 'sn_foil/contexts/update_context'
|
9
|
+
require_relative 'sn_foil/contexts/destroy_context'
|
10
|
+
require_relative 'sn_foil/context'
|
11
|
+
require_relative 'sn_foil/policy'
|
12
|
+
require_relative 'sn_foil/searcher'
|
13
|
+
require 'active_support/core_ext/module/attribute_accessors'
|
14
|
+
require 'logger'
|
15
|
+
|
16
|
+
module SnFoil
|
17
|
+
class Error < StandardError; end
|
18
|
+
|
19
|
+
mattr_accessor :orm, default: 'active_record'
|
20
|
+
mattr_writer :logger
|
21
|
+
|
22
|
+
class << self
|
23
|
+
def logger
|
24
|
+
@logger ||= Logger.new($stdout).tap do |log|
|
25
|
+
log.progname = name
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def adapter
|
30
|
+
return @adapter if @adapter
|
31
|
+
|
32
|
+
@adapter ||= if orm.instance_of?(String) || orm.instance_of?(Symbol)
|
33
|
+
if Object.const_defined?("SnFoil::Adapters::ORMs::#{orm.camelcase}")
|
34
|
+
"SnFoil::Adapters::ORMs::#{orm.camelcase}".constantize
|
35
|
+
else
|
36
|
+
orm.constantize
|
37
|
+
end
|
38
|
+
else
|
39
|
+
orm
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def configure
|
44
|
+
yield self
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
metadata
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: snfoil
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matthew Howes
|
8
8
|
- Danny Murphy
|
9
9
|
autorequire:
|
10
|
-
bindir:
|
10
|
+
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2020-
|
12
|
+
date: 2020-03-04 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
@@ -67,6 +67,20 @@ dependencies:
|
|
67
67
|
- - "~>"
|
68
68
|
- !ruby/object:Gem::Version
|
69
69
|
version: '2.0'
|
70
|
+
- !ruby/object:Gem::Dependency
|
71
|
+
name: dry-struct
|
72
|
+
requirement: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - "~>"
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '1.0'
|
77
|
+
type: :development
|
78
|
+
prerelease: false
|
79
|
+
version_requirements: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - "~>"
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '1.0'
|
70
84
|
- !ruby/object:Gem::Dependency
|
71
85
|
name: pry-byebug
|
72
86
|
requirement: !ruby/object:Gem::Requirement
|
@@ -137,20 +151,6 @@ dependencies:
|
|
137
151
|
- - "~>"
|
138
152
|
- !ruby/object:Gem::Version
|
139
153
|
version: 1.36.0
|
140
|
-
- !ruby/object:Gem::Dependency
|
141
|
-
name: dry-struct
|
142
|
-
requirement: !ruby/object:Gem::Requirement
|
143
|
-
requirements:
|
144
|
-
- - "~>"
|
145
|
-
- !ruby/object:Gem::Version
|
146
|
-
version: '1.0'
|
147
|
-
type: :development
|
148
|
-
prerelease: false
|
149
|
-
version_requirements: !ruby/object:Gem::Requirement
|
150
|
-
requirements:
|
151
|
-
- - "~>"
|
152
|
-
- !ruby/object:Gem::Version
|
153
|
-
version: '1.0'
|
154
154
|
description:
|
155
155
|
email:
|
156
156
|
- howeszy@gmail.com
|
@@ -159,6 +159,10 @@ executables: []
|
|
159
159
|
extensions: []
|
160
160
|
extra_rdoc_files: []
|
161
161
|
files:
|
162
|
+
- CODE_OF_CONDUCT.md
|
163
|
+
- README.md
|
164
|
+
- Rakefile
|
165
|
+
- lib/sn_foil.rb
|
162
166
|
- lib/sn_foil/adapters/orms/active_record.rb
|
163
167
|
- lib/sn_foil/adapters/orms/base_adapter.rb
|
164
168
|
- lib/sn_foil/context.rb
|
@@ -172,6 +176,7 @@ files:
|
|
172
176
|
- lib/sn_foil/contexts/update_context.rb
|
173
177
|
- lib/sn_foil/policy.rb
|
174
178
|
- lib/sn_foil/searcher.rb
|
179
|
+
- lib/sn_foil/version.rb
|
175
180
|
homepage: https://github.com/howeszy/snfoil
|
176
181
|
licenses:
|
177
182
|
- MIT
|