How be9/acl9 works
Project type and purpose
• This is an open-source RubyGem that integrates into Rails applications to provide a role-based authorization DSL.
• Its purpose is to let Rails controllers declare access-control rules in a clear, testable DSL and to manage role assignments on subjects (e.g., users) and objects (e.g., models).
File structure
• Top‐level:
• gemspec, Gemfile, bin/ (CLI shims), Rakefile, Appraisals (matrix testing), .github/workflows
• lib/acl9 – core implementation: controller and model extensions, helpers, prepositions, version
• lib/generators/acl9/setup – Rails generator code and templates to scaffold migrations & models
• test/ – unit tests for controller_extensions and model_extensions, plus a "dummy" Rails app under test/dummy demonstrating integration
• Patterns and conventions:
• Uses Rails “concern”-style mixins for extending ActionController (ControllerExtensions) and ActiveRecord models (ModelExtensions)
• Provides a DSL builder (dsl_base.rb) for access_control blocks
• Offers configuration via Acl9.config with default_* settings
• Follows MVC extension pattern—no standalone services or microservices
Key insights from the README
• Two subsystems:
a. Access Control Subsystem (controller DSL: allow/deny, actions, conditions)
b. Role Subsystem (user.has_role!, has_no_role!, join tables for roles & role_objects)
• Provides a Rails generator (acl9:setup) to create migrations and models
• Configurable defaults for class names, association names, subject methods, global-role protection, name normalization
• Rails App (host)
• Acl9 Gem
– ControllerExtensions (DSL parser & before_action filter)
– ModelExtensions (has_role? / has_role! on subject & object)
– Helpers (view‐level methods)
– Prepositions (DSL support)
– Configuration module (Acl9.config)
– Generators (acl9:setup → migrations + scaffolding)
• ActiveRecord Models
– Role
– RoleObject (join model)
– Subject model (e.g. User)
– Object models (e.g. School, Department)
• Database (tables: roles, role_objects, plus application tables)
• Test Dummy App (example integration and functional tests)
1. HTTP Request → Rails router → Controller (extended by ControllerExtensions)
2. ControllerExtensions runs access_control rules → invokes ModelExtensions on current_user (subject)
3. has_role? queries Role & RoleObject tables via ActiveRecord
4. Decision (allow/deny) → Controller action or access denied response
5. Generators (dev‐time): CLI Thor tasks → templates → create migration & model files in host app
6. Configuration flows: initializers → Acl9.config → affects runtime behavior
7. Tests: dummy app bootstraps Rails + Acl9 to validate behavior end-to-end
c. Architectural patterns and principles:
• Plugin/extension pattern: injects behavior via mixins into Rails MVC layers
• DSL Builder pattern for declarative access_control rules
• Convention over configuration: sensible defaults, overridable via config or model/controller macros
• Modular subsystems (Access Control vs Role Management)
d. Key technologies and libraries:
• Ruby, Bundler
• Rails (ActiveSupport, ActiveRecord, ActionController)
• Thor (for generators)
• Rake (tasks and test runner)
• Appraisals (multi‐Rails version testing)