Sha256: c0a5fe7311607a261bd49ea4aa11cf49df54c11cbeb16ef24ea1b2ca67fa681a

Contents?: true

Size: 1.1 KB

Versions: 9

Compression:

Stored size: 1.1 KB

Contents

module ReportsKit
  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

9 entries across 9 versions & 1 rubygems

Version Path
reports_kit-0.7.1 lib/reports_kit/reports/filter_types/boolean.rb
reports_kit-0.7.0 lib/reports_kit/reports/filter_types/boolean.rb
reports_kit-0.5.0 lib/reports_kit/reports/filter_types/boolean.rb
reports_kit-0.4.1 lib/reports_kit/reports/filter_types/boolean.rb
reports_kit-0.4.0 lib/reports_kit/reports/filter_types/boolean.rb
reports_kit-0.3.3 lib/reports_kit/reports/filter_types/boolean.rb
reports_kit-0.3.2 lib/reports_kit/reports/filter_types/boolean.rb
reports_kit-0.3.1 lib/reports_kit/reports/filter_types/boolean.rb
reports_kit-0.3.0 lib/reports_kit/reports/filter_types/boolean.rb