Class: Arj::Relation

Inherits:
Object
  • Object
show all
Defined in:
lib/arj/relation.rb

Overview

Wrapper for ActiveRecord::Relation which maps record objects to job objects.

See Query

Instance Method Summary collapse

Constructor Details

#initialize(ar_relation) ⇒ Relation

Returns an Arj::Relation which wraps the specified ActiveRecord Relation, WhereChain, etc.



13
14
15
# File 'lib/arj/relation.rb', line 13

def initialize(ar_relation)
  @ar_relation = ar_relation
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method) ⇒ Object

Delegates to the wrapped ActiveRecord relation and maps record objects to job objects.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/arj/relation.rb', line 18

def method_missing(method, *, &)
  result = @ar_relation.send(method, *, &)
  case result
  when ActiveRecord::Relation, ActiveRecord::QueryMethods::WhereChain
    Relation.new(result)
  when Array
    result.map do |item|
      if item.is_a?(Arj.record_class)
        Persistence.from_record(item)
      else
        item
      end
    end
  when Arj.record_class
    Persistence.from_record(result)
  else
    result
  end
end

Instance Method Details

#pretty_print(pp) ⇒ NilClass

Implemented to provide pretty_inspect output containing arrays of jobs rather than arrays of records, similar to the output produced when calling pretty_inspect on ActiveRecord::Relation.

Instead of the default pretty_inspect output:

[1] pry(main)> Arj.all
=> #<Arj::Relation:0x000000010bc44508
@ar_relation=
 [#<Job:0x000000010b54b2d8
  id: 1,
  ...

Produces:

[1] pry(main)> Arj.all
=> [#<Arj::Test::Job:0x0000000110ab0840
  @_scheduled_at_time=nil,
  @arguments=[],
  ...

Returns:

  • (NilClass)


74
75
76
# File 'lib/arj/relation.rb', line 74

def pretty_print(pp)
  pp.pp(to_a)
end

#respond_to_missing?Boolean

Implemented to ensure Arj::Relation#method can be used to retrieve methods provided by ActiveRecord relations.

Returns:

  • (Boolean)


41
42
43
# File 'lib/arj/relation.rb', line 41

def respond_to_missing?(*)
  @ar_relation.respond_to?(*)
end

#update_job!(attributes) ⇒ Array<ActiveJob::Base>

Updates each matching job with the specified attributes.

Returns:

  • (Array<ActiveJob::Base>)


48
49
50
51
52
53
# File 'lib/arj/relation.rb', line 48

def update_job!(attributes)
  to_a.map do |job|
    job.update!(attributes)
    job
  end
end