Sha256: 7079247a7da33e08631b6c2907b032b6285b4519422a54eccc468fbd742451b5

Contents?: true

Size: 1.08 KB

Versions: 1

Compression:

Stored size: 1.08 KB

Contents

require "mobility/backends/sequel/pg_query_methods"
require "mobility/backends/sequel/query_methods"

Sequel.extension :pg_json, :pg_json_ops

module Mobility
  module Backends
    class Sequel::Container::QueryMethods < Sequel::QueryMethods
      include Sequel::PgQueryMethods
      attr_reader :column_name

      def initialize(attributes, options)
        super
        column_name = @column_name = options[:column_name]

        define_query_methods

        attributes.each do |attribute|
          define_method :"first_by_#{attribute}" do |value|
            where(::Sequel.pg_jsonb_op(column_name)[Mobility.locale.to_s].contains({ attribute => value })).
              select_all(model.table_name).first
          end
        end
      end

      private

      def matches(key, value, locale)
        build_op(column_name)[locale][key.to_s] =~ value.to_json
      end

      def has_locale(key, locale)
        build_op(column_name).has_key?(locale) & build_op(column_name)[locale].has_key?(key.to_s)
      end

      def build_op(key)
        ::Sequel.pg_jsonb_op(key)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mobility-0.4.1 lib/mobility/backends/sequel/container/query_methods.rb