initforthe-thinking-sphinx 1.1.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (91) hide show
  1. data/LICENCE +20 -0
  2. data/README.textile +141 -0
  3. data/lib/thinking_sphinx.rb +215 -0
  4. data/lib/thinking_sphinx/active_record.rb +278 -0
  5. data/lib/thinking_sphinx/active_record/attribute_updates.rb +48 -0
  6. data/lib/thinking_sphinx/active_record/delta.rb +87 -0
  7. data/lib/thinking_sphinx/active_record/has_many_association.rb +29 -0
  8. data/lib/thinking_sphinx/active_record/search.rb +57 -0
  9. data/lib/thinking_sphinx/adapters/abstract_adapter.rb +42 -0
  10. data/lib/thinking_sphinx/adapters/mysql_adapter.rb +54 -0
  11. data/lib/thinking_sphinx/adapters/postgresql_adapter.rb +135 -0
  12. data/lib/thinking_sphinx/association.rb +164 -0
  13. data/lib/thinking_sphinx/attribute.rb +268 -0
  14. data/lib/thinking_sphinx/class_facet.rb +15 -0
  15. data/lib/thinking_sphinx/collection.rb +148 -0
  16. data/lib/thinking_sphinx/configuration.rb +262 -0
  17. data/lib/thinking_sphinx/core/string.rb +15 -0
  18. data/lib/thinking_sphinx/deltas.rb +30 -0
  19. data/lib/thinking_sphinx/deltas/datetime_delta.rb +50 -0
  20. data/lib/thinking_sphinx/deltas/default_delta.rb +68 -0
  21. data/lib/thinking_sphinx/deltas/delayed_delta.rb +27 -0
  22. data/lib/thinking_sphinx/deltas/delayed_delta/delta_job.rb +24 -0
  23. data/lib/thinking_sphinx/deltas/delayed_delta/flag_as_deleted_job.rb +27 -0
  24. data/lib/thinking_sphinx/deltas/delayed_delta/job.rb +26 -0
  25. data/lib/thinking_sphinx/deploy/capistrano.rb +82 -0
  26. data/lib/thinking_sphinx/facet.rb +108 -0
  27. data/lib/thinking_sphinx/facet_collection.rb +59 -0
  28. data/lib/thinking_sphinx/field.rb +82 -0
  29. data/lib/thinking_sphinx/index.rb +99 -0
  30. data/lib/thinking_sphinx/index/builder.rb +287 -0
  31. data/lib/thinking_sphinx/index/faux_column.rb +110 -0
  32. data/lib/thinking_sphinx/property.rb +160 -0
  33. data/lib/thinking_sphinx/rails_additions.rb +136 -0
  34. data/lib/thinking_sphinx/search.rb +727 -0
  35. data/lib/thinking_sphinx/search/facets.rb +104 -0
  36. data/lib/thinking_sphinx/source.rb +150 -0
  37. data/lib/thinking_sphinx/source/internal_properties.rb +46 -0
  38. data/lib/thinking_sphinx/source/sql.rb +126 -0
  39. data/lib/thinking_sphinx/tasks.rb +162 -0
  40. data/rails/init.rb +14 -0
  41. data/spec/unit/thinking_sphinx/active_record/delta_spec.rb +136 -0
  42. data/spec/unit/thinking_sphinx/active_record/has_many_association_spec.rb +53 -0
  43. data/spec/unit/thinking_sphinx/active_record/search_spec.rb +107 -0
  44. data/spec/unit/thinking_sphinx/active_record_spec.rb +329 -0
  45. data/spec/unit/thinking_sphinx/association_spec.rb +246 -0
  46. data/spec/unit/thinking_sphinx/attribute_spec.rb +338 -0
  47. data/spec/unit/thinking_sphinx/collection_spec.rb +15 -0
  48. data/spec/unit/thinking_sphinx/configuration_spec.rb +222 -0
  49. data/spec/unit/thinking_sphinx/core/string_spec.rb +9 -0
  50. data/spec/unit/thinking_sphinx/facet_collection_spec.rb +64 -0
  51. data/spec/unit/thinking_sphinx/facet_spec.rb +302 -0
  52. data/spec/unit/thinking_sphinx/field_spec.rb +154 -0
  53. data/spec/unit/thinking_sphinx/index/builder_spec.rb +355 -0
  54. data/spec/unit/thinking_sphinx/index/faux_column_spec.rb +30 -0
  55. data/spec/unit/thinking_sphinx/index_spec.rb +45 -0
  56. data/spec/unit/thinking_sphinx/rails_additions_spec.rb +191 -0
  57. data/spec/unit/thinking_sphinx/search_spec.rb +228 -0
  58. data/spec/unit/thinking_sphinx/source_spec.rb +217 -0
  59. data/spec/unit/thinking_sphinx_spec.rb +151 -0
  60. data/tasks/distribution.rb +67 -0
  61. data/tasks/rails.rake +1 -0
  62. data/tasks/testing.rb +78 -0
  63. data/vendor/after_commit/LICENSE +20 -0
  64. data/vendor/after_commit/README +16 -0
  65. data/vendor/after_commit/Rakefile +22 -0
  66. data/vendor/after_commit/init.rb +8 -0
  67. data/vendor/after_commit/lib/after_commit.rb +45 -0
  68. data/vendor/after_commit/lib/after_commit/active_record.rb +114 -0
  69. data/vendor/after_commit/lib/after_commit/connection_adapters.rb +103 -0
  70. data/vendor/after_commit/test/after_commit_test.rb +53 -0
  71. data/vendor/delayed_job/lib/delayed/job.rb +251 -0
  72. data/vendor/delayed_job/lib/delayed/message_sending.rb +7 -0
  73. data/vendor/delayed_job/lib/delayed/performable_method.rb +55 -0
  74. data/vendor/delayed_job/lib/delayed/worker.rb +54 -0
  75. data/vendor/riddle/lib/riddle.rb +30 -0
  76. data/vendor/riddle/lib/riddle/client.rb +619 -0
  77. data/vendor/riddle/lib/riddle/client/filter.rb +53 -0
  78. data/vendor/riddle/lib/riddle/client/message.rb +65 -0
  79. data/vendor/riddle/lib/riddle/client/response.rb +84 -0
  80. data/vendor/riddle/lib/riddle/configuration.rb +33 -0
  81. data/vendor/riddle/lib/riddle/configuration/distributed_index.rb +48 -0
  82. data/vendor/riddle/lib/riddle/configuration/index.rb +142 -0
  83. data/vendor/riddle/lib/riddle/configuration/indexer.rb +19 -0
  84. data/vendor/riddle/lib/riddle/configuration/remote_index.rb +17 -0
  85. data/vendor/riddle/lib/riddle/configuration/searchd.rb +25 -0
  86. data/vendor/riddle/lib/riddle/configuration/section.rb +43 -0
  87. data/vendor/riddle/lib/riddle/configuration/source.rb +23 -0
  88. data/vendor/riddle/lib/riddle/configuration/sql_source.rb +34 -0
  89. data/vendor/riddle/lib/riddle/configuration/xml_source.rb +28 -0
  90. data/vendor/riddle/lib/riddle/controller.rb +44 -0
  91. metadata +190 -0
