Repository navigation
fix(agent): apply the scope of collections reached through a relation #421
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| module ForestAdminAgent | ||
| module Utils | ||
| # The scope of a collection reached through a to-one relation empties the related record rather | ||
| # than filtering the root rows: a scope on `accounts` restricts which accounts are seen, never | ||
| # which `cards` exist. | ||
| class RelationScopes | ||
| RelationScope = Struct.new(:path, :collection, :scope) | ||
|
|
||
| def initialize(scopes = []) | ||
| @scopes = scopes | ||
| end | ||
|
|
||
| def with_scope_fields(projection) | ||
| scope_fields = @scopes.map { |relation| relation.scope.projection.nest(prefix: relation.path) } | ||
|
|
||
| projection.union(*scope_fields) | ||
| end | ||
|
|
||
| def redact(records, projection, timezone) | ||
| return records if @scopes.empty? | ||
|
|
||
| out_of_scope_paths = records.map { |record| out_of_scope_paths(record, timezone) } | ||
|
|
||
| projection.apply(records).each_with_index do |record, index| | ||
| out_of_scope_paths[index].each { |path| self.class.clear(record, path) } | ||
| end | ||
| end | ||
|
|
||
| def self.clear(record, path) | ||
| relation, rest = path.split(':', 2) | ||
|
|
||
| if rest.nil? | ||
| record[relation] = nil | ||
| elsif record[relation] | ||
| clear(record[relation], rest) | ||
| end | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def out_of_scope_paths(record, timezone) | ||
| stringified = ForestAdminDatasourceToolkit::Utils::HashHelper.convert_keys(record, :to_s) | ||
|
|
||
| @scopes.filter_map do |relation_scope| | ||
| related = ForestAdminDatasourceToolkit::Utils::Record.field_value(stringified, relation_scope.path) | ||
|
|
||
| relation_scope.path if related && !relation_scope.scope.match(related, relation_scope.collection, timezone) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟠 High A related record with a Also found in 1 other location(s)
🚀 Reply "fix it for me" or copy this AI Prompt for your agent: |
||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function with high complexity (count = 8): collect_relation_scopes [qlty:function-complexity]