Module: ActiveRecordTemptable
- Extended by:
- ActiveRecordTemptable
- Included in:
- ActiveRecordTemptable
- Defined in:
- lib/active_record_temptable.rb,
lib/active_record_temptable/version.rb
Constant Summary collapse
- DEFAULT_TABLE_NAME =
'activerecord_temptable_records'.freeze
- VERSION =
"0.1.4".freeze
Instance Method Summary collapse
-
#create_indexes(connection, table_name, indexes) ⇒ void
Calls add_index for every element in array.
-
#with_temptable(relation, indexes = [], table_name = DEFAULT_TABLE_NAME) ⇒ void
Yields provided relation loaded in temporary table.
Instance Method Details
#create_indexes(connection, table_name, indexes) ⇒ void
This method returns an undefined value.
Calls add_index for every element in array
39 40 41 42 43 |
# File 'lib/active_record_temptable.rb', line 39 def create_indexes(connection, table_name, indexes) indexes.each do |fields, | connection.add_index(table_name, fields, || {}) end end |
#with_temptable(relation, indexes = [], table_name = DEFAULT_TABLE_NAME) ⇒ void
This method returns an undefined value.
Yields provided relation loaded in temporary table
18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/active_record_temptable.rb', line 18 def with_temptable(relation, indexes = [], table_name = DEFAULT_TABLE_NAME) ActiveRecord::Base.connection_pool.with_connection do |connection| connection.transaction do begin connection.create_table(table_name, temporary: true, as: relation.to_sql) create_indexes(connection, table_name, indexes) klass = relation.klass yield klass.unscoped.from("#{table_name} AS #{klass.table_name}") if block_given? ensure connection.drop_table(table_name, if_exists: true) end end end end |