Class: ThinkingSphinx::Property

Inherits:
Object
  • Object
show all
Defined in:
lib/thinking_sphinx/property.rb

Direct Known Subclasses

Attribute, Field

Instance Attribute Summary collapse

Instance Method Summary collapse

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, options = {})
  @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    = options[:as]
  @faceted  = options[:facet]
  @admin    = options[: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

#adminObject

Returns the value of attribute admin.



3
4
5
# File 'lib/thinking_sphinx/property.rb', line 3

def admin
  @admin
end

#aliasObject

Returns the value of attribute alias.



3
4
5
# File 'lib/thinking_sphinx/property.rb', line 3

def alias
  @alias
end

#associationsObject

Returns the value of attribute associations.



3
4
5
# File 'lib/thinking_sphinx/property.rb', line 3

def associations
  @associations
end

#columnsObject

Returns the value of attribute columns.



3
4
5
# File 'lib/thinking_sphinx/property.rb', line 3

def columns
  @columns
end

#facetedObject

Returns the value of attribute faceted.



3
4
5
# File 'lib/thinking_sphinx/property.rb', line 3

def faceted
  @faceted
end

#modelObject

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

Returns:

  • (Boolean)


72
73
74
# File 'lib/thinking_sphinx/property.rb', line 72

def admin?
  admin
end

#changed?(instance) ⇒ Boolean

Returns:

  • (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

Returns:

  • (Boolean)


76
77
78
# File 'lib/thinking_sphinx/property.rb', line 76

def public?
  !admin
end

#to_facetObject



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_sqlObject

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_nameObject

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