Sha256: 1e9fe93e99fdd630a0029e1be440d8bd287d0d78d8f528e1e44021af81f045fd

Contents?: true

Size: 1018 Bytes

Versions: 19

Compression:

Stored size: 1018 Bytes

Contents

module AdminIt
  module ActiveRecordData
    #
    module ValueFilter
      def all_values(collection = nil, &block)
        enum = Enumerator.new do |yielder|
          field
            .entity_class
            .select(self.class.field.field_name)
            .group(self.class.field.field_name)
            .count
            .each do |v, c|
              yielder << { value: v, count: c }
            end
        end
        block_given? ? enum.each(&block) : enum
      end

      def apply(collection)
        return collection if @values.empty?
        binding = []
        conditions = ''
        if @values.size == 1 && @values[0].nil?
          conditions = "#{field.field_name} IS NULL"
        else
          conditions = "#{field.field_name} IN (?)"
          binding << @values.select { |v| !v.nil? }
          if @values.any? { |v| v.nil? }
            conditions += " OR #{field.field_name} IS NULL"
          end
        end
        collection.where(conditions, *binding)
      end
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
admin_it-1.3.8 lib/admin_it/data/active_record/filter.rb
admin_it-1.3.7 lib/admin_it/data/active_record/filter.rb
admin_it-1.3.6 lib/admin_it/data/active_record/filter.rb
admin_it-1.3.5 lib/admin_it/data/active_record/filter.rb
admin_it-1.3.4 lib/admin_it/data/active_record/filter.rb
admin_it-1.3.3 lib/admin_it/data/active_record/filter.rb
admin_it-1.3.2 lib/admin_it/data/active_record/filter.rb
admin_it-1.3.1 lib/admin_it/data/active_record/filter.rb
admin_it-1.3.0 lib/admin_it/data/active_record/filter.rb
admin_it-1.2.8 lib/admin_it/data/active_record/filter.rb
admin_it-1.2.7 lib/admin_it/data/active_record/filter.rb
admin_it-1.2.6 lib/admin_it/data/active_record/filter.rb
admin_it-1.2.5 lib/admin_it/data/active_record/filter.rb
admin_it-1.2.4 lib/admin_it/data/active_record/filter.rb
admin_it-1.2.3 lib/admin_it/data/active_record/filter.rb
admin_it-1.2.2 lib/admin_it/data/active_record/filter.rb
admin_it-1.2.1 lib/admin_it/data/active_record/filter.rb
admin_it-1.2.0 lib/admin_it/data/active_record/filter.rb
admin_it-1.1.0 lib/admin_it/data/active_record/filter.rb