Skip to content
This repository was archived by the owner on Mar 18, 2025. It is now read-only.

Fix #352 and #353 #2

Merged
merged 3 commits into from
May 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions lib/algoliasearch-rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -953,8 +953,8 @@ def algolia_attribute_changed?(object, attr_name)
return object.send("will_save_change_to_#{attr_name}?")
end

# Nil means we don't know if the attribute has changed
nil
# We don't know if the attribute has changed, so conservatively assume it has
true
end

def automatic_changed_method?(object, method_name)
Expand Down Expand Up @@ -1018,7 +1018,9 @@ def algolia_mark_for_auto_indexing
end

def algolia_mark_must_reindex
@algolia_must_reindex =
# algolia_must_reindex flag is reset after every commit as part. If we must reindex at any point in
# a stransaction, keep flag set until it is explicitly unset
@algolia_must_reindex ||=
if defined?(::Sequel) && is_a?(Sequel::Model)
new? || self.class.algolia_must_reindex?(self)
else
Expand Down
56 changes: 48 additions & 8 deletions spec/integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
end
create_table :namespaced_models do |t|
t.string :name
t.integer :another_private_value
end
create_table :uniq_users, :id => false do |t|
t.string :name
Expand Down Expand Up @@ -181,6 +182,10 @@ def hex_changed?
def will_save_change_to_short_name?
false
end

def will_save_change_to__tags?
false
end
end

class DisabledBoolean < ActiveRecord::Base
Expand Down Expand Up @@ -216,20 +221,16 @@ def self.table_name_prefix
class Namespaced::Model < ActiveRecord::Base
include AlgoliaSearch

algoliasearch do
algoliasearch :synchronous => true, :index_name => safe_index_name(algolia_index_name({})) do
attribute :customAttr do
40 + another_private_value
end
attribute :myid do
id
end
searchableAttributes ['customAttr']
tags ['static_tag1', 'static_tag2']
end

private
def another_private_value
2
end
end

class UniqUser < ActiveRecord::Base
Expand Down Expand Up @@ -583,6 +584,23 @@ class SerializedObject < ActiveRecord::Base
color.delete
end

it "should detect attribute changes even in a transaction" do
color = Color.new :name => "dark-blue", :short_name => "blue"
color.save

color.instance_variable_get("@algolia_must_reindex").should == nil
Color.transaction do
color.name = "red"
color.save
color.not_indexed = "strstr"
color.save
color.instance_variable_get("@algolia_must_reindex").should == true
end
color.instance_variable_get("@algolia_must_reindex").should == nil

color.delete
end

it "should detect change with algolia_dirty? method" do
ebook = Ebook.new :name => "My life", :author => "Myself", :premium => false, :released => true

Expand Down Expand Up @@ -614,16 +632,38 @@ class SerializedObject < ActiveRecord::Base
end

describe 'Namespaced::Model' do
before(:all) do
Namespaced::Model.index.clear_index!
end

it "should have an index name without :: hierarchy" do
Namespaced::Model.index_name.should == "Namespaced_Model"
(Namespaced::Model.index_name.end_with?("Namespaced_Model")).should == true
end

it "should use the block to determine attribute's value" do
m = Namespaced::Model.new
m = Namespaced::Model.new(:another_private_value => 2)
attributes = Namespaced::Model.algoliasearch_settings.get_attributes(m)
attributes['customAttr'].should == 42
attributes['myid'].should == m.id
end

it "should always update when there is no custom _changed? function" do
m = Namespaced::Model.new(:another_private_value => 2)
m.save
results = Namespaced::Model.search(42)
expect(results.size).to eq(1)
expect(results[0].id).to eq(m.id)

m.another_private_value = 5
m.save

results = Namespaced::Model.search(42)
expect(results.size).to eq(0)

results = Namespaced::Model.search(45)
expect(results.size).to eq(1)
expect(results[0].id).to eq(m.id)
end
end

describe 'UniqUsers' do
Expand Down