Sha256: 9954c0c8625451ffa60427a16dc469d3d77230b21ed16d1495f2a9e0d7e92a59

Contents?: true

Size: 957 Bytes

Versions: 1

Compression:

Stored size: 957 Bytes

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
  end
end

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
duck_puncher-0.1.0 lib/duck_puncher/active_record_extensions.rb