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)


141
142
143
144
145
146
# File 'lib/has_remote.rb', line 141

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.

Parameters:

  • force_reload (Boolean) (defaults to: false)

    Forces a reload from the remote server if set to true.



129
130
131
132
133
134
135
# File 'lib/has_remote.rb', line 129

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.



162
163
164
165
166
167
168
169
170
# File 'lib/has_remote.rb', line 162

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

#update_cached_attributes!Object

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

Raises:

  • (ActiveRecord::RecordInvalid)


152
153
154
155
# File 'lib/has_remote.rb', line 152

def update_cached_attributes!
  update_cached_attributes
  save!
end