Module: HasRemote::ClassMethods

Defined in:
lib/has_remote.rb

Instance Method Summary (collapse)

Instance Method Details

- (Object) has_remote(options) {|remote| ... }

Binds your ActiveRecord objects to a remote ActiveResource resource, and enables you to look for certain attributes remotely.

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"

Parameters:

  • options (Hash)

    a customizable set of options

Options Hash (options):

  • :foreign_key (Symbol, String) — default: :remote_id

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

  • :remote_primary_key (Symbol, String) — default: :id

    The name of the remote resource's primary key.

  • :through (String)

    Optional custom ActiveResource class name to use for the proxy. If not set, a default class called Remote will be created dynamically, namespaced inside the current model. Note that any ActiveResource configuration options will still be applied to this class.

  • Other (Object)

    All other options you pass in will be used to configure the ActiveResource model, you can use any setting for ActiveResource::Base, such as :site, :user and :password.

Yield Parameters:

  • remote (Config)

    Configure attributes to be delegated or a custom finder.



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
115
116
117
118
# File 'lib/has_remote.rb', line 72

def has_remote(options, &block)
  unless options[:through] || self.const_defined?("Remote")
    self.const_set("Remote", Class.new(ActiveResource::Base))
  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