Sha256: 623638b6ed59448857fd296896944193001185190c4c29b79eb7e78db0de9457

Contents?: true

Size: 1.66 KB

Versions: 14

Compression:

Stored size: 1.66 KB

Contents

# frozen-string-literal: true

module Mobility
=begin

Plugin to use an original column for a given locale, and otherwise use the backend.

=end
  module Plugins
    module Sequel
      module ColumnFallback
        extend Plugin

        requires :column_fallback, include: false

        included_hook do |_, backend_class|
          backend_class.include BackendInstanceMethods
          backend_class.extend BackendClassMethods
        end

        def self.use_column_fallback?(options, locale)
          case column_fallback = options[:column_fallback]
          when TrueClass
            locale == I18n.default_locale
          when Array
            column_fallback.include?(locale)
          when Proc
            column_fallback.call(locale)
          else
            false
          end
        end

        module BackendInstanceMethods
          def read(locale, **)
            if ColumnFallback.use_column_fallback?(options, locale)
              model[attribute.to_sym]
            else
              super
            end
          end

          def write(locale, value, **)
            if ColumnFallback.use_column_fallback?(options, locale)
              model[attribute.to_sym] = value
            else
              super
            end
          end
        end

        module BackendClassMethods
          def build_op(attr, locale)
            if ColumnFallback.use_column_fallback?(options, locale)
              ::Sequel::SQL::QualifiedIdentifier.new(model_class.table_name, attr.to_sym)
            else
              super
            end
          end
        end
      end
    end

    register_plugin(:sequel_column_fallback, Sequel::ColumnFallback)
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
mobility-1.3.1 lib/mobility/plugins/sequel/column_fallback.rb
mobility-1.3.0 lib/mobility/plugins/sequel/column_fallback.rb
mobility-1.3.0.rc3 lib/mobility/plugins/sequel/column_fallback.rb
mobility-1.3.0.rc2 lib/mobility/plugins/sequel/column_fallback.rb
mobility-1.3.0.rc1 lib/mobility/plugins/sequel/column_fallback.rb
mobility-1.2.9 lib/mobility/plugins/sequel/column_fallback.rb
mobility-1.2.8 lib/mobility/plugins/sequel/column_fallback.rb
mobility-1.2.7 lib/mobility/plugins/sequel/column_fallback.rb
mobility-1.2.6 lib/mobility/plugins/sequel/column_fallback.rb
mobility-1.2.5 lib/mobility/plugins/sequel/column_fallback.rb
mobility-1.2.4 lib/mobility/plugins/sequel/column_fallback.rb
mobility-1.2.3 lib/mobility/plugins/sequel/column_fallback.rb
mobility-1.2.2 lib/mobility/plugins/sequel/column_fallback.rb
mobility-1.2.1 lib/mobility/plugins/sequel/column_fallback.rb