class Sunspot::Rails::Adapters::ActiveRecordDataAccessor

Attributes

include[RW]

options for the find

scopes[RW]
select[R]

Public Class Methods

new(clazz) click to toggle source
Calls superclass method
# File lib/sunspot/rails/adapters.rb, line 27
def initialize(clazz)
  super(clazz)
  @inherited_attributes = [:include, :select, :scopes]
end

Public Instance Methods

load(id) click to toggle source

Get one ActiveRecord instance out of the database by ID

Parameters

id<String>

Database ID of model to retreive

Returns

ActiveRecord::Base

ActiveRecord model

# File lib/sunspot/rails/adapters.rb, line 56
def load(id)
  @clazz.where(@clazz.primary_key => id).merge(scope_for_load).first
end
load_all(ids) click to toggle source

Get a collection of ActiveRecord instances out of the database by ID

Parameters

ids<Array>

Database IDs of models to retrieve

Returns

Array

Collection of ActiveRecord models

# File lib/sunspot/rails/adapters.rb, line 71
def load_all(ids)
  @clazz.where(@clazz.primary_key => ids).merge(scope_for_load)
end
select=(value) click to toggle source

Set the fields to select from the database. This will be passed to ActiveRecord.

Parameters

value<Mixed>

String of comma-separated columns or array of columns

# File lib/sunspot/rails/adapters.rb, line 40
def select=(value)
  value = value.join(', ') if value.respond_to?(:join)
  @select = value
end

Private Instance Methods

relation() click to toggle source

COMPATIBILITY: Rails 4 has deprecated the 'scoped' method in favour of 'all'

# File lib/sunspot/rails/adapters.rb, line 88
def relation
  ::Rails.version >= '4' ? @clazz.all : @clazz.scoped
end
scope_for_load() click to toggle source
# File lib/sunspot/rails/adapters.rb, line 77
def scope_for_load
  scope = relation
  scope = scope.includes(@include) if @include.present?
  scope = scope.select(@select)    if @select.present?
  Array.wrap(@scopes).each do |s|
    scope = scope.send(s)
  end
  scope 
end