refactor map model a bit and fix bugs
This commit is contained in:
parent
3f9077b380
commit
c76de5b1d5
1 changed files with 22 additions and 30 deletions
|
@ -111,45 +111,37 @@ class Map < ApplicationRecord
|
||||||
|
|
||||||
# user param helps determine what records are visible
|
# user param helps determine what records are visible
|
||||||
def contains(user)
|
def contains(user)
|
||||||
allmappers = contributors
|
{
|
||||||
allcollaborators = editors
|
map: self,
|
||||||
alltopics = Pundit.policy_scope(user, topics).to_a
|
topics: Pundit.policy_scope(user, topics).to_a,
|
||||||
allsynapses = Pundit.policy_scope(user, synapses).to_a
|
synapses: Pundit.policy_scope(user, synapses).to_a,
|
||||||
allmappings = Pundit.policy_scope(user, mappings).to_a
|
mappings: Pundit.policy_scope(user, mappings).to_a,
|
||||||
|
mappers: contributors,
|
||||||
json = {}
|
collaborators: editors,
|
||||||
json['map'] = self
|
messages: messages.sort_by(&:created_at),
|
||||||
json['topics'] = alltopics
|
stars: stars
|
||||||
json['synapses'] = allsynapses
|
}
|
||||||
json['mappings'] = allmappings
|
|
||||||
json['mappers'] = allmappers
|
|
||||||
json['collaborators'] = allcollaborators
|
|
||||||
json['messages'] = messages.sort_by(&:created_at)
|
|
||||||
json['stars'] = stars
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def add_new_collaborators(user_ids)
|
def add_new_collaborators(user_ids)
|
||||||
added = []
|
|
||||||
users = User.where(id: user_ids)
|
users = User.where(id: user_ids)
|
||||||
users.each do |user|
|
current_collaborators = collaborators + [user]
|
||||||
if user && user != current_user && !collaborators.include?(user)
|
added = users.map do |new_user|
|
||||||
UserMap.create(user_id: uid.to_i, map_id: id)
|
next nil if current_collaborators.include?(new_user)
|
||||||
user = User.find(uid.to_i)
|
UserMap.create(user_id: new_user.id, map_id: id)
|
||||||
added << user.id
|
new_user.id
|
||||||
end
|
|
||||||
end
|
end
|
||||||
added
|
added.compact
|
||||||
end
|
end
|
||||||
|
|
||||||
def remove_old_collaborators(user_ids)
|
def remove_old_collaborators(user_ids)
|
||||||
removed = []
|
current_collaborators = collaborators + [user]
|
||||||
collaborators.map(&:id).each do |user_id|
|
removed = current_collaborators.map(&:id).map do |old_user_id|
|
||||||
if !user_ids.include?(user_id)
|
next nil if user_ids.include?(old_user_id)
|
||||||
user_maps.select { |um| um.user_id == user_id }.each(&:destroy)
|
user_maps.where(user_id: old_user_id).find_each(&:destroy)
|
||||||
removed << user_id
|
old_user_id
|
||||||
end
|
|
||||||
end
|
end
|
||||||
removed
|
removed.compact
|
||||||
end
|
end
|
||||||
|
|
||||||
def base64_screenshot(encoded_image)
|
def base64_screenshot(encoded_image)
|
||||||
|
|
Loading…
Reference in a new issue