Module: ObjectJSONMapper::Local::ClassMethods

Defined in:
lib/object_json_mapper/local.rb

Instance Method Summary collapse

Instance Method Details

#find_by_local(source, &scope) ⇒ ObjectJSONMapper:Relation

Allows you to apply filters from local model to remote data.

Examples:

class User < ObjectJSONMapper::Base
  def self.local
    LocalUser
  end
end

class LocalUser < ActiveRecord::Base
end

User.find_by_local(User.all) do
  where(local_column: 'value')
end
# => SELECT * FROM local_users WHERE local_column = 'value'
# => GET http://example.com/users?id_in=1,2,3

Parameters:

Returns:

  • (ObjectJSONMapper:Relation)


47
48
49
50
51
52
53
54
55
# File 'lib/object_json_mapper/local.rb', line 47

def find_by_local(source, &scope)
  source.where(
    id_in: source.klass
                 .local
                 .all
                 .instance_exec(&scope)
                 .pluck(:id)
  )
end

#localObject



19
20
21
22
23
24
# File 'lib/object_json_mapper/local.rb', line 19

def local
  return @local if @local
  @local = Class.new(ActiveRecord::Base)
  @local.table_name = name.underscore.pluralize
  @local
end