Sha256: 9da4d71aa28749c035037cc4d0b964fdb7b7208b217aab30f8fa9cfed3056245

Contents?: true

Size: 890 Bytes

Versions: 8

Compression:

Stored size: 890 Bytes

Contents

require_dependency 'avo/application_controller'

module Avo
  class RelationsController < ApplicationController
    before_action :authorize_user

    def attach
      resource.send(params[:attachment_name]) << attachment_model

      render json: {
        success: true,
        message: I18n.t('avo.attachment_class_attached', attachment_class: attachment_class),
      }
    end

    def detach
      resource.send(params[:attachment_name]).delete attachment_model

      render json: {
        success: true,
        message: I18n.t('avo.attachment_class_detached', attachment_class: attachment_class),
      }
    end

    private
      def attachment_class
        App.get_model_class_by_name params[:attachment_name].pluralize 1
      end

      def attachment_model
        resource._reflections[params[:attachment_name].to_s].klass.find params[:attachment_id]
      end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
avo-0.4.10 app/controllers/avo/relations_controller.rb
avo-0.4.9 app/controllers/avo/relations_controller.rb
avo-0.4.8 app/controllers/avo/relations_controller.rb
avo-0.4.7 app/controllers/avo/relations_controller.rb
avo-0.4.6 app/controllers/avo/relations_controller.rb
avo-0.4.5 app/controllers/avo/relations_controller.rb
avo-0.4.4 app/controllers/avo/relations_controller.rb
avo-0.4.3 app/controllers/avo/relations_controller.rb