Module: ActiveRecord::Associations::HyperHasAndBelongsToManyAssociationExtension

Included in:
HasAndBelongsToManyAssociation
Defined in:
lib/associations/hyper_has_and_belongs_to_many_association_extension.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/associations/hyper_has_and_belongs_to_many_association_extension.rb', line 29

def self.included(base)
  base.class_eval do
    alias_method :find_without_hypertable, :find
    alias_method :find, :find_with_hypertable

    alias_method :delete_records_without_hypertable, :delete_records
    alias_method :delete_records, :delete_records_with_hypertable

    alias_method :insert_record_without_hypertable, :insert_record
    alias_method :insert_record, :insert_record_with_hypertable

    alias_method :create_record_without_hypertable, :create_record
    alias_method :create_record, :create_record_with_hypertable
  end
end

Instance Method Details

#delete_records_with_hypertable(records) ⇒ Object

Remove the association from the assocation columns.



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/associations/hyper_has_and_belongs_to_many_association_extension.rb', line 67

def delete_records_with_hypertable(records)
  if @reflection.klass <= ActiveRecord::HyperBase
    cells_to_delete_by_table = Hash.new{|h,k| h[k] = []}

    records.each {|r|
      # remove association cells from in memory object
      @owner.send(@reflection.association_foreign_key).delete(r.ROW)
      r.send(@reflection.primary_key_name).delete(@owner.ROW)

      # make list of cells that need to be removed from hypertable
      cells_to_delete_by_table[@owner.class.table_name] << @owner.connection.cell_native_array(@owner.ROW, @reflection.association_foreign_key, r.ROW)
      cells_to_delete_by_table[r.class.table_name] << @owner.connection.cell_native_array(r.ROW, @reflection.primary_key_name, @owner.ROW)
    }

    for table in cells_to_delete_by_table.keys
      @owner.delete_cells(cells_to_delete_by_table[table], table)
    end
  else
    delete_records_without_hypertable(records)
  end
end

#find_with_hypertable(*args) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/associations/hyper_has_and_belongs_to_many_association_extension.rb', line 45

def find_with_hypertable(*args)
  if @reflection.klass <= ActiveRecord::HyperBase
    associated_object_ids = @owner.send(@reflection.association_foreign_key).keys
    @reflection.klass.find(associated_object_ids)
  else
    find_without_hypertable(*args)
  end
end

#insert_record_with_hypertable(record, force = true) ⇒ Object

Record the association in the assocation columns.



55
56
57
58
59
60
61
62
63
64
# File 'lib/associations/hyper_has_and_belongs_to_many_association_extension.rb', line 55

def insert_record_with_hypertable(record, force=true)
  if @reflection.klass <= ActiveRecord::HyperBase
    @owner.send(@reflection.association_foreign_key)[record.ROW] = 1
    @owner.write_cells([@owner.connection.cell_native_array(@owner.ROW, @reflection.association_foreign_key, record.ROW, "1")])
    record.send(@reflection.primary_key_name)[@owner.ROW] = 1
    record.write_cells([@owner.connection.cell_native_array(record.ROW, @reflection.primary_key_name, @owner.ROW, "1")])
  else
    insert_record_without_hypertable(record, force)
  end
end