manual rubocop fixes (#1163)

This commit is contained in:
Devin Howard 2018-01-21 14:21:00 -08:00 committed by GitHub
parent e0d72fce14
commit b7761a3627
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
35 changed files with 234 additions and 169 deletions

View file

@ -22,3 +22,8 @@ Style/Documentation:
Style/EmptyMethod: Style/EmptyMethod:
EnforcedStyle: expanded EnforcedStyle: expanded
# I like this cop, but occasionally code is more readable without a guard clause,
# and I don't want to write rubocop:disable comments every time that happens
Style/GuardClause:
Enabled: false

View file

@ -2,7 +2,9 @@
class MapsController < ApplicationController class MapsController < ApplicationController
before_action :require_user, only: %i[create update destroy events follow unfollow] before_action :require_user, only: %i[create update destroy events follow unfollow]
before_action :set_map, only: %i[show conversation update destroy contains events export follow unfollow unfollow_from_email] before_action :set_map, only: %i[show conversation update destroy
contains events export
follow unfollow unfollow_from_email]
after_action :verify_authorized after_action :verify_authorized
# GET maps/:id # GET maps/:id

View file

@ -17,7 +17,7 @@ module TopicsHelper
rtype: is_map ? 'map' : 'topic', rtype: is_map ? 'map' : 'topic',
inmaps: is_map ? [] : t.inmaps(current_user), inmaps: is_map ? [] : t.inmaps(current_user),
inmapsLinks: is_map ? [] : t.inmapsLinks(current_user), inmapsLinks: is_map ? [] : t.inmaps_links(current_user),
type: is_map ? metamap_metacode.name : t.metacode.name, type: is_map ? metamap_metacode.name : t.metacode.name,
typeImageURL: is_map ? metamap_metacode.icon : t.metacode.icon, typeImageURL: is_map ? metamap_metacode.icon : t.metacode.icon,
mapCount: is_map ? 0 : t.maps.count, mapCount: is_map ? 0 : t.maps.count,

View file

@ -11,7 +11,7 @@ class Event < ApplicationRecord
belongs_to :map belongs_to :map
belongs_to :user belongs_to :user
scope :chronologically, -> { order('created_at asc') } scope :chronologically, (-> { order('created_at asc') })
after_create :notify_webhooks!, if: :map after_create :notify_webhooks!, if: :map

View file

@ -11,13 +11,11 @@ class Follow < ApplicationRecord
after_create :add_subsetting after_create :add_subsetting
scope :active, -> { scope :active, (-> { where(muted: false) })
where(muted: false)
}
private private
def add_subsetting def add_subsetting
follow_reason = FollowReason.create!(follow: self) FollowReason.create!(follow: self)
end end
end end

View file

@ -153,12 +153,11 @@ class Map < ApplicationRecord
end end
def after_updated_async def after_updated_async
if ATTRS_TO_WATCH.any? { |k| changed_attributes.key?(k) } return unless ATTRS_TO_WATCH.any? { |k| changed_attributes.key?(k) }
FollowService.follow(self, updated_by, 'contributed') FollowService.follow(self, updated_by, 'contributed')
# NotificationService.notify_followers(self, 'map_updated', changed_attributes) # NotificationService.notify_followers(self, 'map_updated', changed_attributes)
# or better yet publish an event # or better yet publish an event
end end
end
handle_asynchronously :after_updated_async handle_asynchronously :after_updated_async
def before_destroyed def before_destroyed

View file

@ -1,8 +1,8 @@
# frozen_string_literal: true # frozen_string_literal: true
class Mapping < ApplicationRecord class Mapping < ApplicationRecord
scope :topicmapping, -> { where(mappable_type: :Topic) } scope :topicmapping, (-> { where(mappable_type: :Topic) })
scope :synapsemapping, -> { where(mappable_type: :Synapse) } scope :synapsemapping, (-> { where(mappable_type: :Synapse) })
belongs_to :mappable, polymorphic: true belongs_to :mappable, polymorphic: true
belongs_to :map, class_name: 'Map', foreign_key: 'map_id', touch: true belongs_to :map, class_name: 'Map', foreign_key: 'map_id', touch: true
@ -53,18 +53,18 @@ class Mapping < ApplicationRecord
handle_asynchronously :after_created_async handle_asynchronously :after_created_async
def after_updated def after_updated
if (mappable_type == 'Topic') && (xloc_changed? || yloc_changed?) return unless (mappable_type == 'Topic') && (xloc_changed? || yloc_changed?)
meta = { 'x': xloc, 'y': yloc, 'mapping_id': id } meta = { 'x': xloc, 'y': yloc, 'mapping_id': id }
Events::TopicMovedOnMap.publish!(mappable, map, updated_by, meta) Events::TopicMovedOnMap.publish!(mappable, map, updated_by, meta)
ActionCable.server.broadcast 'map_' + map.id.to_s, type: 'topicMoved', id: mappable.id, mapping_id: id, x: xloc, y: yloc ActionCable.server.broadcast('map_' + map.id.to_s, type: 'topicMoved',
end id: mappable.id, mapping_id: id,
x: xloc, y: yloc)
end end
def after_updated_async def after_updated_async
if (mappable_type == 'Topic') && (xloc_changed? || yloc_changed?) return unless (mappable_type == 'Topic') && (xloc_changed? || yloc_changed?)
FollowService.follow(map, updated_by, 'contributed') FollowService.follow(map, updated_by, 'contributed')
end end
end
handle_asynchronously :after_updated_async handle_asynchronously :after_updated_async
def before_destroyed def before_destroyed

View file

@ -6,13 +6,11 @@ class Metacode < ApplicationRecord
has_many :topics has_many :topics
# This method associates the attribute ":aws_icon" with a file attachment # This method associates the attribute ":aws_icon" with a file attachment
has_attached_file :aws_icon, styles: { has_attached_file :aws_icon, styles: { ninetysix: ['96x96#', :png] },
ninetysix: ['96x96#', :png]
},
default_url: 'https://s3.amazonaws.com/metamaps-assets/metacodes/generics/96px/gen_wildcard.png' default_url: 'https://s3.amazonaws.com/metamaps-assets/metacodes/generics/96px/gen_wildcard.png'
# Validate the attached icon is image/jpg, image/png, etc # Validate the attached icon is image/jpg, image/png, etc
validates_attachment_content_type :aws_icon, content_type: /\Aimage\/.*\Z/ validates_attachment_content_type :aws_icon, content_type: %r{\Aimage/.*\Z}
validate :aws_xor_manual_icon validate :aws_xor_manual_icon
validate :manual_icon_https validate :manual_icon_https
@ -31,15 +29,13 @@ class Metacode < ApplicationRecord
def as_json(options = {}) def as_json(options = {})
default = super(options) default = super(options)
default[:icon] = icon default[:icon] = icon
default.except('aws_icon_file_name', 'aws_icon_content_type', 'aws_icon_file_size', 'aws_icon_updated_at', 'manual_icon') default.except(
'aws_icon_file_name', 'aws_icon_content_type', 'aws_icon_file_size', 'aws_icon_updated_at',
'manual_icon'
)
end end
def hasSelected(user) def in_metacode_set(metacode_set)
return true if user.settings.metacodes.include? id.to_s
false
end
def inMetacodeSet(metacode_set)
return true if metacode_sets.include? metacode_set return true if metacode_sets.include? metacode_set
false false
end end
@ -56,10 +52,9 @@ class Metacode < ApplicationRecord
end end
def manual_icon_https def manual_icon_https
if manual_icon.present? return if manual_icon.blank?
unless manual_icon.starts_with? 'https' unless manual_icon.starts_with? 'https'
errors.add(:base, 'Manual icon must begin with https') errors.add(:base, 'Manual icon must begin with https')
end end
end end
end end
end

View file

@ -1,14 +1,18 @@
# frozen_string_literal: true # frozen_string_literal: true
class PermittedParams < Struct.new(:params) class PermittedParams
%w[map synapse topic mapping token].each do |kind| %w[map synapse topic mapping token].each do |kind|
define_method(kind) do define_method(kind) do
permitted_attributes = send("#{kind}_attributes") permitted_attributes = send("#{kind}_attributes")
params.require(kind).permit(*permitted_attributes) @params.require(kind).permit(*permitted_attributes)
end end
alias_method :"api_#{kind}", kind.to_sym alias_method :"api_#{kind}", kind.to_sym
end end
def initialize(params)
@params = params
end
alias read_attribute_for_serialization send alias read_attribute_for_serialization send
def token_attributes def token_attributes

View file

@ -22,9 +22,9 @@ class Synapse < ApplicationRecord
validates :category, inclusion: { in: ['from-to', 'both'], allow_nil: true } validates :category, inclusion: { in: ['from-to', 'both'], allow_nil: true }
scope :for_topic, ->(topic_id = nil) { scope :for_topic, (lambda do |topic_id = nil|
where(topic1_id: topic_id).or(where(topic2_id: topic_id)) where(topic1_id: topic_id).or(where(topic2_id: topic_id))
} end)
before_create :set_perm_by_defer before_create :set_perm_by_defer
after_create :after_created_async after_create :after_created_async
@ -73,7 +73,7 @@ class Synapse < ApplicationRecord
protected protected
def set_perm_by_defer def set_perm_by_defer
permission = defer_to_map.permission if defer_to_map defer_to_map&.permission
end end
def after_created_async def after_created_async
@ -83,7 +83,8 @@ class Synapse < ApplicationRecord
handle_asynchronously :after_created_async handle_asynchronously :after_created_async
def after_updated def after_updated
if ATTRS_TO_WATCH.any? { |k| changed_attributes.key?(k) } return unless ATTRS_TO_WATCH.any? { |k| changed_attributes.key?(k) }
new = attributes.select { |k| ATTRS_TO_WATCH.include?(k) } new = attributes.select { |k| ATTRS_TO_WATCH.include?(k) }
old = changed_attributes.select { |k| ATTRS_TO_WATCH.include?(k) } old = changed_attributes.select { |k| ATTRS_TO_WATCH.include?(k) }
meta = new.merge(old) # we are prioritizing the old values, keeping them meta = new.merge(old) # we are prioritizing the old values, keeping them
@ -93,7 +94,6 @@ class Synapse < ApplicationRecord
ActionCable.server.broadcast 'map_' + map.id.to_s, type: 'synapseUpdated', id: id ActionCable.server.broadcast 'map_' + map.id.to_s, type: 'synapseUpdated', id: id
end end
end end
end
def before_destroyed def before_destroyed
# hard to know how to do this yet, because the synapse actually gets destroyed # hard to know how to do this yet, because the synapse actually gets destroyed

View file

@ -39,13 +39,13 @@ class Topic < ApplicationRecord
topics1.or(topics2) topics1.or(topics2)
end end
scope :relatives, ->(topic_id = nil, user = nil) { scope :relatives, (lambda do |topic_id = nil, user = nil|
# should only see topics through *visible* synapses # should only see topics through *visible* synapses
# e.g. Topic A (commons) -> synapse (private) -> Topic B (commons) must be filtered out # e.g. Topic A (commons) -> synapse (private) -> Topic B (commons) must be filtered out
topic_ids = Pundit.policy_scope(user, Synapse.where(topic1_id: topic_id)).pluck(:topic2_id) topic_ids = Pundit.policy_scope(user, Synapse.where(topic1_id: topic_id)).pluck(:topic2_id)
topic_ids += Pundit.policy_scope(user, Synapse.where(topic2_id: topic_id)).pluck(:topic1_id) topic_ids += Pundit.policy_scope(user, Synapse.where(topic2_id: topic_id)).pluck(:topic1_id)
where(id: topic_ids.uniq) where(id: topic_ids.uniq)
} end)
delegate :name, to: :user, prefix: true delegate :name, to: :user, prefix: true
@ -65,13 +65,13 @@ class Topic < ApplicationRecord
Pundit.policy_scope(user, maps).map(&:name) Pundit.policy_scope(user, maps).map(&:name)
end end
def inmapsLinks(user) def inmaps_links(user)
Pundit.policy_scope(user, maps).map(&:id) Pundit.policy_scope(user, maps).map(&:id)
end end
def as_json(options = {}) def as_json(options = {})
super(methods: %i[user_name user_image collaborator_ids]) super(methods: %i[user_name user_image collaborator_ids])
.merge(inmaps: inmaps(options[:user]), inmapsLinks: inmapsLinks(options[:user]), .merge(inmaps: inmaps(options[:user]), inmapsLinks: inmaps_links(options[:user]),
map_count: map_count(options[:user]), synapse_count: synapse_count(options[:user])) map_count: map_count(options[:user]), synapse_count: synapse_count(options[:user]))
end end
@ -127,15 +127,10 @@ class Topic < ApplicationRecord
end end
end end
end end
if output_format == 'array' return output if output_format == 'array'
return output return output.join('; ') if output_format == 'text'
elsif output_format == 'text'
return output.join('; ')
else
raise 'invalid argument to synapses_csv' raise 'invalid argument to synapses_csv'
end end
output
end
def topic_autocomplete_method def topic_autocomplete_method
"Get: #{name}" "Get: #{name}"
@ -144,7 +139,7 @@ class Topic < ApplicationRecord
protected protected
def set_perm_by_defer def set_perm_by_defer
permission = defer_to_map.permission if defer_to_map self.permission = defer_to_map.permission if defer_to_map
end end
def create_metamap? def create_metamap?
@ -163,7 +158,8 @@ class Topic < ApplicationRecord
handle_asynchronously :after_created_async handle_asynchronously :after_created_async
def after_updated def after_updated
if ATTRS_TO_WATCH.any? { |k| changed_attributes.key?(k) } return unless ATTRS_TO_WATCH.any? { |k| changed_attributes.key?(k) }
new = attributes.select { |k| ATTRS_TO_WATCH.include?(k) } new = attributes.select { |k| ATTRS_TO_WATCH.include?(k) }
old = changed_attributes.select { |k| ATTRS_TO_WATCH.include?(k) } old = changed_attributes.select { |k| ATTRS_TO_WATCH.include?(k) }
meta = new.merge(old) # we are prioritizing the old values, keeping them meta = new.merge(old) # we are prioritizing the old values, keeping them
@ -173,13 +169,12 @@ class Topic < ApplicationRecord
ActionCable.server.broadcast 'map_' + map.id.to_s, type: 'topicUpdated', id: id ActionCable.server.broadcast 'map_' + map.id.to_s, type: 'topicUpdated', id: id
end end
end end
end
def after_updated_async def after_updated_async
if ATTRS_TO_WATCH.any? { |k| changed_attributes.key?(k) } return unless ATTRS_TO_WATCH.any? { |k| changed_attributes.key?(k) }
FollowService.follow(self, updated_by, 'contributed') FollowService.follow(self, updated_by, 'contributed')
end end
end
handle_asynchronously :after_updated_async handle_asynchronously :after_updated_async
def before_destroyed def before_destroyed

View file

@ -52,24 +52,24 @@ class User < ApplicationRecord
validates_attachment_content_type :image, content_type: %r{\Aimage/.*\Z} validates_attachment_content_type :image, content_type: %r{\Aimage/.*\Z}
# override default as_json # override default as_json
def as_json(_options = {}) def as_json(options = {})
json = { id: id, json = { id: id,
name: name, name: name,
image: image.url(:sixtyfour), image: image.url(:sixtyfour),
admin: admin } admin: admin }
if _options[:follows] if options[:follows]
json['follows'] = { json['follows'] = {
topics: following.active.where(followed_type: 'Topic').to_a.map(&:followed_id), topics: following.active.where(followed_type: 'Topic').to_a.map(&:followed_id),
maps: following.active.where(followed_type: 'Map').to_a.map(&:followed_id) maps: following.active.where(followed_type: 'Map').to_a.map(&:followed_id)
} }
end end
if _options[:follow_settings] if options[:follow_settings]
json['follow_topic_on_created'] = settings.follow_topic_on_created == '1' json['follow_topic_on_created'] = settings.follow_topic_on_created == '1'
json['follow_topic_on_contributed'] = settings.follow_topic_on_contributed == '1' json['follow_topic_on_contributed'] = settings.follow_topic_on_contributed == '1'
json['follow_map_on_created'] = settings.follow_map_on_created == '1' json['follow_map_on_created'] = settings.follow_map_on_created == '1'
json['follow_map_on_contributed'] = settings.follow_map_on_contributed == '1' json['follow_map_on_contributed'] = settings.follow_map_on_contributed == '1'
end end
json['email'] = email if _options[:email] json['email'] = email if options[:email]
json json
end end
@ -106,10 +106,14 @@ class User < ApplicationRecord
end end
def most_used_metacodes def most_used_metacodes
topics.to_a.each_with_object(Hash.new(0)) do |topic, memo| metacode_counts = topics.to_a.each_with_object(Hash.new(0)) do |topic, list_so_far|
memo[topic.metacode_id] += 1 list_so_far[topic.metacode_id] += 1
memo list_so_far
end.to_a.sort { |a, b| b[1] <=> a[1] }.map { |i| i[0] }.slice(0, 5) end
id_count_pairs = metacode_counts.to_a
id_count_pairs.sort! { |a, b| b[1] <=> a[1] }
metacode_ids = id_count_pairs.map { |i| i[0] }
metacode_ids.slice(0, 5)
end end
# generate a random 8 letter/digit code that they can use to invite people # generate a random 8 letter/digit code that they can use to invite people
@ -132,11 +136,11 @@ class User < ApplicationRecord
end end
def has_map_open(map) def has_map_open(map)
latestEvent = Event.where(map: map, user: self) latest_event = Event.where(map: map, user: self)
.where(kind: %w[user_present_on_map user_not_present_on_map]) .where(kind: %w[user_present_on_map user_not_present_on_map])
.order(:created_at) .order(:created_at)
.last .last
latestEvent && latestEvent.kind == 'user_present_on_map' latest_event && latest_event.kind == 'user_present_on_map'
end end
def has_map_with_synapse_open(synapse) def has_map_with_synapse_open(synapse)

View file

@ -5,20 +5,24 @@ class UserPreference
:follow_map_on_created, :follow_map_on_contributed :follow_map_on_created, :follow_map_on_contributed
def initialize def initialize
array = [] @metacodes = init_metacodes.compact
%w[Action Aim Idea Question Note Wildcard Subject].each do |m| @metacode_focus = @metacodes[0]
initialize_follow_settings
end
private
def init_metacodes
%w[Action Aim Idea Question Note Wildcard Subject].map do |m|
begin begin
metacode = Metacode.find_by(name: m) metacode = Metacode.find_by(name: m)
array.push(metacode.id.to_s) if metacode metacode.id.to_s if metacode
rescue ActiveRecord::StatementInvalid rescue ActiveRecord::StatementInvalid
if m == 'Action' if m == 'Action'
Rails.logger.warn('TODO: remove this travis workaround in user_preference.rb') Rails.logger.warn('TODO: remove this travis workaround in user_preference.rb')
end end
end end
end end.compact
@metacodes = array
@metacode_focus = array[0]
initialize_follow_settings
end end
def initialize_follow_settings def initialize_follow_settings

View file

@ -3,6 +3,8 @@
class Webhooks::Slack::SynapseAddedToMap < Webhooks::Slack::Base class Webhooks::Slack::SynapseAddedToMap < Webhooks::Slack::Base
def text def text
connector = eventable.desc.empty? ? '->' : eventable.desc connector = eventable.desc.empty? ? '->' : eventable.desc
"\"*#{eventable.topic1.name}* #{connector} *#{eventable.topic2.name}*\" was added as a connection by *#{event.user.name}* to the map *#{view_map_on_metamaps}*" "\"*#{eventable.topic1.name}* #{connector} *#{eventable.topic2.name}*\"" \
" was added as a connection by *#{event.user.name}*" \
" to the map *#{view_map_on_metamaps}*"
end end
end end

View file

@ -3,7 +3,11 @@
class Webhooks::Slack::SynapseRemovedFromMap < Webhooks::Slack::Base class Webhooks::Slack::SynapseRemovedFromMap < Webhooks::Slack::Base
def text def text
connector = eventable.desc.empty? ? '->' : eventable.desc connector = eventable.desc.empty? ? '->' : eventable.desc
# TODO: express correct directionality of arrows when desc is empty # TODO: express correct directionality of arrows when desc is empty
"\"*#{eventable.topic1.name}* #{connector} *#{eventable.topic2.name}*\" was removed by *#{event.user.name}* as a connection from the map *#{view_map_on_metamaps}*"
"\"*#{eventable.topic1.name}* #{connector} *#{eventable.topic2.name}*\"" \
" was removed by *#{event.user.name}* as a connection" \
" from the map *#{view_map_on_metamaps}*"
end end
end end

View file

@ -23,7 +23,7 @@ class MapPolicy < ApplicationPolicy
end end
def conversation? def conversation?
show? && %w[connorturland@gmail.com devin@callysto.com chessscholar@gmail.com solaureum@gmail.com ishanshapiro@gmail.com].include?(user.email) show? && is_tester(user)
end end
def create? def create?

View file

@ -3,12 +3,21 @@
class SynapsePolicy < ApplicationPolicy class SynapsePolicy < ApplicationPolicy
class Scope < Scope class Scope < Scope
def resolve def resolve
return scope.where(permission: %w[public commons]) unless user return authenticated_scope if user
unauthenticated_scope
end
private
def authenticated_scope
scope.where(permission: %w[public commons]) scope.where(permission: %w[public commons])
.or(scope.where(defer_to_map_id: user.all_accessible_maps.map(&:id))) .or(scope.where(defer_to_map_id: user.all_accessible_maps.map(&:id)))
.or(scope.where(user_id: user.id)) .or(scope.where(user_id: user.id))
end end
def unauthenticated_scope
scope.where(permission: %w[public commons])
end
end end
def index? def index?

View file

@ -3,12 +3,21 @@
class TopicPolicy < ApplicationPolicy class TopicPolicy < ApplicationPolicy
class Scope < Scope class Scope < Scope
def resolve def resolve
return scope.where(permission: %w[public commons]) unless user return authenticated_scope if user
unauthenticated_scope
end
private
def authenticated_scope
scope.where(permission: %w[public commons]) scope.where(permission: %w[public commons])
.or(scope.where(defer_to_map_id: user.all_accessible_maps.map(&:id))) .or(scope.where(defer_to_map_id: user.all_accessible_maps.map(&:id)))
.or(scope.where(user_id: user.id)) .or(scope.where(user_id: user.id))
end end
def unauthenticated_scope
scope.where(permission: %w[public commons])
end
end end
def index? def index?

View file

@ -17,7 +17,6 @@ module Api
object.image.url(:sixtyfour) object.image.url(:sixtyfour)
end end
# rubocop:disable Style/PredicateName
def is_admin def is_admin
object.admin object.admin
end end

View file

@ -18,21 +18,21 @@ class FollowService
def unfollow(entity, user) def unfollow(entity, user)
follow = Follow.where(followed: entity, user: user).first follow = Follow.where(followed: entity, user: user).first
if follow return unless follow
unless follow.update(muted: true) unless follow.update(muted: true)
raise follow.errors.full_messages.join("\n") raise follow.errors.full_messages.join("\n")
end end
end end
end
def remove_reason(entity, user, reason) def remove_reason(entity, user, reason)
return unless FollowReason::REASONS.include?(reason) return unless FollowReason::REASONS.include?(reason)
follow = Follow.where(followed: entity, user: user).first follow = Follow.where(followed: entity, user: user).first
if follow return unless follow
follow.follow_reason.update_attribute(reason, false) follow.follow_reason.update_attribute(reason, false)
follow.destroy unless follow.follow_reason.has_reason follow.destroy unless follow.follow_reason.has_reason
end end
end
protected protected
@ -40,17 +40,11 @@ class FollowService
follow = Follow.where(followed: entity, user: user).first follow = Follow.where(followed: entity, user: user).first
return false if follow && follow.muted return false if follow && follow.muted
if entity.class == Topic if entity.class == Topic
if reason == 'created' return user.settings.follow_topic_on_created == '1' if reason == 'created'
return user.settings.follow_topic_on_created == '1' return user.settings.follow_topic_on_contributed == '1' if reason == 'contributed'
elsif reason == 'contributed'
return user.settings.follow_topic_on_contributed == '1'
end
elsif entity.class == Map elsif entity.class == Map
if reason == 'created' return user.settings.follow_map_on_created == '1' if reason == 'created'
return user.settings.follow_map_on_created == '1' return user.settings.follow_map_on_contributed == '1' if reason == 'contributed'
elsif reason == 'contributed'
return user.settings.follow_map_on_contributed == '1'
end
end end
end end
end end

View file

@ -5,7 +5,7 @@ class MapActivityService
'Activity on map ' + map.name 'Activity on map ' + map.name
end end
def self.summarize_data(map, user, until_moment = DateTime.now) def self.summarize_data(map, user, until_moment = DateTime.current)
results = { results = {
stats: {} stats: {}
} }
@ -18,7 +18,7 @@ class MapActivityService
message_count = Message.where(resource: map) message_count = Message.where(resource: map)
.where('created_at > ? AND created_at < ?', since, until_moment) .where('created_at > ? AND created_at < ?', since, until_moment)
.where.not(user: user).count .where.not(user: user).count
results[:stats][:messages_sent] = message_count if message_count > 0 results[:stats][:messages_sent] = message_count if message_count.positive?
moved_count = Event.where(kind: 'topic_moved_on_map', map: map) moved_count = Event.where(kind: 'topic_moved_on_map', map: map)
.where('created_at > ? AND created_at < ?', since, until_moment) .where('created_at > ? AND created_at < ?', since, until_moment)
@ -42,7 +42,9 @@ class MapActivityService
topics_added_events.each do |ta| topics_added_events.each do |ta|
num_adds = topics_added_events.where(eventable_id: ta.eventable_id).count num_adds = topics_added_events.where(eventable_id: ta.eventable_id).count
num_removes = topics_removed_events.where(eventable_id: ta.eventable_id).count num_removes = topics_removed_events.where(eventable_id: ta.eventable_id).count
topics_added_to_include[ta.eventable_id] = ta if num_adds > num_removes && scoped_topic_ids.include?(ta.eventable.id) if num_adds > num_removes && scoped_topic_ids.include?(ta.eventable.id)
topics_added_to_include[ta.eventable_id] = ta
end
end end
unless topics_added_to_include.keys.empty? unless topics_added_to_include.keys.empty?
results[:stats][:topics_added] = topics_added_to_include.keys.length results[:stats][:topics_added] = topics_added_to_include.keys.length
@ -53,7 +55,9 @@ class MapActivityService
topics_removed_events.each do |ta| topics_removed_events.each do |ta|
num_adds = topics_added_events.where(eventable_id: ta.eventable_id).count num_adds = topics_added_events.where(eventable_id: ta.eventable_id).count
num_removes = topics_removed_events.where(eventable_id: ta.eventable_id).count num_removes = topics_removed_events.where(eventable_id: ta.eventable_id).count
topics_removed_to_include[ta.eventable_id] = ta if num_removes > num_adds && TopicPolicy.new(user, ta.eventable).show? if num_removes > num_adds && TopicPolicy.new(user, ta.eventable).show?
topics_removed_to_include[ta.eventable_id] = ta
end
end end
unless topics_removed_to_include.keys.empty? unless topics_removed_to_include.keys.empty?
results[:stats][:topics_removed] = topics_removed_to_include.keys.length results[:stats][:topics_removed] = topics_removed_to_include.keys.length
@ -74,7 +78,9 @@ class MapActivityService
synapses_added_events.each do |ta| synapses_added_events.each do |ta|
num_adds = synapses_added_events.where(eventable_id: ta.eventable_id).count num_adds = synapses_added_events.where(eventable_id: ta.eventable_id).count
num_removes = synapses_removed_events.where(eventable_id: ta.eventable_id).count num_removes = synapses_removed_events.where(eventable_id: ta.eventable_id).count
synapses_added_to_include[ta.eventable_id] = ta if num_adds > num_removes && scoped_synapse_ids.include?(ta.eventable.id) if num_adds > num_removes && scoped_synapse_ids.include?(ta.eventable.id)
synapses_added_to_include[ta.eventable_id] = ta
end
end end
unless synapses_added_to_include.keys.empty? unless synapses_added_to_include.keys.empty?
results[:stats][:synapses_added] = synapses_added_to_include.keys.length results[:stats][:synapses_added] = synapses_added_to_include.keys.length
@ -85,7 +91,9 @@ class MapActivityService
synapses_removed_events.each do |ta| synapses_removed_events.each do |ta|
num_adds = synapses_added_events.where(eventable_id: ta.eventable_id).count num_adds = synapses_added_events.where(eventable_id: ta.eventable_id).count
num_removes = synapses_removed_events.where(eventable_id: ta.eventable_id).count num_removes = synapses_removed_events.where(eventable_id: ta.eventable_id).count
synapses_removed_to_include[ta.eventable_id] = ta if num_removes > num_adds && SynapsePolicy.new(user, ta.eventable).show? if num_removes > num_adds && SynapsePolicy.new(user, ta.eventable).show?
synapses_removed_to_include[ta.eventable_id] = ta
end
end end
unless synapses_removed_to_include.keys.empty? unless synapses_removed_to_include.keys.empty?
results[:stats][:synapses_removed] = synapses_removed_to_include.keys.length results[:stats][:synapses_removed] = synapses_removed_to_include.keys.length

View file

@ -7,7 +7,7 @@ class NotificationService
include ActionView::Helpers::SanitizeHelper include ActionView::Helpers::SanitizeHelper
def self.renderer def self.renderer
renderer ||= ApplicationController.renderer.new( @renderer ||= ApplicationController.renderer.new(
http_host: ENV['MAILER_DEFAULT_URL'], http_host: ENV['MAILER_DEFAULT_URL'],
https: Rails.env.production? ? true : false https: Rails.env.production? ? true : false
) )
@ -70,19 +70,25 @@ class NotificationService
def self.access_request(request) def self.access_request(request)
subject = access_request_subject(request.map) subject = access_request_subject(request.map)
body = renderer.render(template: 'map_mailer/access_request', locals: { map: request.map, request: request }, layout: false) body = renderer.render(template: 'map_mailer/access_request',
locals: { map: request.map, request: request },
layout: false)
request.map.user.notify(subject, body, request, false, MAP_ACCESS_REQUEST, true, request.user) request.map.user.notify(subject, body, request, false, MAP_ACCESS_REQUEST, true, request.user)
end end
def self.access_approved(request) def self.access_approved(request)
subject = access_approved_subject(request.map) subject = access_approved_subject(request.map)
body = renderer.render(template: 'map_mailer/access_approved', locals: { map: request.map }, layout: false) body = renderer.render(template: 'map_mailer/access_approved',
locals: { map: request.map },
layout: false)
request.user.notify(subject, body, request, false, MAP_ACCESS_APPROVED, true, request.map.user) request.user.notify(subject, body, request, false, MAP_ACCESS_APPROVED, true, request.map.user)
end end
def self.invite_to_edit(user_map) def self.invite_to_edit(user_map)
subject = invite_to_edit_subject(user_map.map) subject = invite_to_edit_subject(user_map.map)
body = renderer.render(template: 'map_mailer/invite_to_edit', locals: { map: user_map.map, inviter: user_map.map.user }, layout: false) body = renderer.render(template: 'map_mailer/invite_to_edit',
locals: { map: user_map.map, inviter: user_map.map.user },
layout: false)
user_map.user.notify(subject, body, user_map, false, MAP_INVITE_TO_EDIT, true, user_map.map.user) user_map.user.notify(subject, body, user_map, false, MAP_INVITE_TO_EDIT, true, user_map.map.user)
end end

View file

@ -35,7 +35,7 @@
<% $i = 0 %> <% $i = 0 %>
<% @m = Metacode.order("name").all %> <% @m = Metacode.order("name").all %>
<% while $i < (Metacode.all.length / 4) do %> <% while $i < (Metacode.all.length / 4) do %>
<li id="<%= @m[$i].id %>" <% if not @m[$i].inMetacodeSet(@metacode_set) %>class="toggledOff"<% end %> <li id="<%= @m[$i].id %>" <% if not @m[$i].in_metacode_set(@metacode_set) %>class="toggledOff"<% end %>
onclick="Metamaps.Admin.liClickHandler.call(this);"> onclick="Metamaps.Admin.liClickHandler.call(this);">
<img src="<%= asset_path @m[$i].icon %>" alt="<%= @m[$i].name %>" /> <img src="<%= asset_path @m[$i].icon %>" alt="<%= @m[$i].name %>" />
<p><%= @m[$i].name.downcase %></p> <p><%= @m[$i].name.downcase %></p>
@ -46,7 +46,7 @@
</ul> </ul>
<ul id="filters-two"> <ul id="filters-two">
<% while $i < (Metacode.all.length / 4 * 2) do %> <% while $i < (Metacode.all.length / 4 * 2) do %>
<li id="<%= @m[$i].id %>" <% if not @m[$i].inMetacodeSet(@metacode_set) %>class="toggledOff"<% end %> <li id="<%= @m[$i].id %>" <% if not @m[$i].in_metacode_set(@metacode_set) %>class="toggledOff"<% end %>
onclick="Metamaps.Admin.liClickHandler.call(this);"> onclick="Metamaps.Admin.liClickHandler.call(this);">
<img src="<%= asset_path @m[$i].icon %>" alt="<%= @m[$i].name %>" /> <img src="<%= asset_path @m[$i].icon %>" alt="<%= @m[$i].name %>" />
<p><%= @m[$i].name.downcase %></p> <p><%= @m[$i].name.downcase %></p>
@ -57,7 +57,7 @@
</ul> </ul>
<ul id="filters-three"> <ul id="filters-three">
<% while $i < (Metacode.all.length / 4 * 3) do %> <% while $i < (Metacode.all.length / 4 * 3) do %>
<li id="<%= @m[$i].id %>" <% if not @m[$i].inMetacodeSet(@metacode_set) %>class="toggledOff"<% end %> <li id="<%= @m[$i].id %>" <% if not @m[$i].in_metacode_set(@metacode_set) %>class="toggledOff"<% end %>
onclick="Metamaps.Admin.liClickHandler.call(this);"> onclick="Metamaps.Admin.liClickHandler.call(this);">
<img src="<%= asset_path @m[$i].icon %>" alt="<%= @m[$i].name %>" /> <img src="<%= asset_path @m[$i].icon %>" alt="<%= @m[$i].name %>" />
<p><%= @m[$i].name.downcase %></p> <p><%= @m[$i].name.downcase %></p>
@ -68,7 +68,7 @@
</ul> </ul>
<ul id="filters-four"> <ul id="filters-four">
<% while $i < Metacode.all.length do %> <% while $i < Metacode.all.length do %>
<li id="<%= @m[$i].id %>" <% if not @m[$i].inMetacodeSet(@metacode_set) %>class="toggledOff"<% end %> <li id="<%= @m[$i].id %>" <% if not @m[$i].in_metacode_set(@metacode_set) %>class="toggledOff"<% end %>
onclick="Metamaps.Admin.liClickHandler.call(this);"> onclick="Metamaps.Admin.liClickHandler.call(this);">
<img src="<%= asset_path @m[$i].icon %>" alt="<%= @m[$i].name %>" /> <img src="<%= asset_path @m[$i].icon %>" alt="<%= @m[$i].name %>" />
<p><%= @m[$i].name.downcase %></p> <p><%= @m[$i].name.downcase %></p>

View file

@ -6,7 +6,7 @@
<script> <script>
<% Metacode.all.each do |m| %> <% Metacode.all.each do |m| %>
<% if m.inMetacodeSet(@metacode_set) %> <% if m.in_metacode_set(@metacode_set) %>
Metamaps.Admin.selectMetacodes.push("<%= m.id %>"); Metamaps.Admin.selectMetacodes.push("<%= m.id %>");
<% end %> <% end %>
Metamaps.Admin.allMetacodes.push("<%= m.id %>"); Metamaps.Admin.allMetacodes.push("<%= m.id %>");

View file

@ -6,7 +6,7 @@
<script> <script>
<% Metacode.all.each do |m| %> <% Metacode.all.each do |m| %>
<% if m.inMetacodeSet(@metacode_set) %> <% if m.in_metacode_set(@metacode_set) %>
Metamaps.Admin.selectMetacodes.push("<%= m.id %>"); Metamaps.Admin.selectMetacodes.push("<%= m.id %>");
<% end %> <% end %>
Metamaps.Admin.allMetacodes.push("<%= m.id %>"); Metamaps.Admin.allMetacodes.push("<%= m.id %>");

View file

@ -25,7 +25,7 @@ Rails.application.configure do
# Print deprecation notices to the Rails logger # Print deprecation notices to the Rails logger
config.active_support.deprecation = :log config.active_support.deprecation = :log
config.action_mailer.preview_path = "#{Rails.root}/spec/mailers/previews" config.action_mailer.preview_path = Rails.root.join('spec', 'mailers', 'previews')
# Expands the lines which load the assets # Expands the lines which load the assets
config.assets.debug = false config.assets.debug = false

View file

@ -4,7 +4,9 @@
Rails.application.configure do Rails.application.configure do
# Settings specified here will take precedence over those in config/application.rb # Settings specified here will take precedence over those in config/application.rb
config.action_cable.allowed_request_origins = ['https://metamaps.herokuapp.com', 'http://metamaps.herokuapp.com', 'https://metamaps.cc'] config.action_cable.allowed_request_origins = [
'https://metamaps.herokuapp.com', 'http://metamaps.herokuapp.com', 'https://metamaps.cc'
]
# log to stdout # log to stdout
logger = Logger.new(STDOUT) logger = Logger.new(STDOUT)

View file

@ -11,4 +11,4 @@ Delayed::Worker.class_eval do
prepend ExceptionNotifierInDelayedJob prepend ExceptionNotifierInDelayedJob
end end
Delayed::Worker.logger = Logger.new(File.join(Rails.root, 'log', 'delayed_job.log')) Delayed::Worker.logger = Logger.new(Rails.root.join('log', 'delayed_job.log'))

View file

@ -1,5 +1,8 @@
# frozen_string_literal: true # frozen_string_literal: true
def is_tester(user) def is_tester(user)
user && %w[connorturland@gmail.com devin@callysto.com chessscholar@gmail.com solaureum@gmail.com ishanshapiro@gmail.com].include?(user.email) user && %w[
connorturland@gmail.com devin@callysto.com chessscholar@gmail.com solaureum@gmail.com
ishanshapiro@gmail.com
].include?(user.email)
end end

View file

@ -15,7 +15,7 @@ RSpec.describe ExploreController, type: :controller do
expect(JSON.parse(response.body)).to eq [] expect(JSON.parse(response.body)).to eq []
end end
it 'with 1 record' do it 'with 1 record' do
map = create(:map) create(:map)
get :active, format: :json get :active, format: :json
expect(JSON.parse(response.body).class).to be Array expect(JSON.parse(response.body).class).to be Array
end end

View file

@ -40,7 +40,10 @@ RSpec.describe SynapsesController, type: :controller do
context 'with private topics' do context 'with private topics' do
it 'redirects to /' do it 'redirects to /' do
post :create, format: :json, params: { post :create, format: :json, params: {
synapse: valid_attributes.merge(topic1_id: create(:topic, permission: 'private'), topic2_id: create(:topic, permission: 'private')) synapse: valid_attributes.merge(
topic1_id: create(:topic, permission: 'private'),
topic2_id: create(:topic, permission: 'private')
)
} }
expect(response.status).to eq 302 expect(response.status).to eq 302
expect(response).to redirect_to('/') expect(response).to redirect_to('/')

View file

@ -69,17 +69,20 @@ class MapActivityMailerPreview < ActionMailer::Preview
end end
def generate_user def generate_user
User.create(name: Faker::Name.name, email: Faker::Internet.email, password: 'password', password_confirmation: 'password', joinedwithcode: 'qwertyui') User.create(name: Faker::Name.name, email: Faker::Internet.email,
password: 'password', password_confirmation: 'password',
joinedwithcode: 'qwertyui')
end end
def generate_map def generate_map
Map.create(name: Faker::HarryPotter.book, permission: 'commons', arranged: false, user: generate_user) Map.create(name: Faker::HarryPotter.book, permission: 'commons',
arranged: false, user: generate_user)
end end
def topic_added_to_map(map) def topic_added_to_map(map)
user = generate_user user = generate_user
topic = Topic.create(name: Faker::Friends.quote, permission: 'commons', user: user) topic = Topic.create(name: Faker::Friends.quote, permission: 'commons', user: user)
mapping = Mapping.create(map: map, mappable: topic, user: user) Mapping.create(map: map, mappable: topic, user: user)
end end
def topic_moved_on_map(mapping) def topic_moved_on_map(mapping)
@ -95,8 +98,9 @@ class MapActivityMailerPreview < ActionMailer::Preview
def synapse_added_to_map(map, topic1, topic2) def synapse_added_to_map(map, topic1, topic2)
user = generate_user user = generate_user
topic = Synapse.create(desc: 'describes', permission: 'commons', user: user, topic1: topic1, topic2: topic2) topic = Synapse.create(desc: 'describes', permission: 'commons',
mapping = Mapping.create(map: map, mappable: topic, user: user) user: user, topic1: topic1, topic2: topic2)
Mapping.create(map: map, mappable: topic, user: user)
end end
def synapse_removed_from_map(mapping) def synapse_removed_from_map(mapping)

View file

@ -11,13 +11,13 @@ if Rails.env.production?
end end
# require all support files # require all support files
Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f } Dir[Rails.root.join('spec', 'support', '**', '*.rb')].each { |f| require f }
ActiveRecord::Migration.maintain_test_schema! ActiveRecord::Migration.maintain_test_schema!
RSpec.configure do |config| RSpec.configure do |config|
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures" config.fixture_path = ::Rails.root.join('spec', 'fixtures')
config.use_transactional_fixtures = true config.use_transactional_fixtures = true

