Class: ThinkingSphinx::Property
- Inherits:
-
Object
- Object
- ThinkingSphinx::Property
- Defined in:
- lib/thinking_sphinx/property.rb
Instance Attribute Summary collapse
-
#admin ⇒ Object
Returns the value of attribute admin.
-
#alias ⇒ Object
Returns the value of attribute alias.
-
#associations ⇒ Object
Returns the value of attribute associations.
-
#columns ⇒ Object
Returns the value of attribute columns.
-
#faceted ⇒ Object
Returns the value of attribute faceted.
-
#model ⇒ Object
Returns the value of attribute model.
Instance Method Summary collapse
- #admin? ⇒ Boolean
- #changed?(instance) ⇒ Boolean
-
#initialize(source, columns, options = {}) ⇒ Property
constructor
A new instance of Property.
- #public? ⇒ Boolean
- #to_facet ⇒ Object
-
#to_group_sql ⇒ Object
Get the part of the GROUP BY clause related to this attribute - if one is needed.
-
#unique_name ⇒ Object
Returns the unique name of the attribute - which is either the alias of the attribute, or the name of the only column - if there is only one.
Constructor Details
#initialize(source, columns, options = {}) ⇒ Property
Returns a new instance of Property.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/thinking_sphinx/property.rb', line 5 def initialize(source, columns, = {}) @source = source @model = source.model @columns = Array(columns) @associations = {} 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) } @alias = [:as] @faceted = [:facet] @admin = [:admin] @alias = @alias.to_sym unless @alias.blank? @columns.each { |col| @associations[col] = association_stack(col.__stack.clone).each { |assoc| assoc.join_to(source.base) assoc.columns << col } } end |
Instance Attribute Details
#admin ⇒ Object
Returns the value of attribute admin.
3 4 5 |
# File 'lib/thinking_sphinx/property.rb', line 3 def admin @admin end |
#alias ⇒ Object
Returns the value of attribute alias.
3 4 5 |
# File 'lib/thinking_sphinx/property.rb', line 3 def alias @alias end |
#associations ⇒ Object
Returns the value of attribute associations.
3 4 5 |
# File 'lib/thinking_sphinx/property.rb', line 3 def associations @associations end |
#columns ⇒ Object
Returns the value of attribute columns.
3 4 5 |
# File 'lib/thinking_sphinx/property.rb', line 3 def columns @columns end |
#faceted ⇒ Object
Returns the value of attribute faceted.
3 4 5 |
# File 'lib/thinking_sphinx/property.rb', line 3 def faceted @faceted end |
#model ⇒ Object
Returns the value of attribute model.
3 4 5 |
# File 'lib/thinking_sphinx/property.rb', line 3 def model @model end |
Instance Method Details
#admin? ⇒ Boolean
72 73 74 |
# File 'lib/thinking_sphinx/property.rb', line 72 def admin? admin end |
#changed?(instance) ⇒ Boolean
63 64 65 66 67 68 69 70 |
# File 'lib/thinking_sphinx/property.rb', line 63 def changed?(instance) return true if is_string? || @columns.any? { |col| !col.__stack.empty? } !@columns.all? { |col| instance.respond_to?("#{col.__name.to_s}_changed?") && !instance.send("#{col.__name.to_s}_changed?") } end |
#public? ⇒ Boolean
76 77 78 |
# File 'lib/thinking_sphinx/property.rb', line 76 def public? !admin end |
#to_facet ⇒ Object
40 41 42 43 44 |
# File 'lib/thinking_sphinx/property.rb', line 40 def to_facet return nil unless @faceted ThinkingSphinx::Facet.new(self) end |
#to_group_sql ⇒ Object
Get the part of the GROUP BY clause related to this attribute - if one is needed. If not, all you’ll get back is nil. The latter will happen if there isn’t actually a real column to get data from, or if there’s multiple data values (read: a has_many or has_and_belongs_to_many association).
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/thinking_sphinx/property.rb', line 52 def to_group_sql case when is_many?, is_string?, ThinkingSphinx.use_group_by_shortcut? nil else @columns.collect { |column| column_with_prefix(column) } end end |
#unique_name ⇒ Object
Returns the unique name of the attribute - which is either the alias of the attribute, or the name of the only column - if there is only one. If there isn’t, there should be an alias. Else things probably won’t work. Consider yourself warned.
32 33 34 35 36 37 38 |
# File 'lib/thinking_sphinx/property.rb', line 32 def unique_name if @columns.length == 1 @alias || @columns.first.__name else @alias end end |