Module: Torque::PostgreSQL::Base::ClassMethods
- Defined in:
- lib/torque/postgresql/base.rb
Instance Method Summary collapse
-
#inherited(subclass) ⇒ Object
Wenever it’s inherited, add a new list of auxiliary statements It also adds an auxiliary statement to load inherited records’ relname.
Instance Method Details
#inherited(subclass) ⇒ Object
Wenever it’s inherited, add a new list of auxiliary statements It also adds an auxiliary statement to load inherited records’ relname
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 |
# File 'lib/torque/postgresql/base.rb', line 11 def inherited(subclass) super subclass.class_attribute(:auxiliary_statements_list) subclass.auxiliary_statements_list = Hash.new record_class = ActiveRecord::Relation._record_class_attribute # Define helper methods to return the class of the given records subclass.auxiliary_statement record_class do |cte| pg_class = ::Arel::Table.new('pg_class') arel_query = ::Arel::SelectManager.new(pg_class) arel_query.project(pg_class['oid'], pg_class['relname'].as(record_class.to_s)) cte.query 'pg_class', arel_query.to_sql cte.attributes col(record_class) => record_class cte.join tableoid: :oid end # Define the dynamic attribute that returns the same information as # the one provided by the auxiliary statement subclass.dynamic_attribute(record_class) do next self.class.table_name unless self.class.physically_inheritances? pg_class = ::Arel::Table.new('pg_class') source = ::Arel::Table.new(subclass.table_name, as: 'source') quoted_id = ::Arel::Nodes::Quoted.new(self.class.connection.quote(id)) query = ::Arel::SelectManager.new(pg_class) query.join(source).on(pg_class['oid'].eq(source['tableoid'])) query.where(source[subclass.primary_key].eq(quoted_id)) query.project(pg_class['relname']) self.class.connection.select_value(query) end end |