Class: ActiveStash::Relation

Inherits:
ActiveRecord::Relation
  • Object
show all
Includes:
ActiveStash::RelationHelp::Compatability
Defined in:
lib/active_stash/relation.rb

Instance Method Summary collapse

Methods included from ActiveStash::RelationHelp::Compatability

included, #unsupported!

Constructor Details

#initialize(scope) ⇒ Relation

Returns a new instance of Relation.



12
13
14
15
16
17
# File 'lib/active_stash/relation.rb', line 12

def initialize(scope)
  @klass = scope
  @scope = scope

  super
end

Instance Method Details

#countObject



51
52
53
54
55
56
57
# File 'lib/active_stash/relation.rb', line 51

def count
  if stash_query?
    self.load.count
  else
    super
  end
end

#exists?Boolean

Returns:

  • (Boolean)


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

def exists?
  self.load
  !self.first.blank?
end

#firstObject



34
35
36
37
38
39
40
41
# File 'lib/active_stash/relation.rb', line 34

def first
  if stash_query?
    limit(1)
    self.load[0]
  else
    super
  end
end

#last(n = nil) ⇒ Object



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

def last(n = nil)
  if stash_query?
    n ? self.load.last(n) : self.load.last
  else
    super
  end
end

#limit(value) ⇒ Object



24
25
26
27
# File 'lib/active_stash/relation.rb', line 24

def limit(value)
  self.limit_value = value
  self
end

#loadObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/active_stash/relation.rb', line 78

def load
  if stash_query?
    return @records if @loaded

    load_stash_ids_if_needed

    relation = @scope.where(stash_id: @stash_ids)
    relation = relation.in_order_of(:stash_id, @stash_ids) if @stash_order
    @loaded = true
    @records = relation.load
  else
    super
  end
end

#offset(value) ⇒ Object



29
30
31
32
# File 'lib/active_stash/relation.rb', line 29

def offset(value)
  self.offset_value = value
  self
end

#order(*args) ⇒ Object



68
69
70
71
# File 'lib/active_stash/relation.rb', line 68

def order(*args)
  @stash_order = process_order_args(*args)
  self
end

#query(*args, &block) ⇒ Object



19
20
21
22
# File 'lib/active_stash/relation.rb', line 19

def query(*args, &block)
  @query = QueryBuilder.build_query(@klass, *args, &block)
  self
end

#stash_idsObject



59
60
61
62
63
64
65
66
# File 'lib/active_stash/relation.rb', line 59

def stash_ids
  if stash_query?
    load_stash_ids_if_needed
    @stash_ids
  else
    raise "Can't request stash IDs on a non-stash query"
  end
end

#stash_query?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/active_stash/relation.rb', line 93

def stash_query?
  @query || @stash_order
end