Skip to content

Commit cdaf42a

Browse files
committed
add todos
todos todo TODOs todos
1 parent d6e844b commit cdaf42a

4 files changed

Lines changed: 18 additions & 13 deletions

File tree

README.md

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ Rabarber::Role.all_names # All roles grouped by context
136136
# Get users assigned to a role
137137
Rabarber::Role.assignees(:admin)
138138
```
139+
<!-- TODO: maybe all these methods should be namespaced under Rabarber, i.e. Rabarber.add_role to hide existence of the model -->
140+
<!-- TODO: in this case names and all_names will become Rabarber.roles and Rabarber.all_roles respectively, seems more readable -->
139141

140142
## Controller Authorization
141143

@@ -181,11 +183,6 @@ class TicketsController < ApplicationController
181183
def index
182184
# Accessible to admin, manager, and support roles
183185
end
184-
185-
grant_access action: :destroy, roles: :owner, context: -> { Ticket.find(params[:id]) }
186-
def destroy
187-
# Accessible to admin and owner of the ticket
188-
end
189186
end
190187
```
191188

@@ -244,9 +241,7 @@ class ApplicationController < ActionController::Base
244241
private
245242

246243
def when_unauthorized
247-
# Default behavior: redirect back (HTML) or return 403 (other formats)
248-
# Custom behavior example:
249-
head :not_found # Hide existence of protected resources
244+
head :not_found # Custom behavior to hide existence of protected resources
250245
end
251246
end
252247
```
@@ -288,7 +283,7 @@ end
288283

289284
## Multi-tenancy / Context
290285

291-
All Rabarber methods accept a `context` parameter, allowing you to work with roles within specific scopes rather than globally.
286+
All Rabarber methods accept a `context` parameter, allowing you to work with roles within specific scopes. By default, context is `nil`, meaning roles are global.
292287

293288
### Contextual Role Assignment
294289

@@ -304,6 +299,9 @@ user.assign_roles(:admin, context: Project)
304299
user.has_role?(:owner, context: project)
305300
user.has_role?(:admin, context: Project)
306301

302+
# Revoke roles within a specific context
303+
user.revoke_roles(:owner, context: project)
304+
307305
# Get roles within context
308306
user.roles(context: project)
309307
Rabarber::Role.names(context: Project)
@@ -341,11 +339,11 @@ end
341339
Handle context changes when models are renamed or removed. These are irreversible data migrations.
342340

343341
```rb
344-
# Rename a context class (e.g., when you rename your Project model to Campaign)
345-
migrate_authorization_context!("Project", "Campaign")
342+
# Rename a context class (e.g., when you rename your Ticket model to Task)
343+
migrate_authorization_context!("Ticket", "Task")
346344

347-
# Remove orphaned context data (e.g., when you delete a model entirely)
348-
delete_authorization_context!("DeletedModel")
345+
# Remove orphaned context data (e.g., when you delete Ticket model entirely)
346+
delete_authorization_context!("Ticket")
349347
```
350348

351349
## View Helpers

lib/rabarber/core/integrity_checker.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ def check_for_missing_class_context
2020
end
2121
end
2222

23+
# TODO: maybe context pruning should be a part of public API
24+
# TODO: although it would be cool if orphaned instance context roles could be deleted automatically along with the context itself
25+
# TODO: maybe integrity checker is not needed at all, and developers should handle all by themselves
2326
def prune_missing_instance_context
2427
ids = Rabarber::Role.where.not(context_id: nil).includes(:context).filter_map do |role|
2528
role.context

lib/rabarber/inputs.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ def process(value, as:, optional: false, error: Rabarber::InvalidArgumentError,
3535

3636
result = checker[value]
3737

38+
# TODO: intuitively doesn't feel right
3839
[:role_context, :authorization_context].include?(as) ? resolve_context(result) : result
3940
rescue Dry::Types::CoercionError => e
4041
raise error, message || e.message
@@ -44,13 +45,15 @@ def process(value, as:, optional: false, error: Rabarber::InvalidArgumentError,
4445

4546
def type_for(name) = self::TYPES.fetch(name)
4647

48+
# TODO: there should be some separate context resolver or smth
4749
def resolve_context(value)
4850
case value
4951
when nil
5052
{ context_type: nil, context_id: nil }
5153
when Class
5254
{ context_type: value.to_s, context_id: nil }
5355
when ActiveRecord::Base
56+
# TODO: this should be included in the type definition somehow, or maybe dry-validation?
5457
raise Dry::Types::CoercionError, "instance context not persisted" unless value.persisted?
5558

5659
{ context_type: value.class.to_s, context_id: value.public_send(value.class.primary_key) }

lib/rabarber/models/role.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ def assignees(name, context: nil)
6969
private
7070

7171
def delete_roleables_cache(role, context:)
72+
# TODO: maybe in_batches is not really needed
7273
role.roleables.in_batches(of: 1000) do |batch|
7374
Rabarber::Core::Cache.delete(*batch.pluck(:id).flat_map { [[_1, context], [_1, :all]] })
7475
end

0 commit comments

Comments
 (0)