From 3ee92f7c19a987c1ce7e264c8032d9c1fd129cd6 Mon Sep 17 00:00:00 2001 From: Alexandr Borisov Date: Thu, 24 Dec 2015 10:47:09 +0300 Subject: [PATCH] added touch option --- README.md | 4 ++++ README.ru.md | 5 +++++ lib/activerecord/sortable/acts_as_sortable.rb | 5 ++++- .../acts_as_sortable/instance_methods.rb | 7 ++++++- spec/spec_helper.rb | 1 - spec/support/activerecord_sortable.rb | 16 ++++++++++++++-- 6 files changed, 33 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index b1b5fa3..a321f4d 100644 --- a/README.md +++ b/README.md @@ -146,6 +146,10 @@ class Thing < ActiveRecord::Base # integer column to specify order # position by default config[:position_column] = :position + + # touch other members of relation on prepend + # true by default + config[:touch] = true end end ``` diff --git a/README.ru.md b/README.ru.md index 2c7a61a..5f2eb64 100644 --- a/README.ru.md +++ b/README.ru.md @@ -146,6 +146,11 @@ class Thing < ActiveRecord::Base # используемое целочисленное поле в модели # по-умолчанию – position config[:position_column] = :position + + # обновлять updated_at/updated_on для других экземпляров моделей + # внутри набора + # true по-умолчанию + config[:touch] = true end end ``` diff --git a/lib/activerecord/sortable/acts_as_sortable.rb b/lib/activerecord/sortable/acts_as_sortable.rb index ae93c60..b9f3882 100644 --- a/lib/activerecord/sortable/acts_as_sortable.rb +++ b/lib/activerecord/sortable/acts_as_sortable.rb @@ -12,7 +12,8 @@ def acts_as_sortable(&block) options = { relation: ->(instance) { instance.class }, append: false, - position_column: :position + position_column: :position, + touch: true } block.call(options) if block_given? @@ -34,6 +35,7 @@ def sortable_relation_create_accessors cattr_accessor :sortable_append, instance_reader: true, instance_writer: false cattr_accessor :sortable_position_column, instance_reader: true, instance_writer: false cattr_accessor :escaped_sortable_position_column, instance_reader: true, instance_writer: false + cattr_accessor :sortable_touch, instance_reader: true, instance_writer: false end def sortable_relation_provide_accessor_values(options) @@ -41,6 +43,7 @@ def sortable_relation_provide_accessor_values(options) self.sortable_append = options[:append] self.sortable_position_column = options[:position_column] self.escaped_sortable_position_column = ActiveRecord::Base.connection.quote_column_name(options[:position_column]) + self.sortable_touch = options[:touch] end end end diff --git a/lib/activerecord/sortable/acts_as_sortable/instance_methods.rb b/lib/activerecord/sortable/acts_as_sortable/instance_methods.rb index 972a38e..06bb33e 100644 --- a/lib/activerecord/sortable/acts_as_sortable/instance_methods.rb +++ b/lib/activerecord/sortable/acts_as_sortable/instance_methods.rb @@ -63,13 +63,18 @@ def sortable_updates_with_timestamps(base_query) query_items = [base_query] - update_values = timestamp_attributes_for_update_in_model.map do |attr| + update_values = sortable_timestamp_attributes_for_update_in_model.map do |attr| query_items << "#{attr} = ?" current_time end [query_items.join(', ')] + update_values end + def sortable_timestamp_attributes_for_update_in_model + return [] unless sortable_touch + timestamp_attributes_for_update_in_model + end + def sortable_shift_on_destroy position_threshold = send(sortable_position_column) position_update = sortable_updates_with_timestamps("#{escaped_sortable_position_column} = #{escaped_sortable_position_column} - 1") diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 8c48d3a..91cc7e2 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -20,7 +20,6 @@ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f } RSpec.configure do |config| - config.treat_symbols_as_metadata_keys_with_true_values = true config.run_all_when_everything_filtered = true config.order = 'random' end diff --git a/spec/support/activerecord_sortable.rb b/spec/support/activerecord_sortable.rb index c0e5637..dffeb57 100644 --- a/spec/support/activerecord_sortable.rb +++ b/spec/support/activerecord_sortable.rb @@ -37,8 +37,20 @@ expect(subject.send(position_column)).to eq(0) end - it 'should change first thing updated_at' do - expect { subject }.to change { thing1.reload.updated_at } + context 'touch is on' do + it 'should change first thing updated_at' do + expect { subject }.to change { thing1.reload.updated_at } + end + end + + context 'touch is off' do + before { described_class.sortable_touch = false } + + it 'should not change first thing updated_at' do + expect { subject }.not_to change { thing1.reload.updated_at } + end + + after { described_class.sortable_touch = true } end end end