Sha256: 7943d04f6f170c16b6fc47e3d5a141577a07ad2526aefe62392cd66c1b235c6c

Contents?: true

Size: 609 Bytes

Versions: 5

Compression:

Stored size: 609 Bytes

Contents

# frozen_string_literal: true

class UpdateAllScope
  def initialize(model: nil, relation: nil)
    @queries = []
    @relation = relation || model.class.where(id: model.id)
  end

  def where(*args)
    @relation = @relation.where(*args)
    return self
  end

  def update(query, *binding_values)
    args = binding_values.size > 0 ? [[query, *binding_values]] : [query]
    @queries << klass.send(:sanitize_sql_for_assignment, *args)
    return self
  end

  def do_query!
    return 0 if @queries.empty?
    return @relation.update_all(@queries.join(','))
  end

  def klass
    @relation.klass
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
atomically-1.0.6 lib/atomically/update_all_scope.rb
atomically-1.0.5 lib/atomically/update_all_scope.rb
atomically-1.0.4 lib/atomically/update_all_scope.rb
atomically-1.0.3 lib/atomically/update_all_scope.rb
atomically-1.0.2 lib/atomically/update_all_scope.rb