Sha256: da81aac925674efe8eff2f3a998e8f67e6ec01781c5c650b264a57e5c163719b

Contents?: true

Size: 1.33 KB

Versions: 16

Compression:

Stored size: 1.33 KB

Contents

class ActiveRecord::Associations::HasAndBelongsToManyAssociation
  def should_wrap_the_connection?
    @owner.respond_to?(:current_shard) && @owner.current_shard != nil
  end
  
  def insert_record(record, force = true, validate = true)
    if record.new_record?
      if force
        record.save!
      else
        return false unless record.save(:validate => validate)
      end
    end

    if @reflection.options[:insert_sql]
      @owner.connection.insert(interpolate_sql(@reflection.options[:insert_sql], record))
    else
      relation = Arel::Table.new(@reflection.options[:join_table])
      attributes = columns.inject({}) do |attrs, column|
        case column.name.to_s
        when @reflection.primary_key_name.to_s
          attrs[relation[column.name]] = owner_quoted_id
        when @reflection.association_foreign_key.to_s
          attrs[relation[column.name]] = record.quoted_id
        else
          if record.has_attribute?(column.name)
            value = @owner.send(:quote_value, record[column.name], column)
            attrs[relation[column.name]] = value unless value.nil?
          end
        end
        attrs
      end
      
      if should_wrap_the_connection?
        @owner.using(@owner.current_shard) { relation.insert(attributes) } 
      else
        relation.insert(attributes)
      end
    end

    return true
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
ar-octopus-0.0.19 lib/octopus/has_and_belongs_to_many_association.rb
ar-octopus-0.0.18 lib/octopus/has_and_belongs_to_many_association.rb
ar-octopus-0.0.16 lib/octopus/has_and_belongs_to_many_association.rb
ar-octopus-0.0.15 lib/octopus/has_and_belongs_to_many_association.rb
ar-octopus-0.0.14 lib/octopus/has_and_belongs_to_many_association.rb
ar-octopus-0.0.13 lib/octopus/has_and_belongs_to_many_association.rb
ar-octopus-0.0.12 lib/octopus/has_and_belongs_to_many_association.rb
ar-octopus-0.0.11 lib/octopus/has_and_belongs_to_many_association.rb
ar-octopus-0.0.10 lib/octopus/has_and_belongs_to_many_association.rb
ar-octopus-0.0.9 lib/octopus/has_and_belongs_to_many_association.rb
ar-octopus-0.0.8 lib/octopus/has_and_belongs_to_many_association.rb
ar-octopus-0.0.7 lib/octopus/has_and_belongs_to_many_association.rb
ar-octopus-0.0.6 lib/octopus/has_and_belongs_to_many_association.rb
ar-octopus-0.0.5 lib/octopus/has_and_belongs_to_many_association.rb
ar-octopus-0.0.4 lib/octopus/has_and_belongs_to_many_association.rb
ar-octopus-0.0.3 lib/octopus/has_and_belongs_to_many_association.rb