Sha256: 06981a6bc82004deb3e930a2824b295afa9ce923c38e94a62251a615bb398926

Contents?: true

Size: 1.73 KB

Versions: 16

Compression:

Stored size: 1.73 KB

Contents

module Gaku
  class ContactsController < GakuController

    # load_and_authorize_resource :contact, class: Gaku::Contact

    include PolymorphicResourceConcern

    respond_to :js

    before_action :set_contact_types,    only: %i( new edit )
    before_action :set_contact,          only: %i( edit update destroy make_primary )
    before_action :set_polymorphic_resource

    def index
      @contacts = @polymorphic_resource.contacts
      respond_with @contacts
    end

    def new
      @contact = Contact.new
      respond_with @contact
    end

    def create
      creator = ContactCreation.new(contact_params.merge(contactable: @polymorphic_resource))
      creator.save
      @contact = creator.contact
      set_count
      respond_with @contact
    end

    def edit
    end

    def update
      ContactUpdation.new(@contact).update(contact_params)
      respond_with @contact
    end

    def destroy
      if @contact.destroy
        @polymorphic_resource.contacts.first.try(:make_primary) if @contact.primary?
      end
      set_count
      respond_with @contact
    end

    def make_primary
      @contact.make_primary
      respond_with @contact
    end

    private

    def set_contact
      @contact = Contact.find(params[:id])
    end

    def contact_params
      params.require(:contact).permit(attributes)
    end

    def attributes
      %i( data details contact_type_id primary emergency )
    end

    def resource_klass
      'contact'
    end

    def polymorphic_klasses
      [Gaku::School, Gaku::Campus, Gaku::Student, Gaku::Guardian, Gaku::Teacher]
    end

    def set_contact_types
      @contact_types = ContactType.all
    end

    def set_count
      @count = @polymorphic_resource.reload.contacts_count
    end

  end
end

Version data entries

16 entries across 16 versions & 2 rubygems

Version Path
gaku_frontend-0.3.0 app/controllers/gaku/contacts_controller.rb
gaku_frontend-0.3.0.pre.4 app/controllers/gaku/contacts_controller.rb
gaku_frontend-0.3.0.pre.3 app/controllers/gaku/contacts_controller.rb
gaku_frontend-0.3.0.pre.2 app/controllers/gaku/contacts_controller.rb
gaku_frontend-0.3.0.pre.1 app/controllers/gaku/contacts_controller.rb
gaku_frontend-0.3.0.pre.0 app/controllers/gaku/contacts_controller.rb
gaku-0.2.4 frontend/app/controllers/gaku/contacts_controller.rb
gaku_frontend-0.2.4 app/controllers/gaku/contacts_controller.rb
gaku-0.2.3 frontend/app/controllers/gaku/contacts_controller.rb
gaku_frontend-0.2.3 app/controllers/gaku/contacts_controller.rb
gaku-0.2.2 frontend/app/controllers/gaku/contacts_controller.rb
gaku_frontend-0.2.2 app/controllers/gaku/contacts_controller.rb
gaku-0.2.1 frontend/app/controllers/gaku/contacts_controller.rb
gaku_frontend-0.2.1 app/controllers/gaku/contacts_controller.rb
gaku-0.2.0 frontend/app/controllers/gaku/contacts_controller.rb
gaku_frontend-0.2.0 app/controllers/gaku/contacts_controller.rb