Sha256: 1c2c7064620950568460f1593bcdff39010ff7ce810acda7965d61c64dff4b19

Contents?: true

Size: 1.57 KB

Versions: 6

Compression:

Stored size: 1.57 KB

Contents

require_dependency "customer_vault/application_controller"

module CustomerVault
  class LinksController < ApplicationController
    before_action :load_linkable, only: [:new, :edit, :create, :update, :destroy]

    def new
      authorize! :update, @person
      
      @link = Link.new
    end

    def edit
      authorize! :update, @person
      
      @link = Link.find(params[:id])
    end

    def create
      authorize! :update, @person
      
      params = link_params
      bob = params[:bob].split("-")

      @link = Link.new(title: params[:title], alice_id: @person.id, alice_type: @person.class.to_s, bob_id: bob[1], bob_type: bob[0])

      if @link.save
        redirect_to @person, notice: 'Link was successfully created.'
      else
        render action: 'new'
      end
    end

    def update
      authorize! :update, @person
      
      @link = Link.find(params[:id])
      if @link.update(link_params)
        redirect_to @person, notice: 'Link was successfully updated.'
      else
        render action: 'edit'
      end
    end

    def destroy
      authorize! :update, @person
      
      Link.find(params[:id]).destroy
      redirect_to @person, notice: 'Individual was successfully destroyed.'
    end


    private
    def load_linkable
      klass = [Individual, Corporation].detect { |c| params["#{c.name.demodulize.underscore}_id"] }
      @person = klass.find(params["#{klass.name.demodulize.underscore}_id"])
    end
      # Only allow a trusted parameter "white list" through.
      def link_params
        params.require(:link).permit(:bob, :title)
      end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
customer_vault-1.2.5 app/controllers/customer_vault/links_controller.rb
customer_vault-1.2.4 app/controllers/customer_vault/links_controller.rb
customer_vault-1.2.3 app/controllers/customer_vault/links_controller.rb
customer_vault-1.2.2 app/controllers/customer_vault/links_controller.rb
customer_vault-1.2.1 app/controllers/customer_vault/links_controller.rb
customer_vault-1.2.0 app/controllers/customer_vault/links_controller.rb