friendlyfashion-thinking-sphinx 2.0.13

Sign up to get free protection for your applications and to get access to all the features.
Files changed (175) hide show
  1. data/HISTORY +244 -0
  2. data/LICENCE +20 -0
  3. data/README.textile +235 -0
  4. data/features/abstract_inheritance.feature +10 -0
  5. data/features/alternate_primary_key.feature +27 -0
  6. data/features/attribute_transformation.feature +22 -0
  7. data/features/attribute_updates.feature +77 -0
  8. data/features/deleting_instances.feature +67 -0
  9. data/features/direct_attributes.feature +11 -0
  10. data/features/excerpts.feature +21 -0
  11. data/features/extensible_delta_indexing.feature +9 -0
  12. data/features/facets.feature +88 -0
  13. data/features/facets_across_model.feature +29 -0
  14. data/features/field_sorting.feature +18 -0
  15. data/features/handling_edits.feature +94 -0
  16. data/features/retry_stale_indexes.feature +24 -0
  17. data/features/searching_across_models.feature +20 -0
  18. data/features/searching_by_index.feature +40 -0
  19. data/features/searching_by_model.feature +175 -0
  20. data/features/searching_with_find_arguments.feature +56 -0
  21. data/features/sphinx_detection.feature +25 -0
  22. data/features/sphinx_scopes.feature +68 -0
  23. data/features/step_definitions/alpha_steps.rb +16 -0
  24. data/features/step_definitions/beta_steps.rb +7 -0
  25. data/features/step_definitions/common_steps.rb +201 -0
  26. data/features/step_definitions/extensible_delta_indexing_steps.rb +7 -0
  27. data/features/step_definitions/facet_steps.rb +96 -0
  28. data/features/step_definitions/find_arguments_steps.rb +36 -0
  29. data/features/step_definitions/gamma_steps.rb +15 -0
  30. data/features/step_definitions/scope_steps.rb +19 -0
  31. data/features/step_definitions/search_steps.rb +94 -0
  32. data/features/step_definitions/sphinx_steps.rb +35 -0
  33. data/features/sti_searching.feature +19 -0
  34. data/features/support/env.rb +27 -0
  35. data/features/support/lib/generic_delta_handler.rb +8 -0
  36. data/features/thinking_sphinx/database.example.yml +3 -0
  37. data/features/thinking_sphinx/db/.gitignore +1 -0
  38. data/features/thinking_sphinx/db/fixtures/alphas.rb +8 -0
  39. data/features/thinking_sphinx/db/fixtures/authors.rb +1 -0
  40. data/features/thinking_sphinx/db/fixtures/betas.rb +11 -0
  41. data/features/thinking_sphinx/db/fixtures/boxes.rb +9 -0
  42. data/features/thinking_sphinx/db/fixtures/categories.rb +1 -0
  43. data/features/thinking_sphinx/db/fixtures/cats.rb +3 -0
  44. data/features/thinking_sphinx/db/fixtures/comments.rb +24 -0
  45. data/features/thinking_sphinx/db/fixtures/developers.rb +31 -0
  46. data/features/thinking_sphinx/db/fixtures/dogs.rb +3 -0
  47. data/features/thinking_sphinx/db/fixtures/extensible_betas.rb +10 -0
  48. data/features/thinking_sphinx/db/fixtures/foxes.rb +3 -0
  49. data/features/thinking_sphinx/db/fixtures/gammas.rb +10 -0
  50. data/features/thinking_sphinx/db/fixtures/music.rb +4 -0
  51. data/features/thinking_sphinx/db/fixtures/people.rb +1001 -0
  52. data/features/thinking_sphinx/db/fixtures/post_keywords.txt +1 -0
  53. data/features/thinking_sphinx/db/fixtures/posts.rb +10 -0
  54. data/features/thinking_sphinx/db/fixtures/robots.rb +8 -0
  55. data/features/thinking_sphinx/db/fixtures/tags.rb +27 -0
  56. data/features/thinking_sphinx/db/migrations/create_alphas.rb +8 -0
  57. data/features/thinking_sphinx/db/migrations/create_animals.rb +5 -0
  58. data/features/thinking_sphinx/db/migrations/create_authors.rb +3 -0
  59. data/features/thinking_sphinx/db/migrations/create_authors_posts.rb +6 -0
  60. data/features/thinking_sphinx/db/migrations/create_betas.rb +5 -0
  61. data/features/thinking_sphinx/db/migrations/create_boxes.rb +5 -0
  62. data/features/thinking_sphinx/db/migrations/create_categories.rb +3 -0
  63. data/features/thinking_sphinx/db/migrations/create_comments.rb +10 -0
  64. data/features/thinking_sphinx/db/migrations/create_developers.rb +7 -0
  65. data/features/thinking_sphinx/db/migrations/create_extensible_betas.rb +5 -0
  66. data/features/thinking_sphinx/db/migrations/create_gammas.rb +3 -0
  67. data/features/thinking_sphinx/db/migrations/create_genres.rb +3 -0
  68. data/features/thinking_sphinx/db/migrations/create_music.rb +6 -0
  69. data/features/thinking_sphinx/db/migrations/create_people.rb +13 -0
  70. data/features/thinking_sphinx/db/migrations/create_posts.rb +6 -0
  71. data/features/thinking_sphinx/db/migrations/create_robots.rb +4 -0
  72. data/features/thinking_sphinx/db/migrations/create_taggings.rb +5 -0
  73. data/features/thinking_sphinx/db/migrations/create_tags.rb +4 -0
  74. data/features/thinking_sphinx/models/alpha.rb +23 -0
  75. data/features/thinking_sphinx/models/andrew.rb +17 -0
  76. data/features/thinking_sphinx/models/animal.rb +5 -0
  77. data/features/thinking_sphinx/models/author.rb +3 -0
  78. data/features/thinking_sphinx/models/beta.rb +13 -0
  79. data/features/thinking_sphinx/models/box.rb +8 -0
  80. data/features/thinking_sphinx/models/cat.rb +3 -0
  81. data/features/thinking_sphinx/models/category.rb +4 -0
  82. data/features/thinking_sphinx/models/comment.rb +10 -0
  83. data/features/thinking_sphinx/models/developer.rb +21 -0
  84. data/features/thinking_sphinx/models/dog.rb +3 -0
  85. data/features/thinking_sphinx/models/extensible_beta.rb +9 -0
  86. data/features/thinking_sphinx/models/fox.rb +5 -0
  87. data/features/thinking_sphinx/models/gamma.rb +5 -0
  88. data/features/thinking_sphinx/models/genre.rb +3 -0
  89. data/features/thinking_sphinx/models/medium.rb +5 -0
  90. data/features/thinking_sphinx/models/music.rb +10 -0
  91. data/features/thinking_sphinx/models/person.rb +24 -0
  92. data/features/thinking_sphinx/models/post.rb +22 -0
  93. data/features/thinking_sphinx/models/robot.rb +12 -0
  94. data/features/thinking_sphinx/models/tag.rb +3 -0
  95. data/features/thinking_sphinx/models/tagging.rb +4 -0
  96. data/lib/cucumber/thinking_sphinx/external_world.rb +12 -0
  97. data/lib/cucumber/thinking_sphinx/internal_world.rb +137 -0
  98. data/lib/cucumber/thinking_sphinx/sql_logger.rb +28 -0
  99. data/lib/thinking-sphinx.rb +1 -0
  100. data/lib/thinking_sphinx/action_controller.rb +31 -0
  101. data/lib/thinking_sphinx/active_record/attribute_updates.rb +53 -0
  102. data/lib/thinking_sphinx/active_record/collection_proxy.rb +47 -0
  103. data/lib/thinking_sphinx/active_record/collection_proxy_with_scopes.rb +27 -0
  104. data/lib/thinking_sphinx/active_record/delta.rb +67 -0
  105. data/lib/thinking_sphinx/active_record/has_many_association.rb +44 -0
  106. data/lib/thinking_sphinx/active_record/has_many_association_with_scopes.rb +21 -0
  107. data/lib/thinking_sphinx/active_record/log_subscriber.rb +61 -0
  108. data/lib/thinking_sphinx/active_record/scopes.rb +110 -0
  109. data/lib/thinking_sphinx/active_record.rb +386 -0
  110. data/lib/thinking_sphinx/adapters/abstract_adapter.rb +87 -0
  111. data/lib/thinking_sphinx/adapters/mysql_adapter.rb +62 -0
  112. data/lib/thinking_sphinx/adapters/postgresql_adapter.rb +188 -0
  113. data/lib/thinking_sphinx/association.rb +230 -0
  114. data/lib/thinking_sphinx/attribute.rb +405 -0
  115. data/lib/thinking_sphinx/auto_version.rb +40 -0
  116. data/lib/thinking_sphinx/bundled_search.rb +44 -0
  117. data/lib/thinking_sphinx/class_facet.rb +20 -0
  118. data/lib/thinking_sphinx/configuration.rb +375 -0
  119. data/lib/thinking_sphinx/context.rb +76 -0
  120. data/lib/thinking_sphinx/core/string.rb +15 -0
  121. data/lib/thinking_sphinx/deltas/default_delta.rb +62 -0
  122. data/lib/thinking_sphinx/deltas.rb +28 -0
  123. data/lib/thinking_sphinx/deploy/capistrano.rb +99 -0
  124. data/lib/thinking_sphinx/excerpter.rb +23 -0
  125. data/lib/thinking_sphinx/facet.rb +135 -0
  126. data/lib/thinking_sphinx/facet_search.rb +170 -0
  127. data/lib/thinking_sphinx/field.rb +98 -0
  128. data/lib/thinking_sphinx/index/builder.rb +315 -0
  129. data/lib/thinking_sphinx/index/faux_column.rb +118 -0
  130. data/lib/thinking_sphinx/index.rb +159 -0
  131. data/lib/thinking_sphinx/join.rb +37 -0
  132. data/lib/thinking_sphinx/property.rb +187 -0
  133. data/lib/thinking_sphinx/railtie.rb +43 -0
  134. data/lib/thinking_sphinx/search.rb +1061 -0
  135. data/lib/thinking_sphinx/search_methods.rb +439 -0
  136. data/lib/thinking_sphinx/sinatra.rb +7 -0
  137. data/lib/thinking_sphinx/source/internal_properties.rb +51 -0
  138. data/lib/thinking_sphinx/source/sql.rb +174 -0
  139. data/lib/thinking_sphinx/source.rb +194 -0
  140. data/lib/thinking_sphinx/tasks.rb +142 -0
  141. data/lib/thinking_sphinx/test.rb +55 -0
  142. data/lib/thinking_sphinx/version.rb +3 -0
  143. data/lib/thinking_sphinx.rb +297 -0
  144. data/spec/fixtures/data.sql +32 -0
  145. data/spec/fixtures/database.yml.default +3 -0
  146. data/spec/fixtures/models.rb +164 -0
  147. data/spec/fixtures/structure.sql +146 -0
  148. data/spec/spec_helper.rb +61 -0
  149. data/spec/sphinx_helper.rb +60 -0
  150. data/spec/support/rails.rb +25 -0
  151. data/spec/thinking_sphinx/active_record/delta_spec.rb +122 -0
  152. data/spec/thinking_sphinx/active_record/has_many_association_spec.rb +173 -0
  153. data/spec/thinking_sphinx/active_record/scopes_spec.rb +176 -0
  154. data/spec/thinking_sphinx/active_record_spec.rb +573 -0
  155. data/spec/thinking_sphinx/adapters/abstract_adapter_spec.rb +145 -0
  156. data/spec/thinking_sphinx/association_spec.rb +250 -0
  157. data/spec/thinking_sphinx/attribute_spec.rb +552 -0
  158. data/spec/thinking_sphinx/auto_version_spec.rb +103 -0
  159. data/spec/thinking_sphinx/configuration_spec.rb +326 -0
  160. data/spec/thinking_sphinx/context_spec.rb +126 -0
  161. data/spec/thinking_sphinx/core/array_spec.rb +9 -0
  162. data/spec/thinking_sphinx/core/string_spec.rb +9 -0
  163. data/spec/thinking_sphinx/excerpter_spec.rb +49 -0
  164. data/spec/thinking_sphinx/facet_search_spec.rb +176 -0
  165. data/spec/thinking_sphinx/facet_spec.rb +359 -0
  166. data/spec/thinking_sphinx/field_spec.rb +127 -0
  167. data/spec/thinking_sphinx/index/builder_spec.rb +532 -0
  168. data/spec/thinking_sphinx/index/faux_column_spec.rb +36 -0
  169. data/spec/thinking_sphinx/index_spec.rb +189 -0
  170. data/spec/thinking_sphinx/search_methods_spec.rb +156 -0
  171. data/spec/thinking_sphinx/search_spec.rb +1455 -0
  172. data/spec/thinking_sphinx/source_spec.rb +267 -0
  173. data/spec/thinking_sphinx/test_spec.rb +20 -0
  174. data/spec/thinking_sphinx_spec.rb +204 -0
  175. metadata +524 -0
