Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add composite primary key support #430

Merged
merged 12 commits into from
Jun 2, 2024
15 changes: 10 additions & 5 deletions lib/acts_as_list/active_record/acts/list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ def higher_items(limit=nil)
limit ||= acts_as_list_list.count
acts_as_list_list.
where("#{quoted_position_column_with_table_name} <= ?", current_position).
where("#{quoted_table_name}.#{self.class.primary_key} != ?", self.send(self.class.primary_key)).
where.not(primary_key_condition).
reorder(acts_as_list_order_argument(:desc)).
limit(limit)
end
Expand All @@ -188,7 +188,7 @@ def lower_items(limit=nil)
limit ||= acts_as_list_list.count
acts_as_list_list.
where("#{quoted_position_column_with_table_name} >= ?", current_position).
where("#{quoted_table_name}.#{self.class.primary_key} != ?", self.send(self.class.primary_key)).
where.not(primary_key_condition).
reorder(acts_as_list_order_argument(:asc)).
limit(limit)
end
Expand Down Expand Up @@ -273,7 +273,7 @@ def bottom_item(except = nil)
scope = acts_as_list_list

if except
scope = scope.where("#{quoted_table_name}.#{self.class.primary_key} != ?", except.id)
scope = scope.where.not(primary_key_condition(except.id))
end

scope.in_list.reorder(acts_as_list_order_argument(:desc)).first
Expand All @@ -300,7 +300,7 @@ def increment_positions_on_lower_items(position, avoid_id = nil)
scope = acts_as_list_list

if avoid_id
scope = scope.where("#{quoted_table_name}.#{self.class.primary_key} != ?", avoid_id)
scope = scope.where.not(primary_key_condition(avoid_id))
end

if sequential_updates?
Expand Down Expand Up @@ -341,7 +341,7 @@ def shuffle_positions_on_intermediate_items(old_position, new_position, avoid_id
scope = acts_as_list_list

if avoid_id
scope = scope.where("#{quoted_table_name}.#{self.class.primary_key} != ?", avoid_id)
scope = scope.where.not(primary_key_condition(avoid_id))
end

if old_position < new_position
Expand Down Expand Up @@ -480,6 +480,11 @@ def active_record_version_is?(version_requirement)
version = Gem.loaded_specs['activerecord'].version
requirement.satisfied_by?(version)
end

def primary_key_condition(id = nil)
primary_keys = Array.wrap(self.class.primary_key)
id ? primary_keys.zip(Array.wrap(id)).to_h : slice(*primary_keys)
end
brendon marked this conversation as resolved.
Show resolved Hide resolved
end

end
Expand Down
Loading