Sha256: 9da3e962b4665a7d1afe75096993edd9d9e3e6298474c0f6d3f3067be6f74806

Contents?: true

Size: 1.09 KB

Versions: 6

Compression:

Stored size: 1.09 KB

Contents

module Light
  module Decorator
    module Concerns
      module Associations
        module CollectionProxy
          extend ActiveSupport::Concern

          # Decorate ActiveRecord::Model associations
          #
          # @param [Hash] options (optional)
          # @return [ActiveRecord::Associations::CollectionProxy]
          def decorate(options = {})
            @decorated = true

            override_scope(options)
            override_load_target(options)

            self
          end

          # Check current association is decorated or not
          #
          # @return [Bool]
          def decorated?
            !@decorated.nil?
          end

          private

          def override_scope(options)
            @association.define_singleton_method :scope do |opts = {}|
              super(opts).decorate(options)
            end
          end

          def override_load_target(options)
            @association.define_singleton_method :load_target do
              super().map { |target| target.decorate(options) }
            end
          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/associations/collection_proxy.rb
light-decorator-1.0.0 lib/light/decorator/concerns/associations/collection_proxy.rb
light-decorator-0.5.3 lib/light/decorator/concerns/associations/collection_proxy.rb
light-decorator-0.5.2 lib/light/decorator/concerns/associations/collection_proxy.rb
light-decorator-0.5.1 lib/light/decorator/concerns/associations/collection_proxy.rb
light-decorator-0.5.0 lib/light/decorator/concerns/associations/collection_proxy.rb