Sha256: 04a144772ceed9a6d9cd7d1b2452beb7c0baceb083a57745983085b38d2a17da

Contents?: true

Size: 876 Bytes

Versions: 9

Compression:

Stored size: 876 Bytes

Contents

module Superstore
  module FinderMethods
    def find(ids)
      if ids.is_a?(Array)
        find_some(ids)
      else
        find_one(ids)
      end
    end

    def find_by_id(ids)
      find(ids)
    rescue Superstore::RecordNotFound
      nil
    end

    def all
      clone
    end

    def first
      limit(1).to_a.first
    end

    def to_ids
      klass.adapter.to_ids self
    end

    private

      def find_one(id)
        if id.blank?
          raise Superstore::RecordNotFound, "Couldn't find #{self.name} with key #{id.inspect}"
        elsif record = where_ids(id).first
          record
        else
          raise Superstore::RecordNotFound
        end
      end

      def find_some(ids)
        ids = ids.flatten
        ids.compact!
        return [] if ids.empty?

        ids = ids.map(&:to_s).uniq

        where_ids(ids).to_a
      end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
superstore-2.4.4 lib/superstore/scope/finder_methods.rb
superstore-2.4.3 lib/superstore/scope/finder_methods.rb
superstore-2.4.2 lib/superstore/scope/finder_methods.rb
superstore-2.4.1 lib/superstore/scope/finder_methods.rb
superstore-2.4.0 lib/superstore/scope/finder_methods.rb
superstore-2.3.0 lib/superstore/scope/finder_methods.rb
superstore-2.2.0 lib/superstore/scope/finder_methods.rb
superstore-2.1.3 lib/superstore/scope/finder_methods.rb
superstore-2.1.2 lib/superstore/scope/finder_methods.rb