Sha256: 97251accdeaf9fe6afec91ff8400aa1da387d05c59c6530ecfef053ae8256081

Contents?: true

Size: 1.41 KB

Versions: 7

Compression:

Stored size: 1.41 KB

Contents

module Unidom::Common::Concerns::NotationColumn

  extend ActiveSupport::Concern

  included do |includer|

    scope :notation_column_where, ->(name, operator, value) do
      operation = :like==operator ? { operator: 'ILIKE', value: "%#{value}%" } : { operator: operator.to_s, value: value }
      where "#{table_name}.notation -> 'columns' ->> '#{name}' #{operation[:operator]} :value", value: operation[:value]
    end

    scope :notation_boolean_column_where, ->(name, value) do
      where "(#{table_name}.notation -> 'columns' ->> '#{name}')::boolean = :value", value: (value ? true : false)
    end

  end

  module ClassMethods

    def notation_column(*names)
      names.each do |name|
        name = name.to_s
        instance_eval do
          define_method(name) do
            notation.try(:[], 'columns').try(:[], name)
          end
          define_method("#{name}=") do |value|
            notation['columns'] ||= {}
            notation['columns'][name] = value
          end
        end
      end
    end

    def notation_boolean_column(*names)
      names.each do |name|
        name = name.to_s
        instance_eval do
          define_method("#{name}?") do
            notation.try(:[], 'columns').try(:[], name)
          end
          define_method("#{name}=") do |value|
            notation['columns'] ||= {}
            notation['columns'][name] = value
          end
        end
      end
    end

  end

end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
unidom-common-1.9.2 app/models/unidom/common/concerns/notation_column.rb
unidom-common-1.9.1 app/models/unidom/common/concerns/notation_column.rb
unidom-common-1.8 app/models/unidom/common/concerns/notation_column.rb
unidom-common-1.7.2 app/models/unidom/common/concerns/notation_column.rb
unidom-common-1.7.1 app/models/unidom/common/concerns/notation_column.rb
unidom-common-1.7 app/models/unidom/common/concerns/notation_column.rb
unidom-common-1.6 app/models/unidom/common/concerns/notation_column.rb