Skip to content

Instantly share code, notes, and snippets.

@xaviershay
Created January 20, 2011 04:12
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xaviershay/787378 to your computer and use it in GitHub Desktop.
Save xaviershay/787378 to your computer and use it in GitHub Desktop.
Bad idea of the week: ARel + DataMapper
# Because I can't figure out how the fuck to get SQL out of datamapper itself
class PGEngine < Arel::Visitors::PostgreSQL
attr_accessor :engine
def initialize(engine)
super
self.engine = engine
end
def connection_pool
self
end
def connection
self
end
def tables
[]
end
def table_exists?(*args)
true
end
def columns(name, description)
model = DataMapper::Model.descendants.detect {|x| name == x.storage_name } || raise("Can't find model for #{name}")
model.properties.map {|x|
OpenStruct.new({name: x.name, type: x.class.to_s.split("::").last.downcase.to_sym})
}
end
def with_connection
yield(self)
end
def quote(value, column)
"'#{value}'"
end
def quote_column_name(value)
"'#{value}'"
end
def quote_table_name(value)
"'#{value.underscore}'"
end
end
posts = Arel::Table.new(:posts, :engine => PGEngine.new(bogus))
posts.project('*').where(pages[:author_id].eq(1)).to_sql
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment