Skip to content

Commit

Permalink
use .all when loading sequel models, identify is the same as in Orm, …
Browse files Browse the repository at this point in the history
…use more proper code for all_scope
  • Loading branch information
mrbrdo committed Dec 8, 2015
1 parent ae17e63 commit 1cffd85
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
18 changes: 11 additions & 7 deletions lib/chewy/type/adapter/orm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,7 @@ def load *args
additional_scope = load_options[load_options[:_type].type_name.to_sym].try(:[], :scope) || load_options[:scope]

scope = all_scope_where_ids_in(objects.map(&primary_key))
loaded_objects = if additional_scope.is_a?(Proc)
scope.instance_exec(&additional_scope)
elsif additional_scope.is_a?(relation_class) && scope.respond_to?(:merge)
scope.merge(additional_scope)
else
scope
end.index_by { |object| object.public_send(primary_key).to_s }
loaded_objects = load_scope_objects(scope, additional_scope).index_by { |object| object.public_send(primary_key).to_s }

objects.map { |object| loaded_objects[object.public_send(primary_key).to_s] }
end
Expand Down Expand Up @@ -148,6 +142,16 @@ def all_scope
def model_of_relation relation
relation.klass
end

def load_scope_objects(scope, additional_scope)
if additional_scope.is_a?(Proc)
scope.instance_exec(&additional_scope)
elsif additional_scope.is_a?(relation_class) && scope.respond_to?(:merge)
scope.merge(additional_scope)
else
scope
end
end
end
end
end
Expand Down
20 changes: 9 additions & 11 deletions lib/chewy/type/adapter/sequel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,6 @@ def self.accepts?(target)
target.is_a?(::Sequel::Dataset))
end

def identify collection
if collection.is_a?(relation_class)
pluck_ids(collection)
else
Array.wrap(collection).map do |entity|
entity.is_a?(object_class) ? entity.public_send(primary_key) : entity
end
end
end

private

def cleanup_default_scope!
Expand All @@ -40,7 +30,7 @@ def import_scope(scope, batch_size)
result = true

while ids.any?
result &= yield grouped_objects(default_scope_where_ids_in(ids))
result &= yield grouped_objects(default_scope_where_ids_in(ids).all)
break if ids.size < batch_size
ids = pluck_ids(scope.where { |o| o.__send__(primary_key) > ids.last })
end
Expand All @@ -52,6 +42,10 @@ def primary_key
target.primary_key
end

def all_scope
target.dataset
end

def pluck_ids(scope)
scope.distinct.select_map(primary_key)
end
Expand All @@ -71,6 +65,10 @@ def relation_class
def object_class
::Sequel::Model
end

def load_scope_objects(scope, additional_scope)
super.all
end
end
end
end
Expand Down

0 comments on commit 1cffd85

Please sign in to comment.