Sha256: b63a7e705d5a95a94ae66ca9aba5803defc2dfb8a889121db88fc65d21aa231e

Contents?: true

Size: 1.52 KB

Versions: 2

Compression:

Stored size: 1.52 KB

Contents

module Xporter
  class Exporter
    module Decorator
      extend ActiveSupport::Concern

      included do
        class_attribute :_decorates, instance_accessor: false
        class_attribute :_decorator_class, instance_accessor: false

        self._decorates = false
        self._decorator_class = nil

        private_class_method :decorates
      end

      class_methods do
        def decorates(boolean_or_class)
          if boolean_or_class.nil? || boolean_or_class == false
            self._decorates = false
            self._decorator_class = nil
            return
          end

          self._decorates = true

          if boolean_or_class.is_a?(Class)
            self._decorator_class = boolean_or_class
          end
        end
      end

      private

      def view_context
        return unless defined?(ActionView)

        @view_context ||= begin
          ActionView::Base.new(ActionController::Base.view_paths).tap do |view|
            view.class_eval do
              include Rails.application.routes.url_helpers
              include ApplicationHelper
            end
          end
        end
      end

      def decorator?
        self.class._decorates == true
      end

      def decorate(record)
        if self.class._decorator_class.present?
          instantiate_decorated_object(self.class._decorator_class, record)
        else
          record.decorate(view_context)
        end
      end

      def instantiate_decorated_object(klass, record)
        klass.new(record, view_context)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
xporter-0.2.0 lib/xporter/exporter/decorator.rb
xporter-0.1.0 lib/xporter/exporter/decorator.rb