Sha256: fa67073d63fee55c27e140dbdadf81ec7a7205f56d9082fb602ed6d1ac14c92b

Contents?: true

Size: 1.81 KB

Versions: 6

Compression:

Stored size: 1.81 KB

Contents

module Light
  module Decorator
    module Concerns
      module Base
        extend ActiveSupport::Concern

        # Decorate ActiveRecord::Model
        #
        # @param [Hash] options (optional)
        # @return [Object] decorator or self
        def decorate(options = {})
          return self if decorated?

          klass = decorator_class(options)
          klass.decorate(self, options)
        rescue Light::Decorator::NotFound => e
          raise e unless options[:soft]
          self
        end

        # Check current ActiveRecord::Model is decorated or not
        #
        # @return [Bool]
        def decorated?
          false
        end

        def ==(other)
          super || other.respond_to?(:object) && self == other.object
        end

        def eql?(other)
          super || other.respond_to?(:object) && eql?(other.object)
        end

        private

        def decorator_class(options)
          with = options.delete(:with)
          return "#{self.class}Decorator".constantize unless with

          with = with.constantize unless with.is_a?(Class)
          with
        rescue NameError
          raise Light::Decorator::NotFound, "Decorator#{with ? " #{with}" : ''} for #{self.class} not found"
        end

        module ClassMethods
          # Decorate all scope for ActiveRecord::Model
          #
          # @param [Hash] options (optional)
          # @return [ActiveRecord::Relation]
          def decorate(options = {})
            all.decorate(options)
          end

          # Find ActiveRecord::Model and decorate it
          #
          # @param [Integer] id
          # @param [Hash] options (optional)
          # @return [ActiveRecord::Model]
          def find_and_decorate(id, options = {})
            find(id).decorate(options)
          end
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
light-decorator-1.0.1 lib/light/decorator/concerns/base.rb
light-decorator-1.0.0 lib/light/decorator/concerns/base.rb
light-decorator-0.5.3 lib/light/decorator/concerns/base.rb
light-decorator-0.5.2 lib/light/decorator/concerns/base.rb
light-decorator-0.5.1 lib/light/decorator/concerns/base.rb
light-decorator-0.5.0 lib/light/decorator/concerns/base.rb