thinking-sphinx 2.0.9 → 2.0.10

Sign up to get free protection for your applications and to get access to all the features.
data/HISTORY CHANGED
@@ -1,3 +1,6 @@
1
+ 2.0.10 - November 4th 2011
2
+ * 1.4.10 changes.
3
+
1
4
  2.0.9 - October 10th 2011
2
5
  * 1.4.9 changes (fixed invalid gemspec).
3
6
 
@@ -50,6 +53,11 @@
50
53
  * Rails 3 support.
51
54
  * 1.4.0 changes.
52
55
 
56
+ 1.4.10 - November 4th 2011
57
+ * Updating Riddle references for impending 1.5.0 release of Riddle.
58
+ * Handle out-of-date indexed_models references.
59
+ * Fixing STI model handling during context index loading (Kenn Ejima).
60
+
53
61
  1.4.9 - October 10th 2011
54
62
  * Fixing invalid gemspec.
55
63
 
@@ -224,3 +224,4 @@ Since I first released this library, there's been quite a few people who have su
224
224
  * Pete Deffendol
225
225
  * Eduardo Casanova
226
226
  * Tony Pitale
227
+ * Kenn Ejima
@@ -310,7 +310,7 @@ module ThinkingSphinx
310
310
  private
311
311
 
312
312
  def local_sphinx_indexes
