Module: WCC::API::ActiveRecordShim::ClassMethods

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

Instance Method Summary collapse

Instance Method Details

#find(id) ⇒ Object


24
25
26
# File 'lib/wcc/api/active_record_shim.rb', line 24

def find(id)
  client.public_send(endpoint).find(id)
end

#find_all(**filters) ⇒ Object


28
29
30
# File 'lib/wcc/api/active_record_shim.rb', line 28

def find_all(**filters)
  client.public_send(endpoint).list(filters)
end

#find_by(**filters) ⇒ Object

Raises:

  • (ArgumentError)

32
33
34
35
36
# File 'lib/wcc/api/active_record_shim.rb', line 32

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


50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/wcc/api/active_record_shim.rb', line 50

def find_in_batches(options, &block)
  options = options ? options.dup : {}
  batch_size = options.delete(:batch_size) || 1000
  skip_param = [:skip, :offset]

  filter = {
    limit: batch_size,
    offset: options.delete(:start) || 0
  }

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

#model_nameObject


38
39
40
# File 'lib/wcc/api/active_record_shim.rb', line 38

def model_name
  name
end

#table_nameObject


42
43
44
# File 'lib/wcc/api/active_record_shim.rb', line 42

def table_name
  endpoint
end

#unscopedObject


46
47
48
# File 'lib/wcc/api/active_record_shim.rb', line 46

def unscoped
  yield
end

#where(**conditions) ⇒ Object


63
64
65
66
67
# File 'lib/wcc/api/active_record_shim.rb', line 63

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