Sha256: 1bdc24937e42a721598a5dbf2f7c3d9b05e3777be0b6709ffa888f5c072eb8be

Contents?: true

Size: 919 Bytes

Versions: 4

Compression:

Stored size: 919 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|
      define_method "_render_option_#{type}" do |resource, options|
        exporter = build_exporter(resource, options)
        exporter ? super(exporter, options) : super
      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

4 entries across 4 versions & 1 rubygems

Version Path
active_model_exporters-0.2.0 lib/action_controller/exportation.rb
active_model_exporters-0.1.0 lib/action_controller/exportation.rb
active_model_exporters-0.0.5 lib/action_controller/exportation.rb
active_model_exporters-0.0.4 lib/action_controller/exportation.rb