Module: HasRemote::InstanceMethods

Defined in:
lib/has_remote.rb

Instance Method Summary collapse

Instance Method Details

#has_remote?Boolean

Checks whether a remote proxy exists.

Returns:

  • (Boolean)


137
138
139
140
141
142
# File 'lib/has_remote.rb', line 137

def has_remote?
  # NOTE ARes#exists? is broken:
  # https://rails.lighthouseapp.com/projects/8994/tickets/1223-activeresource-head-request-sends-headers-with-a-nil-key
  #
  return !remote(true).nil? rescue false
end

#remote(force_reload = false) ⇒ Object

Returns the remote proxy for this record as an ActiveResource::Base object. Returns nil if foreign key is nil.

Arguments

  • force_reload: Forces a reload from the remote server if set to true. Defaults to false.



127
128
129
130
131
132
133
# File 'lib/has_remote.rb', line 127

def remote(force_reload = false)
  if force_reload || @remote.nil?
    id = self.send(self.class.remote_foreign_key)
    @remote = id ? (self.class.remote_finder ? self.class.remote_finder[id] : self.class.remote_class.find(id)) : nil
  end
  @remote
end

#update_cached_attributesObject

Synchronizes all locally cached remote attributes to this object, but does not save the object.

Note that when the remote does no longer exist, all remote attributes will be set to nil.



156
157
158
159
160
161
162
163
# File 'lib/has_remote.rb', line 156

def update_cached_attributes
  unless self.skip_update_cache || self.class.cached_attributes.empty?
    self.class.cached_attributes.each do |remote_attr|
      local_attr = self.class.remote_attribute_aliases[remote_attr] || remote_attr
      write_attribute(local_attr, has_remote? ? remote(true).send(remote_attr) : nil)
    end
  end
end

#update_cached_attributes!Object

Synchronizes all locally cached remote attributes to this object and saves the object.



146
147
148
149
# File 'lib/has_remote.rb', line 146

def update_cached_attributes!
  update_cached_attributes
  save!
end