Sha256: daf64cc18d07e77bd5cbd12caf7011157ff6f3e31c21658d1923544ef0a314ac

Contents?: true

Size: 998 Bytes

Versions: 1

Compression:

Stored size: 998 Bytes

Contents

module ActionController
  module Exportation
    extend ActiveSupport::Concern

    included do
      class_attribute :_exportation_scope
      self._exportation_scope = :current_user
    end

    module ClassMethods
      def exportation_scope(scope)
        self._exportation_scope = scope
      end
    end

    ActiveModel::Exporter::TYPES.each do |type|
      [:_render_option_, :_render_with_renderer_].each do |prefix|
        define_method "#{prefix}#{type}" do |resource, options|
          exporter = build_exporter(resource, options)
          exporter ? super(exporter, options) : super
        end
      end
    end

    private

    def exportation_scope
      scope = self.class._exportation_scope
      send(scope) if scope && respond_to?(scope, true)
    end

    def build_exporter(resource, options)
      if exporter = ActiveModel::Exporter.exporter_for(resource)
        options[:scope] ||= exportation_scope
        exporter.new(resource, options)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
active_model_exporters-0.3.1 lib/action_controller/exportation.rb