Class: AspectsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/aspects_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


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/controllers/aspects_controller.rb', line 12

def create
  @aspect = current_user.aspects.build(aspect_params)
  aspecting_person_id = params[:aspect][:person_id]

  if @aspect.save
    flash[:notice] = I18n.t('aspects.create.success', :name => @aspect.name)

    if current_user.getting_started || request.referer.include?("contacts")
      redirect_to :back
    elsif aspecting_person_id.present?
      connect_person_to_aspect(aspecting_person_id)
    else
      redirect_to contacts_path(:a_id => @aspect.id)
    end
  else
    respond_to do |format|
      format.js { render :text => I18n.t('aspects.create.failure'), :status => 422 }
      format.html do
        flash[:error] = I18n.t('aspects.create.failure')
        redirect_to :back
      end
    end
  end
end

#destroyObject


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'app/controllers/aspects_controller.rb', line 46

def destroy
  @aspect = current_user.aspects.where(:id => params[:id]).first

  begin
    @aspect.destroy
    flash[:notice] = I18n.t 'aspects.destroy.success', :name => @aspect.name
  rescue ActiveRecord::StatementInvalid => e
    flash[:error] = I18n.t 'aspects.destroy.failure', :name => @aspect.name
  end
  if request.referer.include?('contacts')
    redirect_to contacts_path
  else
    redirect_to aspects_path
  end
end

#editObject


70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'app/controllers/aspects_controller.rb', line 70

def edit
  @aspect = current_user.aspects.where(:id => params[:id]).includes(:contacts => {:person => :profile}).first

  @contacts_in_aspect = @aspect.contacts.includes(:aspect_memberships, :person => :profile).all.sort! { |x, y| x.person.name <=> y.person.name }
  c = Contact.arel_table
  if @contacts_in_aspect.empty?
    @contacts_not_in_aspect = current_user.contacts.includes(:aspect_memberships, :person => :profile).all.sort! { |x, y| x.person.name <=> y.person.name }
  else
    @contacts_not_in_aspect = current_user.contacts.where(c[:id].not_in(@contacts_in_aspect.map(&:id))).includes(:aspect_memberships, :person => :profile).all.sort! { |x, y| x.person.name <=> y.person.name }
  end

  @contacts = @contacts_in_aspect + @contacts_not_in_aspect

  unless @aspect
    render :file => Rails.root.join('public', '404.html').to_s, :layout => false, :status => 404
  else
    @aspect_ids = [@aspect.id]
    @aspect_contacts_count = @aspect.contacts.size
    render :layout => false
  end
end

#newObject


37
38
39
40
41
42
43
44
# File 'app/controllers/aspects_controller.rb', line 37

def new
  @aspect = Aspect.new
  @person_id = params[:person_id]
  @remote = params[:remote] == "true"
  respond_to do |format|
    format.html { render :layout => false }
  end
end

#showObject


62
63
64
65
66
67
68
# File 'app/controllers/aspects_controller.rb', line 62

def show
  if @aspect = current_user.aspects.where(:id => params[:id]).first
    redirect_to aspects_path('a_ids[]' => @aspect.id)
  else
    redirect_to aspects_path
  end
end

#toggle_contact_visibilityObject


103
104
105
106
107
108
109
110
111
112
# File 'app/controllers/aspects_controller.rb', line 103

def toggle_contact_visibility
  @aspect = current_user.aspects.where(:id => params[:aspect_id]).first

  if @aspect.contacts_visible?
    @aspect.contacts_visible = false
  else
    @aspect.contacts_visible = true
  end
  @aspect.save
end

#updateObject


92
93
94
95
96
97
98
99
100
101
# File 'app/controllers/aspects_controller.rb', line 92

def update
  @aspect = current_user.aspects.where(:id => params[:id]).first

  if @aspect.update_attributes!(aspect_params)
    flash[:notice] = I18n.t 'aspects.update.success', :name => @aspect.name
  else
    flash[:error] = I18n.t 'aspects.update.failure', :name => @aspect.name
  end
  render :json => { :id => @aspect.id, :name => @aspect.name }
end