View file

@ -16,7 +16,8 @@ RSpec.describe MapActivityService do
describe 'topics added to map' do describe 'topics added to map' do
it 'includes a topic added within the last 24 hours' do it 'includes a topic added within the last 24 hours' do
topic = create(:topic) topic = create(:topic)
mapping = create(:mapping, user: other_user, map: map, mappable: topic, created_at: 6.hours.ago) create(:mapping, user: other_user, map: map, mappable: topic, created_at: 6.hours.ago)
event = Event.find_by(kind: 'topic_added_to_map', eventable_id: topic.id) event = Event.find_by(kind: 'topic_added_to_map', eventable_id: topic.id)
event.update_columns(created_at: 6.hours.ago) event.update_columns(created_at: 6.hours.ago)
response = MapActivityService.summarize_data(map, email_user) response = MapActivityService.summarize_data(map, email_user)
@ -47,26 +48,33 @@ RSpec.describe MapActivityService do
mapping.destroy mapping.destroy
Event.find_by(kind: 'topic_removed_from_map', eventable_id: topic.id).update_columns(created_at: 6.hours.ago) Event.find_by(kind: 'topic_removed_from_map', eventable_id: topic.id).update_columns(created_at: 6.hours.ago)
mapping2 = create(:mapping, user: other_user, map: map, mappable: topic, created_at: 5.hours.ago) mapping2 = create(:mapping, user: other_user, map: map, mappable: topic, created_at: 5.hours.ago)
Event.where(kind: 'topic_added_to_map').where("meta->>'mapping_id' = ?", mapping2.id.to_s).first.update_columns(created_at: 5.hours.ago) Event.where(kind: 'topic_added_to_map').where("meta->>'mapping_id' = ?", mapping2.id.to_s)
.first
.update_columns(created_at: 5.hours.ago)
response = MapActivityService.summarize_data(map, email_user) response = MapActivityService.summarize_data(map, email_user)
expect(response).to eq empty_response expect(response).to eq empty_response
end end
it 'excludes a topic added outside the last 24 hours' do it 'excludes a topic added outside the last 24 hours' do
topic = create(:topic) topic = create(:topic)
mapping = create(:mapping, user: other_user, map: map, mappable: topic, created_at: 25.hours.ago) create(:mapping, user: other_user, map: map, mappable: topic, created_at: 25.hours.ago)
Event.find_by(kind: 'topic_added_to_map', eventable_id: topic.id).update_columns(created_at: 25.hours.ago) Event.find_by(kind: 'topic_added_to_map', eventable_id: topic.id).update_columns(created_at: 25.hours.ago)
response = MapActivityService.summarize_data(map, email_user) response = MapActivityService.summarize_data(map, email_user)
expect(response).to eq empty_response expect(response).to eq empty_response
end end
it 'excludes topics added by the user who will receive the data' do it 'excludes topics added by the user who will receive the data' do
topic = create(:topic) topic = create(:topic)
topic2 = create(:topic) create(:mapping, user: other_user, map: map, mappable: topic, created_at: 5.hours.ago)
mapping = create(:mapping, user: other_user, map: map, mappable: topic, created_at: 5.hours.ago)
event = Event.find_by(kind: 'topic_added_to_map', eventable_id: topic.id) event = Event.find_by(kind: 'topic_added_to_map', eventable_id: topic.id)
event.update_columns(created_at: 5.hours.ago) event.update_columns(created_at: 5.hours.ago)
mapping2 = create(:mapping, user: email_user, map: map, mappable: topic2, created_at: 5.hours.ago)
topic2 = create(:topic)
create(:mapping, user: email_user, map: map, mappable: topic2, created_at: 5.hours.ago)
Event.find_by(kind: 'topic_added_to_map', eventable_id: topic2.id).update_columns(created_at: 5.hours.ago) Event.find_by(kind: 'topic_added_to_map', eventable_id: topic2.id).update_columns(created_at: 5.hours.ago)
response = MapActivityService.summarize_data(map, email_user) response = MapActivityService.summarize_data(map, email_user)
expect(response[:stats][:topics_added]).to eq(1) expect(response[:stats][:topics_added]).to eq(1)
@ -166,7 +174,7 @@ RSpec.describe MapActivityService do
describe 'synapses added to map' do describe 'synapses added to map' do
it 'includes a synapse added within the last 24 hours' do it 'includes a synapse added within the last 24 hours' do
synapse = create(:synapse) synapse = create(:synapse)
mapping = create(:mapping, user: other_user, map: map, mappable: synapse, created_at: 6.hours.ago) create(:mapping, user: other_user, map: map, mappable: synapse, created_at: 6.hours.ago)
event = Event.find_by(kind: 'synapse_added_to_map', eventable_id: synapse.id) event = Event.find_by(kind: 'synapse_added_to_map', eventable_id: synapse.id)
event.update_columns(created_at: 6.hours.ago) event.update_columns(created_at: 6.hours.ago)
response = MapActivityService.summarize_data(map, email_user) response = MapActivityService.summarize_data(map, email_user)
@ -197,14 +205,16 @@ RSpec.describe MapActivityService do
mapping.destroy mapping.destroy
Event.find_by(kind: 'synapse_removed_from_map', eventable_id: synapse.id).update_columns(created_at: 6.hours.ago) Event.find_by(kind: 'synapse_removed_from_map', eventable_id: synapse.id).update_columns(created_at: 6.hours.ago)
mapping2 = create(:mapping, user: other_user, map: map, mappable: synapse, created_at: 5.hours.ago) mapping2 = create(:mapping, user: other_user, map: map, mappable: synapse, created_at: 5.hours.ago)
Event.where(kind: 'synapse_added_to_map').where("meta->>'mapping_id' = ?", mapping2.id.to_s).first.update_columns(created_at: 5.hours.ago) Event.where(kind: 'synapse_added_to_map').where("meta->>'mapping_id' = ?", mapping2.id.to_s)
.first
.update_columns(created_at: 5.hours.ago)
response = MapActivityService.summarize_data(map, email_user) response = MapActivityService.summarize_data(map, email_user)
expect(response).to eq empty_response expect(response).to eq empty_response
end end
it 'excludes a synapse added outside the last 24 hours' do it 'excludes a synapse added outside the last 24 hours' do
synapse = create(:synapse) synapse = create(:synapse)
mapping = create(:mapping, user: other_user, map: map, mappable: synapse, created_at: 25.hours.ago) create(:mapping, user: other_user, map: map, mappable: synapse, created_at: 25.hours.ago)
Event.find_by(kind: 'synapse_added_to_map', eventable_id: synapse.id).update_columns(created_at: 25.hours.ago) Event.find_by(kind: 'synapse_added_to_map', eventable_id: synapse.id).update_columns(created_at: 25.hours.ago)
response = MapActivityService.summarize_data(map, email_user) response = MapActivityService.summarize_data(map, email_user)
expect(response).to eq empty_response expect(response).to eq empty_response
@ -212,11 +222,14 @@ RSpec.describe MapActivityService do
it 'excludes synapses added by the user who will receive the data' do it 'excludes synapses added by the user who will receive the data' do
synapse = create(:synapse) synapse = create(:synapse)
synapse2 = create(:synapse) create(:mapping, user: other_user, map: map, mappable: synapse, created_at: 5.hours.ago)
mapping = create(:mapping, user: other_user, map: map, mappable: synapse, created_at: 5.hours.ago)
event = Event.find_by(kind: 'synapse_added_to_map', eventable_id: synapse.id) event = Event.find_by(kind: 'synapse_added_to_map', eventable_id: synapse.id)
event.update_columns(created_at: 5.hours.ago) event.update_columns(created_at: 5.hours.ago)
mapping2 = create(:mapping, user: email_user, map: map, mappable: synapse2, created_at: 5.hours.ago)
synapse2 = create(:synapse)
create(:mapping, user: email_user, map: map, mappable: synapse2, created_at: 5.hours.ago)
Event.find_by(kind: 'synapse_added_to_map', eventable_id: synapse2.id).update_columns(created_at: 5.hours.ago) Event.find_by(kind: 'synapse_added_to_map', eventable_id: synapse2.id).update_columns(created_at: 5.hours.ago)
response = MapActivityService.summarize_data(map, email_user) response = MapActivityService.summarize_data(map, email_user)
expect(response[:stats][:synapses_added]).to eq(1) expect(response[:stats][:synapses_added]).to eq(1)
@ -262,7 +275,9 @@ RSpec.describe MapActivityService do
mapping2.destroy mapping2.destroy
event = Event.find_by(kind: 'synapse_removed_from_map', eventable_id: synapse.id) event = Event.find_by(kind: 'synapse_removed_from_map', eventable_id: synapse.id)
event.update_columns(created_at: 5.hours.ago) event.update_columns(created_at: 5.hours.ago)
Event.find_by(kind: 'synapse_removed_from_map', eventable_id: synapse2.id).update_columns(created_at: 5.hours.ago) Event
.find_by(kind: 'synapse_removed_from_map', eventable_id: synapse2.id)
.update_columns(created_at: 5.hours.ago)
response = MapActivityService.summarize_data(map, email_user) response = MapActivityService.summarize_data(map, email_user)
expect(response[:stats][:synapses_removed]).to eq(1) expect(response[:stats][:synapses_removed]).to eq(1)
expect(response[:synapses_removed]).to eq([event]) expect(response[:synapses_removed]).to eq([event])
@ -297,7 +312,8 @@ RSpec.describe MapActivityService do
old_topic = create(:topic, permission: 'commons', user: other_user) old_topic = create(:topic, permission: 'commons', user: other_user)
old_topic_mapping = create(:mapping, map: map, mappable: old_topic, user: other_user) old_topic_mapping = create(:mapping, map: map, mappable: old_topic, user: other_user)
old_private_topic = create(:topic, permission: 'private', user: other_user) old_private_topic = create(:topic, permission: 'private', user: other_user)
old_private_topic_mapping = create(:mapping, map: map, mappable: old_private_topic, user: other_user) old_private_topic_mapping = create(:mapping, map: map, mappable: old_private_topic,
user: other_user)
end end
Timecop.return Timecop.return
@ -347,7 +363,8 @@ RSpec.describe MapActivityService do
old_synapse = create(:synapse, permission: 'commons', user: other_user) old_synapse = create(:synapse, permission: 'commons', user: other_user)
old_synapse_mapping = create(:mapping, map: map, mappable: old_synapse, user: other_user) old_synapse_mapping = create(:mapping, map: map, mappable: old_synapse, user: other_user)
old_private_synapse = create(:synapse, permission: 'private', user: other_user) old_private_synapse = create(:synapse, permission: 'private', user: other_user)
old_private_synapse_mapping = create(:mapping, map: map, mappable: old_private_synapse, user: other_user) old_private_synapse_mapping = create(:mapping, map: map, mappable: old_private_synapse,
user: other_user)
end end
Timecop.return Timecop.return

