summaryrefslogtreecommitdiff
path: root/vendor/plugins/thinking-sphinx/lib/thinking_sphinx/rails_additions.rb
blob: c7db0a15eb582a9a2c2409fbbcf15f8115b4fdd5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
module ThinkingSphinx
  module HashExcept
    # Returns a new hash without the given keys.
    def except(*keys)
      rejected = Set.new(respond_to?(:convert_key) ? keys.map { |key| convert_key(key) } : keys)
      reject { |key,| rejected.include?(key) }
    end

    # Replaces the hash without only the given keys.
    def except!(*keys)
      replace(except(*keys))
    end
  end
end

Hash.send(
  :include, ThinkingSphinx::HashExcept
) unless Hash.instance_methods.include?("except")

module ThinkingSphinx
  module ArrayExtractOptions
    def extract_options!
      last.is_a?(::Hash) ? pop : {}
    end
  end
end

Array.send(
  :include, ThinkingSphinx::ArrayExtractOptions
) unless Array.instance_methods.include?("extract_options!")

module ThinkingSphinx
  module AbstractQuotedTableName
    def quote_table_name(name)
      quote_column_name(name)
    end
  end
end

ActiveRecord::ConnectionAdapters::AbstractAdapter.send(
  :include, ThinkingSphinx::AbstractQuotedTableName
) unless ActiveRecord::ConnectionAdapters::AbstractAdapter.instance_methods.include?("quote_table_name")

module ThinkingSphinx
  module MysqlQuotedTableName
    def quote_table_name(name) #:nodoc:
      quote_column_name(name).gsub('.', '`.`')
    end
  end
end

if ActiveRecord::ConnectionAdapters.constants.include?("MysqlAdapter") or ActiveRecord::Base.respond_to?(:jdbcmysql_connection)
  adapter = ActiveRecord::ConnectionAdapters.const_get(
    defined?(JRUBY_VERSION) ? :JdbcAdapter : :MysqlAdapter
  )
  unless adapter.instance_methods.include?("quote_table_name")
    adapter.send(:include, ThinkingSphinx::MysqlQuotedTableName)
  end
end

module ThinkingSphinx
  module ActiveRecordQuotedName
    def quoted_table_name
      self.connection.quote_table_name(self.table_name)
    end 
  end
end

ActiveRecord::Base.extend(
  ThinkingSphinx::ActiveRecordQuotedName
) unless ActiveRecord::Base.respond_to?("quoted_table_name")

module ThinkingSphinx
  module ActiveRecordStoreFullSTIClass
    def store_full_sti_class
      false
    end
  end
end

ActiveRecord::Base.extend(
  ThinkingSphinx::ActiveRecordStoreFullSTIClass
) unless ActiveRecord::Base.respond_to?(:store_full_sti_class)

module ThinkingSphinx
  module ClassAttributeMethods
    def cattr_reader(*syms)
      syms.flatten.each do |sym|
        next if sym.is_a?(Hash)
        class_eval(<<-EOS, __FILE__, __LINE__)
          unless defined? @@#{sym}
            @@#{sym} = nil
          end

          def self.#{sym}
            @@#{sym}
          end

          def #{sym}
            @@#{sym}
          end
        EOS
      end
    end

    def cattr_writer(*syms)
      options = syms.extract_options!
      syms.flatten.each do |sym|
        class_eval(<<-EOS, __FILE__, __LINE__)
          unless defined? @@#{sym}
            @@#{sym} = nil
          end

          def self.#{sym}=(obj)
            @@#{sym} = obj
          end

          #{"
          def #{sym}=(obj)
            @@#{sym} = obj
          end
          " unless options[:instance_writer] == false }
        EOS
      end
    end

    def cattr_accessor(*syms)
      cattr_reader(*syms)
      cattr_writer(*syms)
    end
  end
end

Class.extend(
  ThinkingSphinx::ClassAttributeMethods
) unless Class.respond_to?(:cattr_reader)