Sha256: 7ea4efacd06babba0295fa1d8981f8a37328e825c46aa57432425d33fded3447

Contents?: true

Size: 495 Bytes

Versions: 6

Compression:

Stored size: 495 Bytes

Contents

require 'active_record'

module ActiveRecord
  class Relation
    def percentage(*filters)
      return 0 if count == 0

      if filters.first.is_a?(Symbol)
        filtered = filters.inject(self) do |relation, scope|
          relation.public_send(scope)
        end
      else
        filtered = where(*filters)
      end

      filtered.count * 1.0 / count
    end

    def pluck_as_json(*keys)
      keys.empty? ? map(&:as_json) : pluck(*keys).map { |i| i.as_hash(keys) }
    end
  end
end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
darthjee-active_ext-1.3.2 lib/active_record/relation_ext.rb
darthjee-active_ext-1.3.1 lib/active_record/relation_ext.rb
darthjee-active_ext-1.3.0 lib/active_record/relation_ext.rb
darthjee-active_ext-1.2.0 lib/active_record/relation_ext.rb
bidu-active_ext-1.1.0 lib/active_record/relation_ext.rb
bidu-active_ext-1.0.0 lib/active_record/relation_ext.rb