diff --git a/.codeclimate.yml b/.codeclimate.yml index fbd96af2..719b2807 100644 --- a/.codeclimate.yml +++ b/.codeclimate.yml @@ -8,6 +8,7 @@ engines: enabled: true config: languages: + count_threshold: 3 # rule of three ruby: mass_threshold: 36 # default: 18 javascript: diff --git a/app/models/map.rb b/app/models/map.rb index 86a89a24..36b2d284 100644 --- a/app/models/map.rb +++ b/app/models/map.rb @@ -32,14 +32,19 @@ class Map < ApplicationRecord # 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 + def mappings topicmappings.or(synapsemappings) end - def mk_permission - Perm.short(permission) - end - def contributors User.where(id: mappings.map(&:user_id).uniq) end @@ -48,28 +53,10 @@ class Map < ApplicationRecord User.where(id: user_id).or(User.where(id: collaborators)) end - def topic_count - topics.length - end - - def synapse_count - synapses.length - end - - delegate :name, to: :user, prefix: true - def user_image user.image.url(:thirtytwo) end - def contributor_count - contributors.length - end - - def star_count - stars.length - end - def collaborator_ids collaborators.map(&:id) end @@ -131,4 +118,9 @@ class Map < ApplicationRecord end removed.compact 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 diff --git a/app/models/synapse.rb b/app/models/synapse.rb index 37c9c72d..08512e4f 100644 --- a/app/models/synapse.rb +++ b/app/models/synapse.rb @@ -36,11 +36,7 @@ class Synapse < ApplicationRecord end end - def calculated_permission - defer_to_map&.permission || permission - end - def as_json(_options = {}) - super(methods: [:user_name, :user_image, :calculated_permission, :collaborator_ids]) + super(methods: [:user_name, :user_image, :collaborator_ids]) end end diff --git a/app/models/topic.rb b/app/models/topic.rb index 85f670c3..256fc604 100644 --- a/app/models/topic.rb +++ b/app/models/topic.rb @@ -75,12 +75,8 @@ class Topic < ApplicationRecord Pundit.policy_scope(user, maps).map(&:id) end - def calculated_permission - defer_to_map&.permission || permission - end - def as_json(options = {}) - super(methods: [:user_name, :user_image, :calculated_permission, :collaborator_ids]) + super(methods: [:user_name, :user_image, :collaborator_ids]) .merge(inmaps: inmaps(options[:user]), inmapsLinks: inmapsLinks(options[:user]), map_count: map_count(options[:user]), synapse_count: synapse_count(options[:user])) end @@ -129,15 +125,14 @@ class Topic < ApplicationRecord "Get: #{name}" end - def mk_permission - Perm.short(permission) - end - protected - def create_metamap? - if link == '' and metacode.name == 'Metamap' - @map = Map.create({ name: name, permission: permission, desc: '', arranged: true, user_id: user_id }) - self.link = Rails.application.routes.url_helpers.map_url(:host => ENV['MAILER_DEFAULT_URL'], :id => @map.id) - end - end + + def create_metamap? + return unless (link == '') && (metacode.name == 'Metamap') + + @map = Map.create(name: name, permission: permission, desc: '', + arranged: true, user_id: user_id) + self.link = Rails.application.routes.url_helpers + .map_url(host: ENV['MAILER_DEFAULT_URL'], id: @map.id) + end end diff --git a/app/policies/synapse_policy.rb b/app/policies/synapse_policy.rb index 145f7432..e3190c18 100644 --- a/app/policies/synapse_policy.rb +++ b/app/policies/synapse_policy.rb @@ -2,11 +2,10 @@ class SynapsePolicy < ApplicationPolicy class Scope < Scope def resolve - visible = %w(public commons) - return scope.where(permission: visible) unless user + return scope.where(permission: %w(public commons)) unless user - scope.where(permission: visible) - .or(scope.where.not(defer_to_map_id: nil).where(defer_to_map_id: user.all_accessible_maps.map(&:id))) + scope.where(permission: %w(public commons)) + .or(scope.where(defer_to_map_id: user.all_accessible_maps.map(&:id))) .or(scope.where(user_id: user.id)) end end diff --git a/app/policies/topic_policy.rb b/app/policies/topic_policy.rb index b29d9b44..64463b4a 100644 --- a/app/policies/topic_policy.rb +++ b/app/policies/topic_policy.rb @@ -2,11 +2,10 @@ class TopicPolicy < ApplicationPolicy class Scope < Scope def resolve - visible = %w(public commons) - return scope.where(permission: visible) unless user + return scope.where(permission: %w(public commons)) unless user - scope.where(permission: visible) - .or(scope.where.not(defer_to_map_id: nil).where(defer_to_map_id: user.all_accessible_maps.map(&:id))) + scope.where(permission: %w(public commons)) + .or(scope.where(defer_to_map_id: user.all_accessible_maps.map(&:id))) .or(scope.where(user_id: user.id)) end end diff --git a/frontend/src/Metamaps/DataModel/Synapse.js b/frontend/src/Metamaps/DataModel/Synapse.js index 5f2a6b88..e6a7f1c7 100644 --- a/frontend/src/Metamaps/DataModel/Synapse.js +++ b/frontend/src/Metamaps/DataModel/Synapse.js @@ -38,7 +38,6 @@ const Synapse = Backbone.Model.extend({ newOptions.success = function(model, response, opt) { if (s) s(model, response, opt) - model.set('calculated_permission', model.get('permission')) model.trigger('saved') if (permBefore === 'private' && model.get('permission') !== 'private') { @@ -85,7 +84,7 @@ const Synapse = Backbone.Model.extend({ ` }, authorizeToEdit: function(mapper) { - if (mapper && (this.get('calculated_permission') === 'commons' || this.get('collaborator_ids').includes(mapper.get('id')) || this.get('user_id') === mapper.get('id'))) return true + if (mapper && (this.get('permission') === 'commons' || this.get('collaborator_ids').includes(mapper.get('id')) || this.get('user_id') === mapper.get('id'))) return true else return false }, authorizePermissionChange: function(mapper) { diff --git a/frontend/src/Metamaps/DataModel/Topic.js b/frontend/src/Metamaps/DataModel/Topic.js index dff635f2..0d71c973 100644 --- a/frontend/src/Metamaps/DataModel/Topic.js +++ b/frontend/src/Metamaps/DataModel/Topic.js @@ -37,7 +37,6 @@ const Topic = Backbone.Model.extend({ newOptions.success = function(model, response, opt) { if (s) s(model, response, opt) - model.set('calculated_permission', model.get('permission')) model.trigger('saved') if (permBefore === 'private' && model.get('permission') !== 'private') { @@ -82,7 +81,7 @@ const Topic = Backbone.Model.extend({ authorizeToEdit: function(mapper) { if (mapper && (this.get('user_id') === mapper.get('id') || - this.get('calculated_permission') === 'commons' || + this.get('permission') === 'commons' || this.get('collaborator_ids').includes(mapper.get('id')))) { return true } else { diff --git a/frontend/src/Metamaps/Import.js b/frontend/src/Metamaps/Import.js index 7d1b3aa3..deb71048 100644 --- a/frontend/src/Metamaps/Import.js +++ b/frontend/src/Metamaps/Import.js @@ -295,8 +295,7 @@ const Import = { permission: topicPermision, defer_to_map_id: deferToMapId, desc: desc || '', - link: link || '', - calculated_permission: Active.Map.get('permission') + link: link || '' }) DataModel.Topics.add(topic) diff --git a/frontend/src/Metamaps/SynapseCard.js b/frontend/src/Metamaps/SynapseCard.js index b7b58821..d2feb03a 100644 --- a/frontend/src/Metamaps/SynapseCard.js +++ b/frontend/src/Metamaps/SynapseCard.js @@ -174,7 +174,7 @@ const SynapseCard = { add_perms_form: function(synapse) { // permissions - if owner, also allow permission editing - $('#editSynLowerBar').append('
') + $('#editSynLowerBar').append('
') // ability to change permission var selectingPermission = false diff --git a/frontend/src/Metamaps/TopicCard.js b/frontend/src/Metamaps/TopicCard.js index 71140bdd..3fa9a999 100644 --- a/frontend/src/Metamaps/TopicCard.js +++ b/frontend/src/Metamaps/TopicCard.js @@ -448,8 +448,8 @@ const TopicCard = { nodeValues.inmaps += '
  • ' + inmapsAr[i] + '
  • ' } } - nodeValues.permission = topic.get('calculated_permission') - nodeValues.mk_permission = topic.get('calculated_permission').substring(0, 2) + nodeValues.permission = topic.get('permission') + nodeValues.mk_permission = topic.get('permission').substring(0, 2) nodeValues.map_count = topic.get('map_count').toString() nodeValues.synapse_count = topic.get('synapse_count').toString() nodeValues.id = topic.isNew() ? topic.cid : topic.id