freelancing-god-thinking-sphinx 1.1.7 → 1.1.8

Sign up to get free protection for your applications and to get access to all the features.
data/README CHANGED
@@ -125,3 +125,4 @@ Since I first released this library, there's been quite a few people who have su
125
125
  - Andrei Bocan
126
126
  - László Bácsi
127
127
  - Peter Wagenet
128
+ - Max Lapshin
@@ -36,7 +36,7 @@ module ThinkingSphinx
36
36
  module Version #:nodoc:
37
37
  Major = 1
38
38
  Minor = 1
39
- Tiny = 7
39
+ Tiny = 8
40
40
 
41
41
  String = [Major, Minor, Tiny].join('.')
42
42
  end
@@ -1,3 +1,4 @@
1
+ require 'thinking_sphinx/active_record/attribute_updates'
1
2
  require 'thinking_sphinx/active_record/delta'
2
3
  require 'thinking_sphinx/active_record/search'
3
4
  require 'thinking_sphinx/active_record/has_many_association'
@@ -79,6 +80,8 @@ module ThinkingSphinx
79
80
 
80
81
  after_destroy :toggle_deleted
81
82
 
83
+ include ThinkingSphinx::ActiveRecord::AttributeUpdates
84
+
82
85
  index
83
86
  end
84
87
  alias_method :sphinx_index, :define_index
@@ -0,0 +1,48 @@
1
+ module ThinkingSphinx
2
+ module ActiveRecord
3
+ module AttributeUpdates
4
+ def self.included(base)
5
+ base.class_eval do
6
+ after_commit :update_attribute_values
7
+ end
8
+ end
9
+
10
+ private
11
+
12
+ def update_attribute_values
13
+ return unless ThinkingSphinx.updates_enabled? && ThinkingSphinx.sphinx_running?
14
+
15
+ config = ThinkingSphinx::Configuration.instance
16
+ client = Riddle::Client.new config.address, config.port
17
+
18
+ self.sphinx_indexes.each do |index|
19
+ attribute_pairs = attribute_values_for_index(index)
20
+ attribute_names = attribute_pairs.keys
21
+ attribute_values = attribute_names.collect { |key|
22
+ attribute_pairs[key]
23
+ }
24
+
25
+ client.update "#{index.name}_core", attribute_names, {
26
+ sphinx_document_id => attribute_values
27
+ } if in_core_index?
28
+ end
29
+ end
30
+
31
+ def updatable_attributes(index)
32
+ index.attributes.select { |attrib| attrib.updatable? }
33
+ end
34
+
35
+ def attribute_values_for_index(index)
36
+ updatable_attributes(index).inject({}) { |hash, attrib|
37
+ if attrib.type == :datetime
38
+ hash[attrib.unique_name.to_s] = attrib.live_value(self).to_time.to_i
39
+ else
40
+ hash[attrib.unique_name.to_s] = attrib.live_value self
41
+ end
42
+
43
+ hash
44
+ }
45
+ end
46
+ end
47
+ end
48
+ end
@@ -69,7 +69,16 @@ module ThinkingSphinx
69
69
  end
70
70
 
71
71
  def should_toggle_delta?
72
- !self.respond_to?(:changed?) || self.changed? || self.new_record?
72
+ self.new_record? || indexed_data_changed?
73
+ end
74
+
75
+ def indexed_data_changed?
76
+ sphinx_indexes.any? { |index|
77
+ index.fields.any? { |field| field.changed?(self) } ||
78
+ index.attributes.any? { |attrib|
79
+ attrib.public? && attrib.changed?(self) && !attrib.updatable?
80
+ }
81
+ }
73
82
  end
74
83
  end
75
84
  end
@@ -157,6 +157,17 @@ module ThinkingSphinx
157
157
  end
158
158
  end
159
159
 
160
+ def updatable?
161
+ [:integer, :datetime, :boolean].include?(type) && !is_string?
162
+ end
163
+
164
+ def live_value(instance)
165
+ object = instance
166
+ column = @columns.first
167
+ column.__stack.each { |method| object = object.send(method) }
168
+ object.send(column.__name)
169
+ end
170
+
160
171
  private
161
172
 
162
173
  def source_value(offset)
@@ -49,6 +49,23 @@ module ThinkingSphinx
49
49
  end
50
50
  end
51
51
 
52
+ def changed?(instance)
53
+ return true if is_string? || @columns.any? { |col| !col.__stack.empty? }
54
+
55
+ !@columns.all? { |col|
56
+ instance.respond_to?("#{col.__name.to_s}_changed?") &&
57
+ !instance.send("#{col.__name.to_s}_changed?")
58
+ }
59
+ end
60
+
61
+ def admin?
62
+ admin
63
+ end
64
+
65
+ def public?
66
+ !admin
67
+ end
68
+
52
69
  private
53
70
 
54
71
  # Could there be more than one value related to the parent record? If so,
@@ -64,15 +64,15 @@ describe "ThinkingSphinx::ActiveRecord" do
64
64
 
65
65
  TestModule::TestModel.define_index do; end
66
66
 
67
- TestModule::TestModel.should have_received(:before_save)
68
- TestModule::TestModel.should have_received(:after_commit)
67
+ TestModule::TestModel.should have_received(:before_save).with(:toggle_delta)
68
+ TestModule::TestModel.should have_received(:after_commit).with(:index_delta)
69
69
  end
70
70
 
71
71
  it "should not add before_save and after_commit hooks to the model if delta indexing is disabled" do
72
72
  TestModule::TestModel.define_index do; end
73
73
 
74
- TestModule::TestModel.should_not have_received(:before_save)
75
- TestModule::TestModel.should_not have_received(:after_commit)
74
+ TestModule::TestModel.should_not have_received(:before_save).with(:toggle_delta)
75
+ TestModule::TestModel.should_not have_received(:after_commit).with(:index_delta)
76
76
  end
77
77
 
78
78
  it "should add an after_destroy hook with delta indexing enabled" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: freelancing-god-thinking-sphinx
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.7
4
+ version: 1.1.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pat Allan
@@ -22,6 +22,7 @@ extensions: []
22
22
  extra_rdoc_files: []
23
23
 
24
24
  files:
25
+ - lib/thinking_sphinx/active_record/attribute_updates.rb
25
26
  - lib/thinking_sphinx/active_record/delta.rb
26
27
  - lib/thinking_sphinx/active_record/has_many_association.rb
27
28
  - lib/thinking_sphinx/active_record/search.rb