-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
plural_association_methods
plugin with add_*s
and remove_*s
…
… methods
- Loading branch information
1 parent
5938c92
commit 01855a0
Showing
2 changed files
with
908 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# frozen-string-literal: true | ||
|
||
module Sequel | ||
module Plugins | ||
module PluralAssociationMethods | ||
# The `plural_association_methods` plugin adds `Sequel::Model#add_*s` and | ||
# `Sequel::Model#remove_*s` methods for handy batch operations. Example: | ||
# | ||
# artist.albums # => [album1] | ||
# artist.add_albums([album2, album3]) | ||
# artist.albums # => [album1, album2, album3] | ||
# artist.remove_albums([album3, album1]) | ||
# artist.albums # => [album2] | ||
# | ||
# It can handle situations with prymary keys, composite keys, redefined adders and removers, | ||
# additional arguments, etc. - everything like with singular methods. | ||
# | ||
# It also saves the reciprocal's state: | ||
# | ||
# artist.remove_albums([album3, album1]) | ||
# album1.artist # => nil | ||
# album3.artist # => nil | ||
# | ||
# There is one caveat; it runs with separate queries for adder and remover logic preserve. | ||
# | ||
# Usage: | ||
# | ||
# # Add plural association methods for all model subclass instances | ||
# # (called before associations defining) | ||
# Sequel::Model.plugin :plural_association_methods | ||
# | ||
# # Add plural association methods for the `Artist` class | ||
# Album.plugin :plural_association_methods | ||
module ClassMethods | ||
# Define the plural association instance methods. | ||
def def_association_instance_methods(opts) | ||
super | ||
|
||
if opts[:adder] | ||
association_module_def(:"add_#{opts[:name]}", opts) do |objs, *args| | ||
objs.map { |obj| send(opts[:add_method], obj, *args) }.compact | ||
end | ||
end | ||
|
||
if opts[:remover] | ||
association_module_def(:"remove_#{opts[:name]}", opts) do |objs, *args| | ||
objs.map { |obj| send(opts[:remove_method], obj, *args) }.compact | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.