Sha256: 90c1f2334b398b9ad1c070a2053431032fa67d2adbd86cd527a98dc2a3339e5b

Contents?: true

Size: 1.2 KB

Versions: 5

Compression:

Stored size: 1.2 KB

Contents

module SpreeMobility::CoreExt::Mobility::Backends::ActiveRecord::Table
  module MobilityActsAsParanoidDecorator
    private
    def using_acts_as_paranoid?
      translation_class = model_class.const_get(subclass_name)
      return false unless translation_class
      translation_class.column_names.include?('deleted_at')
    end

    # If joining a table with a deleted_at column (acts_as_paranoid), then
    # filter the join with deleted_at IS NULL
    def join_translations(relation, locale, join_type)
      if using_acts_as_paranoid?
        # check if joins changed
        old_joins = relation.joins_values
        relation = super
        new_joins = relation.joins_values - old_joins
        return relation if new_joins.empty?
        fail "join_translations new_joins.size > 1" unless new_joins.size == 1
        # append deleted_at IS NULL to joins ON clause using Arel
        new_join = new_joins.first
        joined_table = model_class.const_get(subclass_name).arel_table.alias(table_alias(locale))
        relation.joins_values -= [new_join]
        new_join.right = new_join.right.and(joined_table[:deleted_at].eq(nil))
        relation.joins(new_join)
      else
        super
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
spree_mobility-1.4.0 lib/spree_mobility/core_ext/mobility/backends/active_record/table/mobility_acts_as_paranoid_decorator.rb
spree_mobility-1.3.0 lib/spree_mobility/core_ext/mobility/backends/active_record/table/mobility_acts_as_paranoid_decorator.rb
spree_mobility-1.2.0 lib/spree_mobility/core_ext/mobility/backends/active_record/table/mobility_acts_as_paranoid_decorator.rb
spree_mobility-1.1.0 lib/spree_mobility/core_ext/mobility/backends/active_record/table/mobility_acts_as_paranoid_decorator.rb
spree_mobility-1.0.0 lib/spree_mobility/core_ext/mobility/backends/active_record/table/mobility_acts_as_paranoid_decorator.rb