lib/indexes/collection.rb in indexes-0.0.1 vs lib/indexes/collection.rb in indexes-4.0.0.0

- old
+ new

@@ -6,21 +6,33 @@ delegate :model_name, to: :model delegate :each, :map, :size, :length, :count, :[], :to_a, to: :records alias_method :to_ary, :to_a - def initialize(index, *args, &block) + def initialize(index, args=[], options={}, &block) @loaded = false @index = index - @options = args.extract_options! @args = args + @options = options @block = block end + def with(ids) + chain with: ids + end + + def without(ids) + chain without: ids + end + + def includes(*args) + chain includes: args + end + def page(number, options={}) - length = (fetch_option(options, :length) || 10) - padding = (fetch_option(options, :padding) || 0) + length = page_option(options, :length, 10) + padding = page_option(options, :padding, 0) current_page = [number.to_i, 1].max values = Module.new do define_method :page_length do length end @@ -29,36 +41,32 @@ end define_method :current_page do current_page end end - overrides = { + chain( + Pagination, + values, from: ((length * (current_page - 1)) + padding), size: length - } - %i(with without).each do |name| - if options.has_key?(name) - overrides[name] = options[name] - end - end - chain Pagination, values, overrides + ) end def order(options) mappings = Indexes.configuration.mappings - sort = [] + values = [] options.each do |property, direction| if block = Indexes.configuration.computed_sorts[property] - sort << Dsl::Api.new(direction, &block).to_h + values << Dsl::Api.new(direction, &block).to_h elsif property == :id - sort << { _uid: { order: direction } } + values << { _uid: { order: direction } } elsif mappings.has_key?(property) && mappings[property][:type] == 'string' - sort << { "#{property}.raw" => { order: direction } } + values << { "#{property}.raw" => { order: direction } } end end - if sort.any? - chain sort: sort + if values.any? + chain sort: values else chain end end @@ -73,11 +81,11 @@ def query @query ||= begin pagination = options.slice(:from, :size, :sort) without_ids = fetch_ids(options[:without]) - body = Dsl::Search.new(args.append(options), &block).to_h[:query] + body = Dsl::Search.new(args, &block).to_h[:query] request = Dsl::Search.new do if without_ids.any? query do filtered do filter do @@ -95,13 +103,13 @@ end end else query body end - %i(from size).each do |option| - if pagination.has_key?(option) - send option, pagination[option] + %i(from size).each do |name| + if pagination.has_key?(name) + send name, pagination[name] end end if pagination.has_key?(:sort) sort pagination[:sort] else @@ -120,51 +128,34 @@ attr_reader :index, :args, :options, :block def records @records ||= begin + hit_ids = response['hits']['hits'].map{ |hit| hit['_id'].to_i } + missing_ids = (fetch_ids(options[:with]) - hit_ids) if missing_ids.any? last_index = -(missing_ids.length + 1) ids = (missing_ids.sort.reverse + hit_ids.to(last_index)) else ids = hit_ids end + includes = options.fetch(:includes, []) index.model.includes(includes).where(id: ids).sort do |a,b| ids.index(a.id) <=> ids.index(b.id) end end end - def with_ids - @with_ids ||= fetch_ids(options[:with]) - end - - def hit_ids - @hit_ids ||= response['hits']['hits'].map{ |hit| hit['_id'].to_i } - end - - def missing_ids - @missing_ids ||= (with_ids - hit_ids) - end - - def includes - @inclues ||= begin - if options.has_key?(:includes) - Array options[:includes] - else - [] - end - end - end - - def fetch_option(options, name) - options[name] || begin + def page_option(source, name, default) + source[name] || begin if Rails.configuration.cache_classes == false Rails.application.eager_load! end if defined?(Pagers) - Pagers.config[name] + Pagers.configuration.send name + else + default end end end def fetch_ids(source) @@ -182,14 +173,14 @@ end end def chain(*extensions) overrides = extensions.extract_options! - Collection.new(index, *args.append(options.merge(overrides)), &block).tap do |collection| - extensions.each do |extension| - collection.extend extension - end + collection = Collection.new(index, args, options.merge(overrides), &block) + extensions.each do |extension| + collection.extend extension end + collection end end end