Sha256: 39860152b0f4687b73fe327313e29c79689d1497b922ba672e62e6de5f18fc1f

Contents?: true

Size: 991 Bytes

Versions: 4

Compression:

Stored size: 991 Bytes

Contents

module Draper
  # Provides automatically-decorated finder methods for your decorators. You
  # do not have to extend this module directly; it is extended by
  # {Decorator.decorates_finders}.
  module Finders

    def find(id, options = {})
      decorate(source_class.find(id), options)
    end

    def all(options = {})
      decorate_collection(source_class.all, options)
    end

    def first(options = {})
      decorate(source_class.first, options)
    end

    def last(options = {})
      decorate(source_class.last, options)
    end

    # Decorates dynamic finder methods (`find_all_by_` and friends).
    def method_missing(method, *args, &block)
      return super unless method =~ /^find_(all_|last_|or_(initialize_|create_))?by_/

      result = source_class.send(method, *args, &block)
      options = args.extract_options!

      if method =~ /^find_all/
        decorate_collection(result, options)
      else
        decorate(result, options)
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
draper-1.2.0 lib/draper/finders.rb
jamesgolick-draper-1.1.1a lib/draper/finders.rb
draper-1.1.0 lib/draper/finders.rb
draper-1.0.0 lib/draper/finders.rb