View file

@ -2,11 +2,10 @@
RSpec::Matchers.define :match_json_schema do |schema_name| RSpec::Matchers.define :match_json_schema do |schema_name|
match do |response| match do |response|
schema_directory = Rails.root.join('doc', 'api', 'schemas').to_s schema_path = Rails.root.join('doc', 'api', 'schemas', "#{schema_name}.json").to_s
schema = "#{schema_directory}/#{schema_name}.json"
# schema customizations # schema customizations
schema = JSON.parse(File.read(schema)) schema = JSON.parse(File.read(schema_path))
schema = update_file_refs(schema) schema = update_file_refs(schema)
data = JSON.parse(response.body) data = JSON.parse(response.body)
@ -15,7 +14,7 @@ RSpec::Matchers.define :match_json_schema do |schema_name|
end end
def get_json_example(resource) def get_json_example(resource)
filepath = "#{Rails.root}/doc/api/examples/#{resource}.json" filepath = Rails.root.join('doc', 'api', 'examples', "#{resource}.json")
OpenStruct.new(body: File.read(filepath)) OpenStruct.new(body: File.read(filepath))
end end
@ -25,7 +24,7 @@ def update_file_refs(schema)
schema[key] = if value.is_a? Hash schema[key] = if value.is_a? Hash
update_file_refs(value) update_file_refs(value)
elsif key == '$ref' elsif key == '$ref'
"#{Rails.root}/doc/api/schemas/#{value}" Rails.root.join('doc', 'api', 'schemas', value).to_s
else else
value value
end end