aspect_memberships_controller.rb 2.71 KB
Newer Older
danielgrippi's avatar
danielgrippi committed
1
#   Copyright (c) 2010-2011, Diaspora Inc.  This file is
2 3 4 5
#   licensed under the Affero General Public License version 3 or later.  See
#   the COPYRIGHT file.
#

6
class AspectMembershipsController < ApplicationController
7
  before_action :authenticate_user!
8

9
  respond_to :html, :json
10

11
  def destroy
12 13
    aspect = current_user.aspects.joins(:aspect_memberships).where(aspect_memberships: {id: params[:id]}).first
    contact = current_user.contacts.joins(:aspect_memberships).where(aspect_memberships: {id: params[:id]}).first
14

15
    raise ActiveRecord::RecordNotFound unless aspect.present? && contact.present?
16

17 18
    raise Diaspora::NotMine unless current_user.mine?(aspect) &&
                                   current_user.mine?(contact)
19

20
    membership = contact.aspect_memberships.where(aspect_id: aspect.id).first
21

22 23 24 25 26 27
    raise ActiveRecord::RecordNotFound unless membership.present?

    # do it!
    success = membership.destroy

    # set the flash message
28
    respond_to do |format|
29 30
      format.json do
        if success
31
          render json: AspectMembershipPresenter.new(membership).base_hash
32
        else
33
          render text: membership.errors.full_messages, status: 403
34
        end
35
      end
36

37 38 39 40 41 42 43 44
      format.all do
        if success
          flash.now[:notice] = I18n.t "aspect_memberships.destroy.success"
        else
          flash.now[:error] = I18n.t "aspect_memberships.destroy.failure"
        end
        redirect_to :back
      end
45 46
    end
  end
maxwell's avatar
maxwell committed
47 48 49

  def create
    @person = Person.find(params[:person_id])
50
    @aspect = current_user.aspects.where(id: params[:aspect_id]).first
51

52
    @contact = current_user.share_with(@person, @aspect)
maxwell's avatar
maxwell committed
53

54
    if @contact.present?
55
      respond_to do |format|
56
        format.json do
57 58
          render json: AspectMembershipPresenter.new(
            AspectMembership.where(contact_id: @contact.id, aspect_id: @aspect.id).first)
59
          .base_hash
60 61
        end

62 63 64 65
        format.all do
          flash.now[:notice] = I18n.t("aspects.add_to_aspect.success")
          redirect_to :back
        end
66
      end
67
    else
68 69 70 71 72 73 74 75 76 77
      respond_to do |format|
        format.json do
          render text: I18n.t("aspects.add_to_aspect.failure"), status: 409
        end

        format.all do
          flash.now[:error] = I18n.t("aspects.add_to_aspect.failure")
          render nothing: true, status: 409
        end
      end
maxwell's avatar
maxwell committed
78 79 80
    end
  end

81
  rescue_from ActiveRecord::StatementInvalid do
82
    render text: I18n.t("aspect_memberships.destroy.invalid_statement"), status: 400
83
  end
84 85

  rescue_from ActiveRecord::RecordNotFound do
86
    render text: I18n.t("aspect_memberships.destroy.no_membership"), status: 404
87 88 89
  end

  rescue_from Diaspora::NotMine do
90
    render text: I18n.t("aspect_memberships.destroy.forbidden"), status: 403
91
  end
92
end