maps controller code climate

This commit is contained in:
Devin Howard 2016-09-26 00:36:08 +08:00
parent f75ad41a82
commit 3ee8d41298

View file

@ -1,7 +1,9 @@
# frozen_string_literal: true # frozen_string_literal: true
class MapsController < ApplicationController class MapsController < ApplicationController
before_action :require_user, only: [:create, :update, :destroy, :access, :events, :screenshot, :star, :unstar] before_action :require_user, only: [:create, :update, :destroy, :access, :events,
before_action :set_map, only: [:show, :update, :destroy, :access, :contains, :events, :export, :screenshot, :star, :unstar] :screenshot, :star, :unstar]
before_action :set_map, only: [:show, :update, :destroy, :access, :contains,
:events, :export, :screenshot, :star, :unstar]
after_action :verify_authorized after_action :verify_authorized
autocomplete :map, :name, full: true, extra_data: [:user_id] autocomplete :map, :name, full: true, extra_data: [:user_id]
@ -18,7 +20,8 @@ class MapsController < ApplicationController
@allmessages = @map.messages.sort_by(&:created_at) @allmessages = @map.messages.sort_by(&:created_at)
@allstars = @map.stars @allstars = @map.stars
respond_with(@allmappers, @allcollaborators, @allmappings, @allsynapses, @alltopics, @allmessages, @allstars, @map) respond_with(@allmappers, @allcollaborators, @allmappings, @allsynapses,
@alltopics, @allmessages, @allstars, @map)
end end
format.json { render json: @map } format.json { render json: @map }
format.csv { redirect_to action: :export, format: :csv } format.csv { redirect_to action: :export, format: :csv }
@ -41,10 +44,10 @@ class MapsController < ApplicationController
# POST maps # POST maps
def create def create
@user = current_user
@map = Map.new(create_map_params) @map = Map.new(create_map_params)
@map.user = @user @map.user = current_user
@map.arranged = false @map.arranged = false
authorize @map
if params[:topicsToMap].present? if params[:topicsToMap].present?
create_topics! create_topics!
@ -52,8 +55,6 @@ class MapsController < ApplicationController
@map.arranged = true @map.arranged = true
end end
authorize @map
respond_to do |format| respond_to do |format|
if @map.save if @map.save
format.json { render json: @map } format.json { render json: @map }
@ -89,8 +90,9 @@ class MapsController < ApplicationController
def access def access
user_ids = params[:access] || [] user_ids = params[:access] || []
added = @map.add_new_collaborators(user_ids) @map.add_new_collaborators(user_ids).each do |user_id|
added.each do |user_id| # add_new_collaborators returns array of added users,
# who we then send an email to
MapMailer.invite_to_edit_email(@map, current_user, User.find(user_id)).deliver_later MapMailer.invite_to_edit_email(@map, current_user, User.find(user_id)).deliver_later
end end
@map.remove_old_collaborators(user_ids) @map.remove_old_collaborators(user_ids)
@ -150,7 +152,7 @@ class MapsController < ApplicationController
# POST maps/:id/star # POST maps/:id/star
def star def star
star = Star.find_or_create_by(map_id: @map.id, user_id: current_user.id) Star.find_or_create_by(map_id: @map.id, user_id: current_user.id)
respond_to do |format| respond_to do |format|
format.json do format.json do
@ -187,29 +189,20 @@ class MapsController < ApplicationController
end end
def create_topics! def create_topics!
topics = params[:topicsToMap] params[:topicsToMap].split(',').each do |topic|
topics = topics.split(',')
topics.each do |topic|
topic = topic.split('/') topic = topic.split('/')
mapping = Mapping.new mapping = Mapping.new(map: @map, user: current_user,
mapping.map = @map mappable: Topic.find(topic[0]),
mapping.user = @user xloc: topic[1], yloc: topic[2])
mapping.mappable = Topic.find(topic[0])
mapping.xloc = topic[1]
mapping.yloc = topic[2]
authorize mapping, :create? authorize mapping, :create?
mapping.save mapping.save
end end
end end
def create_synapses! def create_synapses!
@synAll = params[:synapsesToMap] params[:synapsesToMap].split(',').each do |synapse_id|
@synAll = @synAll.split(',') mapping = Mapping.new(map: @map, user: current_user,
@synAll.each do |synapse_id| mappable: Synapse.find(synapse_id))
mapping = Mapping.new
mapping.map = @map
mapping.user = @user
mapping.mappable = Synapse.find(synapse_id)
authorize mapping, :create? authorize mapping, :create?
mapping.save mapping.save
end end