From 686d80e27412bbe9a088a300ed86b57f91810467 Mon Sep 17 00:00:00 2001 From: Devin Howard Date: Fri, 23 Sep 2016 17:02:52 +0800 Subject: [PATCH] move more logic into map model --- app/controllers/maps_controller.rb | 31 ++++++--------------------- app/models/map.rb | 34 ++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 24 deletions(-) diff --git a/app/controllers/maps_controller.rb b/app/controllers/maps_controller.rb index ff7a1686..1eba68e9 100644 --- a/app/controllers/maps_controller.rb +++ b/app/controllers/maps_controller.rb @@ -90,24 +90,13 @@ class MapsController < ApplicationController # POST maps/:id/access def access - userIds = params[:access] || [] - added = userIds.select do |uid| - user = User.find(uid) - if user.nil? || (current_user && user == current_user) - false - else - !@map.collaborators.include?(user) - end - end - removed = @map.collaborators.select { |user| !userIds.include?(user.id.to_s) }.map(&:id) - added.each do |uid| - UserMap.create(user_id: uid.to_i, map_id: @map.id) - user = User.find(uid.to_i) - MapMailer.invite_to_edit_email(@map, current_user, user).deliver_later - end - removed.each do |uid| - @map.user_maps.select { |um| um.user_id == uid }.each(&:destroy) + user_ids = params[:access] || [] + + added = @map.add_new_collaborators(user_ids) + added.each do |user_id| + MapMailer.invite_to_edit_email(@map, current_user, User.find(user_id)).deliver_later end + @map.remove_old_collaborators(user_ids) respond_to do |format| format.json do @@ -154,13 +143,7 @@ class MapsController < ApplicationController # POST maps/:id/upload_screenshot def screenshot - png = Base64.decode64(params[:encoded_image]['data:image/png;base64,'.length..-1]) - StringIO.open(png) do |data| - data.class.class_eval { attr_accessor :original_filename, :content_type } - data.original_filename = 'map-' + @map.id.to_s + '-screenshot.png' - data.content_type = 'image/png' - @map.screenshot = data - end + @map.base64_screenshot(params[:encoded_image]) if @map.save render json: { message: 'Successfully uploaded the map screenshot.' } diff --git a/app/models/map.rb b/app/models/map.rb index 7de744a2..ab3f3eb1 100644 --- a/app/models/map.rb +++ b/app/models/map.rb @@ -127,4 +127,38 @@ class Map < ApplicationRecord json['messages'] = messages.sort_by(&:created_at) json['stars'] = stars end + + def add_new_collaborators(user_ids) + added = [] + users = User.where(id: user_ids) + users.each do |user| + if user && user != current_user && !collaborators.include?(user) + UserMap.create(user_id: uid.to_i, map_id: id) + user = User.find(uid.to_i) + added << user.id + end + end + added + end + + def remove_old_collaborators(user_ids) + removed = [] + collaborators.map(&:id).each do |user_id| + if !user_ids.include?(user_id) + user_maps.select { |um| um.user_id == user_id }.each(&:destroy) + removed << user_id + end + end + removed + end + + def base64_screenshot(encoded_image) + png = Base64.decode64(encoded_image['data:image/png;base64,'.length..-1]) + StringIO.open(png) do |data| + data.class.class_eval { attr_accessor :original_filename, :content_type } + data.original_filename = 'map-' + @map.id.to_s + '-screenshot.png' + data.content_type = 'image/png' + @map.screenshot = data + end + end end