Sha256: e1ef7862e68373f4aeb5ab1f4e11e8c7c0e45130ca5d511e9099359d6acffe00

Contents?: true

Size: 1.8 KB

Versions: 35

Compression:

Stored size: 1.8 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)
        reload_schema_from_cache
        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 load_schema!
        super
        attribute_types.each do |name, type|
          decorated_type = attribute_type_decorations.apply(name, type)
          define_attribute(name, 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

35 entries across 35 versions & 4 rubygems

Version Path
activerecord-5.0.7.2 lib/active_record/attribute_decorators.rb
activerecord-5.0.7.1 lib/active_record/attribute_decorators.rb
activerecord-5.0.7 lib/active_record/attribute_decorators.rb
activerecord-5.0.6 lib/active_record/attribute_decorators.rb
activerecord-5.0.6.rc1 lib/active_record/attribute_decorators.rb
activerecord-5.0.5 lib/active_record/attribute_decorators.rb
activerecord-5.0.5.rc2 lib/active_record/attribute_decorators.rb
activerecord-5.0.5.rc1 lib/active_record/attribute_decorators.rb
activerecord-5.0.4 lib/active_record/attribute_decorators.rb
activerecord-5.0.4.rc1 lib/active_record/attribute_decorators.rb
activerecord-5.0.3 lib/active_record/attribute_decorators.rb
enju_leaf-1.2.1 vendor/bundle/ruby/2.3/gems/activerecord-5.0.2/lib/active_record/attribute_decorators.rb
activerecord-5.0.2 lib/active_record/attribute_decorators.rb
activerecord-5.0.2.rc1 lib/active_record/attribute_decorators.rb
autocompl-0.2.2 test/dummy/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.1/lib/active_record/attribute_decorators.rb
autocompl-0.2.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.1/lib/active_record/attribute_decorators.rb
autocompl-0.2.0 test/dummy/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.1/lib/active_record/attribute_decorators.rb
autocompl-0.1.2 test/dummy/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.1/lib/active_record/attribute_decorators.rb
autocompl-0.1.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.1/lib/active_record/attribute_decorators.rb
autocompl-0.1.0 test/dummy/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.1/lib/active_record/attribute_decorators.rb