1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/test/support/fake_record.rb

124 lines
2.3 KiB
Ruby
Raw Normal View History

2010-09-24 19:23:25 -04:00
module FakeRecord
class Column < Struct.new(:name, :type)
end
class Connection
2013-03-15 01:22:34 -04:00
attr_reader :tables
attr_accessor :visitor
2010-09-24 19:23:25 -04:00
def initialize(visitor = nil)
@tables = %w{ users photos developers products}
2010-09-24 19:23:25 -04:00
@columns = {
'users' => [
Column.new('id', :integer),
Column.new('name', :string),
Column.new('bool', :boolean),
Column.new('created_at', :date)
],
'products' => [
Column.new('id', :integer),
Column.new('price', :decimal)
2010-09-24 19:23:25 -04:00
]
}
@columns_hash = {
'users' => Hash[@columns['users'].map { |x| [x.name, x] }],
'products' => Hash[@columns['products'].map { |x| [x.name, x] }]
}
2010-09-24 19:23:25 -04:00
@primary_keys = {
'users' => 'id',
'products' => 'id'
2010-09-24 19:23:25 -04:00
}
@visitor = visitor
2010-09-24 19:23:25 -04:00
end
2013-03-15 01:22:34 -04:00
def columns_hash table_name
@columns_hash[table_name]
end
2010-09-24 19:23:25 -04:00
def primary_key name
@primary_keys[name.to_s]
end
def table_exists? name
@tables.include? name.to_s
end
def columns name, message = nil
@columns[name.to_s]
end
def quote_table_name name
"\"#{name.to_s}\""
end
def quote_column_name name
"\"#{name.to_s}\""
end
def schema_cache
self
end
2010-09-24 19:23:25 -04:00
def quote thing, column = nil
2010-09-27 14:32:04 -04:00
if column && column.type == :integer
return 'NULL' if thing.nil?
return thing.to_i
end
2010-09-24 19:23:25 -04:00
case thing
when true
"'t'"
when false
"'f'"
when nil
'NULL'
when Numeric
thing
else
"'#{thing}'"
end
end
end
class ConnectionPool
class Spec < Struct.new(:config)
end
attr_reader :spec, :connection
2010-09-24 19:23:25 -04:00
def initialize
@spec = Spec.new(:adapter => 'america')
@connection = Connection.new
@connection.visitor = Arel::Visitors::ToSql.new(connection)
2010-09-24 19:23:25 -04:00
end
def with_connection
yield connection
end
def table_exists? name
connection.tables.include? name.to_s
end
def columns_hash
connection.columns_hash
end
def schema_cache
connection
end
2010-09-24 19:23:25 -04:00
end
class Base
attr_accessor :connection_pool
def initialize
@connection_pool = ConnectionPool.new
2010-09-24 19:23:25 -04:00
end
def connection
2010-09-24 19:23:25 -04:00
connection_pool.connection
end
end
end