Module: FmRest::Spyke::Model::Associations

Extended by:
ActiveSupport::Concern
Included in:
FmRest::Spyke::Model
Defined in:
lib/fmrest/spyke/model/associations.rb

Instance Method Summary collapse

Instance Method Details

#association(name) ⇒ Object

Override Spyke's association reader to keep a cache of loaded portals. Spyke's default behavior is to reload the association each time.



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/fmrest/spyke/model/associations.rb', line 51

def association(name)
  @loaded_portals ||= {}

  if @loaded_portals.has_key?(name.to_sym)
    return @loaded_portals[name.to_sym]
  end

  super.tap do |assoc|
    next unless assoc.kind_of?(FmRest::Spyke::Portal)
    @loaded_portals[name.to_sym] = assoc
  end
end

#portalsObject



68
69
70
71
72
73
74
# File 'lib/fmrest/spyke/model/associations.rb', line 68

def portals
  self.class.associations.each_with_object([]) do |(key, _), portals|
    candidate = association(key)
    next unless candidate.kind_of?(FmRest::Spyke::Portal)
    portals << candidate
  end
end

#reloadObject



64
65
66
# File 'lib/fmrest/spyke/model/associations.rb', line 64

def reload
  super.tap { @loaded_portals = nil }
end