Class: ActiveShotgun::AssociationsProxy

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/active_shotgun/associations_proxy.rb

Instance Method Summary collapse

Constructor Details

#initialize(base_class:, base_id:, field_name:, possible_types: {}) ⇒ AssociationsProxy

Returns a new instance of AssociationsProxy.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/active_shotgun/associations_proxy.rb', line 5

def initialize(base_class:, base_id:, field_name:, possible_types: {})
  @base_class = base_class
  @base_id = base_id
  @field_name = field_name
  @possible_types = possible_types
  @queries =
    possible_types.map do |type, klass|
      Query.new(type: type, klass: klass)
    end
  @global_limit = 1000
  @global_offset = 0
  @global_orders = nil
  @global_fields = nil
end

Instance Method Details

#allObject Also known as: to_a



65
66
67
# File 'lib/active_shotgun/associations_proxy.rb', line 65

def all
  @array ||= resolve_all_queries # rubocop:disable Naming/MemoizedInstanceVariableName
end

#delete(id, type = nil) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/active_shotgun/associations_proxy.rb', line 41

def delete(id, type = nil)
  raise "Many types possible. Please specify." if !type && has_many_types?

  type ||= @possible_types.keys.first
  already_here =
    @queries.reduce([]) do |result, query|
      result + query.dup.limit(1000).pluck(:id).map{ |item_id|
                 { type: query.instance_variable_get(:@type), id: item_id }
               }
    end
  new_items = already_here.reject{ |item| item[:type] == type && item[:id] == id }

  Client.shotgun.entities(@base_class.to_s).update(
    @base_id,
    @field_name => new_items.uniq
  )
  @array = nil
  true
end

#find_by(conditions) ⇒ Object



107
108
109
# File 'lib/active_shotgun/associations_proxy.rb', line 107

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

#first(number = 1) ⇒ Object



70
71
72
73
74
75
76
77
# File 'lib/active_shotgun/associations_proxy.rb', line 70

def first(number = 1)
  results = limit(number).all
  if @global_limit == 1
    results.first
  else
    results
  end
end

#has_many_types?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/active_shotgun/associations_proxy.rb', line 61

def has_many_types?
  @has_many_types ||= @queries.size > 1
end

#limit(number) ⇒ Object



79
80
81
82
83
84
85
86
# File 'lib/active_shotgun/associations_proxy.rb', line 79

def limit(number)
  @global_limit = number
  @queries =
    @queries.map do |query|
      has_many_types? ? query.limit(1000) : query.limit(number)
    end
  self
end

#offset(number) ⇒ Object



88
89
90
91
92
93
94
95
96
# File 'lib/active_shotgun/associations_proxy.rb', line 88

def offset(number)
  @global_offset = number
  @queries =
    @queries.map do |query|
      has_many_types? ? query : query.offset(number)
    end
  @array = nil
  self
end

#orders(new_orders) ⇒ Object



111
112
113
114
115
116
117
118
119
# File 'lib/active_shotgun/associations_proxy.rb', line 111

def orders(new_orders)
  @global_orders = new_orders
  @queries =
    @queries.map do |query|
      query.orders(new_orders)
    end
  @array = nil
  self
end

#pluck(*fields) ⇒ Object



130
131
132
133
134
# File 'lib/active_shotgun/associations_proxy.rb', line 130

def pluck(*fields)
  fields.flatten!
  result = select(fields).map{ |e| fields.map{ |field| e.public_send(field) } }
  fields.size == 1 ? result.flatten : result
end

#push(model) ⇒ Object Also known as: <<



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/active_shotgun/associations_proxy.rb', line 20

def push(model)
  already_here =
    @queries.reduce([]) do |result, query|
      result + query.dup.limit(1000).pluck(:id).map{ |id|
                 { type: query.instance_variable_get(:@type), id: id }
               }
    end
  Client.shotgun.entities(@base_class.to_s).update(
    @base_id,
    @field_name => already_here.push(
      {
        type: model.class.shotgun_type,
        id: model.id,
      }
    ).uniq
  )
  @array = nil
  true
end

#select(*fields) ⇒ Object



121
122
123
124
125
126
127
128
# File 'lib/active_shotgun/associations_proxy.rb', line 121

def select(*fields)
  @queries =
    @queries.map do |query|
      query.select(fields)
    end
  @array = nil
  self
end

#where(conditions) ⇒ Object



98
99
100
101
102
103
104
105
# File 'lib/active_shotgun/associations_proxy.rb', line 98

def where(conditions)
  @queries =
    @queries.map do |query|
      query.where(conditions)
    end
  @array = nil
  self
end