Module: HasRemote::ClassMethods

Defined in:
lib/has_remote.rb

Instance Method Summary collapse

Instance Method Details

#has_remote(options, &block) ⇒ Object

Gives your local ActiveRecord model a remote proxy (ActiveResource::Base), which enables you to look for certain attributes remotely.

Options

:foreign_key

The name of the column used to store the id of the remote resource. Defaults to :remote_id.

:remote_primary_key

The name of the remote resource’s primary key. Defaults to :id.

:site, :user, :password, …

Basically all ActiveResource configuration settings are available, see api.rubyonrails.org/classes/ActiveResource/Base.html

:through

Optional custom ActiveResource class name to use for the proxy. If not set, a default class called “<ModelName>::Remote” will be created dynamically. Note that any ActiveResource configuration options will still be applied to this class.

Usage

class User < ActiveRecord::Base
  has_remote :site => 'http://people.local'
end

# In a migration:
add_column :users, :remote_id, :integer

User.find(1).remote
# => #<User::Remote> (inherits from ActiveResource::Base)
User.find(1).remote.username
# => "User name from remote server"

has_remote also takes a block which is passed in a HasRemote::Config object which can be used to specify remote attributes:

class User < ActiveRecord::Base
  has_remote :site => '...' do |remote|
    remote.attribute :username
    remote.attribute :full_name, :local_cache => true
    remote.attribute :email_address, :as => :email
  end 
end

User.find(1).username
# => "User name from remote server"


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/has_remote.rb', line 68

def has_remote(options, &block)
  unless options[:through] || self.const_defined?("Remote")
    self.const_set("Remote", ActiveResource::Base.clone)
  end
  
  @remote_class = options[:through] ? options.delete(:through).constantize : self::Remote
  
  @remote_foreign_key = options.delete(:foreign_key) || :remote_id
  
  @remote_primary_key = options.delete(:remote_primary_key) || :id
  
  # create extra class methods
  class << self
    attr_reader :remote_class
    attr_reader :remote_foreign_key
    attr_reader :remote_finder
    attr_reader :remote_primary_key
    attr_writer :remote_attribute_aliases
    
    def remote_attributes # :nodoc:
      @remote_attributes ||= []
    end
    
    def remote_attribute_aliases # :nodoc:
      @remote_attribute_aliases ||= {}
    end
    
    include HasRemote::Synchronizable
  end
  
  # set ARes to look for correct resource (only if not manually specified)
  unless options[:element_name] || @remote_class.element_name != "remote"
    @remote_class.element_name = self.name.underscore.split('/').last
  end
  
  # setup ARes class with given options
  options.each do |option, value|
    @remote_class.send "#{option}=", value 
  end
  
  attr_accessor :skip_update_cache
  
  block.call( Config.new(self) ) if block_given?
        
  include InstanceMethods
  HasRemote.models << self
end