Skip to content

Commit f3f785b

Browse files
authored
Expose configuration via class-level method (#447)
1 parent 4742e51 commit f3f785b

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## Unreleased
88

9+
- Expose configuration via class-level method (acts_as_list_options) [\#447](https://github.com/brendon/acts_as_list/pull/447) ([anthony0030])
10+
911
## v1.2.5 - 2025-10-21
1012

1113
- Fix crash when deleting an association with composite primary keys [\#450](https://github.com/brendon/acts_as_list/pull/450) ([smathieu])

lib/acts_as_list/active_record/acts/list.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ def acts_as_list(options = {})
2727
configuration = { column: "position", scope: "1 = 1", top_of_list: 1, add_new_at: :bottom, touch_on_update: true }
2828
configuration.update(options) if options.is_a?(Hash)
2929

30+
# * Expose configuration via class-level method
31+
define_singleton_method(:acts_as_list_options) do
32+
configuration.dup.freeze
33+
end
34+
3035
caller_class = self
3136

3237
ActiveRecord::Acts::List::PositionColumnMethodDefiner.call(caller_class, configuration[:column], configuration[:touch_on_update])

test/test_acts_as_list_options.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# frozen_string_literal: true
2+
3+
require 'helper'
4+
5+
class ActsAsListOptionsTest
6+
def setup
7+
setup_db
8+
end
9+
10+
def test_acts_as_list_options_returns_the_macro_configuration # rubocop:disable Metrics/MethodLength
11+
klass = Class.new(ActiveRecord::Base) do
12+
self.table_name = 'mixins'
13+
acts_as_list(
14+
column: :pos,
15+
scope: %i[parent_id parent_type],
16+
add_new_at: :top,
17+
top_of_list: 0,
18+
touch_on_update: false
19+
)
20+
end
21+
22+
opts = klass.acts_as_list_options
23+
24+
assert_equal %i[parent_id parent_type], opts[:scope]
25+
assert_equal :pos, opts[:column]
26+
assert_equal :top, opts[:add_new_at]
27+
assert_equal 0, opts[:top_of_list]
28+
assert_equal false, opts[:touch_on_update]
29+
assert_equal true, opts.frozen?
30+
end
31+
end

0 commit comments

Comments
 (0)