Class: ThinkingSphinx::ActiveRecord::SQLSource

Inherits:
Riddle::Configuration::SQLSource
  • Object
show all
Includes:
Core::Settings
Defined in:
lib/thinking_sphinx/active_record/sql_source.rb

Defined Under Namespace

Classes: Template

Constant Summary collapse

OPTIONS =
[:name, :offset, :delta_processor, :delta?, :delta_options,
:disable_range?, :group_concat_max_len, :utf8?, :position,
:minimal_group_by?, :big_document_ids]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, options = {}) ⇒ SQLSource

Returns a new instance of SQLSource.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/thinking_sphinx/active_record/sql_source.rb', line 16

def initialize(model, options = {})
  @model             = model
  @options           = {
    :utf8? => (database_settings[:encoding].to_s[/^utf8/])
  }.merge options

  @fields            = []
  @attributes        = []
  @associations      = []
  @conditions        = []
  @groupings         = []
  @polymorphs        = []

  Template.new(self).apply

  name = "#{options[:name] || model.name.downcase}_#{options[:position]}"

  super name, type

  apply_defaults!
end

Instance Attribute Details

#associationsObject

Returns the value of attribute associations.



9
10
11
# File 'lib/thinking_sphinx/active_record/sql_source.rb', line 9

def associations
  @associations
end

#attributesObject

Returns the value of attribute attributes.



9
10
11
# File 'lib/thinking_sphinx/active_record/sql_source.rb', line 9

def attributes
  @attributes
end

#conditionsObject

Returns the value of attribute conditions.



9
10
11
# File 'lib/thinking_sphinx/active_record/sql_source.rb', line 9

def conditions
  @conditions
end

#fieldsObject

Returns the value of attribute fields.



9
10
11
# File 'lib/thinking_sphinx/active_record/sql_source.rb', line 9

def fields
  @fields
end

#groupingsObject

Returns the value of attribute groupings.



9
10
11
# File 'lib/thinking_sphinx/active_record/sql_source.rb', line 9

def groupings
  @groupings
end

#modelObject (readonly)

Returns the value of attribute model.



8
9
10
# File 'lib/thinking_sphinx/active_record/sql_source.rb', line 8

def model
  @model
end

#optionsObject (readonly)

Returns the value of attribute options.



8
9
10
# File 'lib/thinking_sphinx/active_record/sql_source.rb', line 8

def options
  @options
end

#polymorphsObject

Returns the value of attribute polymorphs.



9
10
11
# File 'lib/thinking_sphinx/active_record/sql_source.rb', line 9

def polymorphs
  @polymorphs
end

Instance Method Details

#adapterObject



38
39
40
# File 'lib/thinking_sphinx/active_record/sql_source.rb', line 38

def adapter
  @adapter ||= DatabaseAdapters.adapter_for(@model)
end

#add_attribute(attribute) ⇒ Object



42
43
44
45
46
# File 'lib/thinking_sphinx/active_record/sql_source.rb', line 42

def add_attribute(attribute)
  attributes.delete_if { |existing| existing.name == attribute.name }

  attributes << attribute
end

#add_field(field) ⇒ Object



48
49
50
51
52
# File 'lib/thinking_sphinx/active_record/sql_source.rb', line 48

def add_field(field)
  fields.delete_if { |existing| existing.name == field.name }

  fields << field
end

#delta?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/thinking_sphinx/active_record/sql_source.rb', line 58

def delta?
  options[:delta?]
end

#delta_processorObject



54
55
56
# File 'lib/thinking_sphinx/active_record/sql_source.rb', line 54

def delta_processor
  options[:delta_processor].try(:new, adapter, @options[:delta_options] || {})
end

#disable_range?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/thinking_sphinx/active_record/sql_source.rb', line 62

def disable_range?
  options[:disable_range?]
end

#facetsObject



66
67
68
# File 'lib/thinking_sphinx/active_record/sql_source.rb', line 66

def facets
  properties.select(&:facet?)
end

#offsetObject



70
71
72
# File 'lib/thinking_sphinx/active_record/sql_source.rb', line 70

def offset
  options[:offset]
end

#primary_keyObject



74
75
76
# File 'lib/thinking_sphinx/active_record/sql_source.rb', line 74

def primary_key
  options[:primary_key]
end

#propertiesObject



78
79
80
# File 'lib/thinking_sphinx/active_record/sql_source.rb', line 78

def properties
  fields + attributes
end

#renderObject



82
83
84
85
86
# File 'lib/thinking_sphinx/active_record/sql_source.rb', line 82

def render
  prepare_for_render unless @prepared

  super
end

#set_database_settings(settings) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/thinking_sphinx/active_record/sql_source.rb', line 88

def set_database_settings(settings)
  @sql_host ||= settings[:host]     || 'localhost'
  @sql_user ||= settings[:username] || settings[:user] || ENV['USER']
  @sql_pass ||= settings[:password].to_s.gsub('#', '\#')
  @sql_db   ||= settings[:database]
  @sql_port ||= settings[:port]
  @sql_sock ||= settings[:socket]
  @mysql_ssl_cert ||= settings[:sslcert]
  @mysql_ssl_key  ||= settings[:sslkey]
  @mysql_ssl_ca   ||= settings[:sslca]
end

#typeObject



100
101
102
103
104
105
106
107
108
109
# File 'lib/thinking_sphinx/active_record/sql_source.rb', line 100

def type
  @type ||= case adapter
  when DatabaseAdapters::MySQLAdapter
    'mysql'
  when DatabaseAdapters::PostgreSQLAdapter
    'pgsql'
  else
    raise UnknownDatabaseAdapter, "Provided type: #{adapter.class.name}"
  end
end