Skip to content

Commit

Permalink
added touch option
Browse files Browse the repository at this point in the history
  • Loading branch information
aishek committed Dec 24, 2015
1 parent c3d8d09 commit 3ee92f7
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 5 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand Down
5 changes: 5 additions & 0 deletions README.ru.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ class Thing < ActiveRecord::Base
# используемое целочисленное поле в модели
# по-умолчанию – position
config[:position_column] = :position

# обновлять updated_at/updated_on для других экземпляров моделей
# внутри набора
# true по-умолчанию
config[:touch] = true
end
end
```
Expand Down
5 changes: 4 additions & 1 deletion lib/activerecord/sortable/acts_as_sortable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?

Expand All @@ -34,13 +35,15 @@ 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)
self.sortable_relation = options[:relation]
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
1 change: 0 additions & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
16 changes: 14 additions & 2 deletions spec/support/activerecord_sortable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3ee92f7

Please sign in to comment.