Sha256: 6cff2546941f697bb9883dd4a7430189a03cccc63a59b1c2def603aa2d8f92ae

Contents?: true

Size: 1.99 KB

Versions: 2

Compression:

Stored size: 1.99 KB

Contents

# frozen_string_literal: true
require "mobility/backends/sequel/query_methods"

module Mobility
  module Backends
    class Sequel::KeyValue::QueryMethods < Sequel::QueryMethods
      def initialize(attributes, association_name: nil, class_name: nil, **)
        super

        define_join_method(association_name, class_name)
        define_query_methods(association_name)
      end

      private

      def define_join_method(association_name, translation_class)
        define_method :"join_#{association_name}" do |*attributes, **options|
          attributes.inject(self) do |relation, attribute|
            join_type = options[:outer_join] ? :left_outer : :inner
            relation.join_table(join_type,
                                translation_class.table_name,
                                {
                                  key: attribute.to_s,
                                  locale: Mobility.locale.to_s,
                                  translatable_type: model.name,
                                  translatable_id: ::Sequel[:"#{model.table_name}"][:id]
                                },
                                table_alias: "#{attribute}_#{association_name}")
          end
        end
      end

      def define_query_methods(association_name)
        q = self

        %w[exclude or where].each do |method_name|
          define_method method_name do |*conds, &block|
            if i18n_keys = q.extract_attributes(conds.first)
              cond = conds.first.dup
              i18n_nulls = i18n_keys.select { |key| cond[key].nil? }
              i18n_keys.each { |attr| cond[::Sequel[:"#{attr}_#{association_name}"][:value]] = cond.delete(attr) }
              super(cond, &block).
                send("join_#{association_name}", *(i18n_keys - i18n_nulls), outer_join: method_name == "or").
                send("join_#{association_name}", *i18n_nulls, outer_join: true)
            else
              super(*conds, &block)
            end
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mobility-0.5.1 lib/mobility/backends/sequel/key_value/query_methods.rb
mobility-0.5.0 lib/mobility/backends/sequel/key_value/query_methods.rb