Class: AspectMembershipsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/aspect_memberships_controller.rb

Overview

Copyright © 2010-2011, Diaspora Inc. This file is

licensed under the Affero General Public License version 3 or later.  See
the COPYRIGHT file.

Instance Method Summary collapse

Instance Method Details

#createObject


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/controllers/aspect_memberships_controller.rb', line 50

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

  @contact = current_user.share_with(@person, @aspect)

  if @contact.present?
    flash.now[:notice] =  I18n.t('aspects.add_to_aspect.success')
    respond_with do |format|
      format.json do
        render :json => AspectMembership.where(:contact_id => @contact.id, :aspect_id => @aspect.id).first.to_json
      end

      format.all { redirect_to :back }
    end
  else
    flash.now[:error] = I18n.t('contacts.create.failure')
    render :nothing => true, :status => 409
  end
end

#destroyObject

Raises:

  • (ActiveRecord::RecordNotFound)

11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/controllers/aspect_memberships_controller.rb', line 11

def destroy
  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

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

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

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

  raise ActiveRecord::RecordNotFound unless membership.present?

  # do it!
  success = membership.destroy

  # set the flash message
  if success
    flash.now[:notice] = I18n.t 'aspect_memberships.destroy.success'
  else
    flash.now[:error] = I18n.t 'aspect_memberships.destroy.failure'
  end

  respond_with do |format|
    format.json do
      if success
        render :json => {
          :person_id  => contact.person_id,
          :aspect_ids => contact.aspects.map{|a| a.id}
        }
      else
        render :text => membership.errors.full_messages, :status => 403
      end
    end

    format.all { redirect_to :back }
  end
end