A Ruby gem that maps incoming controller parameters to named scopes for dynamic resource filtering in Rails applications.
HasScope is a Ruby gem that dynamically applies named ActiveRecord scopes to resources based on incoming parameters, typically from controller requests. It solves the problem of building flexible filtering interfaces by automatically translating query parameters into database queries without manual parameter handling.
Rails developers building APIs, admin panels, or search interfaces that require dynamic filtering of ActiveRecord models based on user-provided parameters.
Developers choose HasScope because it reduces boilerplate code for filtering logic, provides a clean separation between parameter handling and business logic, and offers both Rails controller integration and standalone usage for maximum flexibility.
Map incoming controller parameters to named scopes in your resources
Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.
Maps query parameters directly to ActiveRecord scopes with minimal code, as shown in controller examples where ?featured=true automatically applies the featured scope.
Supports boolean, hash, and array parameter types with control options like :only, :except, :if, and :unless, allowing precise scope application based on conditions.
Can be included in plain Ruby objects (POROs) for reuse outside Rails controllers, enabling filtering logic in service objects or background jobs, as demonstrated in the GraduationsSearchQuery example.
Provides a current_scopes helper to easily check applied scopes, useful for debugging or conditional logic in views, returning a hash like { featured: true, by_degree: 'phd' }.
Tightly coupled to ActiveRecord scopes, making it unsuitable for projects using other ORMs like Sequel or raw SQL, limiting its use to Rails-centric applications.
For scopes with keyword arguments or custom logic, the block syntax becomes verbose and less intuitive, as seen in the keyword arguments example requiring manual scope calling.
Requires all filtering logic to be encapsulated in named model scopes upfront, which can be restrictive for highly dynamic or evolving query requirements not anticipated during development.