bp3-request_state 0.1.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 +7 -0
- data/.rspec +3 -0
- data/.rubocop.yml +37 -0
- data/.yardopts +8 -0
- data/CHANGELOG.md +9 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +139 -0
- data/LICENSE.txt +21 -0
- data/README.md +118 -0
- data/Rakefile +12 -0
- data/bp3-request_state.gemspec +48 -0
- data/lib/bp3/request_state/base.rb +214 -0
- data/lib/bp3/request_state/site.rb +21 -0
- data/lib/bp3/request_state/test.rb +20 -0
- data/lib/bp3/request_state/version.rb +7 -0
- data/lib/bp3/request_state.rb +10 -0
- data/lib/bp3-request_state.rb +3 -0
- data/sig/bp3/request_state.rbs +6 -0
- metadata +209 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: d89a2dba8a730e34a86bd5712f1b673bb3be432e179e20f1ee1daf79c017f939
|
|
4
|
+
data.tar.gz: 9ffefdfe6cc331637ff093dd2bfb422b86a0f4caaf16d56c64b3e95741df2e0c
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 1b0c29af713260d43e75a88472c1941e92f151f867570d1bd338a44d2dc963299a52bbcfaaada963b71c585e98aa1b815b970af0a568e095aceb5f6620f9e45d
|
|
7
|
+
data.tar.gz: 6e08dc1fcd0d0340ed04be8b3827dede51104867230902734a8cf925db7b4ac6e3e4559f979e7efe3573709a873bdedbb8e95c50065224fde2c3ba5a5fefc841
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
require:
|
|
2
|
+
- rubocop-rake
|
|
3
|
+
- rubocop-rspec
|
|
4
|
+
- rubocop-capybara
|
|
5
|
+
- rubocop-factory_bot
|
|
6
|
+
|
|
7
|
+
AllCops:
|
|
8
|
+
TargetRubyVersion: 3.2.2
|
|
9
|
+
NewCops: enable
|
|
10
|
+
|
|
11
|
+
Gemspec/DevelopmentDependencies:
|
|
12
|
+
EnforcedStyle: gemspec
|
|
13
|
+
|
|
14
|
+
Metrics/AbcSize:
|
|
15
|
+
Max: 26
|
|
16
|
+
|
|
17
|
+
Metrics/CyclomaticComplexity:
|
|
18
|
+
Max: 10
|
|
19
|
+
|
|
20
|
+
Metrics/MethodLength:
|
|
21
|
+
Max: 15
|
|
22
|
+
|
|
23
|
+
Metrics/PerceivedComplexity:
|
|
24
|
+
Max: 10
|
|
25
|
+
|
|
26
|
+
Naming/FileName:
|
|
27
|
+
Exclude:
|
|
28
|
+
- lib/bp3-request_state.rb
|
|
29
|
+
|
|
30
|
+
RSpec/ExampleLength:
|
|
31
|
+
Max: 10
|
|
32
|
+
|
|
33
|
+
RSpec/MultipleExpectations:
|
|
34
|
+
Max: 4
|
|
35
|
+
|
|
36
|
+
Style/Documentation:
|
|
37
|
+
Enabled: false
|
data/.yardopts
ADDED
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
PATH
|
|
2
|
+
remote: .
|
|
3
|
+
specs:
|
|
4
|
+
bp3-request_state (0.1.1)
|
|
5
|
+
actionpack (>= 7.1.2, < 8)
|
|
6
|
+
activesupport (>= 7.1.2, < 8)
|
|
7
|
+
request_store (>= 1.5.1, < 2)
|
|
8
|
+
|
|
9
|
+
GEM
|
|
10
|
+
remote: https://rubygems.org/
|
|
11
|
+
specs:
|
|
12
|
+
actionpack (7.1.3)
|
|
13
|
+
actionview (= 7.1.3)
|
|
14
|
+
activesupport (= 7.1.3)
|
|
15
|
+
nokogiri (>= 1.8.5)
|
|
16
|
+
racc
|
|
17
|
+
rack (>= 2.2.4)
|
|
18
|
+
rack-session (>= 1.0.1)
|
|
19
|
+
rack-test (>= 0.6.3)
|
|
20
|
+
rails-dom-testing (~> 2.2)
|
|
21
|
+
rails-html-sanitizer (~> 1.6)
|
|
22
|
+
actionview (7.1.3)
|
|
23
|
+
activesupport (= 7.1.3)
|
|
24
|
+
builder (~> 3.1)
|
|
25
|
+
erubi (~> 1.11)
|
|
26
|
+
rails-dom-testing (~> 2.2)
|
|
27
|
+
rails-html-sanitizer (~> 1.6)
|
|
28
|
+
activesupport (7.1.3)
|
|
29
|
+
base64
|
|
30
|
+
bigdecimal
|
|
31
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
|
32
|
+
connection_pool (>= 2.2.5)
|
|
33
|
+
drb
|
|
34
|
+
i18n (>= 1.6, < 2)
|
|
35
|
+
minitest (>= 5.1)
|
|
36
|
+
mutex_m
|
|
37
|
+
tzinfo (~> 2.0)
|
|
38
|
+
ast (2.4.2)
|
|
39
|
+
base64 (0.2.0)
|
|
40
|
+
bigdecimal (3.1.6)
|
|
41
|
+
builder (3.2.4)
|
|
42
|
+
byebug (11.1.3)
|
|
43
|
+
concurrent-ruby (1.2.3)
|
|
44
|
+
connection_pool (2.4.1)
|
|
45
|
+
crass (1.0.6)
|
|
46
|
+
diff-lcs (1.5.0)
|
|
47
|
+
drb (2.2.0)
|
|
48
|
+
ruby2_keywords
|
|
49
|
+
erubi (1.12.0)
|
|
50
|
+
i18n (1.14.1)
|
|
51
|
+
concurrent-ruby (~> 1.0)
|
|
52
|
+
json (2.7.1)
|
|
53
|
+
language_server-protocol (3.17.0.3)
|
|
54
|
+
loofah (2.22.0)
|
|
55
|
+
crass (~> 1.0.2)
|
|
56
|
+
nokogiri (>= 1.12.0)
|
|
57
|
+
minitest (5.21.2)
|
|
58
|
+
mutex_m (0.2.0)
|
|
59
|
+
nokogiri (1.16.0-x86_64-darwin)
|
|
60
|
+
racc (~> 1.4)
|
|
61
|
+
parallel (1.24.0)
|
|
62
|
+
parser (3.3.0.4)
|
|
63
|
+
ast (~> 2.4.1)
|
|
64
|
+
racc
|
|
65
|
+
racc (1.7.3)
|
|
66
|
+
rack (3.0.8)
|
|
67
|
+
rack-session (2.0.0)
|
|
68
|
+
rack (>= 3.0.0)
|
|
69
|
+
rack-test (2.1.0)
|
|
70
|
+
rack (>= 1.3)
|
|
71
|
+
rails-dom-testing (2.2.0)
|
|
72
|
+
activesupport (>= 5.0.0)
|
|
73
|
+
minitest
|
|
74
|
+
nokogiri (>= 1.6)
|
|
75
|
+
rails-html-sanitizer (1.6.0)
|
|
76
|
+
loofah (~> 2.21)
|
|
77
|
+
nokogiri (~> 1.14)
|
|
78
|
+
rainbow (3.1.1)
|
|
79
|
+
rake (13.1.0)
|
|
80
|
+
regexp_parser (2.9.0)
|
|
81
|
+
request_store (1.5.1)
|
|
82
|
+
rack (>= 1.4)
|
|
83
|
+
rexml (3.2.6)
|
|
84
|
+
rspec (3.12.0)
|
|
85
|
+
rspec-core (~> 3.12.0)
|
|
86
|
+
rspec-expectations (~> 3.12.0)
|
|
87
|
+
rspec-mocks (~> 3.12.0)
|
|
88
|
+
rspec-core (3.12.2)
|
|
89
|
+
rspec-support (~> 3.12.0)
|
|
90
|
+
rspec-expectations (3.12.3)
|
|
91
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
92
|
+
rspec-support (~> 3.12.0)
|
|
93
|
+
rspec-mocks (3.12.6)
|
|
94
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
95
|
+
rspec-support (~> 3.12.0)
|
|
96
|
+
rspec-support (3.12.1)
|
|
97
|
+
rubocop (1.60.1)
|
|
98
|
+
json (~> 2.3)
|
|
99
|
+
language_server-protocol (>= 3.17.0)
|
|
100
|
+
parallel (~> 1.10)
|
|
101
|
+
parser (>= 3.3.0.2)
|
|
102
|
+
rainbow (>= 2.2.2, < 4.0)
|
|
103
|
+
regexp_parser (>= 1.8, < 3.0)
|
|
104
|
+
rexml (>= 3.2.5, < 4.0)
|
|
105
|
+
rubocop-ast (>= 1.30.0, < 2.0)
|
|
106
|
+
ruby-progressbar (~> 1.7)
|
|
107
|
+
unicode-display_width (>= 2.4.0, < 3.0)
|
|
108
|
+
rubocop-ast (1.30.0)
|
|
109
|
+
parser (>= 3.2.1.0)
|
|
110
|
+
rubocop-capybara (2.20.0)
|
|
111
|
+
rubocop (~> 1.41)
|
|
112
|
+
rubocop-factory_bot (2.25.1)
|
|
113
|
+
rubocop (~> 1.41)
|
|
114
|
+
rubocop-rake (0.6.0)
|
|
115
|
+
rubocop (~> 1.0)
|
|
116
|
+
rubocop-rspec (2.26.1)
|
|
117
|
+
rubocop (~> 1.40)
|
|
118
|
+
rubocop-capybara (~> 2.17)
|
|
119
|
+
rubocop-factory_bot (~> 2.22)
|
|
120
|
+
ruby-progressbar (1.13.0)
|
|
121
|
+
ruby2_keywords (0.0.5)
|
|
122
|
+
tzinfo (2.0.6)
|
|
123
|
+
concurrent-ruby (~> 1.0)
|
|
124
|
+
unicode-display_width (2.5.0)
|
|
125
|
+
|
|
126
|
+
PLATFORMS
|
|
127
|
+
x86_64-darwin-22
|
|
128
|
+
|
|
129
|
+
DEPENDENCIES
|
|
130
|
+
bp3-request_state!
|
|
131
|
+
byebug
|
|
132
|
+
rake (~> 13.0)
|
|
133
|
+
rspec (~> 3.0)
|
|
134
|
+
rubocop (~> 1.21)
|
|
135
|
+
rubocop-rake (~> 0.6)
|
|
136
|
+
rubocop-rspec (~> 2.25)
|
|
137
|
+
|
|
138
|
+
BUNDLED WITH
|
|
139
|
+
2.4.22
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023-2024 Wim den Braven
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
# Bp3::RequestState
|
|
2
|
+
|
|
3
|
+
bp3-action_dispatch provides the `Bp3::RequestState::Base` class for BP3, the persuavis/black_phoebe_3
|
|
4
|
+
multi-site multi-tenant rails application.
|
|
5
|
+
|
|
6
|
+
`Bp3::RequestState::Base` can be used to store global state per request, or per background job.
|
|
7
|
+
It keeps state per thread and is supposed to be thread safe.
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
Add this line to your application's Gemfile:
|
|
12
|
+
|
|
13
|
+
```ruby
|
|
14
|
+
gem 'bp3-request_state'
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
And then execute:
|
|
18
|
+
|
|
19
|
+
$ bundle install
|
|
20
|
+
|
|
21
|
+
Or install it yourself as:
|
|
22
|
+
|
|
23
|
+
$ gem install bp3-request_state
|
|
24
|
+
|
|
25
|
+
## Usage
|
|
26
|
+
|
|
27
|
+
In your app, create a model or service that inherits from `Bp3::Request::State`, and
|
|
28
|
+
specify
|
|
29
|
+
- `hash_key_map`, mapping members of the global state to their class,
|
|
30
|
+
- `base_attrs`, defining the list of global state member objects, and
|
|
31
|
+
- `hash_attrs`, defining the list of members of the global state hash.
|
|
32
|
+
Override `.with_current` as needed.
|
|
33
|
+
|
|
34
|
+
Here is an example from BP3:
|
|
35
|
+
```ruby
|
|
36
|
+
class GlobalRequestState < Bp3::RequestState::Base
|
|
37
|
+
self.hash_key_map =
|
|
38
|
+
{
|
|
39
|
+
target_site: 'Sites::Site',
|
|
40
|
+
target_tenant: 'Tenant',
|
|
41
|
+
target_workspace: 'Workspaces::Workspace',
|
|
42
|
+
current_site: 'Sites::Site',
|
|
43
|
+
current_tenant: 'Tenant',
|
|
44
|
+
current_workspace: 'Workspaces::Workspace',
|
|
45
|
+
current_user: 'Users::User',
|
|
46
|
+
current_admin: 'Sites::Admin',
|
|
47
|
+
current_root: 'Root',
|
|
48
|
+
current_visitor: 'Users::Visitor'
|
|
49
|
+
}.freeze
|
|
50
|
+
|
|
51
|
+
self.base_attrs =
|
|
52
|
+
%w[inbound_request
|
|
53
|
+
current_site current_workspace current_tenant
|
|
54
|
+
current_user current_admin current_root current_visitor
|
|
55
|
+
target_site target_tenant target_workspace
|
|
56
|
+
locale view_context].freeze
|
|
57
|
+
|
|
58
|
+
self.hash_attrs = (base_attrs - %w[locale view_context]).map { |a| "#{a}_id" }
|
|
59
|
+
|
|
60
|
+
define_accessors
|
|
61
|
+
|
|
62
|
+
# rubocop:disable Metrics/MethodLength, Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/ParameterLists
|
|
63
|
+
def self.with_current(site:, tenant: nil, workspace: nil,
|
|
64
|
+
visitor: nil, login: nil,
|
|
65
|
+
request_id: nil, inbound_request: nil)
|
|
66
|
+
# do NOT store the original in *RequestStore*, as it gets cleared after each request and each que job!!!
|
|
67
|
+
Thread.current[:bp3_original_request_state] = current
|
|
68
|
+
# super
|
|
69
|
+
clear!
|
|
70
|
+
self.request_id = request_id || inbound_request&.rqid
|
|
71
|
+
self.inbound_request = inbound_request if inbound_request
|
|
72
|
+
self.current_visitor = visitor
|
|
73
|
+
self.current_site = site
|
|
74
|
+
self.current_tenant = tenant || site.default_tenant
|
|
75
|
+
self.current_workspace = workspace || site.default_workspace
|
|
76
|
+
case login
|
|
77
|
+
when Root
|
|
78
|
+
self.current_root = login
|
|
79
|
+
when Sites::Admin
|
|
80
|
+
self.current_admin = login
|
|
81
|
+
when Users::User
|
|
82
|
+
self.current_user = login
|
|
83
|
+
end
|
|
84
|
+
yield if block_given?
|
|
85
|
+
ensure
|
|
86
|
+
RequestStore.write(:bp3_request_state, Thread.current[:bp3_original_request_state])
|
|
87
|
+
Thread.current[:bp3_original_request_state] = nil
|
|
88
|
+
end
|
|
89
|
+
# rubocop:enable Metrics/MethodLength, Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/ParameterLists
|
|
90
|
+
end
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
You can then
|
|
94
|
+
- set global state members: `GlobalRequestState.current_site = current_site`
|
|
95
|
+
- access the global state: `GlobalRequestState.current`
|
|
96
|
+
- get the global state hash: `GlobalRequestState.to_hash`
|
|
97
|
+
- reset the global state: `GlobalRequestState.clear!`
|
|
98
|
+
|
|
99
|
+
## Development
|
|
100
|
+
|
|
101
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec`
|
|
102
|
+
to run the tests. You can also run `bin/console` for an interactive prompt that will allow
|
|
103
|
+
you to experiment.
|
|
104
|
+
|
|
105
|
+
To install this gem onto your local machine, run `rake install`. To release a new version,
|
|
106
|
+
update the version number in `version.rb`, and then run `rake release`, which will create
|
|
107
|
+
a git tag for the version, push git commits and the created tag, and push the `.gem` file
|
|
108
|
+
to [rubygems.org](https://rubygems.org).
|
|
109
|
+
|
|
110
|
+
## Testing
|
|
111
|
+
Run `rake` to run rspec tests and rubocop linting.
|
|
112
|
+
|
|
113
|
+
## Documentation
|
|
114
|
+
A `.yardopts` file is provided to support yard documentation.
|
|
115
|
+
|
|
116
|
+
## License
|
|
117
|
+
|
|
118
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'lib/bp3/request_state/version'
|
|
4
|
+
|
|
5
|
+
Gem::Specification.new do |spec|
|
|
6
|
+
spec.name = 'bp3-request_state'
|
|
7
|
+
spec.version = Bp3::RequestState::VERSION
|
|
8
|
+
spec.authors = ['Wim den Braven']
|
|
9
|
+
spec.email = ['wimdenbraven@persuavis.com']
|
|
10
|
+
|
|
11
|
+
spec.summary = 'bp3-request_state provides the Bp3::RequestState::Base class for BP3 (persuavis/black_phoebe_3).'
|
|
12
|
+
# spec.description = "TODO: Write a longer description or delete this line."
|
|
13
|
+
spec.homepage = 'https://www.black-phoebe.com'
|
|
14
|
+
spec.license = 'MIT'
|
|
15
|
+
spec.required_ruby_version = '>= 3.2.0'
|
|
16
|
+
|
|
17
|
+
spec.metadata['allowed_push_host'] = 'https://rubygems.org'
|
|
18
|
+
|
|
19
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
|
20
|
+
spec.metadata['source_code_uri'] = 'https://github.com/persuavis/bp3-request_state'
|
|
21
|
+
spec.metadata['changelog_uri'] = 'https://github.com/persuavis/bp3-request_state/blob/main/CHANGELOG.md'
|
|
22
|
+
|
|
23
|
+
# Specify which files should be added to the gem when it is released.
|
|
24
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
|
25
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
|
26
|
+
`git ls-files -z`.split("\x0").reject do |f|
|
|
27
|
+
(f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
spec.bindir = 'exe'
|
|
31
|
+
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
|
32
|
+
spec.require_paths = ['lib']
|
|
33
|
+
|
|
34
|
+
spec.add_dependency 'actionpack', ['>= 7.1.2', '< 8']
|
|
35
|
+
spec.add_dependency 'activesupport', ['>= 7.1.2', '< 8']
|
|
36
|
+
spec.add_dependency 'request_store', ['>= 1.5.1', '< 2']
|
|
37
|
+
|
|
38
|
+
spec.add_development_dependency 'byebug'
|
|
39
|
+
spec.add_development_dependency 'rake', '~> 13.0'
|
|
40
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
|
41
|
+
spec.add_development_dependency 'rubocop', '~> 1.21'
|
|
42
|
+
spec.add_development_dependency 'rubocop-rake', '~> 0.6'
|
|
43
|
+
spec.add_development_dependency 'rubocop-rspec', '~> 2.25'
|
|
44
|
+
|
|
45
|
+
# For more information and examples about making a new gem, check out our
|
|
46
|
+
# guide at: https://bundler.io/guides/creating_gem.html
|
|
47
|
+
spec.metadata['rubygems_mfa_required'] = 'true'
|
|
48
|
+
end
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'request_store'
|
|
4
|
+
require 'active_support'
|
|
5
|
+
require 'active_support/core_ext/date'
|
|
6
|
+
require 'active_support/core_ext/time'
|
|
7
|
+
require 'active_support/core_ext/date_time'
|
|
8
|
+
require 'active_support/core_ext/object'
|
|
9
|
+
require 'forwardable'
|
|
10
|
+
require 'ostruct'
|
|
11
|
+
|
|
12
|
+
module Bp3
|
|
13
|
+
module RequestState
|
|
14
|
+
# rubocop:disable Metrics/ClassLength, Style/ClassVars
|
|
15
|
+
class Base
|
|
16
|
+
extend Forwardable
|
|
17
|
+
cattr_accessor :hash_key_map, :base_attrs, :hash_attrs
|
|
18
|
+
|
|
19
|
+
@@hash_key_map =
|
|
20
|
+
{ current_site: 'Site',
|
|
21
|
+
current_tenant: 'Tenant',
|
|
22
|
+
current_workspace: 'Workspace',
|
|
23
|
+
current_user: 'User',
|
|
24
|
+
current_admin: 'Admin',
|
|
25
|
+
current_root: 'Root',
|
|
26
|
+
current_visitor: 'Visitor' }.freeze
|
|
27
|
+
|
|
28
|
+
@@base_attrs =
|
|
29
|
+
%w[current_site current_tenant current_workspace
|
|
30
|
+
current_user current_admin current_root current_visitor
|
|
31
|
+
locale view_context].freeze
|
|
32
|
+
|
|
33
|
+
@@hash_attrs =
|
|
34
|
+
%w[current_site_id current_tenant_id current_workspace_id
|
|
35
|
+
current_user_id current_admin_id current_root_id current_visitor_id].freeze
|
|
36
|
+
|
|
37
|
+
def self.clear!
|
|
38
|
+
RequestStore.delete(:bp3_request_state)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def self.current
|
|
42
|
+
RequestStore.fetch(:bp3_request_state) do
|
|
43
|
+
{ started: DateTime.current }
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def self.with_current(site:, tenant: nil, workspace: nil)
|
|
48
|
+
clear!
|
|
49
|
+
self.current_site = site
|
|
50
|
+
self.current_tenant = tenant || site.default_tenant
|
|
51
|
+
self.current_workspace = workspace || site.default_workspace
|
|
52
|
+
yield if block_given?
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def self.to_hash
|
|
56
|
+
{
|
|
57
|
+
request_id:,
|
|
58
|
+
started_string: started.utc.to_s,
|
|
59
|
+
locale: locale&.to_s
|
|
60
|
+
}.tap do |hash|
|
|
61
|
+
hash_attrs.each do |id_attr|
|
|
62
|
+
attr = id_attr.gsub('_id', '')
|
|
63
|
+
hash[id_attr] = send(id_attr) if respond_to?(attr)
|
|
64
|
+
end
|
|
65
|
+
end.stringify_keys
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def self.define_accessors
|
|
69
|
+
base_attrs.each do |attr|
|
|
70
|
+
define_accessors_for_one_attr(attr)
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
# current_login picks one of the logged-in user, site-admin or root.
|
|
74
|
+
# pick the one with least privileges first
|
|
75
|
+
define_singleton_method :current_login do
|
|
76
|
+
current_user || current_admin || current_root
|
|
77
|
+
end
|
|
78
|
+
define_method :current_login do
|
|
79
|
+
current_user || current_admin || current_root
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
define_singleton_method :highest_privilege do
|
|
83
|
+
# do not include current_visitor, as they are not logged in
|
|
84
|
+
current_root || current_admin || current_user # || current_visitor
|
|
85
|
+
end
|
|
86
|
+
define_method :highest_privilege do
|
|
87
|
+
# do not include current_visitor, as they are not logged in
|
|
88
|
+
current_root || current_admin || current_user # || current_visitor
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def self.from_hash(hash)
|
|
93
|
+
state = OpenStruct.new(hash) # rubocop:disable Style/OpenStructUse
|
|
94
|
+
fill_from_map(state)
|
|
95
|
+
fill_details(state)
|
|
96
|
+
self
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
def self.fill_from_map(state)
|
|
100
|
+
hash_key_map.each_key do |attr|
|
|
101
|
+
attr_with_id = "#{attr}_id"
|
|
102
|
+
fill_one_from_map(attr.to_sym, state.send(attr_with_id)) if state.send(attr_with_id)
|
|
103
|
+
end
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
def self.fill_one_from_map(attr, id)
|
|
107
|
+
klass = hash_key_map[attr.to_sym]&.constantize
|
|
108
|
+
writer = "#{attr}="
|
|
109
|
+
send(writer, klass.find_by(id:))
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
def self.fill_details(state)
|
|
113
|
+
self.request_id = state.request_id if state.request_id
|
|
114
|
+
self.started = state.started_string.present? ? DateTime.parse(state.started_string) : nil
|
|
115
|
+
self.locale = state.locale&.to_sym
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
def self.either_site
|
|
119
|
+
target_site || current_site
|
|
120
|
+
end
|
|
121
|
+
def_delegator self, :either_site
|
|
122
|
+
|
|
123
|
+
def self.either_site_id
|
|
124
|
+
target_site_id || current_site_id
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
def self.either_tenant
|
|
128
|
+
target_tenant || current_tenant
|
|
129
|
+
end
|
|
130
|
+
def_delegator self, :either_tenant
|
|
131
|
+
|
|
132
|
+
def self.either_tenant_id
|
|
133
|
+
target_tenant_id || current_tenant_id
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
def self.either_workspace
|
|
137
|
+
target_workspace || current_workspace
|
|
138
|
+
end
|
|
139
|
+
def_delegator self, :either_workspace
|
|
140
|
+
|
|
141
|
+
def self.either_workspace_id
|
|
142
|
+
target_workspace_id || current_workspace_id
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
def self.either_admin
|
|
146
|
+
current_root || current_admin
|
|
147
|
+
end
|
|
148
|
+
def_delegator self, :either_admin
|
|
149
|
+
|
|
150
|
+
def self.duration
|
|
151
|
+
(now - started) * 1.day # in seconds
|
|
152
|
+
end
|
|
153
|
+
def_delegator self, :duration
|
|
154
|
+
|
|
155
|
+
def self.now
|
|
156
|
+
DateTime.current
|
|
157
|
+
end
|
|
158
|
+
def_delegator self, :now
|
|
159
|
+
|
|
160
|
+
def self.started
|
|
161
|
+
current[:started] || now
|
|
162
|
+
end
|
|
163
|
+
def_delegator self, :started
|
|
164
|
+
|
|
165
|
+
def self.started=(time)
|
|
166
|
+
current[:started] = time || now
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
def self.request_id
|
|
170
|
+
current[:request_id]
|
|
171
|
+
end
|
|
172
|
+
def_delegator self, :request_id
|
|
173
|
+
|
|
174
|
+
def self.request_id=(rqid)
|
|
175
|
+
current[:request_id] = rqid
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
def self.define_accessors_for_one_attr(attr)
|
|
179
|
+
define_getter(attr)
|
|
180
|
+
define_setter(attr)
|
|
181
|
+
define_id_getter(attr)
|
|
182
|
+
define_id_setter(attr)
|
|
183
|
+
define_method(attr) do
|
|
184
|
+
self.class.current[attr.to_sym]
|
|
185
|
+
end
|
|
186
|
+
end
|
|
187
|
+
|
|
188
|
+
def self.define_getter(attr)
|
|
189
|
+
define_singleton_method(attr) do
|
|
190
|
+
current[attr.to_sym]
|
|
191
|
+
end
|
|
192
|
+
end
|
|
193
|
+
|
|
194
|
+
def self.define_setter(attr)
|
|
195
|
+
define_singleton_method("#{attr}=") do |obj|
|
|
196
|
+
current[attr.to_sym] = obj
|
|
197
|
+
end
|
|
198
|
+
end
|
|
199
|
+
|
|
200
|
+
def self.define_id_getter(attr)
|
|
201
|
+
define_singleton_method("#{attr}_id") do
|
|
202
|
+
current[attr.to_sym]&.id
|
|
203
|
+
end
|
|
204
|
+
end
|
|
205
|
+
|
|
206
|
+
def self.define_id_setter(attr)
|
|
207
|
+
define_singleton_method("#{attr}_id=") do |id|
|
|
208
|
+
current[attr.to_sym] = fill_one_from_map(attr.to_sym, id)
|
|
209
|
+
end
|
|
210
|
+
end
|
|
211
|
+
end
|
|
212
|
+
# rubocop:enable Metrics/ClassLength, Style/ClassVars
|
|
213
|
+
end
|
|
214
|
+
end
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Bp3
|
|
4
|
+
module RequestState
|
|
5
|
+
class Site
|
|
6
|
+
attr_reader :id
|
|
7
|
+
|
|
8
|
+
def initialize(id)
|
|
9
|
+
@id = id
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def self.find_by(id:)
|
|
13
|
+
new(id)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def ==(other)
|
|
17
|
+
id == other.id
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Bp3
|
|
4
|
+
module RequestState
|
|
5
|
+
class Test < Base
|
|
6
|
+
self.hash_key_map =
|
|
7
|
+
{
|
|
8
|
+
target_site: 'Bp3::RequestState::Site',
|
|
9
|
+
current_site: 'Bp3::RequestState::Site'
|
|
10
|
+
}.freeze
|
|
11
|
+
|
|
12
|
+
self.base_attrs =
|
|
13
|
+
%w[current_site target_site locale view_context].freeze
|
|
14
|
+
|
|
15
|
+
self.hash_attrs = (base_attrs - %w[locale view_context]).map { |a| "#{a}_id" }
|
|
16
|
+
|
|
17
|
+
define_accessors
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: bp3-request_state
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Wim den Braven
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2024-05-31 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: actionpack
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - ">="
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: 7.1.2
|
|
20
|
+
- - "<"
|
|
21
|
+
- !ruby/object:Gem::Version
|
|
22
|
+
version: '8'
|
|
23
|
+
type: :runtime
|
|
24
|
+
prerelease: false
|
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
26
|
+
requirements:
|
|
27
|
+
- - ">="
|
|
28
|
+
- !ruby/object:Gem::Version
|
|
29
|
+
version: 7.1.2
|
|
30
|
+
- - "<"
|
|
31
|
+
- !ruby/object:Gem::Version
|
|
32
|
+
version: '8'
|
|
33
|
+
- !ruby/object:Gem::Dependency
|
|
34
|
+
name: activesupport
|
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
|
36
|
+
requirements:
|
|
37
|
+
- - ">="
|
|
38
|
+
- !ruby/object:Gem::Version
|
|
39
|
+
version: 7.1.2
|
|
40
|
+
- - "<"
|
|
41
|
+
- !ruby/object:Gem::Version
|
|
42
|
+
version: '8'
|
|
43
|
+
type: :runtime
|
|
44
|
+
prerelease: false
|
|
45
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
46
|
+
requirements:
|
|
47
|
+
- - ">="
|
|
48
|
+
- !ruby/object:Gem::Version
|
|
49
|
+
version: 7.1.2
|
|
50
|
+
- - "<"
|
|
51
|
+
- !ruby/object:Gem::Version
|
|
52
|
+
version: '8'
|
|
53
|
+
- !ruby/object:Gem::Dependency
|
|
54
|
+
name: request_store
|
|
55
|
+
requirement: !ruby/object:Gem::Requirement
|
|
56
|
+
requirements:
|
|
57
|
+
- - ">="
|
|
58
|
+
- !ruby/object:Gem::Version
|
|
59
|
+
version: 1.5.1
|
|
60
|
+
- - "<"
|
|
61
|
+
- !ruby/object:Gem::Version
|
|
62
|
+
version: '2'
|
|
63
|
+
type: :runtime
|
|
64
|
+
prerelease: false
|
|
65
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
66
|
+
requirements:
|
|
67
|
+
- - ">="
|
|
68
|
+
- !ruby/object:Gem::Version
|
|
69
|
+
version: 1.5.1
|
|
70
|
+
- - "<"
|
|
71
|
+
- !ruby/object:Gem::Version
|
|
72
|
+
version: '2'
|
|
73
|
+
- !ruby/object:Gem::Dependency
|
|
74
|
+
name: byebug
|
|
75
|
+
requirement: !ruby/object:Gem::Requirement
|
|
76
|
+
requirements:
|
|
77
|
+
- - ">="
|
|
78
|
+
- !ruby/object:Gem::Version
|
|
79
|
+
version: '0'
|
|
80
|
+
type: :development
|
|
81
|
+
prerelease: false
|
|
82
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
83
|
+
requirements:
|
|
84
|
+
- - ">="
|
|
85
|
+
- !ruby/object:Gem::Version
|
|
86
|
+
version: '0'
|
|
87
|
+
- !ruby/object:Gem::Dependency
|
|
88
|
+
name: rake
|
|
89
|
+
requirement: !ruby/object:Gem::Requirement
|
|
90
|
+
requirements:
|
|
91
|
+
- - "~>"
|
|
92
|
+
- !ruby/object:Gem::Version
|
|
93
|
+
version: '13.0'
|
|
94
|
+
type: :development
|
|
95
|
+
prerelease: false
|
|
96
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
97
|
+
requirements:
|
|
98
|
+
- - "~>"
|
|
99
|
+
- !ruby/object:Gem::Version
|
|
100
|
+
version: '13.0'
|
|
101
|
+
- !ruby/object:Gem::Dependency
|
|
102
|
+
name: rspec
|
|
103
|
+
requirement: !ruby/object:Gem::Requirement
|
|
104
|
+
requirements:
|
|
105
|
+
- - "~>"
|
|
106
|
+
- !ruby/object:Gem::Version
|
|
107
|
+
version: '3.0'
|
|
108
|
+
type: :development
|
|
109
|
+
prerelease: false
|
|
110
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
111
|
+
requirements:
|
|
112
|
+
- - "~>"
|
|
113
|
+
- !ruby/object:Gem::Version
|
|
114
|
+
version: '3.0'
|
|
115
|
+
- !ruby/object:Gem::Dependency
|
|
116
|
+
name: rubocop
|
|
117
|
+
requirement: !ruby/object:Gem::Requirement
|
|
118
|
+
requirements:
|
|
119
|
+
- - "~>"
|
|
120
|
+
- !ruby/object:Gem::Version
|
|
121
|
+
version: '1.21'
|
|
122
|
+
type: :development
|
|
123
|
+
prerelease: false
|
|
124
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
125
|
+
requirements:
|
|
126
|
+
- - "~>"
|
|
127
|
+
- !ruby/object:Gem::Version
|
|
128
|
+
version: '1.21'
|
|
129
|
+
- !ruby/object:Gem::Dependency
|
|
130
|
+
name: rubocop-rake
|
|
131
|
+
requirement: !ruby/object:Gem::Requirement
|
|
132
|
+
requirements:
|
|
133
|
+
- - "~>"
|
|
134
|
+
- !ruby/object:Gem::Version
|
|
135
|
+
version: '0.6'
|
|
136
|
+
type: :development
|
|
137
|
+
prerelease: false
|
|
138
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
139
|
+
requirements:
|
|
140
|
+
- - "~>"
|
|
141
|
+
- !ruby/object:Gem::Version
|
|
142
|
+
version: '0.6'
|
|
143
|
+
- !ruby/object:Gem::Dependency
|
|
144
|
+
name: rubocop-rspec
|
|
145
|
+
requirement: !ruby/object:Gem::Requirement
|
|
146
|
+
requirements:
|
|
147
|
+
- - "~>"
|
|
148
|
+
- !ruby/object:Gem::Version
|
|
149
|
+
version: '2.25'
|
|
150
|
+
type: :development
|
|
151
|
+
prerelease: false
|
|
152
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
153
|
+
requirements:
|
|
154
|
+
- - "~>"
|
|
155
|
+
- !ruby/object:Gem::Version
|
|
156
|
+
version: '2.25'
|
|
157
|
+
description:
|
|
158
|
+
email:
|
|
159
|
+
- wimdenbraven@persuavis.com
|
|
160
|
+
executables: []
|
|
161
|
+
extensions: []
|
|
162
|
+
extra_rdoc_files: []
|
|
163
|
+
files:
|
|
164
|
+
- ".rspec"
|
|
165
|
+
- ".rubocop.yml"
|
|
166
|
+
- ".yardopts"
|
|
167
|
+
- CHANGELOG.md
|
|
168
|
+
- Gemfile
|
|
169
|
+
- Gemfile.lock
|
|
170
|
+
- LICENSE.txt
|
|
171
|
+
- README.md
|
|
172
|
+
- Rakefile
|
|
173
|
+
- bp3-request_state.gemspec
|
|
174
|
+
- lib/bp3-request_state.rb
|
|
175
|
+
- lib/bp3/request_state.rb
|
|
176
|
+
- lib/bp3/request_state/base.rb
|
|
177
|
+
- lib/bp3/request_state/site.rb
|
|
178
|
+
- lib/bp3/request_state/test.rb
|
|
179
|
+
- lib/bp3/request_state/version.rb
|
|
180
|
+
- sig/bp3/request_state.rbs
|
|
181
|
+
homepage: https://www.black-phoebe.com
|
|
182
|
+
licenses:
|
|
183
|
+
- MIT
|
|
184
|
+
metadata:
|
|
185
|
+
allowed_push_host: https://rubygems.org
|
|
186
|
+
homepage_uri: https://www.black-phoebe.com
|
|
187
|
+
source_code_uri: https://github.com/persuavis/bp3-request_state
|
|
188
|
+
changelog_uri: https://github.com/persuavis/bp3-request_state/blob/main/CHANGELOG.md
|
|
189
|
+
rubygems_mfa_required: 'true'
|
|
190
|
+
post_install_message:
|
|
191
|
+
rdoc_options: []
|
|
192
|
+
require_paths:
|
|
193
|
+
- lib
|
|
194
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
195
|
+
requirements:
|
|
196
|
+
- - ">="
|
|
197
|
+
- !ruby/object:Gem::Version
|
|
198
|
+
version: 3.2.0
|
|
199
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
200
|
+
requirements:
|
|
201
|
+
- - ">="
|
|
202
|
+
- !ruby/object:Gem::Version
|
|
203
|
+
version: '0'
|
|
204
|
+
requirements: []
|
|
205
|
+
rubygems_version: 3.5.11
|
|
206
|
+
signing_key:
|
|
207
|
+
specification_version: 4
|
|
208
|
+
summary: bp3-request_state provides the Bp3::RequestState::Base class for BP3 (persuavis/black_phoebe_3).
|
|
209
|
+
test_files: []
|