Skip to content

Commit

Permalink
Use ActiveSupport.on_load to hook into ActiveRecord (#272)
Browse files Browse the repository at this point in the history
* Use `ActiveSupport.on_load` to hook into ActiveRecord
* Remove the need for a concern

In our case it’s fairly simple to just call `extend` to bring in the
class method. The instance methods are then included when
`acts_as_list` is called.

`active_record` also has `active_support` as a dependency so we don’t
need to declare that.
  • Loading branch information
brendon committed May 10, 2017
1 parent 84f742a commit 2f84821
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 64 deletions.
4 changes: 2 additions & 2 deletions acts_as_list.gemspec
Expand Up @@ -24,6 +24,6 @@ Gem::Specification.new do |s|


# Dependencies (installed via "bundle install")
s.add_dependency("activerecord", [">= 3.0"])
s.add_development_dependency("bundler", [">= 1.0.0"])
s.add_dependency "activerecord", ">= 3.0"
s.add_development_dependency "bundler", ">= 1.0.0"
end
5 changes: 3 additions & 2 deletions lib/acts_as_list.rb
@@ -1,9 +1,10 @@
require 'acts_as_list/active_record/acts/list'
require "acts_as_list/active_record/acts/list"
require "acts_as_list/active_record/acts/position_column_method_definer"
require "acts_as_list/active_record/acts/scope_method_definer"
require "acts_as_list/active_record/acts/top_of_list_method_definer"
require "acts_as_list/active_record/acts/add_new_at_method_definer"
require "acts_as_list/active_record/acts/aux_method_definer"
require "acts_as_list/active_record/acts/callback_definer"
require 'acts_as_list/active_record/acts/no_update'
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"
3 changes: 3 additions & 0 deletions lib/acts_as_list/active_record/acts/active_record.rb
@@ -0,0 +1,3 @@
ActiveSupport.on_load :active_record do
extend ActiveRecord::Acts::List::ClassMethods
end
123 changes: 63 additions & 60 deletions lib/acts_as_list/active_record/acts/list.rb
@@ -1,64 +1,66 @@
class << ActiveRecord::Base
# Configuration options are:
#
# * +column+ - specifies the column name to use for keeping the position integer (default: +position+)
# * +scope+ - restricts what is to be considered a list. Given a symbol, it'll attach <tt>_id</tt>
# (if it hasn't already been added) and use that as the foreign key restriction. It's also possible
# to give it an entire string that is interpolated if you need a tighter scope than just a foreign key.
# Example: <tt>acts_as_list scope: 'todo_list_id = #{todo_list_id} AND completed = 0'</tt>
# * +top_of_list+ - defines the integer used for the top of the list. Defaults to 1. Use 0 to make the collection
# act more like an array in its indexing.
# * +add_new_at+ - specifies whether objects get added to the :top or :bottom of the list. (default: +bottom+)
# `nil` will result in new items not being added to the list on create.
# * +sequential_updates+ - specifies whether insert_at should update objects positions during shuffling
# one by one to respect position column unique not null constraint.
# Defaults to true if position column has unique index, otherwise false.
# If constraint is <tt>deferrable initially deferred<tt>, overriding it with false will speed up insert_at.
def acts_as_list(options = {})
configuration = { column: "position", scope: "1 = 1", top_of_list: 1, add_new_at: :bottom }
configuration.update(options) if options.is_a?(Hash)

caller_class = self

ActiveRecord::Acts::List::PositionColumnMethodDefiner.call(caller_class, configuration[:column])
ActiveRecord::Acts::List::ScopeMethodDefiner.call(caller_class, configuration[:scope])
ActiveRecord::Acts::List::TopOfListMethodDefiner.call(caller_class, configuration[:top_of_list])
ActiveRecord::Acts::List::AddNewAtMethodDefiner.call(caller_class, configuration[:add_new_at])

ActiveRecord::Acts::List::AuxMethodDefiner.call(caller_class)
ActiveRecord::Acts::List::CallbackDefiner.call(caller_class, configuration[:add_new_at])
ActiveRecord::Acts::List::SequentialUpdatesMethodDefiner.call(caller_class, configuration[:column], configuration[:sequential_updates])

include ActiveRecord::Acts::List::InstanceMethods
include ActiveRecord::Acts::List::NoUpdate
end
end

module ActiveRecord
module Acts #:nodoc:
module List #:nodoc:
# This +acts_as+ extension provides the capabilities for sorting and reordering a number of objects in a list.
# The class that has this specified needs to have a +position+ column defined as an integer on
# the mapped database table.
#
# Todo list example:
#
# class TodoList < ActiveRecord::Base
# has_many :todo_items, order: "position"
# end
#
# class TodoItem < ActiveRecord::Base
# belongs_to :todo_list
# acts_as_list scope: :todo_list
# end
#
# todo_list.first.move_to_bottom
# todo_list.last.move_higher

# All the methods available to a record that has had <tt>acts_as_list</tt> specified. Each method works
# by assuming the object to be the item in the list, so <tt>chapter.move_lower</tt> would move that chapter
# lower in the list of all chapters. Likewise, <tt>chapter.first?</tt> would return +true+ if that chapter is
# the first in the list of all chapters.

module ClassMethods
# Configuration options are:
#
# * +column+ - specifies the column name to use for keeping the position integer (default: +position+)
# * +scope+ - restricts what is to be considered a list. Given a symbol, it'll attach <tt>_id</tt>
# (if it hasn't already been added) and use that as the foreign key restriction. It's also possible
# to give it an entire string that is interpolated if you need a tighter scope than just a foreign key.
# Example: <tt>acts_as_list scope: 'todo_list_id = #{todo_list_id} AND completed = 0'</tt>
# * +top_of_list+ - defines the integer used for the top of the list. Defaults to 1. Use 0 to make the collection
# act more like an array in its indexing.
# * +add_new_at+ - specifies whether objects get added to the :top or :bottom of the list. (default: +bottom+)
# `nil` will result in new items not being added to the list on create.
# * +sequential_updates+ - specifies whether insert_at should update objects positions during shuffling
# one by one to respect position column unique not null constraint.
# Defaults to true if position column has unique index, otherwise false.
# If constraint is <tt>deferrable initially deferred<tt>, overriding it with false will speed up insert_at.
def acts_as_list(options = {})
configuration = { column: "position", scope: "1 = 1", top_of_list: 1, add_new_at: :bottom }
configuration.update(options) if options.is_a?(Hash)

caller_class = self

ActiveRecord::Acts::List::PositionColumnMethodDefiner.call(caller_class, configuration[:column])
ActiveRecord::Acts::List::ScopeMethodDefiner.call(caller_class, configuration[:scope])
ActiveRecord::Acts::List::TopOfListMethodDefiner.call(caller_class, configuration[:top_of_list])
ActiveRecord::Acts::List::AddNewAtMethodDefiner.call(caller_class, configuration[:add_new_at])

ActiveRecord::Acts::List::AuxMethodDefiner.call(caller_class)
ActiveRecord::Acts::List::CallbackDefiner.call(caller_class, configuration[:add_new_at])
ActiveRecord::Acts::List::SequentialUpdatesMethodDefiner.call(caller_class, configuration[:column], configuration[:sequential_updates])

include ActiveRecord::Acts::List::InstanceMethods
include ActiveRecord::Acts::List::NoUpdate
end

# This +acts_as+ extension provides the capabilities for sorting and reordering a number of objects in a list.
# The class that has this specified needs to have a +position+ column defined as an integer on
# the mapped database table.
#
# Todo list example:
#
# class TodoList < ActiveRecord::Base
# has_many :todo_items, order: "position"
# end
#
# class TodoItem < ActiveRecord::Base
# belongs_to :todo_list
# acts_as_list scope: :todo_list
# end
#
# todo_list.first.move_to_bottom
# todo_list.last.move_higher

# All the methods available to a record that has had <tt>acts_as_list</tt> specified. Each method works
# by assuming the object to be the item in the list, so <tt>chapter.move_lower</tt> would move that chapter
# lower in the list of all chapters. Likewise, <tt>chapter.first?</tt> would return +true+ if that chapter is
# the first in the list of all chapters.
end

module InstanceMethods
# Insert the item at the given position (defaults to the top position of 1).
def insert_at(position = acts_as_list_top)
Expand Down Expand Up @@ -376,7 +378,7 @@ def shuffle_positions_on_intermediate_items(old_position, new_position, avoid_id
end
end
end

def insert_at_position(position)
return set_list_position(position) if new_record?
with_lock do
Expand Down Expand Up @@ -408,7 +410,7 @@ def update_positions

def position_before_save
if ActiveRecord::VERSION::MAJOR == 5 && ActiveRecord::VERSION::MINOR >= 1 ||
ActiveRecord::VERSION::MAJOR > 5
ActiveRecord::VERSION::MAJOR > 5

send("#{position_column}_before_last_save")
else
Expand Down Expand Up @@ -460,6 +462,7 @@ def quoted_position_column_with_table_name
@_quoted_position_column_with_table_name ||= "#{quoted_table_name}.#{quoted_position_column}"
end
end

end
end
end

0 comments on commit 2f84821

Please sign in to comment.