Class: Fiona7::TableSwitcher
- Inherits:
-
Object
- Object
- Fiona7::TableSwitcher
- Defined in:
- lib/fiona7/table_switcher.rb
Instance Attribute Summary collapse
-
#klass ⇒ Object
Returns the value of attribute klass.
-
#table_name ⇒ Object
Returns the value of attribute table_name.
-
#table_prefix ⇒ Object
Returns the value of attribute table_prefix.
-
#table_suffix ⇒ Object
Returns the value of attribute table_suffix.
Instance Method Summary collapse
-
#initialize(klass, table_suffix) ⇒ TableSwitcher
constructor
A new instance of TableSwitcher.
- #switch ⇒ Object
Constructor Details
#initialize(klass, table_suffix) ⇒ TableSwitcher
Returns a new instance of TableSwitcher.
6 7 8 9 10 11 |
# File 'lib/fiona7/table_switcher.rb', line 6 def initialize(klass, table_suffix) self.klass = klass self.table_prefix = klass.table_name_prefix self.table_suffix = table_suffix self.table_name = "#{self.table_prefix}#{self.table_suffix}" end |
Instance Attribute Details
#klass ⇒ Object
Returns the value of attribute klass.
4 5 6 |
# File 'lib/fiona7/table_switcher.rb', line 4 def klass @klass end |
#table_name ⇒ Object
Returns the value of attribute table_name.
4 5 6 |
# File 'lib/fiona7/table_switcher.rb', line 4 def table_name @table_name end |
#table_prefix ⇒ Object
Returns the value of attribute table_prefix.
4 5 6 |
# File 'lib/fiona7/table_switcher.rb', line 4 def table_prefix @table_prefix end |
#table_suffix ⇒ Object
Returns the value of attribute table_suffix.
4 5 6 |
# File 'lib/fiona7/table_switcher.rb', line 4 def table_suffix @table_suffix end |
Instance Method Details
#switch ⇒ Object
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 |
# File 'lib/fiona7/table_switcher.rb', line 13 def switch if klass.table_name != self.table_name value = self.table_name klass_name = klass.name Rails.logger.debug "Switching table name to #{value} on #{self.klass.name}" # TODO: cache values for two modes somehow? (to improve performance) # NOTE: we abuse the fact that the two tables have identical schemes # and only swap out the table name sneakly, skipping DB schema lookup # and also reloading of attributes, which can break the app # (by removing primary key accessor :id) klass.instance_eval do connection.clear_cache! @table_name = value @quoted_table_name = nil @arel_table = nil @sequence_name = nil unless defined?(@explicit_sequence_name) && @explicit_sequence_name @relation = ActiveRecord::Relation.create(self, arel_table) if self == Fiona7::WriteObj # reset associations, otherwise they go out of sync has_many :children, :class_name => 'RailsConnector::AbstractObj', :foreign_key => 'parent_obj_id' belongs_to :parent, :class_name => 'RailsConnector::AbstractObj', :foreign_key => 'parent_obj_id' end end end end |