Sha256: ac5ada3e9deed16db124144b72cd1c60e0030b5eef60868759faca6399f60910

Contents?: true

Size: 1.05 KB

Versions: 8

Compression:

Stored size: 1.05 KB

Contents

module ActiveRecordExtensions
  def self.included(base)
    base.extend(ClassMethods)
  end

  def associations?
    associations.present?
  end

  def associations
    reflections.select { |key, _| send(key).present? rescue nil }.keys
  end

  module ClassMethods
    def except_for(*ids)
      scoped.where("#{quoted_table_name}.id NOT IN (?)", ids)
    end

    def since(time)
      scoped.where("#{quoted_table_name}.created_at > ?", time)
    end

    alias created_since since

    def before(time)
      scoped.where("#{quoted_table_name}.created_at < ?", time)
    end

    def updated_since(time)
      scoped.where("#{quoted_table_name}.updated_at > ?", time)
    end

    def between(start_at, end_at)
      scoped.where("#{quoted_table_name}.created_at BETWEEN ? AND ", start_at, end_at)
    end

    def latest
      scoped.order("#{quoted_table_name}.id ASC").last
    end

    # shim for backwards compatibility with Rails 3
    def scoped
      where(nil)
    end if Rails::VERSION::MAJOR != 3
  end
end

ActiveRecord::Base.send(:include, ActiveRecordExtensions)

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
duck_puncher-2.3.0 lib/duck_puncher/active_record_extensions.rb
duck_puncher-2.2.0 lib/duck_puncher/active_record_extensions.rb
duck_puncher-2.1.0 lib/duck_puncher/active_record_extensions.rb
duck_puncher-2.0.0 lib/duck_puncher/active_record_extensions.rb
duck_puncher-1.0.0 lib/duck_puncher/active_record_extensions.rb
duck_puncher-0.2.0 lib/duck_puncher/active_record_extensions.rb
duck_puncher-0.1.2 lib/duck_puncher/active_record_extensions.rb
duck_puncher-0.1.1 lib/duck_puncher/active_record_extensions.rb