metamaps--metamaps/app/models/map.rb

127 lines
4 KiB
Ruby
Raw Normal View History

2016-09-24 03:00:46 +00:00
# frozen_string_literal: true
class Map < ApplicationRecord
2014-08-12 22:14:04 +00:00
belongs_to :user
has_many :topicmappings, -> { Mapping.topicmapping }, class_name: :Mapping, dependent: :destroy
has_many :synapsemappings, -> { Mapping.synapsemapping }, class_name: :Mapping, dependent: :destroy
has_many :topics, through: :topicmappings, source: :mappable, source_type: 'Topic'
has_many :synapses, through: :synapsemappings, source: :mappable, source_type: 'Synapse'
2015-12-11 19:23:41 +00:00
has_many :messages, as: :resource, dependent: :destroy
has_many :stars
has_many :access_requests, dependent: :destroy
has_many :user_maps, dependent: :destroy
has_many :collaborators, through: :user_maps, source: :user
has_many :webhooks, as: :hookable
has_many :events, -> { includes :user }, as: :eventable, dependent: :destroy
# This method associates the attribute ":image" with a file attachment
has_attached_file :screenshot,
styles: {
thumb: ['220x220#', :png]
#:full => ['940x630#', :png]
},
default_url: 'https://s3.amazonaws.com/metamaps-assets/site/missing-map-square.png'
2015-12-22 18:16:03 +00:00
validates :name, presence: true
validates :arranged, inclusion: { in: [true, false] }
validates :permission, presence: true
2015-12-17 01:16:02 +00:00
validates :permission, inclusion: { in: Perm::ISSIONS.map(&:to_s) }
2016-01-22 01:59:09 +00:00
# Validate the attached image is image/jpg, image/png, etc
validates_attachment_content_type :screenshot, content_type: /\Aimage\/.*\Z/
after_save :update_deferring_topics_and_synapses, if: :permission_changed?
delegate :count, to: :topics, prefix: :topic # same as `def topic_count; topics.count; end`
delegate :count, to: :synapses, prefix: :synapse
delegate :count, to: :contributors, prefix: :contributor
delegate :count, to: :stars, prefix: :star
delegate :name, to: :user, prefix: true
2015-12-22 18:16:03 +00:00
def mappings
topicmappings.or(synapsemappings)
2014-08-12 22:14:04 +00:00
end
def contributors
User.where(id: mappings.map(&:user_id).uniq)
end
2014-08-12 15:09:53 +00:00
def editors
User.where(id: user_id).or(User.where(id: collaborators))
end
def user_image
user.image.url(:thirtytwo)
end
def collaborator_ids
collaborators.map(&:id)
end
2014-10-08 04:07:54 +00:00
def screenshot_url
2016-02-09 03:48:07 +00:00
screenshot.url(:thumb)
2014-10-08 04:07:54 +00:00
end
2014-08-27 02:51:50 +00:00
def created_at_str
created_at.strftime('%m/%d/%Y')
2014-08-27 02:51:50 +00:00
end
def updated_at_str
updated_at.strftime('%m/%d/%Y')
2014-08-27 02:51:50 +00:00
end
def starred_by_user?(user)
user&.stars&.where(map: self)&.exists? || false # return false, not nil
end
def as_json(_options = {})
json = super(methods: [:user_name, :user_image, :star_count, :topic_count, :synapse_count, :contributor_count, :collaborator_ids, :screenshot_url], except: [:screenshot_content_type, :screenshot_file_size, :screenshot_file_name, :screenshot_updated_at])
2016-02-09 03:48:07 +00:00
json[:created_at_clean] = created_at_str
json[:updated_at_clean] = updated_at_str
2014-08-27 02:51:50 +00:00
json
2014-08-12 15:09:53 +00:00
end
2016-09-23 08:41:15 +00:00
# user param helps determine what records are visible
def contains(user)
2016-09-24 04:54:14 +00:00
{
map: self,
topics: Pundit.policy_scope(user, topics).to_a,
synapses: Pundit.policy_scope(user, synapses).to_a,
mappings: Pundit.policy_scope(user, mappings).to_a,
mappers: contributors,
collaborators: editors,
messages: messages.sort_by(&:created_at),
stars: stars,
requests: access_requests
2016-09-24 04:54:14 +00:00
}
2016-09-23 08:41:15 +00:00
end
2016-09-23 09:02:52 +00:00
def add_new_collaborators(user_ids)
users = User.where(id: user_ids)
2016-09-24 04:54:14 +00:00
added = users.map do |new_user|
next nil if editors.include?(new_user)
2016-09-24 04:54:14 +00:00
UserMap.create(user_id: new_user.id, map_id: id)
new_user.id
2016-09-23 09:02:52 +00:00
end
2016-09-24 04:54:14 +00:00
added.compact
2016-09-23 09:02:52 +00:00
end
def remove_old_collaborators(user_ids)
removed = editors.map(&:id).map do |old_user_id|
2016-09-24 04:54:14 +00:00
next nil if user_ids.include?(old_user_id)
user_maps.where(user_id: old_user_id).find_each(&:destroy)
access_requests.where(user_id: old_user_id).find_each(&:destroy)
2016-09-24 04:54:14 +00:00
old_user_id
2016-09-23 09:02:52 +00:00
end
2016-09-24 04:54:14 +00:00
removed.compact
2016-09-23 09:02:52 +00:00
end
def update_deferring_topics_and_synapses
Topic.where(defer_to_map_id: id).update_all(permission: permission)
Synapse.where(defer_to_map_id: id).update_all(permission: permission)
end
end