Skip to content

Commit

Permalink
Fixed bug in prop_eval where index rebuilding would not recompute pro…
Browse files Browse the repository at this point in the history
…p_eval.
  • Loading branch information
gaspard committed Mar 15, 2011
1 parent a3b978d commit 5b12910
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 15 deletions.
2 changes: 1 addition & 1 deletion app/models/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ def self.author_proc
# compute vhash (must come before Fulltext)
include Zena::Use::VersionHash::ModelMethods

# computed properties (vclass prop_eval)
# computed properties (vclass prop_eval, must come after MLIndex)
include Zena::Use::PropEval::ModelMethods

# fulltext indices (must come after PropEval)
Expand Down
4 changes: 2 additions & 2 deletions lib/zena/db_helper/abstract_db.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def change_column(*args)

# Set a single attribute directly in the database.
def set_attribute(obj, key, value)
obj.send("#{key}=", value)
obj.write_attribute(key, value)
execute "UPDATE #{obj.class.table_name} SET #{key}=#{quote(value)} WHERE id=#{obj[:id]}"
obj.send(:changed_attributes).delete(key.to_s)
end
Expand Down Expand Up @@ -171,7 +171,7 @@ def insert_dummy_ids
end

# Fixes #98
def prepare_connection_for_timezone
def prepare_connection
# do nothing by default ?
end

Expand Down
6 changes: 5 additions & 1 deletion lib/zena/use/ml_index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,14 @@ def rebuild_index_with_multi_lingual!
@properties = version.prop
@index_langs = nil # force rebuild
# Build ml index for each version
rebuild_index_without_multi_lingual!
rebuild_index_for_version(version)
end
end

def rebuild_index_for_version(v)
rebuild_index_without_multi_lingual!
end

# Hash used to read current values
def index_reader(group_name)
if group_name =~ /^ml_/
Expand Down
8 changes: 5 additions & 3 deletions lib/zena/use/prop_eval.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,14 @@ def self.included(base)
base.before_validation :merge_prop_eval
base.before_validation :need_set__id
base.before_save :set__id
base.alias_method_chain :rebuild_index!, :prop_eval
base.alias_method_chain :rebuild_index_for_version, :prop_eval
end

def rebuild_index_with_prop_eval!
def rebuild_index_for_version_with_prop_eval(v)
merge_prop_eval(true)
rebuild_index_without_prop_eval!
# Only save properties, without changing updated_at date or other callbacks
Zena::Db.set_attribute(v, 'properties', encode_properties(@properties)) if v.changed?
rebuild_index_for_version_without_prop_eval(v)
end

def need_set__id
Expand Down
42 changes: 34 additions & 8 deletions test/unit/zena/use/prop_eval_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@ class NodesRoles < ActiveRecord::Base
set_table_name :nodes_roles
end

def idx_ml_value(obj_id, key, lang=visitor.lang)
IdxNodesMlString.find(:first,
:conditions => {:node_id => obj_id, :lang => lang, :key => key}
).value
end

def idx_value(obj_id, key)
IdxNodesString.find(:first,
:conditions => {:node_id => obj_id, :key => key}
).value
end

context 'A visitor with admin rights' do
setup do
login(:lion)
Expand Down Expand Up @@ -98,21 +110,35 @@ class NodesRoles < ActiveRecord::Base
end

should 'use evaluated prop in ml prop indices' do
idx = IdxNodesMlString.find(:first,
:conditions => {:node_id => subject.id, :lang => visitor.lang, :key => 'search'}
)
assert_equal 'zena enhancements paper:Origami', idx.value
assert_equal 'zena enhancements paper:Origami', idx_ml_value(subject.id, 'search')
end

should 'use evaluated prop in prop indices' do
idx = IdxNodesString.find(:first,
:conditions => {:node_id => subject.id, :key => 'search_mono'}
)
assert_equal 'Origami mono', idx.value
assert_equal 'Origami mono', idx_value(subject.id, 'search_mono')
end
end # with property indices

end # updating attributes

context 'rebuilding index' do
setup do
login(:lion)
vclass = secure(Role) { roles(:Contact) }
# changed prop_eval
assert vclass.update_attributes(:prop_eval => %q[{'title' => "LAST:#{name} FIRST:#{first_name}"}])
end

subject do
secure(Node) { nodes(:ant) }
end

should 'evaluate prop' do
subject.rebuild_index!
assert_equal 'LAST:Invicta FIRST:Solenopsis', idx_ml_value(subject.id, 'title')
assert_equal 'LAST:Invicta FIRST:Solenopsis', nodes(:ant).title
end
end # rebuilding index

end # from a class with evaluated properties

context 'from a class with prop eval used as default' do
Expand Down

0 comments on commit 5b12910

Please sign in to comment.