@@ -0,0 +1,118 @@
1
+ module ThinkingSphinx
2
+ class Index
3
+ # Instances of this class represent database columns and the stack of
4
+ # associations that lead from the base model to them.
5
+ #
6
+ # The name and stack are accessible through methods starting with __ to
7
+ # avoid conflicting with the method_missing calls that build the stack.
8
+ #
9
+ class FauxColumn
10
+ # Create a new column with a pre-defined stack. The top element in the
11
+ # stack will get shifted to be the name value.
12
+ #
13
+ def initialize(*stack)
14
+ @name = stack.pop
15
+ @stack = stack
16
+ end
17
+
18
+ def self.coerce(columns)
19
+ case columns
20
+ when Symbol, String
21
+ FauxColumn.new(columns)
22
+ when Array
23
+ columns.collect { |col| FauxColumn.coerce(col) }
24
+ when FauxColumn
25
+ columns
26
+ else
27
+ nil
28
+ end
29
+ end
30
+
31
+ # Can't use normal method name, as that could be an association or
32
+ # column name.
33
+ #
34
+ def __name
35
+ @name
36
+ end
37
+
38
+ # Can't use normal method name, as that could be an association or
39
+ # column name.
40
+ #
41
+ def __stack
42
+ @stack
43
+ end
44
+
45
+ def __path
46
+ @stack + [@name]
47
+ end
48
+
49
+ # Returns true if the stack is empty *and* if the name is a string -
50
+ # which is an indication that of raw SQL, as opposed to a value from a
51
+ # table's column.
52
+ #
53
+ def is_string?
54
+ @name.is_a?(String) && @stack.empty?
55
+ end
56
+
57
+ def to_ary
58
+ [self]
59
+ end
60
+
61
+ # This handles any 'invalid' method calls and sets them as the name,
62
+ # and pushing the previous name into the stack. The object returns
63
+ # itself.
64
+ #
65
+ # If there's a single argument, it becomes the name, and the method
66
+ # symbol goes into the stack as well. Multiple arguments means new
67
+ # columns with the original stack and new names (from each argument) gets
68
+ # returned.
69
+ #
70
+ # Easier to explain with examples:
71
+ #
72
+ # col = FauxColumn.new :a, :b, :c
73
+ # col.__name #=> :c
74
+ # col.__stack #=> [:a, :b]
75
+ #
76
+ # col.whatever #=> col
77
+ # col.__name #=> :whatever
78
+ # col.__stack #=> [:a, :b, :c]
79
+ #
80
+ # col.something(:id) #=> col
81
+ # col.__name #=> :id
82
+ # col.__stack #=> [:a, :b, :c, :whatever, :something]
83
+ #
84
+ # cols = col.short(:x, :y, :z)
85
+ # cols[0].__name #=> :x
86
+ # cols[0].__stack #=> [:a, :b, :c, :whatever, :something, :short]
87
+ # cols[1].__name #=> :y
88
+ # cols[1].__stack #=> [:a, :b, :c, :whatever, :something, :short]
89
+ # cols[2].__name #=> :z
90
+ # cols[2].__stack #=> [:a, :b, :c, :whatever, :something, :short]
91
+ #
92
+ # Also, this allows method chaining to build up a relevant stack:
93
+ #
94
+ # col = FauxColumn.new :a, :b
95
+ # col.__name #=> :b
96
+ # col.__stack #=> [:a]
97
+ #
98
+ # col.one.two.three #=> col
99
+ # col.__name #=> :three
100
+ # col.__stack #=> [:a, :b, :one, :two]
101
+ #
102
+ def method_missing(method, *args)
103
+ @stack << @name
104
+ @name = method
105
+
106
+ if (args.empty?)
107
+ self
108
+ elsif (args.length == 1)
109
+ method_missing(args.first)
110
+ else
111
+ args.collect { |arg|
112
+ FauxColumn.new(@stack + [@name, arg])
113
+ }
114
+ end
115
+ end
116
+ end
117
+ end
118
+ end
@@ -0,0 +1,159 @@
1
+ require 'thinking_sphinx/index/builder'
2
+ require 'thinking_sphinx/index/faux_column'
3
+
4
+ module ThinkingSphinx
5
+ class Index
6
+ attr_accessor :name, :model, :sources, :delta_object, :additional_indices
7
+
8
+ # Create a new index instance by passing in the model it is tied to, and
9
+ # a block to build it with (optional but recommended). For documentation
10
+ # on the syntax for inside the block, the Builder class is what you want.
11
+ #
12
+ # Quick Example:
13
+ #
14
+ # Index.new(User) do
15
+ # indexes login, email
16
+ #
17
+ # has created_at
18
+ #
19
+ # set_property :delta => true
20
+ # end
21
+ #
22
+ def initialize(model, &block)
23
+ @name = self.class.name_for model
24
+ @model = model
25
+ @sources = []
26
+ @options = {}
27
+ @delta_object = nil
28
+ @additional_indices = []
29
+ end
30
+
31
+ def fields
32
+ @sources.collect { |source| source.fields }.flatten
33
+ end
34
+
35
+ def attributes
36
+ @sources.collect { |source| source.attributes }.flatten
37
+ end
38
+
39
+ def core_name
40
+ "#{name}_core"
41
+ end
42
+
43
+ def delta_name
44
+ "#{name}_delta"
45
+ end
46
+
47
+ def all_names
48
+ names = [core_name]
49
+ names << delta_name if delta?
50
+
51
+ names
52
+ end
53
+
54
+ def self.name_for(model)
55
+ model.name.underscore.tr(':/\\', '_')
56
+ end
57
+
58
+ def prefix_fields
59
+ fields.select { |field| field.prefixes }
60
+ end
61
+
62
+ def infix_fields
63
+ fields.select { |field| field.infixes }
64
+ end
65
+
66
+ def local_options
67
+ @options
68
+ end
69
+
70
+ def options
71
+ all_index_options = config.index_options.clone
72
+ @options.keys.select { |key|
73
+ ThinkingSphinx::Configuration::IndexOptions.include?(key.to_s) ||
74
+ ThinkingSphinx::Configuration::CustomOptions.include?(key.to_s)
75
+ }.each { |key| all_index_options[key.to_sym] = @options[key] }
76
+ all_index_options
77
+ end
78
+
79
+ def delta?
80
+ !@delta_object.nil?
81
+ end
82
+
83
+ def to_riddle(offset)
84
+ indexes = [to_riddle_for_core(offset)]
85
+ indexes << to_riddle_for_delta(offset) if delta?
86
+ indexes << to_riddle_for_distributed
87
+ end
88
+
89
+ private
90
+
91
+ def adapter
92
+ @adapter ||= @model.sphinx_database_adapter
93
+ end
94
+
95
+ def utf8?
96
+ options[:charset_type] == "utf-8"
97
+ end
98
+
99
+ def sql_query_pre_for_delta
100
+ [""]
101
+ end
102
+
103
+ def config
104
+ @config ||= ThinkingSphinx::Configuration.instance
105
+ end
106
+
107
+ def to_riddle_for_core(offset)
108
+ index = Riddle::Configuration::Index.new core_name
109
+ index.path = File.join config.searchd_file_path, index.name
110
+
111
+ set_configuration_options_for_indexes index
112
+ set_field_settings_for_indexes index
113
+
114
+ sources.each_with_index do |source, i|
115
+ index.sources << source.to_riddle_for_core(offset, i)
116
+ end
117
+
118
+ index
119
+ end
120
+
121
+ def to_riddle_for_delta(offset)
122
+ index = Riddle::Configuration::Index.new delta_name
123
+ index.parent = core_name
124
+ index.path = File.join config.searchd_file_path, index.name
125
+
126
+ sources.each_with_index do |source, i|
127
+ index.sources << source.to_riddle_for_delta(offset, i)
128
+ end
129
+
130
+ index
131
+ end
132
+
133
+ def to_riddle_for_distributed
134
+ index = Riddle::Configuration::DistributedIndex.new name
135
+ index.local_indices << core_name
136
+ index.local_indices += additional_indices
137
+ index.local_indices.unshift delta_name if delta?
138
+ index
139
+ end
140
+
141
+ def set_configuration_options_for_indexes(index)
142
+ config.index_options.each do |key, value|
143
+ method = "#{key}=".to_sym
144
+ index.send(method, value) if index.respond_to?(method)
145
+ end
146
+
147
+ options.each do |key, value|
148
+ index.send("#{key}=".to_sym, value) if ThinkingSphinx::Configuration::IndexOptions.include?(key.to_s) && !value.nil?
149
+ end
150
+ end
151
+
152
+ def set_field_settings_for_indexes(index)
153
+ field_names = lambda { |field| field.unique_name.to_s }
154
+
155
+ index.prefix_field_names += prefix_fields.collect(&field_names)
156
+ index.infix_field_names += infix_fields.collect(&field_names)
157
+ end
158
+ end
159
+ end
@@ -0,0 +1,37 @@
1
+ module ThinkingSphinx
2
+ class Join
3
+ attr_accessor :source, :column, :associations
4
+
5
+ def initialize(source, column)
6
+ @source = source
7
+ @column = column
8
+
9
+ @associations = association_stack(column.__path.clone).each { |assoc|
10
+ assoc.join_to(source.base)
11
+ }
12
+
13
+ source.joins << self
14
+ end
15
+
16
+ private
17
+
18
+ # Gets a stack of associations for a specific path.
19
+ #
20
+ def association_stack(path, parent = nil)
21
+ assocs = []
22
+
23
+ if parent.nil?
24
+ assocs = @source.association(path.shift)
25
+ else
26
+ assocs = parent.children(path.shift)
27
+ end
28
+
29
+ until path.empty?
30
+ point = path.shift
31
+ assocs = assocs.collect { |assoc| assoc.children(point) }.flatten
32
+ end
33
+
34
+ assocs
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,187 @@
1
+ module ThinkingSphinx
2
+ class Property
3
+ attr_accessor :alias, :columns, :associations, :model, :faceted, :admin
4
+
5
+ def initialize(source, columns, options = {})
6
+ @source = source
7
+ @model = source.model
8
+ @columns = Array(columns)
9
+ @associations = {}
10
+
11
+ raise "Cannot define a field or attribute in #{source.model.name} with no columns. Maybe you are trying to index a field with a reserved name (id, name). You can fix this error by using a symbol rather than a bare name (:id instead of id)." if @columns.empty? || @columns.any? { |column| !column.respond_to?(:__stack) }
12
+
13
+ @alias = options[:as]
14
+ @faceted = options[:facet]
15
+ @admin = options[:admin]
16
+ @sortable = options[:sortable] || false
17
+ @value_source = options[:value]
18
+
19
+ @alias = @alias.to_sym unless @alias.blank?
20
+
21
+ @columns.each { |col|
22
+ @associations[col.__stack] = association_stack(col.__stack.clone).each { |assoc|
23
+ assoc.join_to(source.base)
24
+ }
25
+ }
26
+ end
27
+
28
+ # Returns the unique name of the attribute - which is either the alias of
29
+ # the attribute, or the name of the only column - if there is only one. If
30
+ # there isn't, there should be an alias. Else things probably won't work.
31
+ # Consider yourself warned.
32
+ #
33
+ def unique_name
34
+ if @columns.length == 1
35
+ @alias || @columns.first.__name
36
+ else
37
+ @alias
38
+ end
39
+ end
40
+
41
+ def to_facet
42
+ return nil unless @faceted
43
+
44
+ ThinkingSphinx::Facet.new(self, @value_source)
45
+ end
46
+
47
+ # Get the part of the GROUP BY clause related to this attribute - if one is
48
+ # needed. If not, all you'll get back is nil. The latter will happen if
49
+ # there isn't actually a real column to get data from, or if there's
50
+ # multiple data values (read: a has_many or has_and_belongs_to_many
51
+ # association).
52
+ #
53
+ def to_group_sql
54
+ case
55
+ when is_many?, is_string?, ThinkingSphinx.use_group_by_shortcut?
56
+ nil
57
+ else
58
+ @columns.collect { |column|
59
+ column_with_prefix(column)
60
+ }
61
+ end
62
+ end
63
+
64
+ def changed?(instance)
65
+ return true if is_string? || @columns.any? { |col| !col.__stack.empty? }
66
+
67
+ @columns.any? { |col|
68
+ instance.send("#{col.__name.to_s}_changed?")
69
+ }
70
+ end
71
+
72
+ def admin?
73
+ admin
74
+ end
75
+
76
+ def public?
77
+ !admin
78
+ end
79
+
80
+ def available?
81
+ columns.any? { |column| column_available?(column) }
82
+ end
83
+
84
+ private
85
+
86
+ # Could there be more than one value related to the parent record? If so,
87
+ # then this will return true. If not, false. It's that simple.
88
+ #
89
+ def is_many?
90
+ associations.values.flatten.any? { |assoc| assoc.is_many? }
91
+ end
92
+
93
+ # Returns true if any of the columns are string values, instead of database
94
+ # column references.
95
+ def is_string?
96
+ columns.all? { |col| col.is_string? }
97
+ end
98
+
99
+ def adapter
100
+ @adapter ||= @model.sphinx_database_adapter
101
+ end
102
+
103
+ def quote_with_table(table, column)
104
+ "#{quote_table_name(table)}.#{quote_column(column)}"
105
+ end
106
+
107
+ def quote_column(column)
108
+ @model.connection.quote_column_name(column)
109
+ end
110
+
111
+ def quote_table_name(table_name)
112
+ @model.connection.quote_table_name(table_name)
113
+ end
114
+
115
+ # Indication of whether the columns should be concatenated with a space
116
+ # between each value. True if there's either multiple sources or multiple
117
+ # associations.
118
+ #
119
+ def concat_ws?
120
+ multiple_associations? || @columns.length > 1
121
+ end
122
+
123
+ # Checks whether any column requires multiple associations (which only
124
+ # happens for polymorphic situations).
125
+ #
126
+ def multiple_associations?
127
+ associations.values.any? { |assocs| assocs.length > 1 }
128
+ end
129
+
130
+ # Builds a column reference tied to the appropriate associations. This
131
+ # dives into the associations hash and their corresponding joins to
132
+ # figure out how to correctly reference a column in SQL.
133
+ #
134
+ def column_with_prefix(column)
135
+ return nil unless column_available?(column)
136
+
137
+ if column.is_string?
138
+ column.__name
139
+ elsif column.__stack.empty?
140
+ "#{@model.quoted_table_name}.#{quote_column(column.__name)}"
141
+ else
142
+ associations[column.__stack].collect { |assoc|
143
+ assoc.has_column?(column.__name) ?
144
+ "#{quote_with_table(assoc.join.aliased_table_name, column.__name)}" :
145
+ nil
146
+ }.compact
147
+ end
148
+ end
149
+
150
+ def columns_with_prefixes
151
+ @columns.collect { |column|
152
+ column_with_prefix column
153
+ }.flatten.compact
154
+ end
155
+
156
+ def column_available?(column)
157
+ if column.is_string?
158
+ true
159
+ elsif column.__stack.empty?
160
+ @model.column_names.include?(column.__name.to_s)
161
+ else
162
+ associations[column.__stack].any? { |assoc|
163
+ assoc.has_column?(column.__name)
164
+ }
165
+ end
166
+ end
167
+
168
+ # Gets a stack of associations for a specific path.
169
+ #
170
+ def association_stack(path, parent = nil)
171
+ assocs = []
172
+
173
+ if parent.nil?
174
+ assocs = @source.association(path.shift)
175
+ else
176
+ assocs = parent.children(path.shift)
177
+ end
178
+
179
+ until path.empty?
180
+ point = path.shift
181
+ assocs = assocs.collect { |assoc| assoc.children(point) }.flatten
182
+ end
183
+
184
+ assocs
185
+ end
186
+ end
187
+ end
@@ -0,0 +1,43 @@
1
+ require 'thinking_sphinx'
2
+ require 'rails'
3
+
4
+ module ThinkingSphinx
5
+ class Railtie < Rails::Railtie
6
+
7
+ initializer 'thinking_sphinx.sphinx' do
8
+ ThinkingSphinx::AutoVersion.detect
9
+ end
10
+
11
+ initializer "thinking_sphinx.active_record" do
12
+ ActiveSupport.on_load :active_record do
13
+ include ThinkingSphinx::ActiveRecord
14
+ end
15
+ end
16
+
17
+ initializer "thinking_sphinx.action_controller" do
18
+ ActiveSupport.on_load :action_controller do
19
+ require 'thinking_sphinx/action_controller'
20
+ include ThinkingSphinx::ActionController
21
+ end
22
+ end
23
+
24
+ initializer "thinking_sphinx.set_app_root" do |app|
25
+ ThinkingSphinx::Configuration.instance.reset # Rails has setup app now
26
+ end
27
+
28
+ config.to_prepare do
29
+ # ActiveRecord::Base.to_crc32s is dependant on the subclasses being loaded
30
+ # consistently. When the environment is reset, subclasses/descendants will
31
+ # be lost but our context will not reload them for us.
32
+ #
33
+ # We reset the context which causes the subclasses/descendants to be
34
+ # reloaded next time the context is called.
35
+ #
36
+ ThinkingSphinx.reset_context!
37
+ end
38
+
39
+ rake_tasks do
40
+ load File.expand_path('../tasks.rb', __FILE__)
41
+ end
42
+ end
43
+ end