Module: HasRemote::ClassMethods
- Defined in:
- lib/has_remote.rb
Instance Method Summary (collapse)
-
- (Object) has_remote(options) {|remote| ... }
Binds your ActiveRecord objects to a remote ActiveResource resource, and enables you to look for certain attributes remotely.
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"
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(, &block) unless [:through] || self.const_defined?("Remote") self.const_set("Remote", Class.new(ActiveResource::Base)) end @remote_class = [:through] ? .delete(:through).constantize : self::Remote @remote_foreign_key = .delete(:foreign_key) || :remote_id @remote_primary_key = .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 [:element_name] || @remote_class.element_name != "remote" @remote_class.element_name = self.name.underscore.split('/').last end # setup ARes class with given 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 |