prioritized_callbacks 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 920ddd09c3a79727ca904220114d5ed4df1f2246422bf708b8ccdce47534dab1
4
- data.tar.gz: 5d1eccf1d4632e79130cd9e388bde7f69b32e88dae781a79161b721e05adb938
3
+ metadata.gz: f503f82601d43373b1d6e3d8462762a03f0ceb789a0989df6c1f8931e7aec5a4
4
+ data.tar.gz: ff6063eb44c68edd45162cf47a4a8adb97af82e3193c45ffc28f991f777f51ef
5
5
  SHA512:
6
- metadata.gz: 0d341ef80863b66a8c31cce8ce407f25475d88426b25c5aa0e4118742173db5939f6e12b5e82844d8242989d79d1ccab5244ce199ce99eb3af8f0577c693a6b8
7
- data.tar.gz: 6909e13c7d26c8f4edcce63ed9a6a72530513e0ab3e8a8a9de3c32e15601fef64763aa512a0257db0401b906fe1b60f20ace242b43066bed4adade2f2262354e
6
+ metadata.gz: 5ae190caa8fb5d8875cf5da6f95f947eda35ec7b44661795a88d974875ad7ae0cf13f11384c36ef3c606b650e8d49848742520a4cb394b56fb7b159c2157f272
7
+ data.tar.gz: f3408a104daee2f7827e187dc49d849d60f7d590967d5a179a52ed217b64baa0195aae00b552515df2a1a2daac6be4e246a58e991aef9f9250e81e149e2fd63a
data/README.md CHANGED
@@ -6,6 +6,16 @@ which are executed last despite of the order in which they were initially added.
6
6
 
7
7
  **WARNING!** Do not use this gem unless you know that's the only way.
8
8
 
9
+ ## Install
10
+
11
+ Add the following line to Gemfile:
12
+
13
+ `gem 'prioritized_callbacks'`
14
+
15
+ ## Compatibility
16
+
17
+ It is checked against all Rails versions >= 5.0 and Ruby >= 2.7
18
+
9
19
  ## Usage
10
20
 
11
21
  ### Using pure ActiveSupport::Callbacks:
@@ -110,4 +120,4 @@ append_save_order :my_order, before: :default
110
120
 
111
121
  You can specify `:before` or `:after` to specify a place where new order item should be inserted.
112
122
 
113
- 2) To set priority for specific callback, you should add `:priority` option to `before_save` or `after_save`.
123
+ 2) To set priority for specific callback, you should add `:priority` option to `before_save` or `after_save`.
@@ -4,20 +4,24 @@ module PrioritizedCallbacks
4
4
  def set_callbacks_order(name, order_items)
5
5
  chain = get_callbacks(name)
6
6
  raise ArgumentError, "Callbacks for #{name} are not defined" unless chain
7
+ order_items = order_items.map(&:to_sym)
7
8
  set_callbacks name, chain.copy_with_new_order(order_items)
8
9
  end
9
10
 
10
11
  def append_callbacks_order(name, order_items, options = {})
11
12
  chain = get_callbacks(name)
12
13
  raise ArgumentError, "Callbacks for #{name} are not defined" unless chain
13
- order = chain.config[:order]
14
- insert_pos = order.size
15
- if (before = options[:before])
14
+ order = chain.config[:order].dup
15
+ insert_pos = order.size
16
+ order_items = order_items.map(&:to_sym)
17
+ existing_items = order & order_items
18
+ raise ArgumentError, "Order item(s) #{existing_items.map(&:inspect).join(', ')} already exist for #{name} callbacks" if existing_items.any?
19
+ if (before = options[:before]&.to_sym)
16
20
  insert_pos = order.index(before)
17
- raise ArgumentError, "Order '#{before}' is not defined in #{name} callbacks" unless insert_pos
18
- elsif (after = options[:after])
21
+ raise ArgumentError, "Order item #{before.inspect} is not defined for #{name} callbacks" unless insert_pos
22
+ elsif (after = options[:after]&.to_sym)
19
23
  insert_pos = order.index(after)
20
- raise ArgumentError, "Order '#{after}' is not defined in #{name} callbacks" unless insert_pos
24
+ raise ArgumentError, "Order item #{after.inspect} is not defined for #{name} callbacks" unless insert_pos
21
25
  insert_pos += 1
22
26
  end
23
27
  order.insert insert_pos, *order_items
@@ -1,3 +1,3 @@
1
1
  module PrioritizedCallbacks
2
- VERSION = '0.0.3'
2
+ VERSION = '0.0.4'
3
3
  end
@@ -137,4 +137,20 @@ RSpec.describe ActiveRecord::Callbacks do
137
137
  it "has proper order when new order item is appended to the beginning" do
138
138
  expect(record6.text).to eq('Y1023X4956')
139
139
  end
140
+
141
+ it "should fail when existing order item is appended" do
142
+ expect do
143
+ class FailingRecord < Record2
144
+ append_save_order :first, :second, :last
145
+ end
146
+ end.to raise_error(ArgumentError, 'Order item(s) :first, :last already exist for save callbacks')
147
+ end
148
+
149
+ it "should fail when order item doesn't exist" do
150
+ expect do
151
+ class FailingRecord < Record2
152
+ append_save_order :second, before: :nonexisting
153
+ end
154
+ end.to raise_error(ArgumentError, 'Order item :nonexisting is not defined for save callbacks')
155
+ end
140
156
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: prioritized_callbacks
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Anton Argirov