Class: ActiveStorage::AwsRecord::Relation

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/active_storage/aws_record/relation.rb

Overview

A lazy relation returned by Attachment.where / Attachment.find_by. It mirrors the in-memory reference Relation: it collects filters/exclusions and, on materialization, runs the cheapest access path for the single-table layout — an owner-adjacency query — then applies residual filters, exclusions, and ordering in Ruby (the attachment set for one owner+name is small). Unsupported filters raise ActiveStorage::QueryNotSupported rather than silently scanning.

In Mode A the query is a strongly-consistent base-table query; in Mode B it runs against the string-keyed GSI (eventually consistent).

Defined Under Namespace

Classes: Ordered, WhereChain

Instance Method Summary collapse

Constructor Details

#initialize(model, filters: {}, exclusions: []) ⇒ Relation

Returns a new instance of Relation.



16
17
18
19
20
# File 'lib/active_storage/aws_record/relation.rb', line 16

def initialize(model, filters: {}, exclusions: [])
  @model = model
  @filters = filters.transform_keys(&:to_sym)
  @exclusions = exclusions
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name) ⇒ Object

Backend filtering beyond the supported keys is not available on the generated collections; query the model class directly instead.

Raises:

  • (ActiveStorage::QueryNotSupported)


73
74
75
76
# File 'lib/active_storage/aws_record/relation.rb', line 73

def method_missing(name, *, &)
  raise ActiveStorage::QueryNotSupported,
    "#{name} is not supported on #{self.class.name}; query #{@model.name}.where(...) directly."
end

Instance Method Details

#delete_allObject

Detach-many’s bulk delete. Wrapped in the model’s transaction so the rows (and their coalesced blob refcount decrements) commit atomically instead of one-at-a-time, matching the contract’s attachment_class.transaction expectation for grouped deletes.



55
56
57
58
59
# File 'lib/active_storage/aws_record/relation.rb', line 55

def delete_all
  records = to_a
  @model.transaction { records.each(&:delete) }
  records
end

#each(&block) ⇒ Object



40
41
42
# File 'lib/active_storage/aws_record/relation.rb', line 40

def each(&block)
  to_a.each(&block)
end

#find_by(attributes) ⇒ Object



36
37
38
# File 'lib/active_storage/aws_record/relation.rb', line 36

def find_by(attributes)
  where(attributes).first
end

#not(attributes) ⇒ Object



28
29
30
# File 'lib/active_storage/aws_record/relation.rb', line 28

def not(attributes)
  self.class.new(@model, filters: @filters, exclusions: @exclusions + [attributes.transform_keys(&:to_sym)])
end

#order(*attributes) ⇒ Object



32
33
34
# File 'lib/active_storage/aws_record/relation.rb', line 32

def order(*attributes)
  Ordered.new(to_a, attributes.flatten)
end

#pluck(*attrs) ⇒ Object



44
45
46
47
48
49
# File 'lib/active_storage/aws_record/relation.rb', line 44

def pluck(*attrs)
  to_a.map do |record|
    values = attrs.map { |a| record.public_send(a) }
    attrs.size == 1 ? values.first : values
  end
end

#reloadObject Also known as: reset



65
66
67
68
# File 'lib/active_storage/aws_record/relation.rb', line 65

def reload
  @to_a = nil
  self
end

#respond_to_missing?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/active_storage/aws_record/relation.rb', line 78

def respond_to_missing?(*)
  false
end

#to_aObject



61
62
63
# File 'lib/active_storage/aws_record/relation.rb', line 61

def to_a
  @to_a ||= filtered(query_records)
end

#where(attributes = nil) ⇒ Object



22
23
24
25
26
# File 'lib/active_storage/aws_record/relation.rb', line 22

def where(attributes = nil)
  return WhereChain.new(self) if attributes.nil?

  self.class.new(@model, filters: @filters.merge(attributes), exclusions: @exclusions)
end