Sha256: c9b18a207b1a52c42d377523b84abf7b0414b12151ad9064e6b5798ebb418fbb

Contents?: true

Size: 1.49 KB

Versions: 12

Compression:

Stored size: 1.49 KB

Contents

# frozen-string-literal: true
require "mobility/arel/nodes"
require "mobility/arel/visitor"

module Mobility
  module Arel
    module MobilityExpressions
      include ::Arel::Expressions

      # @note This is necessary in order to ensure that when a translated
      #   attribute is selected with an alias using +AS+, the resulting
      #   expression can still be counted without blowing up.
      #
      #   Extending +::Arel::Expressions+ is necessary to convince ActiveRecord
      #   that this node should not be stringified, which otherwise would
      #   result in garbage SQL.
      #
      # @see https://github.com/rails/rails/blob/847342c25c61acaea988430dc3ab66a82e3ed486/activerecord/lib/active_record/relation/calculations.rb#L261
      def as(*)
        super
          .extend(::Arel::Expressions)
          .extend(Countable)
      end

      module Countable
        # @note This allows expressions with selected translated attributes to
        #   be counted.
        def count(*args)
          left.count(*args)
        end
      end
    end

    class Attribute < ::Arel::Attributes::Attribute
      include MobilityExpressions

      attr_reader :backend_class
      attr_reader :locale
      attr_reader :attribute_name

      def initialize(relation, column_name, locale, backend_class, attribute_name: nil)
        @backend_class = backend_class
        @locale = locale
        @attribute_name = attribute_name || column_name
        super(relation, column_name)
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
mobility-1.0.0.alpha lib/mobility/arel.rb
mobility-0.8.13 lib/mobility/arel.rb
mobility-0.8.11 lib/mobility/arel.rb
mobility-0.8.10 lib/mobility/arel.rb
mobility-0.8.9 lib/mobility/arel.rb
mobility-0.8.8 lib/mobility/arel.rb
mobility-0.8.7 lib/mobility/arel.rb
mobility-0.8.6 lib/mobility/arel.rb
mobility-0.8.5 lib/mobility/arel.rb
mobility-0.8.4 lib/mobility/arel.rb
mobility-0.8.3 lib/mobility/arel.rb
mobility-0.8.2 lib/mobility/arel.rb