Sha256: 65560b70628d8e8cd9ccc74ceea3a9ec86512e32ed20d006adca70d30a8434f7

Contents?: true

Size: 1.1 KB

Versions: 2

Compression:

Stored size: 1.1 KB

Contents

module ReportsKits
  module Reports
    module FilterTypes
      class Boolean < Base
        DEFAULT_CRITERIA = {
          value: nil
        }

        def apply_conditions(records)
          case conditions
          when ::String
            records.where("(#{conditions}) #{sql_operator} true")
          when ::Hash
            boolean_value ? records.where(conditions) : records.not.where(conditions)
          when ::Proc
            conditions.call(records)
          else
            raise ArgumentError.new("Unsupported conditions type: '#{conditions}'")
          end
        end

        def boolean_value
          case value
          when true, 'true'
            true
          when false, 'false'
            false
          else
            raise ArgumentError.new("Unsupported value: '#{value}'")
          end
        end

        def sql_operator
          boolean_value ? '=' : '!='
        end

        def valid?
          value.present?
        end

        def conditions
          settings[:conditions] || Data::Utils.quote_column_name(properties[:key])
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
reports_kits-0.7.8 lib/reports_kits/reports/filter_types/boolean.rb
reports_kits-0.7.7 lib/reports_kits/reports/filter_types/boolean.rb