313
- sphinx_indexes.select { |index|
313
+ (sphinx_indexes || []).select { |index|
314
314
  index.model == self
315
315
  }
316
316
  end
@@ -151,12 +151,12 @@ module ThinkingSphinx
151
151
  end
152
152
 
153
153
  def generate
154
- @configuration.indexes.clear
154
+ @configuration.indices.clear
155
155
 
156
156
  ThinkingSphinx.context.indexed_models.each do |model|
157
157
  model = model.constantize
158
158
  model.define_indexes
159
- @configuration.indexes.concat model.to_riddle
159
+ @configuration.indices.concat model.to_riddle
160
160
 
161
161
  enforce_common_attribute_types
162
162
  end
@@ -316,7 +316,7 @@ module ThinkingSphinx
316
316
  end
317
317
 
318
318
  def enforce_common_attribute_types
319
- sql_indexes = configuration.indexes.reject { |index|
319
+ sql_indexes = configuration.indices.reject { |index|
320
320
  index.is_a? Riddle::Configuration::DistributedIndex
321
321
  }
322
322
 
@@ -62,6 +62,9 @@ class ThinkingSphinx::Context
62
62
 
63
63
  begin
64
64
  camelized_model.constantize
65
+ rescue LoadError
66
+ # Make sure that STI subclasses in subfolders are loaded.
67
+ model_name.gsub!(/.*[\/\\]/, '').nil? ? next : retry
65
68
  rescue Exception => err
66
69
  STDERR.puts "Warning: Error loading #{file}:"
67
70
  STDERR.puts err.message
@@ -4,7 +4,7 @@ require 'thinking_sphinx/index/faux_column'
4
4
  module ThinkingSphinx
5
5
  class Index
6
6
  attr_accessor :name, :model, :sources, :delta_object
7
-
7
+
8
8
  # Create a new index instance by passing in the model it is tied to, and
9
9
  # a block to build it with (optional but recommended). For documentation
10
10
  # on the syntax for inside the block, the Builder class is what you want.
@@ -13,9 +13,9 @@ module ThinkingSphinx
13
13
  #
14
14
  # Index.new(User) do
15
15
  # indexes login, email
16
- #
16
+ #
17
17
  # has created_at
18
- #
18
+ #
19
19
  # set_property :delta => true
20
20
  # end
21
21
  #
@@ -26,46 +26,46 @@ module ThinkingSphinx
26
26
  @options = {}
27
27
  @delta_object = nil
28
28
  end
29
-
29
+
30
30
  def fields
31
31
  @sources.collect { |source| source.fields }.flatten
32
32
  end
33
-
33
+
34
34
  def attributes
35
35
  @sources.collect { |source| source.attributes }.flatten
36
36
  end
37
-
37
+
38
38
  def core_name
39
39
  "#{name}_core"
40
40
  end
41
-
41
+
42
42
  def delta_name
43
43
  "#{name}_delta"
44
44
  end
45
-
45
+
46
46
  def all_names
47
47
  names = [core_name]
48
48
  names << delta_name if delta?
49
-
49
+
50
50
  names
51
51
  end
52
-
52
+
53
53
  def self.name_for(model)
54
54
  model.name.underscore.tr(':/\\', '_')
55
55
  end
56
-
56
+
57
57
  def prefix_fields
58
58
  fields.select { |field| field.prefixes }
59
59
  end
60
-
60
+
61
61
  def infix_fields
62
62
  fields.select { |field| field.infixes }
63
63
  end
64
-
64
+
65
65
  def local_options
66
66
  @options
67
67
  end
68
-
68
+
69
69
  def options
70
70
  all_index_options = config.index_options.clone
71
71
  @options.keys.select { |key|
@@ -74,82 +74,82 @@ module ThinkingSphinx
74
74
  }.each { |key| all_index_options[key.to_sym] = @options[key] }
75
75
  all_index_options
76
76
  end
77
-
77
+
78
78
  def delta?
79
79
  !@delta_object.nil?
80
80
  end
81
-
81
+
82
82
  def to_riddle(offset)
83
83
  indexes = [to_riddle_for_core(offset)]
84
84
  indexes << to_riddle_for_delta(offset) if delta?
85
85
  indexes << to_riddle_for_distributed
86
86
  end
87
-
87
+
88
88
  private
89
-
89
+
90
90
  def adapter
91
91
  @adapter ||= @model.sphinx_database_adapter
92
92
  end
93
-
93
+
94
94
  def utf8?
95
95
  options[:charset_type] == "utf-8"
96
96
  end
97
-
97
+
98
98
  def sql_query_pre_for_delta
99
99
  [""]
100
100
  end
101
-
101
+
102
102
  def config
103
103
  @config ||= ThinkingSphinx::Configuration.instance
104
104
  end
105
-
105
+
106
106
  def to_riddle_for_core(offset)
107
107
  index = Riddle::Configuration::Index.new core_name
108
108
  index.path = File.join config.searchd_file_path, index.name
109
-
109
+
110
110
  set_configuration_options_for_indexes index
111
111
  set_field_settings_for_indexes index
112
-
112
+
113
113
  sources.each_with_index do |source, i|
114
114
  index.sources << source.to_riddle_for_core(offset, i)
115
115
  end
116
-
116
+
117
117
  index
118
118
  end
119
-
119
+
120
120
  def to_riddle_for_delta(offset)
121
121
  index = Riddle::Configuration::Index.new delta_name
122
122
  index.parent = core_name
123
123
  index.path = File.join config.searchd_file_path, index.name
124
-
124
+
125
125
  sources.each_with_index do |source, i|
126
126
  index.sources << source.to_riddle_for_delta(offset, i)
127
127
  end
128
-
128
+
129
129
  index
130
130
  end
131
-
131
+
132
132
  def to_riddle_for_distributed
133
133
  index = Riddle::Configuration::DistributedIndex.new name
134
- index.local_indexes << core_name
135
- index.local_indexes.unshift delta_name if delta?
134
+ index.local_indices << core_name
135
+ index.local_indices.unshift delta_name if delta?
136
136
  index
137
137
  end
138
-
138
+
139
139
  def set_configuration_options_for_indexes(index)
140
140
  config.index_options.each do |key, value|
141
141
  method = "#{key}=".to_sym
142
142
  index.send(method, value) if index.respond_to?(method)
143
143
  end
144
-
144
+
145
145
  options.each do |key, value|
146
146
  index.send("#{key}=".to_sym, value) if ThinkingSphinx::Configuration::IndexOptions.include?(key.to_s) && !value.nil?
147
147
  end
148
148
  end
149
-
149
+
150
150
  def set_field_settings_for_indexes(index)
151
151
  field_names = lambda { |field| field.unique_name.to_s }
152
-
152
+
153
153
  index.prefix_field_names += prefix_fields.collect(&field_names)
154
154
  index.infix_field_names += infix_fields.collect(&field_names)
155
155
  end
@@ -1,3 +1,3 @@
1
1
  module ThinkingSphinx
2
- Version = '2.0.9'
2
+ Version = '2.0.10'
3
3
  end
@@ -231,7 +231,7 @@ describe ThinkingSphinx::Configuration do
231
231
  config.reset
232
232
  config.generate
233
233
 
234
- config.configuration.indexes.each do |index|
234
+ config.configuration.indices.each do |index|
235
235
  next if index.is_a? Riddle::Configuration::DistributedIndex
236
236
 
237
237
  index.sources.each do |source|
@@ -41,10 +41,9 @@ describe ThinkingSphinx::Context do
41
41
  }.should_not raise_error
42
42
  end
43
43
 
44
- it "should report load errors but not raise them" do
44
+ it "should retry if the first load fails and contains a directory" do
45
+ model_name_lower.should_receive(:gsub!).twice.and_return(true, nil)
45
46
  class_name.stub(:constantize).and_raise(LoadError)
46
- STDERR.stub!(:puts => '')
47
- STDERR.should_receive(:puts).with('Warning: Error loading a.rb:')
48
47
 
49
48
  lambda {
50
49
  ts_context.prepare
@@ -4,179 +4,179 @@ describe ThinkingSphinx::Index do
4
4
  describe "prefix_fields method" do
5
5
  before :each do
6
6
  @index = ThinkingSphinx::Index.new(Person)
7
-
7
+
8
8
  @field_a = stub('field', :prefixes => true)
9
9
  @field_b = stub('field', :prefixes => false)
10
10
  @field_c = stub('field', :prefixes => true)
11
-
11
+
12
12
  @index.stub!(:fields => [@field_a, @field_b, @field_c])
13
13
  end
14
-
14
+
15
15
  it "should return fields that are flagged as prefixed" do
16
16
  @index.prefix_fields.should include(@field_a)
17
17
  @index.prefix_fields.should include(@field_c)
18
18
  end
19
-
19
+
20
20
  it "should not return fields that aren't flagged as prefixed" do
21
21
  @index.prefix_fields.should_not include(@field_b)
22
22
  end
23
23
  end
24
-
24
+
25
25
  describe "infix_fields method" do
26
26
  before :each do
27
27
  @index = ThinkingSphinx::Index.new(Person)
28
-
28
+
29
29
  @field_a = stub('field', :infixes => true)
30
30
  @field_b = stub('field', :infixes => false)
31
31
  @field_c = stub('field', :infixes => true)
32
-
32
+
33
33
  @index.stub!(:fields => [@field_a, @field_b, @field_c])
34
34
  end
35
-
35
+
36
36
  it "should return fields that are flagged as infixed" do
37
37
  @index.infix_fields.should include(@field_a)
38
38
  @index.infix_fields.should include(@field_c)
39
39
  end
40
-
40
+
41
41
  it "should not return fields that aren't flagged as infixed" do
42
42
  @index.infix_fields.should_not include(@field_b)
43
43
  end
44
44
  end
45
-
45
+
46
46
  describe '.name_for' do
47
47
  it "should return the model's name downcased" do
48
48
  ThinkingSphinx::Index.name_for(Alpha).should == 'alpha'
49
49
  end
50
-
50
+
51
51
  it "should separate words by underscores" do
52
52
  ThinkingSphinx::Index.name_for(ActiveRecord).should == 'active_record'
53
53
  end
54
-
54
+
55
55
  it "should separate namespaces by underscores" do
56
56
  ThinkingSphinx::Index.name_for(ActiveRecord::Base).
57
57
  should == 'active_record_base'
58
58
  end
59
59
  end
60
-
60
+
61
61
  describe '#name' do
62
62
  it "should return the downcased name of the index's model" do
63
63
  ThinkingSphinx::Index.new(Alpha).name.should == 'alpha'
64
64
  end
65
-
65
+
66
66
  it "should return a custom name if one is set" do
67
67
  index = ThinkingSphinx::Index.new(Alpha)
68
68
  index.name = 'custom'
69
69
  index.name.should == 'custom'
70
70
  end
71
71
  end
72
-
72
+
73
73
  describe '#core_name' do
74
74
  it "should take the index's name and append _core" do
75
75
  ThinkingSphinx::Index.new(Alpha).core_name.should == 'alpha_core'
76
76
  end
77
77
  end
78
-
78
+
79
79
  describe '#delta_name' do
80
80
  it "should take the index's name and append _delta" do
81
81
  ThinkingSphinx::Index.new(Alpha).delta_name.should == 'alpha_delta'
82
82
  end
83
83
  end
84
-
84
+
85
85
  describe '#all_names' do
86
86
  it "should return the core index name by default" do
87
87
  ThinkingSphinx::Index.new(Alpha).all_names.should == ['alpha_core']
88
88
  end
89
-
89
+
90
90
  it "should return both core and delta names if deltas are enabled" do
91
91
  index = ThinkingSphinx::Index.new(Alpha)
92
92
  index.delta_object = stub('delta')
93
-
93
+
94
94
  index.all_names.should == ['alpha_core', 'alpha_delta']
95
95
  end
96
-
96
+
97
97
  it "should respect custom names" do
98
98
  index = ThinkingSphinx::Index.new(Alpha)
99
99
  index.name = 'custom'
100
-
100
+
101
101
  index.all_names.should == ['custom_core']
102
102
  end
103
-
103
+
104
104
  it "should respect custom names when deltas are enabled" do
105
105
  index = ThinkingSphinx::Index.new(Alpha)
106
106
  index.name = 'custom'
107
107
  index.delta_object = stub('delta')
108
-
108
+
109
109
  index.all_names.should == ['custom_core', 'custom_delta']
110
110
  end
111
111
  end
112
-
112
+
113
113
  describe '#to_riddle' do
114
114
  it "should return two Riddle indexes if deltas are disabled" do
115
115
  index = ThinkingSphinx::Index.new(Alpha)
116
-
116
+
117
117
  index.to_riddle(0).length.should == 2
118
118
  end
119
-
119
+
120
120
  it "should return three Riddle indexes if deltas are enabled" do
121
121
  index = ThinkingSphinx::Index.new(Beta)
122
122
  index.delta_object = stub('delta')
123
-
123
+
124
124
  index.to_riddle(0).length.should == 3
125
125
  end
126
-
126
+
127
127
  it "should include a distributed index" do
128
128
  index = ThinkingSphinx::Index.new(Alpha)
129
-
129
+
130
130
  index.to_riddle(0).last.
131
131
  should be_a(Riddle::Configuration::DistributedIndex)
132
132
  end
133
-
133
+
134
134
  context 'core index' do
135
135
  it "should use the core name" do
136
136
  @index = ThinkingSphinx::Index.new(Alpha).to_riddle(0).first
137
137
  @index.name.should == 'alpha_core'
138
138
  end
139
-
139
+
140
140
  it "should not try to set disable_range on the index" do
141
141
  ThinkingSphinx::Configuration.instance.
142
142
  index_options[:disable_range] = true
143
-
143
+
144
144
  lambda {
145
145
  @index = ThinkingSphinx::Index.new(Alpha).to_riddle(0).first
146
146
  }.should_not raise_error(NoMethodError)
147
147
  end
148
148
  end
149
-
149
+
150
150
  context 'delta index' do
151
151
  before :each do
152
152
  index = ThinkingSphinx::Index.new(Beta)
153
153
  index.delta_object = stub('delta')
154
154
  @index = index.to_riddle(0)[1]
155
155
  end
156
-
156
+
157
157
  it "should use the delta name" do
158
158
  @index.name.should == 'beta_delta'
159
159
  end
160
160
  end
161
-
161
+
162
162
  context 'distributed index' do
163
163
  it "should use the index's name" do
164
164
  index = ThinkingSphinx::Index.new(Alpha)
165
165
 
166
166
  index.to_riddle(0).last.name.should == 'alpha'
167
167
  end
168
-
168
+
169
169
  it "should add the core index" do
170
170
  index = ThinkingSphinx::Index.new(Alpha)
171
171
 
172
- index.to_riddle(0).last.local_indexes.should include('alpha_core')
172
+ index.to_riddle(0).last.local_indices.should include('alpha_core')
173
173
  end
174
-
174
+
175
175
  it "should add the delta index if there is one" do
176
176
  index = ThinkingSphinx::Index.new(Beta)
177
177
  index.delta_object = stub('delta')
178
178
 
179
- index.to_riddle(0).last.local_indexes.should include('beta_delta')
179
+ index.to_riddle(0).last.local_indices.should include('beta_delta')
180
180
  end
181
181
  end
182
182
  end