Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/kanety/activerecord_nested_scope

An ActiveRecord extension to build nested scopes through pre-defined associations
https://github.com/kanety/activerecord_nested_scope

Last synced: 2 months ago
JSON representation

An ActiveRecord extension to build nested scopes through pre-defined associations

Awesome Lists containing this project

README

        

# ActiveRecordNestedScope

An ActiveRecord extension to build nested scopes through pre-defined associations.

## Dependencies

* ruby 2.4+
* activerecord 5.0+
* activesupport 5.0+

## Installation

Add this line to your application's Gemfile:

```ruby
gem 'activerecord_nested_scope'
```

Then execute:

$ bundle

## Usage

For example, define `in_group` scope using nested_scope as follows:

```ruby
class Group < ActiveRecord::Base
nested_scope :in_group # root scope
...
end

class User < ActiveRecord::Base
belongs_to :group
nested_scope :in_group, through: :group # User belongs to Group
...
end

class UserConfig < ActiveRecord::Base
belongs_to :user
nested_scope :in_group, through: :user # UserConfig belongs to Group through User
...
end
```

`in_group` scope generates SQL as follows:

```ruby
User.in_group(id: 1)
#=> SELECT "users".* FROM "users" WHERE "users"."group_id" IN (
# SELECT "groups"."id" FROM "groups" WHERE "groups"."id" = 1)

UserConfig.in_group(id: 1)
#=> SELECT "user_configs".* FROM "user_configs" WHERE "user_configs"."user_id" IN (
# SELECT "users"."id" FROM "users" WHERE "users"."group_id" IN (
# SELECT "groups"."id" FROM "groups" WHERE "groups"."id" = 1))
```

### Polymorphic association

If you define a polymorphic association,
`in_group` generates union of subqueries for each polymorpchic type as follows:

```ruby
class Polymorphism < ActiveRecord::Base
belongs_to :record, polymorphic: true
nested_scope :in_group, through: :record
end

Polymorphism.in_group(id: 1)
#=> SELECT "polymorphisms"."record_type" FROM "polymorphisms" GROUP BY "polymorphisms"."record_type"
#=> SELECT "polymorphisms".* FROM (
# SELECT "polymorphisms".* FROM "polymorphisms" WHERE "polymorphisms"."record_type" = 'Group' AND "polymorphisms"."record_id" IN (SELECT "groups"."id" FROM "groups" WHERE "groups"."id" = 1) UNION
# SELECT "polymorphisms".* FROM "polymorphisms" WHERE "polymorphisms"."record_type" = 'User' AND "polymorphisms"."record_id" IN (SELECT "users"."id" FROM "users" WHERE "users"."group_id" IN (SELECT "groups"."id" FROM "groups" WHERE "groups"."id" = 1)) UNION
# SELECT "polymorphisms".* FROM "polymorphisms" WHERE "polymorphisms"."record_type" = 'UserConfig' AND "polymorphisms"."record_id" IN (SELECT "user_configs"."id" FROM "user_configs" WHERE "user_configs"."user_id" IN (SELECT "users"."id" FROM "users" WHERE "users"."group_id" IN (SELECT "groups"."id" FROM "groups" WHERE "groups"."id" = 1)))
# ) AS polymorphisms
```

Note that the first SQL is executed to load `record_type`,
and the second SQL is built using loaded `record_type` variations.

### Scope arguments

```ruby
# pass a integer
User.in_group(1)

# pass an array
User.in_group([1, 2, 3])

# pass a hash
User.in_group(id: 1)

# pass an AR relation
User.in_group(Group.where(id: 1))
```

## Contributing

Bug reports and pull requests are welcome at https://github.com/kanety/activerecord_nested_scope.

## License

The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).