diff --git a/spec/integration/active_record/attributes_have_changed_spec.rb b/spec/integration/active_record/attributes_have_changed_spec.rb new file mode 100644 index 00000000..debd6f14 --- /dev/null +++ b/spec/integration/active_record/attributes_have_changed_spec.rb @@ -0,0 +1,50 @@ +require 'support/models/color' +require 'support/models/book' + +describe 'When record attributes have changed' do + it 'detects attribute changes' do + color = Color.new name: 'dark-blue', short_name: 'blue' + + expect(Color.ms_must_reindex?(color)).to be(true) + color.save + expect(Color.ms_must_reindex?(color)).to be(false) + + color.hex = 123_456 + expect(Color.ms_must_reindex?(color)).to be(false) + + color.not_indexed = 'strstr' + expect(Color.ms_must_reindex?(color)).to be(false) + color.name = 'red' + expect(Color.ms_must_reindex?(color)).to be(true) + color.delete + end + + it 'detects attribute changes even in a transaction' do + color = Color.new name: 'dark-blue', short_name: 'blue' + color.save + expect(color.instance_variable_get('@ms_must_reindex')).to be_nil + Color.transaction do + color.name = 'red' + color.save + color.not_indexed = 'strstr' + color.save + expect(color.instance_variable_get('@ms_must_reindex')).to be(true) + end + expect(color.instance_variable_get('@ms_must_reindex')).to be_nil + color.delete + end + + it 'detects change with ms_dirty? method' do + book = Book.new name: 'My life', author: 'Myself', premium: false, released: true + + allow(book).to receive(:ms_dirty?).and_return(true) + expect(Book.ms_must_reindex?(book)).to be(true) + + allow(book).to receive(:ms_dirty?).and_return(false) + expect(Book.ms_must_reindex?(book)).to be(false) + + allow(book).to receive(:ms_dirty?).and_return(true) + expect(Book.ms_must_reindex?(book)).to be(true) + end +end + diff --git a/spec/integration_spec.rb b/spec/integration_spec.rb index 3210d30e..22be8929 100644 --- a/spec/integration_spec.rb +++ b/spec/integration_spec.rb @@ -47,50 +47,6 @@ end end -describe 'Attributes change detection' do - it 'detects attribute changes' do - color = Color.new name: 'dark-blue', short_name: 'blue' - - expect(Color.ms_must_reindex?(color)).to be(true) - color.save - expect(Color.ms_must_reindex?(color)).to be(false) - - color.hex = 123_456 - expect(Color.ms_must_reindex?(color)).to be(false) - - color.not_indexed = 'strstr' - expect(Color.ms_must_reindex?(color)).to be(false) - color.name = 'red' - expect(Color.ms_must_reindex?(color)).to be(true) - color.delete - end - - it 'detects attribute changes even in a transaction' do - color = Color.new name: 'dark-blue', short_name: 'blue' - color.save - expect(color.instance_variable_get('@ms_must_reindex')).to be_nil - Color.transaction do - color.name = 'red' - color.save - color.not_indexed = 'strstr' - color.save - expect(color.instance_variable_get('@ms_must_reindex')).to be(true) - end - expect(color.instance_variable_get('@ms_must_reindex')).to be_nil - color.delete - end - - it 'detects change with ms_dirty? method' do - ebook = Ebook.new name: 'My life', author: 'Myself', premium: false, released: true - expect(Ebook.ms_must_reindex?(ebook)).to be(true) # Because it's defined in ms_dirty? method - ebook.current_time = 10 - ebook.published_at = 8 - expect(Ebook.ms_must_reindex?(ebook)).to be(true) - ebook.published_at = 12 - expect(Ebook.ms_must_reindex?(ebook)).to be(false) - end -end - describe 'Namespaced::Model' do before(:all) do Namespaced::Model.index.delete_all_documents! diff --git a/spec/support/active_record_classes.rb b/spec/support/active_record_classes.rb index 3b3cbfa6..2eec88fa 100644 --- a/spec/support/active_record_classes.rb +++ b/spec/support/active_record_classes.rb @@ -9,12 +9,6 @@ create_table :mongo_documents do |t| t.string :name end - create_table :ebooks do |t| - t.string :name - t.string :author - t.boolean :premium - t.boolean :released - end create_table :encoded_strings end @@ -53,23 +47,6 @@ def index! end end -class Ebook < ActiveRecord::Base - include MeiliSearch::Rails - attr_accessor :current_time, :published_at - - meilisearch synchronous: true, index_uid: safe_index_uid('eBooks') do - searchable_attributes [:name] - end - - def ms_dirty? - return true if published_at.nil? || current_time.nil? - - # Consider dirty if published date is in the past - # This doesn't make so much business sense but it's easy to test. - published_at < current_time - end -end - class EncodedString < ActiveRecord::Base include MeiliSearch::Rails