Module: WCC::Media::ActiveRecordShim::ClassMethods

Defined in:
lib/wcc/media/active_record_shim.rb

Instance Method Summary collapse

Instance Method Details

#active_record_shim(&block) ⇒ Object



26
27
28
29
30
31
# File 'lib/wcc/media/active_record_shim.rb', line 26

def active_record_shim(&block)
  builder = ShimBuilder.new
  builder.instance_eval(&block)
  builder.apply(self)
  self
end

#find(id, **params) ⇒ Object



33
34
35
# File 'lib/wcc/media/active_record_shim.rb', line 33

def find(id, **params)
  WCC::Media::Client.default.public_send(endpoint).find(id, **params)
end

#find_all(**filters) ⇒ Object



37
38
39
# File 'lib/wcc/media/active_record_shim.rb', line 37

def find_all(**filters)
  WCC::Media::Client.default.public_send(endpoint).list(**filters)
end

#find_by(**filters) ⇒ Object

Raises:

  • (ArgumentError)


41
42
43
44
45
# File 'lib/wcc/media/active_record_shim.rb', line 41

def find_by(**filters)
  raise ArgumentError, 'You must provide at least one filter' if filters.empty?

  find_all(**filters).first
end

#find_in_batches(options, &block) ⇒ Object



59
60
61
62
63
64
65
66
67
68
# File 'lib/wcc/media/active_record_shim.rb', line 59

def find_in_batches(options, &block)
  options = options ? options.dup : {}
  batch_size = options.delete(:batch_size) || 1000
  filter = {
    limit: batch_size,
    offset: options.delete(:start) || 0
  }

  find_all(**filter).each_slice(batch_size, &block)
end

#model_nameObject



47
48
49
# File 'lib/wcc/media/active_record_shim.rb', line 47

def model_name
  ActiveModel::Name.new(self, nil, name)
end

#table_nameObject



51
52
53
# File 'lib/wcc/media/active_record_shim.rb', line 51

def table_name
  endpoint
end

#unscopedObject



55
56
57
# File 'lib/wcc/media/active_record_shim.rb', line 55

def unscoped
  yield
end

#where(**conditions) ⇒ Object



70
71
72
73
74
# File 'lib/wcc/media/active_record_shim.rb', line 70

def where(**conditions)
  # TODO: return a Query object that implements more of the ActiveRecord query interface
  # https://guides.rubyonrails.org/active_record_querying.html#conditions
  find_all(**conditions)
end