Sha256: af56bda6fc7d110ebc90987439c05cbf7e4d44dbd3c5bd9e6d64ab2b5dc945ab

Contents?: true

Size: 1.81 KB

Versions: 52

Compression:

Stored size: 1.81 KB

Contents

module ActiveRecord
  module AttributeDecorators # :nodoc:
    extend ActiveSupport::Concern

    included do
      class_attribute :attribute_type_decorations, instance_accessor: false # :internal:
      self.attribute_type_decorations = TypeDecorator.new
    end

    module ClassMethods # :nodoc:
      def decorate_attribute_type(column_name, decorator_name, &block)
        matcher = ->(name, _) { name == column_name.to_s }
        key = "_#{column_name}_#{decorator_name}"
        decorate_matching_attribute_types(matcher, key, &block)
      end

      def decorate_matching_attribute_types(matcher, decorator_name, &block)
        clear_caches_calculated_from_columns
        decorator_name = decorator_name.to_s

        # Create new hashes so we don't modify parent classes
        self.attribute_type_decorations = attribute_type_decorations.merge(decorator_name => [matcher, block])
      end

      private

      def add_user_provided_columns(*)
        super.map do |column|
          decorated_type = attribute_type_decorations.apply(column.name, column.cast_type)
          column.with_type(decorated_type)
        end
      end
    end

    class TypeDecorator # :nodoc:
      delegate :clear, to: :@decorations

      def initialize(decorations = {})
        @decorations = decorations
      end

      def merge(*args)
        TypeDecorator.new(@decorations.merge(*args))
      end

      def apply(name, type)
        decorations = decorators_for(name, type)
        decorations.inject(type) do |new_type, block|
          block.call(new_type)
        end
      end

      private

      def decorators_for(name, type)
        matching(name, type).map(&:last)
      end

      def matching(name, type)
        @decorations.values.select do |(matcher, _)|
          matcher.call(name, type)
        end
      end
    end
  end
end

Version data entries

52 entries across 51 versions & 8 rubygems

Version Path
activerecord-4.2.11.3 lib/active_record/attribute_decorators.rb
activerecord-4.2.11.2 lib/active_record/attribute_decorators.rb
activerecord-4.2.11.1 lib/active_record/attribute_decorators.rb
activerecord-4.2.11 lib/active_record/attribute_decorators.rb
activerecord-4.2.10 lib/active_record/attribute_decorators.rb
activerecord-4.2.10.rc1 lib/active_record/attribute_decorators.rb
activerecord-4.2.9 lib/active_record/attribute_decorators.rb
activerecord-4.2.9.rc2 lib/active_record/attribute_decorators.rb
activerecord-4.2.9.rc1 lib/active_record/attribute_decorators.rb
enju_leaf-1.2.1 vendor/bundle/ruby/2.3/gems/activerecord-4.2.8/lib/active_record/attribute_decorators.rb
activerecord-4.2.8 lib/active_record/attribute_decorators.rb
activerecord-4.2.8.rc1 lib/active_record/attribute_decorators.rb
activerecord-4.2.7.1 lib/active_record/attribute_decorators.rb
activerecord-4.2.7 lib/active_record/attribute_decorators.rb
activerecord-4.2.7.rc1 lib/active_record/attribute_decorators.rb
ish_lib_manager-0.0.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.6/lib/active_record/attribute_decorators.rb
activerecord-4.2.6 lib/active_record/attribute_decorators.rb
activerecord-4.2.6.rc1 lib/active_record/attribute_decorators.rb
activerecord-4.2.5.2 lib/active_record/attribute_decorators.rb
activejob-lock-0.0.2 rails/activerecord/lib/active_record/attribute_decorators.rb