Refactoring aspects_controller.rb

closes #8064
This commit is contained in:
FeruzOripov 2019-10-02 09:19:43 +05:00 committed by Benjamin Neff
parent ba16185b8e
commit 3856b44c57
No known key found for this signature in database
GPG key ID: 971464C3F1A90194

View file

@ -29,21 +29,19 @@ class AspectsController < ApplicationController
end end
def destroy def destroy
@aspect = current_user.aspects.where(id: params[:id]).first
begin begin
if current_user.auto_follow_back && @aspect.id == current_user.auto_follow_back_aspect.id 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) 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 flash[:notice] = I18n.t "aspects.destroy.success_auto_follow_back", name: aspect.name
else else
flash[:notice] = I18n.t "aspects.destroy.success", name: @aspect.name flash[:notice] = I18n.t "aspects.destroy.success", name: aspect.name
end end
@aspect.destroy aspect.destroy
rescue ActiveRecord::StatementInvalid => e rescue ActiveRecord::StatementInvalid => e
flash[:error] = I18n.t "aspects.destroy.failure", name: @aspect.name flash[:error] = I18n.t "aspects.destroy.failure", name: aspect.name
end end
if request.referer.include?('contacts') if request.referer.include?("contacts")
redirect_to contacts_path redirect_to contacts_path
else else
redirect_to aspects_path redirect_to aspects_path
@ -51,41 +49,41 @@ class AspectsController < ApplicationController
end end
def show def show
if @aspect = current_user.aspects.where(:id => params[:id]).first if aspect
redirect_to aspects_path('a_ids[]' => @aspect.id) redirect_to aspects_path("a_ids[]" => aspect.id)
else else
redirect_to aspects_path redirect_to aspects_path
end end
end end
def update def update
@aspect = current_user.aspects.where(:id => params[:id]).first if aspect.update!(aspect_params)
flash[:notice] = I18n.t "aspects.update.success", name: aspect.name
if @aspect.update_attributes!(aspect_params)
flash[:notice] = I18n.t 'aspects.update.success', :name => @aspect.name
else else
flash[:error] = I18n.t 'aspects.update.failure', :name => @aspect.name flash[:error] = I18n.t "aspects.update.failure", name: aspect.name
end end
render :json => { :id => @aspect.id, :name => @aspect.name } render json: {id: aspect.id, name: aspect.name}
end end
def update_order def update_order
params[:ordered_aspect_ids].each_with_index do |id, i| params[:ordered_aspect_ids].each_with_index do |id, i|
current_user.aspects.find(id).update_attributes(order_id: i) current_user.aspects.find(id).update(order_id: i)
end end
head :no_content head :no_content
end end
def toggle_chat_privilege def toggle_chat_privilege
@aspect = current_user.aspects.where(:id => params[:aspect_id]).first aspect.chat_enabled = !aspect.chat_enabled
aspect.save
@aspect.chat_enabled = !@aspect.chat_enabled
@aspect.save
head :no_content head :no_content
end end
private private
def aspect
@aspect ||= current_user.aspects.where(id: (params[:id] || params[:aspect_id])).first
end
def connect_person_to_aspect(aspecting_person_id) def connect_person_to_aspect(aspecting_person_id)
@person = Person.find(aspecting_person_id) @person = Person.find(aspecting_person_id)
if @contact = current_user.contact_for(@person) if @contact = current_user.contact_for(@person)