Sha256: d5e2d20afde481dbd60241bb5b551cf583c21f632bf8a76fb2be64bf55227fdf

Contents?: true

Size: 1.05 KB

Versions: 5

Compression:

Stored size: 1.05 KB

Contents

module ActiveCollection
  module Includes

    def self.included(mod)
      mod.extend ClassMethods
      mod.class_eval do
        find_scope :include_options
      end
    end

    module ClassMethods
      def includes(*includes)
        write_inheritable_attribute(:default_includes, includes)
      end

      def default_includes
        read_inheritable_attribute(:default_includes) || []
      end
    end

    def includes
      @includes = self.class.default_includes
    end

    def include(*includes)
      ac = dup
      ac.include! *includes
      ac
    end

    def include!(*includes)
      raise_if_loaded
      @includes = (safe_to_array(includes) + safe_to_array(includes)).uniq
    end

    def include_options
      { :include => @includes } unless @includes.blank?
    end

    protected

    # Taken from ActiveRecord::Base
    #
    # Object#to_a is deprecated, though it does have the desired behavior
    def safe_to_array(o)
      case o
      when NilClass
        []
      when Array
        o
      else
        [o]
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
martinemde-active_collection-0.2.1 lib/active_collection/includes.rb
martinemde-active_collection-0.2.2 lib/active_collection/includes.rb
martinemde-active_collection-0.2.3 lib/active_collection/includes.rb
active_collection-0.2.3 lib/active_collection/includes.rb
active_collection-0.2.1 lib/active_collection/includes.rb