@@ -0,0 +1,287 @@
1
+ module ThinkingSphinx
2
+ class Index
3
+ # The Builder class is the core for the index definition block processing.
4
+ # There are four methods you really need to pay attention to:
5
+ # - indexes
6
+ # - has
7
+ # - where
8
+ # - set_property/set_properties
9
+ #
10
+ # The first two of these methods allow you to define what data makes up
11
+ # your indexes. #where provides a method to add manual SQL conditions, and
12
+ # set_property allows you to set some settings on a per-index basis. Check
13
+ # out each method's documentation for better ideas of usage.
14
+ #
15
+ class Builder
16
+ instance_methods.grep(/^[^_]/).each { |method|
17
+ next if method.to_s == "instance_eval"
18
+ define_method(method) {
19
+ caller.grep(/irb.completion/).empty? ? method_missing(method) : super
20
+ }
21
+ }
22
+
23
+ def self.generate(model, &block)
24
+ index = ThinkingSphinx::Index.new(model)
25
+ model.sphinx_facets ||= []
26
+
27
+ Builder.new(index, &block) if block_given?
28
+
29
+ index.delta_object = ThinkingSphinx::Deltas.parse index
30
+ index
31
+ end
32
+
33
+ def initialize(index, &block)
34
+ @index = index
35
+ @source = ThinkingSphinx::Source.new(@index)
36
+ @index.sources << @source
37
+ @explicit_source = false
38
+
39
+ self.instance_eval &block
40
+
41
+ if @index.sources.any? { |source|
42
+ source.fields.length == 0
43
+ }
44
+ raise "At least one field is necessary for an index"
45
+ end
46
+ end
47
+
48
+ def define_source(&block)
49
+ if @explicit_source
50
+ @source = ThinkingSphinx::Source.new(@index)
51
+ @index.sources << @source
52
+ else
53
+ @explicit_source = true
54
+ end
55
+
56
+ self.instance_eval &block
57
+ end
58
+
59
+ # This is how you add fields - the strings Sphinx looks at - to your
60
+ # index. Technically, to use this method, you need to pass in some
61
+ # columns and options - but there's some neat method_missing stuff
62
+ # happening, so lets stick to the expected syntax within a define_index
63
+ # block.
64
+ #
65
+ # Expected options are :as, which points to a column alias in symbol
66
+ # form, and :sortable, which indicates whether you want to sort by this
67
+ # field.
68
+ #
69
+ # Adding Single-Column Fields:
70
+ #
71
+ # You can use symbols or methods - and can chain methods together to
72
+ # get access down the associations tree.
73
+ #
74
+ # indexes :id, :as => :my_id
75
+ # indexes :name, :sortable => true
76
+ # indexes first_name, last_name, :sortable => true
77
+ # indexes users.posts.content, :as => :post_content
78
+ # indexes users(:id), :as => :user_ids
79
+ #
80
+ # Keep in mind that if any keywords for Ruby methods - such as id or
81
+ # name - clash with your column names, you need to use the symbol
82
+ # version (see the first, second and last examples above).
83
+ #
84
+ # If you specify multiple columns (example #2), a field will be created
85
+ # for each. Don't use the :as option in this case. If you want to merge
86
+ # those columns together, continue reading.
87
+ #
88
+ # Adding Multi-Column Fields:
89
+ #
90
+ # indexes [first_name, last_name], :as => :name
91
+ # indexes [location, parent.location], :as => :location
92
+ #
93
+ # To combine multiple columns into a single field, you need to wrap
94
+ # them in an Array, as shown by the above examples. There's no
95
+ # limitations on whether they're symbols or methods or what level of
96
+ # associations they come from.
97
+ #
98
+ # Adding SQL Fragment Fields
99
+ #
100
+ # You can also define a field using an SQL fragment, useful for when
101
+ # you would like to index a calculated value.
102
+ #
103
+ # indexes "age < 18", :as => :minor
104
+ #
105
+ def indexes(*args)
106
+ options = args.extract_options!
107
+ args.each do |columns|
108
+ field = Field.new(@source, FauxColumn.coerce(columns), options)
109
+
110
+ add_sort_attribute field, options if field.sortable
111
+ add_facet_attribute field, options if field.faceted
112
+ end
113
+ end
114
+
115
+ # This is the method to add attributes to your index (hence why it is
116
+ # aliased as 'attribute'). The syntax is the same as #indexes, so use
117
+ # that as starting point, but keep in mind the following points.
118
+ #
119
+ # An attribute can have an alias (the :as option), but it is always
120
+ # sortable - so you don't need to explicitly request that. You _can_
121
+ # specify the data type of the attribute (the :type option), but the
122
+ # code's pretty good at figuring that out itself from peering into the
123
+ # database.
124
+ #
125
+ # Attributes are limited to the following types: integers, floats,
126
+ # datetimes (converted to timestamps), booleans and strings. Don't
127
+ # forget that Sphinx converts string attributes to integers, which are
128
+ # useful for sorting, but that's about it.
129
+ #
130
+ # You can also have a collection of integers for multi-value attributes
131
+ # (MVAs). Generally these would be through a has_many relationship,
132
+ # like in this example:
133
+ #
134
+ # has posts(:id), :as => :post_ids
135
+ #
136
+ # This allows you to filter on any of the values tied to a specific
137
+ # record. Might be best to read through the Sphinx documentation to get
138
+ # a better idea of that though.
139
+ #
140
+ # Adding SQL Fragment Attributes
141
+ #
142
+ # You can also define an attribute using an SQL fragment, useful for
143
+ # when you would like to index a calculated value. Don't forget to set
144
+ # the type of the attribute though:
145
+ #
146
+ # has "age < 18", :as => :minor, :type => :boolean
147
+ #
148
+ # If you're creating attributes for latitude and longitude, don't
149
+ # forget that Sphinx expects these values to be in radians.
150
+ #
151
+ def has(*args)
152
+ options = args.extract_options!
153
+ args.each do |columns|
154
+ attribute = Attribute.new(@source, FauxColumn.coerce(columns), options)
155
+
156
+ add_facet_attribute attribute, options if attribute.faceted
157
+ end
158
+ end
159
+
160
+ def facet(*args)
161
+ options = args.extract_options!
162
+ options[:facet] = true
163
+
164
+ args.each do |columns|
165
+ attribute = Attribute.new(@source, FauxColumn.coerce(columns), options)
166
+
167
+ add_facet_attribute attribute, options
168
+ end
169
+ end
170
+
171
+ # Use this method to add some manual SQL conditions for your index
172
+ # request. You can pass in as many strings as you like, they'll get
173
+ # joined together with ANDs later on.
174
+ #
175
+ # where "user_id = 10"
176
+ # where "parent_type = 'Article'", "created_at < NOW()"
177
+ #
178
+ def where(*args)
179
+ @source.conditions += args
180
+ end
181
+
182
+ # Use this method to add some manual SQL strings to the GROUP BY
183
+ # clause. You can pass in as many strings as you'd like, they'll get
184
+ # joined together with commas later on.
185
+ #
186
+ # group_by "lat", "lng"
187
+ #
188
+ def group_by(*args)
189
+ @source.groupings += args
190
+ end
191
+
192
+ # This is what to use to set properties on the index. Chief amongst
193
+ # those is the delta property - to allow automatic updates to your
194
+ # indexes as new models are added and edited - but also you can
195
+ # define search-related properties which will be the defaults for all
196
+ # searches on the model.
197
+ #
198
+ # set_property :delta => true
199
+ # set_property :field_weights => {"name" => 100}
200
+ # set_property :order => "name ASC"
201
+ # set_property :include => :picture
202
+ # set_property :select => 'name'
203
+ #
204
+ # Also, the following two properties are particularly relevant for
205
+ # geo-location searching - latitude_attr and longitude_attr. If your
206
+ # attributes for these two values are named something other than
207
+ # lat/latitude or lon/long/longitude, you can dictate what they are
208
+ # when defining the index, so you don't need to specify them for every
209
+ # geo-related search.
210
+ #
211
+ # set_property :latitude_attr => "lt", :longitude_attr => "lg"
212
+ #
213
+ # Please don't forget to add a boolean field named 'delta' to your
214
+ # model's database table if enabling the delta index for it.
215
+ # Valid options for the delta property are:
216
+ #
217
+ # true
218
+ # false
219
+ # :default
220
+ # :delayed
221
+ # :datetime
222
+ #
223
+ # You can also extend ThinkingSphinx::Deltas::DefaultDelta to implement
224
+ # your own handling for delta indexing.
225
+ #
226
+ def set_property(*args)
227
+ options = args.extract_options!
228
+ options.each do |key, value|
229
+ set_single_property key, value
230
+ end
231
+
232
+ set_single_property args[0], args[1] if args.length == 2
233
+ end
234
+ alias_method :set_properties, :set_property
235
+
236
+ # Handles the generation of new columns for the field and attribute
237
+ # definitions.
238
+ #
239
+ def method_missing(method, *args)
240
+ FauxColumn.new(method, *args)
241
+ end
242
+
243
+ # A method to allow adding fields from associations which have names
244
+ # that clash with method names in the Builder class (ie: properties,
245
+ # fields, attributes).
246
+ #
247
+ # Example: indexes assoc(:properties).column
248
+ #
249
+ def assoc(assoc, *args)
250
+ FauxColumn.new(assoc, *args)
251
+ end
252
+
253
+ private
254
+
255
+ def set_single_property(key, value)
256
+ source_options = ThinkingSphinx::Configuration::SourceOptions
257
+ if source_options.include?(key.to_s)
258
+ @source.options.merge! key => value
259
+ else
260
+ @index.local_options.merge! key => value
261
+ end
262
+ end
263
+
264
+ def add_sort_attribute(field, options)
265
+ add_internal_attribute field, options, "_sort"
266
+ end
267
+
268
+ def add_facet_attribute(property, options)
269
+ add_internal_attribute property, options, "_facet", true
270
+ @index.model.sphinx_facets << property.to_facet
271
+ end
272
+
273
+ def add_internal_attribute(property, options, suffix, crc = false)
274
+ return unless ThinkingSphinx::Facet.translate?(property)
275
+
276
+ Attribute.new(@source,
277
+ property.columns.collect { |col| col.clone },
278
+ options.merge(
279
+ :type => property.is_a?(Field) ? :string : options[:type],
280
+ :as => property.unique_name.to_s.concat(suffix).to_sym,
281
+ :crc => crc
282
+ ).except(:facet)
283
+ )
284
+ end
285
+ end
286
+ end
287
+ end
@@ -0,0 +1,110 @@
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
+ # Returns true if the stack is empty *and* if the name is a string -
46
+ # which is an indication that of raw SQL, as opposed to a value from a
47
+ # table's column.
48
+ #
49
+ def is_string?
50
+ @name.is_a?(String) && @stack.empty?
51
+ end
52
+
53
+ # This handles any 'invalid' method calls and sets them as the name,
54
+ # and pushing the previous name into the stack. The object returns
55
+ # itself.
56
+ #
57
+ # If there's a single argument, it becomes the name, and the method
58
+ # symbol goes into the stack as well. Multiple arguments means new
59
+ # columns with the original stack and new names (from each argument) gets
60
+ # returned.
61
+ #
62
+ # Easier to explain with examples:
63
+ #
64
+ # col = FauxColumn.new :a, :b, :c
65
+ # col.__name #=> :c
66
+ # col.__stack #=> [:a, :b]
67
+ #
68
+ # col.whatever #=> col
69
+ # col.__name #=> :whatever
70
+ # col.__stack #=> [:a, :b, :c]
71
+ #
72
+ # col.something(:id) #=> col
73
+ # col.__name #=> :id
74
+ # col.__stack #=> [:a, :b, :c, :whatever, :something]
75
+ #
76
+ # cols = col.short(:x, :y, :z)
77
+ # cols[0].__name #=> :x
78
+ # cols[0].__stack #=> [:a, :b, :c, :whatever, :something, :short]
79
+ # cols[1].__name #=> :y
80
+ # cols[1].__stack #=> [:a, :b, :c, :whatever, :something, :short]
81
+ # cols[2].__name #=> :z
82
+ # cols[2].__stack #=> [:a, :b, :c, :whatever, :something, :short]
83
+ #
84
+ # Also, this allows method chaining to build up a relevant stack:
85
+ #
86
+ # col = FauxColumn.new :a, :b
87
+ # col.__name #=> :b
88
+ # col.__stack #=> [:a]
89
+ #
90
+ # col.one.two.three #=> col
91
+ # col.__name #=> :three
92
+ # col.__stack #=> [:a, :b, :one, :two]
93
+ #
94
+ def method_missing(method, *args)
95
+ @stack << @name
96
+ @name = method
97
+
98
+ if (args.empty?)
99
+ self
100
+ elsif (args.length == 1)
101
+ method_missing(args.first)
102
+ else
103
+ args.collect { |arg|
104
+ FauxColumn.new(@stack + [@name, arg])
105
+ }
106
+ end
107
+ end
108
+ end
109
+ end
110
+ end
@@ -0,0 +1,160 @@
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
+
17
+ @columns.each { |col|
18
+ @associations[col] = association_stack(col.__stack.clone).each { |assoc|
19
+ assoc.join_to(source.base)
20
+ }
21
+ }
22
+ end
23
+
24
+ # Returns the unique name of the attribute - which is either the alias of
25
+ # the attribute, or the name of the only column - if there is only one. If
26
+ # there isn't, there should be an alias. Else things probably won't work.
27
+ # Consider yourself warned.
28
+ #
29
+ def unique_name
30
+ if @columns.length == 1
31
+ @alias || @columns.first.__name
32
+ else
33
+ @alias
34
+ end
35
+ end
36
+
37
+ def to_facet
38
+ return nil unless @faceted
39
+
40
+ ThinkingSphinx::Facet.new(self)
41
+ end
42
+
43
+ # Get the part of the GROUP BY clause related to this attribute - if one is
44
+ # needed. If not, all you'll get back is nil. The latter will happen if
45
+ # there isn't actually a real column to get data from, or if there's
46
+ # multiple data values (read: a has_many or has_and_belongs_to_many
47
+ # association).
48
+ #
49
+ def to_group_sql
50
+ case
51
+ when is_many?, is_string?, ThinkingSphinx.use_group_by_shortcut?
52
+ nil
53
+ else
54
+ @columns.collect { |column|
55
+ column_with_prefix(column)
56
+ }
57
+ end
58
+ end
59
+
60
+ def changed?(instance)
61
+ return true if is_string? || @columns.any? { |col| !col.__stack.empty? }
62
+
63
+ !@columns.all? { |col|
64
+ instance.respond_to?("#{col.__name.to_s}_changed?") &&
65
+ !instance.send("#{col.__name.to_s}_changed?")
66
+ }
67
+ end
68
+
69
+ def admin?
70
+ admin
71
+ end
72
+
73
+ def public?
74
+ !admin
75
+ end
76
+
77
+ private
78
+
79
+ # Could there be more than one value related to the parent record? If so,
80
+ # then this will return true. If not, false. It's that simple.
81
+ #
82
+ def is_many?
83
+ associations.values.flatten.any? { |assoc| assoc.is_many? }
84
+ end
85
+
86
+ # Returns true if any of the columns are string values, instead of database
87
+ # column references.
88
+ def is_string?
89
+ columns.all? { |col| col.is_string? }
90
+ end
91
+
92
+ def adapter
93
+ @adapter ||= @model.sphinx_database_adapter
94
+ end
95
+
96
+ def quote_with_table(table, column)
97
+ "#{quote_table_name(table)}.#{quote_column(column)}"
98
+ end
99
+
100
+ def quote_column(column)
101
+ @model.connection.quote_column_name(column)
102
+ end
103
+
104
+ def quote_table_name(table_name)
105
+ @model.connection.quote_table_name(table_name)
106
+ end
107
+
108
+ # Indication of whether the columns should be concatenated with a space
109
+ # between each value. True if there's either multiple sources or multiple
110
+ # associations.
111
+ #
112
+ def concat_ws?
113
+ multiple_associations? || @columns.length > 1
114
+ end
115
+
116
+ # Checks whether any column requires multiple associations (which only
117
+ # happens for polymorphic situations).
118
+ #
119
+ def multiple_associations?
120
+ associations.any? { |col,assocs| assocs.length > 1 }
121
+ end
122
+
123
+ # Builds a column reference tied to the appropriate associations. This
124
+ # dives into the associations hash and their corresponding joins to
125
+ # figure out how to correctly reference a column in SQL.
126
+ #
127
+ def column_with_prefix(column)
128
+ if column.is_string?
129
+ column.__name
130
+ elsif associations[column].empty?
131
+ "#{@model.quoted_table_name}.#{quote_column(column.__name)}"
132
+ else
133
+ associations[column].collect { |assoc|
134
+ assoc.has_column?(column.__name) ?
135
+ "#{quote_with_table(assoc.join.aliased_table_name, column.__name)}" :
136
+ nil
137
+ }.compact.join(', ')
138
+ end
139
+ end
140
+
141
+ # Gets a stack of associations for a specific path.
142
+ #
143
+ def association_stack(path, parent = nil)
144
+ assocs = []
145
+
146
+ if parent.nil?
147
+ assocs = @source.association(path.shift)
148
+ else
149
+ assocs = parent.children(path.shift)
150
+ end
151
+
152
+ until path.empty?
153
+ point = path.shift
154
+ assocs = assocs.collect { |assoc| assoc.children(point) }.flatten
155
+ end
156
+
157
+ assocs
158
+ end
159
+ end
160
+ end