Skip to content

Commit

Permalink
Add AdvisoryLock module
Browse files Browse the repository at this point in the history
  • Loading branch information
Silex committed Oct 30, 2018
1 parent 28b2db6 commit 83ed136
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 0 deletions.
1 change: 1 addition & 0 deletions acts_as_list.gemspec
Expand Up @@ -25,5 +25,6 @@ Gem::Specification.new do |s|

# Dependencies (installed via "bundle install")
s.add_dependency "activerecord", ">= 4.2"
s.add_dependency "with_advisory_lock", "~> 4.0"
s.add_development_dependency "bundler", ">= 1.0.0"
end
1 change: 1 addition & 0 deletions lib/acts_as_list.rb
Expand Up @@ -10,3 +10,4 @@
require "acts_as_list/active_record/acts/no_update"
require "acts_as_list/active_record/acts/sequential_updates_method_definer"
require "acts_as_list/active_record/acts/active_record"
require "acts_as_list/active_record/acts/advisory_lock"
43 changes: 43 additions & 0 deletions lib/acts_as_list/active_record/acts/advisory_lock.rb
@@ -0,0 +1,43 @@
# frozen_string_literal: true

require 'with_advisory_lock'

module ActiveRecord
module Acts #:nodoc:
module List #:nodoc:
module AdvisoryLock #:nodoc:
def self.included(base)
base.prepend(InstanceMethods)
end

def self.acts_as_list_methods
ActiveRecord::Acts::List::InstanceMethods.public_instance_methods
end

def self.acts_as_list_lockable_methods
acts_as_list_methods.grep(/^(insert_|move_|remove_|set_|(dec|inc)rement_)/)
end

module InstanceMethods
AdvisoryLock.acts_as_list_lockable_methods.each do |m|
define_method(m) do |*args|
with_table_lock do
super(*args)
end
end
end

def advisory_lock_name
format('lock-%s', acts_as_list_class.to_s.downcase)
end

def with_table_lock
acts_as_list_class.with_advisory_lock(advisory_lock_name) do
yield
end
end
end
end
end
end
end
1 change: 1 addition & 0 deletions lib/acts_as_list/active_record/acts/list.rb
Expand Up @@ -37,6 +37,7 @@ def acts_as_list(options = {})

include ActiveRecord::Acts::List::InstanceMethods
include ActiveRecord::Acts::List::NoUpdate
include ActiveRecord::Acts::List::AdvisoryLock if configuration[:advisory_lock]
end

# This +acts_as+ extension provides the capabilities for sorting and reordering a number of objects in a list.
Expand Down

0 comments on commit 83ed136

Please sign in to comment.