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

Raphael's avatar
Raphael committed
5
class AspectsController < ApplicationController
6
  before_action :authenticate_user!
7

8 9 10
  respond_to :html,
             :js,
             :json
11

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

16
    if @aspect.save
maxwell's avatar
maxwell committed
17
      flash[:notice] = I18n.t('aspects.create.success', :name => @aspect.name)
danielgrippi's avatar
danielgrippi committed
18

19 20 21 22
      if current_user.getting_started || request.referer.include?("contacts")
        redirect_to :back
      elsif aspecting_person_id.present?
        connect_person_to_aspect(aspecting_person_id)
maxwell's avatar
maxwell committed
23
      else
24
        redirect_to contacts_path(:a_id => @aspect.id)
maxwell's avatar
maxwell committed
25
      end
Stephen Caudill's avatar
Stephen Caudill committed
26
    else
27 28 29 30 31 32 33
      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
Stephen Caudill's avatar
Stephen Caudill committed
34
    end
35
  end
36

37
  def new
Raphael's avatar
Raphael committed
38
    @aspect = Aspect.new
39
    @person_id = params[:person_id]
40
    @remote = params[:remote] == "true"
MrZYX's avatar
MrZYX committed
41
    respond_to do |format|
42
      format.html { render :layout => false }
MrZYX's avatar
MrZYX committed
43
    end
44
  end
45

46
  def destroy
47
    @aspect = current_user.aspects.where(id: params[:id]).first
danielvincent's avatar
danielvincent committed
48 49

    begin
50 51 52 53 54 55
      if current_user.auto_follow_back && @aspect.id == current_user.auto_follow_back_aspect.id
        current_user.update(auto_follow_back: false, auto_follow_back_aspect: nil)
        flash[:notice] = I18n.t "aspects.destroy.success_auto_follow_back", name: @aspect.name
      else
        flash[:notice] = I18n.t "aspects.destroy.success", name: @aspect.name
      end
56
      @aspect.destroy
57
    rescue ActiveRecord::StatementInvalid => e
58
      flash[:error] = I18n.t "aspects.destroy.failure", name: @aspect.name
59
    end
60

61 62 63
    if request.referer.include?('contacts')
      redirect_to contacts_path
    else
64
      redirect_to aspects_path
65
    end
66
  end
67

68
  def show
69
    if @aspect = current_user.aspects.where(:id => params[:id]).first
70 71 72 73
      redirect_to aspects_path('a_ids[]' => @aspect.id)
    else
      redirect_to aspects_path
    end
74 75 76
  end

  def update
Raphael's avatar
Raphael committed
77
    @aspect = current_user.aspects.where(:id => params[:id]).first
78

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

87 88 89 90 91 92 93 94
  def toggle_chat_privilege
    @aspect = current_user.aspects.where(:id => params[:aspect_id]).first

    @aspect.chat_enabled = !@aspect.chat_enabled
    @aspect.save
    render :nothing => true
  end

95 96 97 98 99 100 101 102 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
104
    render :nothing => true
105
  end
106 107 108 109 110 111 112 113 114 115 116

  private

  def connect_person_to_aspect(aspecting_person_id)
    @person = Person.find(aspecting_person_id)
    if @contact = current_user.contact_for(@person)
      @contact.aspects << @aspect
    else
      @contact = current_user.share_with(@person, @aspect)
    end
  end
117 118

  def aspect_params
119
    params.require(:aspect).permit(:name, :contacts_visible, :chat_enabled, :order_id)
120
  end
121
end