Sha256: df13a56d3c421fbcb48da52ac69ae7e629ab6457f759c77d00660576fc222e90

Contents?: true

Size: 1.5 KB

Versions: 1

Compression:

Stored size: 1.5 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

    ##
    # 定义 JSON 类型的列。如:
    # notation_column :given_name, :family_name
    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

1 entries across 1 versions & 1 rubygems

Version Path
unidom-common-1.10 app/models/unidom/common/concerns/notation_column.rb