1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/lib/arel/table.rb
Sean Griffin 7508284800 Remove engine from the constructor arguments Arel::Table
It is never used outside of convenience methods which are only used in
tests. In practice, it just made constructing tables more complicated on
the rails side. This is the minimum possible change to remove the
constructor argument, but continue to have the tests passing.

I'm not sure if we have a reason to keep `project` and friends, and the
solution might actually just be to remove the engine from
`SelectManager` and friends. As such I've held off on deleting those
methods.

We need to figure out what to do with `Table#from`. It's old invocation,
which read `table.from(table)` was certainly nonsensical.
2014-11-29 16:08:51 -07:00

113 lines
2.4 KiB
Ruby

module Arel
class Table
include Arel::Crud
include Arel::FactoryMethods
@engine = nil
class << self; attr_accessor :engine; end
attr_accessor :name, :engine, :aliases, :table_alias
# TableAlias and Table both have a #table_name which is the name of the underlying table
alias :table_name :name
def initialize name, options = {}
@name = name.to_s
@columns = nil
@aliases = []
@engine = Table.engine
# Sometime AR sends an :as parameter to table, to let the table know
# that it is an Alias. We may want to override new, and return a
# TableAlias node?
@table_alias = options[:as]
if @table_alias.to_s == @name
@table_alias = nil
end
end
def alias name = "#{self.name}_2"
Nodes::TableAlias.new(self, name).tap do |node|
@aliases << node
end
end
def from engine = Table.engine
SelectManager.new(engine, self)
end
def join relation, klass = Nodes::InnerJoin
return from unless relation
case relation
when String, Nodes::SqlLiteral
raise if relation.empty?
klass = Nodes::StringJoin
end
from.join(relation, klass)
end
def outer_join relation
join(relation, Nodes::OuterJoin)
end
def group *columns
from.group(*columns)
end
def order *expr
from.order(*expr)
end
def where condition
from.where condition
end
def project *things
from.project(*things)
end
def take amount
from.take amount
end
def skip amount
from.skip amount
end
def having expr
from.having expr
end
def [] name
::Arel::Attribute.new self, name
end
def hash
# Perf note: aliases and table alias is excluded from the hash
# aliases can have a loop back to this table breaking hashes in parent
# relations, for the vast majority of cases @name is unique to a query
@name.hash
end
def eql? other
self.class == other.class &&
self.name == other.name &&
self.aliases == other.aliases &&
self.table_alias == other.table_alias
end
alias :== :eql?
private
def attributes_for columns
return nil unless columns
columns.map do |column|
Attributes.for(column).new self, column.name.to_sym
end
end
end
end