Module: Remotely::Associations

Included in:
ActiveRecord::Base, Model
Defined in:
lib/remotely/associations.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

:nodoc:



191
192
193
# File 'lib/remotely/associations.rb', line 191

def self.included(base) #:nodoc:
  base.extend(ClassMethods)
end

Instance Method Details

#path_to(name, type) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/remotely/associations.rb', line 166

def path_to(name, type)
  opts = remote_associations[name]
  raise HasManyForeignKeyError if opts[:foreign_key] && [:has_many, :has_one].include?(type)

  base = self.class.base_class.model_name.element.pluralize
  fkey = opts[:foreign_key] || :"#{name}_id"
  path = opts[:path]
  path = self.instance_exec(&path) if path.is_a?(Proc)

  # :path option takes precedence
  return interpolate URL(path) if path

  singular_path = name.to_s.singularize
  plural_path   = name.to_s.pluralize

  case type
  when :has_many
    interpolate URL(base, self.id, plural_path)
  when :has_one
    interpolate URL(base, self.id, singular_path)
  when :belongs_to
    interpolate URL(plural_path, public_send(fkey))
  end
end

#remote_associationsObject



162
163
164
# File 'lib/remotely/associations.rb', line 162

def remote_associations
  self.class.remote_associations ||= {}
end