Merge pull request #656 from metamaps/feature/tech-debt

rubocop style updates
This commit is contained in:
Devin Howard 2016-09-24 13:59:27 +08:00 committed by GitHub
commit 03eacde753
162 changed files with 488 additions and 303 deletions

View file

@ -6,9 +6,16 @@ AllCops:
- 'bin/**/*'
- 'vendor/**/*'
- 'app/assets/javascripts/node_modules/**/*'
- 'Vagrantfile'
Rails:
Enabled: true
Metrics/LineLength:
Max: 100
Metrics/AbcSize:
Max: 16
Style/Documentation:
Enabled: false

View file

@ -1,3 +1,4 @@
# frozen_string_literal: true
source 'https://rubygems.org'
ruby '2.3.0'

View file

@ -1,4 +1,5 @@
#!/usr/bin/env rake
# frozen_string_literal: true
# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.

2
Vagrantfile vendored
View file

@ -31,7 +31,7 @@ sudo -u postgres psql -c "ALTER USER postgres WITH PASSWORD '3112';"
SCRIPT
VAGRANTFILE_API_VERSION = '2'.freeze
VAGRANTFILE_API_VERSION = '2'
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.box = 'trusty64'

View file

@ -1,9 +1,12 @@
# frozen_string_literal: true
module Api
module V1
class DeprecatedController < ApplicationController
# rubocop:disable Style/MethodMissing
def method_missing
render json: { error: "/api/v1 is deprecated! Please use /api/v2 instead." }
render json: { error: '/api/v1 is deprecated! Please use /api/v2 instead.' }
end
# rubocop:enable Style/MethodMissing
end
end
end

View file

@ -1,3 +1,4 @@
# frozen_string_literal: true
module Api
module V1
class MappingsController < DeprecatedController

View file

@ -1,3 +1,4 @@
# frozen_string_literal: true
module Api
module V1
class MapsController < DeprecatedController

View file

@ -1,3 +1,4 @@
# frozen_string_literal: true
module Api
module V1
class SynapsesController < DeprecatedController

View file

@ -1,3 +1,4 @@
# frozen_string_literal: true
module Api
module V1
class TokensController < DeprecatedController

View file

@ -1,3 +1,4 @@
# frozen_string_literal: true
module Api
module V1
class TopicsController < DeprecatedController

View file

@ -1,3 +1,4 @@
# frozen_string_literal: true
module Api
module V2
class MappingsController < RestfulController

View file

@ -1,3 +1,4 @@
# frozen_string_literal: true
module Api
module V2
class MapsController < RestfulController

View file

@ -1,3 +1,4 @@
# frozen_string_literal: true
module Api
module V2
class RestfulController < ActionController::Base
@ -51,7 +52,8 @@ module Api
"Api::V2::#{resource_name.camelize}Serializer".constantize
end
def respond_with_resource(scope: default_scope, serializer: resource_serializer, root: serializer_root)
def respond_with_resource(scope: default_scope, serializer: resource_serializer,
root: serializer_root)
if resource.errors.empty?
render json: resource, scope: scope, serializer: serializer, root: root
else
@ -59,8 +61,11 @@ module Api
end
end
def respond_with_collection(resources: collection, scope: default_scope, serializer: resource_serializer, root: serializer_root)
render json: resources, scope: scope, each_serializer: serializer, root: root, meta: pagination(resources), meta_key: :page
def respond_with_collection(resources: collection, scope: default_scope,
serializer: resource_serializer, root: serializer_root)
pagination_link_headers!(pagination(resources))
render json: resources, scope: scope, each_serializer: serializer, root: root,
meta: pagination(resources), meta_key: :page
end
def default_scope
@ -94,33 +99,36 @@ module Api
end
def pagination(collection)
per = (params[:per] || 25).to_i
current_page = (params[:page] || 1).to_i
total_pages = (collection.total_count.to_f / per).ceil
prev_page = current_page > 1 ? current_page - 1 : 0
next_page = current_page < total_pages ? current_page + 1 : 0
return @pagination_data unless @pagination_data.nil?
current_page = (params[:page] || 1).to_i
per = (params[:per] || 25).to_i
total_pages = (collection.total_count.to_f / per).ceil
@pagination_data = {
current_page: current_page,
next_page: current_page < total_pages ? current_page + 1 : 0,
prev_page: current_page > 1 ? current_page - 1 : 0,
total_pages: total_pages,
total_count: collection.total_count,
per: per
}
end
def pagination_link_headers!(data)
base_url = request.base_url + request.path
nxt = request.query_parameters.merge(page: next_page).map{|x| x.join('=')}.join('&')
prev = request.query_parameters.merge(page: prev_page).map{|x| x.join('=')}.join('&')
last = request.query_parameters.merge(page: total_pages).map{|x| x.join('=')}.join('&')
old_query = request.query_parameters
nxt = old_query.merge(page: data[:next_page]).map { |x| x.join('=') }.join('&')
prev = old_query.merge(page: data[:prev_page]).map { |x| x.join('=') }.join('&')
last = old_query.merge(page: data[:total_pages]).map { |x| x.join('=') }.join('&')
response.headers['Link'] = [
%(<#{base_url}?#{nxt}>; rel="next"),
%(<#{base_url}?#{prev}>; rel="prev"),
%(<#{base_url}?#{last}>; rel="last")
].join(',')
response.headers['X-Total-Pages'] = collection.total_pages.to_s
response.headers['X-Total-Count'] = collection.total_count.to_s
response.headers['X-Per-Page'] = per.to_s
{
current_page: current_page,
next_page: next_page,
prev_page: prev_page,
total_pages: total_pages,
total_count: collection.total_count,
per: per
}
response.headers['X-Total-Pages'] = data[:total_pages].to_s
response.headers['X-Total-Count'] = data[:total_count].to_s
response.headers['X-Per-Page'] = data[:per].to_s
end
def instantiate_collection
@ -163,7 +171,7 @@ module Api
builder = builder.order(sort => direction)
end
end
return builder
builder
end
def visible_records

View file

@ -1,9 +1,10 @@
# frozen_string_literal: true
module Api
module V2
class SessionsController < ApplicationController
def create
@user = User.find_by(email: params[:email])
if @user && @user.valid_password(params[:password])
if @user&.valid_password(params[:password])
sign_in(@user)
render json: @user
else

View file

@ -1,3 +1,4 @@
# frozen_string_literal: true
module Api
module V2
class SynapsesController < RestfulController

View file

@ -1,3 +1,4 @@
# frozen_string_literal: true
module Api
module V2
class TokensController < RestfulController

View file

@ -1,3 +1,4 @@
# frozen_string_literal: true
module Api
module V2
class TopicsController < RestfulController

View file

@ -1,3 +1,4 @@
# frozen_string_literal: true
class ApplicationController < ActionController::Base
include ApplicationHelper
include Pundit
@ -5,7 +6,7 @@ class ApplicationController < ActionController::Base
rescue_from Pundit::NotAuthorizedError, with: :handle_unauthorized
protect_from_forgery(with: :exception)
before_action :get_invite_link
before_action :invite_link
after_action :allow_embedding
def default_serializer_options
@ -41,29 +42,26 @@ class ApplicationController < ActionController::Base
private
def get_invite_link
def invite_link
@invite_link = "#{request.base_url}/join" + (current_user ? "?code=#{current_user.code}" : '')
end
def require_no_user
if authenticated?
redirect_to edit_user_path(user), notice: 'You must be logged out.'
return false
end
return true unless authenticated?
redirect_to edit_user_path(user), notice: 'You must be logged out.'
return false
end
def require_user
unless authenticated?
redirect_to new_user_session_path, notice: 'You must be logged in.'
return false
end
return true if authenticated?
redirect_to new_user_session_path, notice: 'You must be logged in.'
return false
end
def require_admin
unless authenticated? && admin?
redirect_to root_url, notice: 'You need to be an admin for that.'
return false
end
return true if authenticated? && admin?
redirect_to root_url, notice: 'You need to be an admin for that.'
false
end
def user

View file

@ -1,3 +1,4 @@
# frozen_string_literal: true
class MainController < ApplicationController
include TopicsHelper
include MapsHelper
@ -12,13 +13,13 @@ class MainController < ApplicationController
def home
@maps = policy_scope(Map).order('updated_at DESC').page(1).per(20)
respond_to do |format|
format.html {
if !authenticated?
render 'main/home'
else
render 'maps/activemaps'
end
}
format.html do
if !authenticated?
render 'main/home'
else
render 'maps/activemaps'
end
end
end
end
@ -163,8 +164,8 @@ class MainController < ApplicationController
@synapses = []
end
#limit to 5 results
@synapses = @synapses.to_a.slice(0,5)
# limit to 5 results
@synapses = @synapses.to_a.slice(0, 5)
render json: autocomplete_synapse_array_json(@synapses)
end

View file

@ -1,3 +1,4 @@
# frozen_string_literal: true
class MappingsController < ApplicationController
before_action :require_user, only: [:create, :update, :destroy]
after_action :verify_authorized, except: :index

View file

@ -1,3 +1,4 @@
# frozen_string_literal: true
class MapsController < ApplicationController
before_action :require_user, only: [:create, :update, :access, :star, :unstar, :screenshot, :events, :destroy]
after_action :verify_authorized, except: [:activemaps, :featuredmaps, :mymaps, :sharedmaps, :starredmaps, :usermaps]
@ -107,14 +108,14 @@ class MapsController < ApplicationController
# GET maps/new
def new
@map = Map.new(name: "Untitled Map", permission: "public", arranged: true)
@map = Map.new(name: 'Untitled Map', permission: 'public', arranged: true)
authorize @map
respond_to do |format|
format.html do
@map.user = current_user
@map.save
redirect_to(map_path(@map) + '?new')
redirect_to(map_path(@map) + '?new')
end
end
end
@ -305,9 +306,7 @@ class MapsController < ApplicationController
@map = Map.find(params[:id])
authorize @map
star = Star.find_by_map_id_and_user_id(@map.id, current_user.id)
if not star
star = Star.create(map_id: @map.id, user_id: current_user.id)
end
star = Star.create(map_id: @map.id, user_id: current_user.id) unless star
respond_to do |format|
format.json do
@ -321,9 +320,7 @@ class MapsController < ApplicationController
@map = Map.find(params[:id])
authorize @map
star = Star.find_by_map_id_and_user_id(@map.id, current_user.id)
if star
star.delete
end
star&.delete
respond_to do |format|
format.json do

View file

@ -1,3 +1,4 @@
# frozen_string_literal: true
class MessagesController < ApplicationController
before_action :require_user, except: [:show]
after_action :verify_authorized

View file

@ -1,3 +1,4 @@
# frozen_string_literal: true
class MetacodeSetsController < ApplicationController
before_action :require_admin

View file

@ -1,5 +1,7 @@
# frozen_string_literal: true
class MetacodesController < ApplicationController
before_action :require_admin, except: [:index, :show]
before_action :set_metacode, only: [:edit, :update]
# GET /metacodes
# GET /metacodes.json
@ -8,10 +10,7 @@ class MetacodesController < ApplicationController
respond_to do |format|
format.html do
unless authenticated? && user.admin
redirect_to root_url, notice: 'You need to be an admin for that.'
return false
end
return unless require_admin
render :index
end
format.json { render json: @metacodes }
@ -23,7 +22,7 @@ class MetacodesController < ApplicationController
# GET /metacodes/action.json
def show
@metacode = Metacode.where('DOWNCASE(name) = ?', downcase(params[:name])).first if params[:name]
@metacode = Metacode.find(params[:id]) unless @metacode
set_metacode unless @metacode
respond_to do |format|
format.json { render json: @metacode }
@ -36,14 +35,13 @@ class MetacodesController < ApplicationController
@metacode = Metacode.new
respond_to do |format|
format.html # new.html.erb
format.html
format.json { render json: @metacode }
end
end
# GET /metacodes/1/edit
def edit
@metacode = Metacode.find(params[:id])
end
# POST /metacodes
@ -65,8 +63,6 @@ class MetacodesController < ApplicationController
# PUT /metacodes/1
# PUT /metacodes/1.json
def update
@metacode = Metacode.find(params[:id])
respond_to do |format|
if @metacode.update(metacode_params)
format.html { redirect_to metacodes_url, notice: 'Metacode was successfully updated.' }
@ -84,4 +80,8 @@ class MetacodesController < ApplicationController
def metacode_params
params.require(:metacode).permit(:id, :name, :aws_icon, :manual_icon, :color)
end
def set_metacode
@metacode = Metacode.find(params[:id])
end
end

View file

@ -1,3 +1,4 @@
# frozen_string_literal: true
class SynapsesController < ApplicationController
include TopicsHelper

View file

@ -1,3 +1,4 @@
# frozen_string_literal: true
class TopicsController < ApplicationController
include TopicsHelper

View file

@ -1,3 +1,4 @@
# frozen_string_literal: true
class Users::PasswordsController < Devise::PasswordsController
protected
@ -5,7 +6,7 @@ class Users::PasswordsController < Devise::PasswordsController
signed_in_root_path(resource)
end
def after_sending_reset_password_instructions_path_for(resource_name)
def after_sending_reset_password_instructions_path_for(_resource_name)
new_user_session_path if is_navigational_format?
end
end

View file

@ -1,3 +1,4 @@
# frozen_string_literal: true
class Users::RegistrationsController < Devise::RegistrationsController
before_action :configure_sign_up_params, only: [:create]
before_action :configure_account_update_params, only: [:update]

View file

@ -1,3 +1,4 @@
# frozen_string_literal: true
class UsersController < ApplicationController
before_action :require_user, only: [:edit, :update, :updatemetacodes]

View file

@ -1,13 +1,14 @@
# frozen_string_literal: true
module ApplicationHelper
def get_metacodeset
@m = current_user.settings.metacodes
set = @m[0].include?('metacodeset') ? MetacodeSet.find(@m[0].sub('metacodeset-', '').to_i) : false
set
def metacodeset
metacodes = current_user.settings.metacodes
return false unless metacodes[0].include?('metacodeset')
MetacodeSet.find(metacodes[0].sub('metacodeset-', '').to_i)
end
def user_metacodes
@m = current_user.settings.metacodes
set = get_metacodeset
set = metacodeset
@metacodes = if set
set.metacodes.to_a
else
@ -16,7 +17,7 @@ module ApplicationHelper
@metacodes.sort! { |m1, m2| m2.name.downcase <=> m1.name.downcase }.rotate!(-1)
end
def determine_invite_link
def invite_link
"#{request.base_url}/join" + (current_user ? "?code=#{current_user.code}" : '')
end
end

View file

@ -1,3 +1,4 @@
# frozen_string_literal: true
module ContentHelper
def resource_name
:user

View file

@ -1,3 +1,4 @@
# frozen_string_literal: true
module DeviseHelper
def devise_error_messages!
resource.errors.to_a[0]

View file

@ -1,2 +1,3 @@
# frozen_string_literal: true
module InMetacodeSetsHelper
end

View file

@ -1,2 +1,3 @@
# frozen_string_literal: true
module MainHelper
end

View file

@ -1,2 +1,3 @@
# frozen_string_literal: true
module MappingHelper
end

View file

@ -1,34 +1,42 @@
# frozen_string_literal: true
module MapsHelper
## this one is for building our custom JSON autocomplete format for typeahead
# JSON autocomplete format for typeahead
def autocomplete_map_array_json(maps)
temp = []
maps.each do |m|
map = {}
map['id'] = m.id
map['label'] = m.name
map['value'] = m.name
map['description'] = m.desc.try(:truncate, 30)
map['permission'] = m.permission
map['topicCount'] = m.topics.count
map['synapseCount'] = m.synapses.count
map['contributorCount'] = m.contributors.count
map['rtype'] = 'map'
contributorTip = ''
firstContributorImage = 'https://s3.amazonaws.com/metamaps-assets/site/user.png'
if m.contributors.count > 0
firstContributorImage = m.contributors[0].image.url(:thirtytwo)
m.contributors.each_with_index do |c, _index|
userImage = c.image.url(:thirtytwo)
name = c.name
contributorTip += '<li> <img class="tipUserImage" width="25" height="25" src=' + userImage + ' />' + '<span>' + name + '</span> </li>'
end
end
map['contributorTip'] = contributorTip
map['mapContributorImage'] = firstContributorImage
temp.push map
maps.map do |m|
{
id: m.id,
label: m.name,
value: m.name,
description: m.desc.try(:truncate, 30),
permission: m.permission,
topicCount: m.topics.count,
synapseCount: m.synapses.count,
contributorCount: m.contributors.count,
rtype: 'map',
contributorTip: contributor_tip(map),
mapContributorImage: first_contributor_image(map)
}
end
temp
end
def first_contributor_image(map)
if map.contributors.count.positive?
return map.contributors[0].image.url(:thirtytwo)
end
'https://s3.amazonaws.com/metamaps-assets/site/user.png'
end
def contributor_tip(map)
output = ''
if map.contributors.count.positive?
map.contributors.each_with_index do |contributor, _index|
user_image = contributor.image.url(:thirtytwo)
output += '<li>'
output += %(<img class="tipUserImage" width="25" height="25" src="#{user_image}" />)
output += "<span>#{contributor.name}</span>"
output += '</li>'
end
end
output
end
end

View file

@ -1,2 +1,3 @@
# frozen_string_literal: true
module MetacodeSetsHelper
end

View file

@ -1,2 +1,3 @@
# frozen_string_literal: true
module MetacodesHelper
end

View file

@ -1,33 +1,25 @@
# frozen_string_literal: true
module SynapsesHelper
## this one is for building our custom JSON autocomplete format for typeahead
def autocomplete_synapse_generic_json(unique)
temp = []
unique.each do |s|
synapse = {}
synapse['label'] = s.desc
synapse['value'] = s.desc
temp.push synapse
unique.map do |s|
{ label: s.desc, value: s.desc }
end
temp
end
## this one is for building our custom JSON autocomplete format for typeahead
def autocomplete_synapse_array_json(synapses)
temp = []
synapses.each do |s|
synapse = {}
synapse['id'] = s.id
synapse['label'] = s.desc.nil? || s.desc == '' ? '(no description)' : s.desc
synapse['value'] = s.desc
synapse['permission'] = s.permission
synapse['mapCount'] = s.maps.count
synapse['originator'] = s.user.name
synapse['originatorImage'] = s.user.image.url(:thirtytwo)
synapse['rtype'] = 'synapse'
temp.push synapse
synapses.map do |s|
{
id: s.id,
label: s.desc.blank? ? '(no description)' : s.desc,
value: s.desc,
permission: s.permission,
mapCount: s.maps.count,
originator: s.user.name,
originatorImage: s.user.image.url(:thirtytwo),
rtype: 'synapse'
}
end
temp
end
end

View file

@ -1,56 +1,39 @@
# frozen_string_literal: true
module TopicsHelper
## this one is for building our custom JSON autocomplete format for typeahead
def autocomplete_array_json(topics)
temp = []
topics.each do |t|
topic = {}
topic['id'] = t.id
topic['label'] = t.name
topic['value'] = t.name
topic['description'] = t.desc ? t.desc.truncate(70) : '' # make this return matched results
topic['type'] = t.metacode.name
topic['typeImageURL'] = t.metacode.icon
topic['permission'] = t.permission
topic['mapCount'] = t.maps.count
topic['synapseCount'] = t.synapses.count
topic['originator'] = t.user.name
topic['originatorImage'] = t.user.image.url(:thirtytwo)
topic['rtype'] = 'topic'
topic['inmaps'] = t.inmaps
topic['inmapsLinks'] = t.inmapsLinks
temp.push topic
topics.map do |t|
{
id: t.id,
label: t.name,
value: t.name,
description: t.desc ? t.desc&.truncate(70) : '', # make this return matched results
type: t.metacode.name,
typeImageURL: t.metacode.icon,
permission: t.permission,
mapCount: t.maps.count,
synapseCount: t.synapses.count,
originator: t.user.name,
originatorImage: t.user.image.url(:thirtytwo),
rtype: :topic,
inmaps: t.inmaps,
inmapsLinks: t.inmapsLinks
}
end
temp
end
# find all nodes in any given nodes network
# recursively find all nodes in any given nodes network
def network(node, array, count)
# recurse starting with a node to find all connected nodes and return an array of topics that constitutes the starting nodes network
# if the array of nodes is empty initialize it
array = [] if array.nil?
# add the node to the array
array.push(node)
return array if count == 0
count -= 1
return array if count.zero?
# check if each relative is already in the array and if not, call the network function again
if !node.relatives.empty?
if (node.relatives - array).empty?
return array
else
(node.relatives - array).each do |relative|
array = (array | network(relative, array, count))
end
return array
end
elsif node.relatives.empty?
return array
remaining_relatives = node.relatives.to_a - array
remaining_relatives.each do |relative|
array = (array | network(relative, array, count - 1))
end
array
end
end

View file

@ -1,3 +1,4 @@
# frozen_string_literal: true
module UsersHelper
# build custom json autocomplete for typeahead
def autocomplete_user_array_json(users)

View file

@ -1,3 +1,4 @@
# frozen_string_literal: true
class ApplicationMailer < ActionMailer::Base
default from: 'team@metamaps.cc'
layout 'mailer'

View file

@ -1,3 +1,4 @@
# frozen_string_literal: true
class MapMailer < ApplicationMailer
default from: 'team@metamaps.cc'

View file

@ -1,3 +1,4 @@
# frozen_string_literal: true
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
end

View file

@ -1,3 +1,4 @@
# frozen_string_literal: true
module Routing
extend ActiveSupport::Concern
include Rails.application.routes.url_helpers

View file

@ -1,3 +1,4 @@
# frozen_string_literal: true
class Event < ApplicationRecord
KINDS = %w(user_present_on_map conversation_started_on_map topic_added_to_map synapse_added_to_map).freeze

View file

@ -1,3 +1,4 @@
# frozen_string_literal: true
class Events::ConversationStartedOnMap < Event
# after_create :notify_users!

View file

@ -1,3 +1,4 @@
# frozen_string_literal: true
class Events::NewMapping < Event
# after_create :notify_users!

View file

@ -1,3 +1,4 @@
# frozen_string_literal: true
class Events::UserPresentOnMap < Event
# after_create :notify_users!

View file

@ -1,3 +1,4 @@
# frozen_string_literal: true
class InMetacodeSet < ApplicationRecord
belongs_to :metacode, class_name: 'Metacode', foreign_key: 'metacode_id'
belongs_to :metacode_set, class_name: 'MetacodeSet', foreign_key: 'metacode_set_id'

View file

@ -1,3 +1,4 @@
# frozen_string_literal: true
class Map < ApplicationRecord
belongs_to :user
@ -19,7 +20,7 @@ class Map < ApplicationRecord
thumb: ['188x126#', :png]
#:full => ['940x630#', :png]
},
default_url: 'https://s3.amazonaws.com/metamaps-assets/site/missing-map-white.png'
default_url: 'https://s3.amazonaws.com/metamaps-assets/site/missing-map-white.png'
validates :name, presence: true
validates :arranged, inclusion: { in: [true, false] }

View file

@ -1,3 +1,4 @@
# frozen_string_literal: true
class Mapping < ApplicationRecord
scope :topicmapping, -> { where(mappable_type: :Topic) }
scope :synapsemapping, -> { where(mappable_type: :Synapse) }

View file

@ -1,3 +1,4 @@
# frozen_string_literal: true
class Message < ApplicationRecord
belongs_to :user
belongs_to :resource, polymorphic: true

View file

@ -1,3 +1,4 @@
# frozen_string_literal: true
class Metacode < ApplicationRecord
has_many :in_metacode_sets
has_many :metacode_sets, through: :in_metacode_sets

View file

@ -1,3 +1,4 @@
# frozen_string_literal: true
class MetacodeSet < ApplicationRecord
belongs_to :user
has_many :in_metacode_sets

View file

@ -1,3 +1,4 @@
# frozen_string_literal: true
class PermittedParams < Struct.new(:params)
%w(map synapse topic mapping token).each do |kind|
define_method(kind) do

View file

@ -1,3 +1,4 @@
# frozen_string_literal: true
class Star < ActiveRecord::Base
belongs_to :user
belongs_to :map

View file

@ -1,3 +1,4 @@
# frozen_string_literal: true
class Synapse < ApplicationRecord
belongs_to :user
belongs_to :defer_to_map, class_name: 'Map', foreign_key: 'defer_to_map_id'
@ -21,17 +22,12 @@ class Synapse < ApplicationRecord
where('node1_id = ? OR node2_id = ?', topic_id, topic_id)
}
# :nocov:
delegate :name, to: :user, prefix: true
# :nocov:
# :nocov:
def user_image
user.image.url
end
# :nocov:
# :nocov:
def collaborator_ids
if defer_to_map
defer_to_map.editors.select { |mapper| mapper != user }.map(&:id)
@ -39,21 +35,12 @@ class Synapse < ApplicationRecord
[]
end
end
# :nocov:
# :nocov:
def calculated_permission
if defer_to_map
defer_to_map.permission
else
permission
end
defer_to_map&.permission || permission
end
# :nocov:
# :nocov:
def as_json(_options = {})
super(methods: [:user_name, :user_image, :calculated_permission, :collaborator_ids])
end
# :nocov:
end

View file

@ -1,3 +1,4 @@
# frozen_string_literal: true
class Token < ApplicationRecord
belongs_to :user

View file

@ -1,3 +1,4 @@
# frozen_string_literal: true
class Topic < ApplicationRecord
include TopicsHelper
@ -73,11 +74,7 @@ class Topic < ApplicationRecord
end
def calculated_permission
if defer_to_map
defer_to_map.permission
else
permission
end
defer_to_map&.permission || permission
end
def as_json(_options = {})

View file

@ -1,3 +1,4 @@
# frozen_string_literal: true
require 'open-uri'
class User < ApplicationRecord
@ -41,7 +42,7 @@ class User < ApplicationRecord
default_url: 'https://s3.amazonaws.com/metamaps-assets/site/user.png'
# Validate the attached image is image/jpg, image/png, etc
validates_attachment_content_type :image, content_type: %r(\Aimage/.*\Z)
validates_attachment_content_type :image, content_type: %r{\Aimage/.*\Z}
# override default as_json
def as_json(_options = {})
@ -79,8 +80,8 @@ class User < ApplicationRecord
end
end
def starred_map?(map)
return self.stars.where(map_id: map.id).exists?
def starred_map?(map)
stars.where(map_id: map.id).exists?
end
def settings

View file

@ -1,3 +1,4 @@
# frozen_string_literal: true
class UserMap < ApplicationRecord
belongs_to :map
belongs_to :user

View file

@ -1,3 +1,4 @@
# frozen_string_literal: true
class UserPreference
attr_accessor :metacodes
@ -9,7 +10,7 @@ class UserPreference
array.push(metacode.id.to_s) if metacode
rescue ActiveRecord::StatementInvalid
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

View file

@ -1,3 +1,4 @@
# frozen_string_literal: true
class Webhook < ApplicationRecord
belongs_to :hookable, polymorphic: true

View file

@ -1,3 +1,4 @@
# frozen_string_literal: true
Webhooks::Slack::Base = Struct.new(:event) do
include Routing

View file

@ -1,3 +1,4 @@
# frozen_string_literal: true
class Webhooks::Slack::ConversationStartedOnMap < Webhooks::Slack::Base
def text
"There is a live conversation starting on map *#{event.map.name}*. #{view_map_on_metamaps('Join in!')}"

View file

@ -1,3 +1,4 @@
# frozen_string_literal: true
class Webhooks::Slack::SynapseAddedToMap < Webhooks::Slack::Base
def text
"\"*#{eventable.mappable.topic1.name}* #{eventable.mappable.desc || '->'} *#{eventable.mappable.topic2.name}*\" was added as a connection to the map *#{view_map_on_metamaps}*"

View file

@ -1,3 +1,4 @@
# frozen_string_literal: true
class Webhooks::Slack::TopicAddedToMap < Webhooks::Slack::Base
def text
"New #{eventable.mappable.metacode.name} topic *#{eventable.mappable.name}* was added to the map *#{view_map_on_metamaps}*"

View file

@ -1,3 +1,4 @@
# frozen_string_literal: true
class Webhooks::Slack::UserPresentOnMap < Webhooks::Slack::Base
def text
"Mapper *#{event.user.name}* has joined the map *#{event.map.name}*. #{view_map_on_metamaps('Map with them')}"

View file

@ -1,3 +1,4 @@
# frozen_string_literal: true
class ApplicationPolicy
attr_reader :user, :record
@ -39,7 +40,7 @@ class ApplicationPolicy
# explicitly say they want to (E.g. seeing/editing/deleting private
# maps - they should be able to, but not by accident)
def admin_override
user && user.admin
user&.admin
end
def scope

View file

@ -1,3 +1,4 @@
# frozen_string_literal: true
class MainPolicy < ApplicationPolicy
def initialize(user, _record)
@user = user

View file

@ -1,14 +1,13 @@
# frozen_string_literal: true
class MapPolicy < ApplicationPolicy
class Scope < Scope
def resolve
visible = %w(public commons)
permission = 'maps.permission IN (?)'
if user
shared_maps = user.shared_maps.map(&:id)
scope.where(permission + ' OR maps.id IN (?) OR maps.user_id = ?', visible, shared_maps, user.id)
else
scope.where(permission, visible)
end
return scope.where(permission: visible) unless user
scope.where(permission: visible)
.or(scope.where(id: user.shared_maps.map(&:id)))
.or(scope.where(user_id: user.id))
end
end
@ -17,7 +16,9 @@ class MapPolicy < ApplicationPolicy
end
def show?
record.permission == 'commons' || record.permission == 'public' || record.collaborators.include?(user) || record.user == user
record.permission.in?(['commons', 'public']) ||
record.collaborators.include?(user) ||
record.user == user
end
def create?
@ -25,7 +26,10 @@ class MapPolicy < ApplicationPolicy
end
def update?
user.present? && (record.permission == 'commons' || record.collaborators.include?(user) || record.user == user)
return false unless user.present?
record.permission == 'commons' ||
record.collaborators.include?(user) ||
record.user == user
end
def destroy?
@ -33,7 +37,7 @@ class MapPolicy < ApplicationPolicy
end
def access?
# note that this is to edit access
# note that this is to edit who can access the map
user.present? && record.user == user
end

View file

@ -1,3 +1,4 @@
# frozen_string_literal: true
class MappingPolicy < ApplicationPolicy
class Scope < Scope
def resolve

View file

@ -1,3 +1,4 @@
# frozen_string_literal: true
class MessagePolicy < ApplicationPolicy
class Scope < Scope
def resolve

View file

@ -1,13 +1,14 @@
# frozen_string_literal: true
class SynapsePolicy < ApplicationPolicy
class Scope < Scope
def resolve
visible = %w(public commons)
permission = 'synapses.permission IN (?)'
if user
scope.where(permission + ' OR synapses.defer_to_map_id IN (?) OR synapses.user_id = ?', visible, user.shared_maps.map(&:id), user.id)
else
scope.where(permission, visible)
end
return scope.where(permission: visible) unless user
scope.where(permission: visible)
.or(scope.where(defer_to_map_id: user.shared_maps.map(&:id)))
.or(scope.where(user_id: user.id))
end
end

View file

@ -1,3 +1,4 @@
# frozen_string_literal: true
class TokenPolicy < ApplicationPolicy
class Scope < Scope
def resolve

View file

@ -1,13 +1,13 @@
# frozen_string_literal: true
class TopicPolicy < ApplicationPolicy
class Scope < Scope
def resolve
visible = %w(public commons)
permission = 'topics.permission IN (?)'
if user
scope.where(permission + ' OR topics.defer_to_map_id IN (?) OR topics.user_id = ?', visible, user.shared_maps.map(&:id), user.id)
else
scope.where(permission, visible)
end
return scope.where(permission: visible) unless user
scope.where(permission: visible)
.or(scope.where(defer_to_map_id: user.shared_maps.map(&:id)))
.or(scope.where(user_id: user.id))
end
end
@ -23,14 +23,13 @@ class TopicPolicy < ApplicationPolicy
if record.defer_to_map.present?
map_policy.show?
else
record.permission == 'commons' || record.permission == 'public' || record.user == user
record.permission.in?(['commons', 'public']) || record.user == user
end
end
def update?
if !user.present?
false
elsif record.defer_to_map.present?
return false unless user.present?
if record.defer_to_map.present?
map_policy.update?
else
record.permission == 'commons' || record.user == user

View file

@ -1,3 +1,4 @@
# frozen_string_literal: true
module Api
module V2
class ApplicationSerializer < ActiveModel::Serializer
@ -6,21 +7,40 @@ module Api
end
def embeds
# subclasses can override self.embeddable, and then it will whitelist
# scope[:embeds] based on the contents. That way scope[:embeds] can just pull
# from params and the whitelisting happens here
@embeds ||= (scope[:embeds] || []).select { |e| self.class.embeddable.keys.include?(e) }
end
# self.embeddable might look like this:
# topic1: { attr: :node1, serializer: TopicSerializer }
# topic2: { attr: :node2, serializer: TopicSerializer }
# contributors: { serializer: UserSerializer}
# This method will remove the :attr key if the underlying attribute name
# is different than the name provided in the final json output. All other keys
# in the hash will be passed to the ActiveModel::Serializer `attribute` method
# directly (e.g. serializer in the examples will be passed).
#
# This setup means if you passed this self.embeddable config and sent no
# ?embed= query param with your API request, you would get the regular attributes
# plus topic1_id, topic2_id, and contributor_ids. If you pass
# ?embed=topic1,topic2,contributors, then instead of two ids and an array of ids,
# you would get two serialized topics and an array of serialized users
def self.embed_dat
embeddable.each_pair do |key, opts|
attr = opts.delete(:attr) || key
if attr.to_s.pluralize == attr.to_s
attribute "#{attr.to_s.singularize}_ids".to_sym, opts.merge(unless: -> { embeds.include?(key) }) do
attribute("#{attr.to_s.singularize}_ids".to_sym,
opts.merge(unless: -> { embeds.include?(key) })) do
object.send(attr).map(&:id)
end
has_many attr, opts.merge(if: -> { embeds.include?(key) })
has_many(attr, opts.merge(if: -> { embeds.include?(key) }))
else
id_opts = opts.merge(key: "#{key}_id")
attribute "#{attr}_id".to_sym, id_opts.merge(unless: -> { embeds.include?(key) })
attribute key, opts.merge(if: -> { embeds.include?(key) })
attribute("#{attr}_id".to_sym,
id_opts.merge(unless: -> { embeds.include?(key) }))
attribute(key, opts.merge(if: -> { embeds.include?(key) }))
end
end
end

View file

@ -1,3 +1,4 @@
# frozen_string_literal: true
module Api
module V2
class EventSerializer < ApplicationSerializer

View file

@ -1,13 +1,14 @@
# frozen_string_literal: true
module Api
module V2
class MapSerializer < ApplicationSerializer
attributes :id,
:name,
:desc,
:permission,
:screenshot,
:created_at,
:updated_at
:name,
:desc,
:permission,
:screenshot,
:created_at,
:updated_at
def self.embeddable
{
@ -20,7 +21,7 @@ module Api
}
end
self.class_eval do
class_eval do
embed_dat
end
end

View file

@ -1,11 +1,12 @@
# frozen_string_literal: true
module Api
module V2
class MappingSerializer < ApplicationSerializer
attributes :id,
:created_at,
:updated_at,
:mappable_id,
:mappable_type
:created_at,
:updated_at,
:mappable_id,
:mappable_type
attribute :xloc, if: -> { object.mappable_type == 'Topic' }
attribute :yloc, if: -> { object.mappable_type == 'Topic' }
@ -17,7 +18,7 @@ module Api
}
end
self.class_eval do
class_eval do
embed_dat
end
end

View file

@ -1,11 +1,12 @@
# frozen_string_literal: true
module Api
module V2
class MetacodeSerializer < ApplicationSerializer
attributes :id,
:name,
:manual_icon,
:color,
:aws_icon
:name,
:manual_icon,
:color,
:aws_icon
end
end
end

View file

@ -1,12 +1,13 @@
# frozen_string_literal: true
module Api
module V2
class SynapseSerializer < ApplicationSerializer
attributes :id,
:desc,
:category,
:permission,
:created_at,
:updated_at
:desc,
:category,
:permission,
:created_at,
:updated_at
def self.embeddable
{
@ -16,7 +17,7 @@ module Api
}
end
self.class_eval do
class_eval do
embed_dat
end
end

View file

@ -1,10 +1,11 @@
# frozen_string_literal: true
module Api
module V2
class TokenSerializer < ApplicationSerializer
attributes :id,
:token,
:description,
:created_at
:token,
:description,
:created_at
end
end
end

View file

@ -1,13 +1,14 @@
# frozen_string_literal: true
module Api
module V2
class TopicSerializer < ApplicationSerializer
attributes :id,
:name,
:desc,
:link,
:permission,
:created_at,
:updated_at
:name,
:desc,
:link,
:permission,
:created_at,
:updated_at
def self.embeddable
{
@ -16,7 +17,7 @@ module Api
}
end
self.class_eval do
class_eval do
embed_dat
end
end

View file

@ -1,19 +1,22 @@
# frozen_string_literal: true
module Api
module V2
class UserSerializer < ApplicationSerializer
attributes :id,
:name,
:avatar,
:is_admin,
:generation
:name,
:avatar,
:is_admin,
:generation
def avatar
object.image.url(:sixtyfour)
end
# rubocop:disable Style/PredicateName
def is_admin
object.admin
end
# rubocop:enable Style/PredicateName
end
end
end

View file

@ -1,3 +1,4 @@
# frozen_string_literal: true
module Api
module V2
class WebhookSerializer < ApplicationSerializer

View file

@ -1,4 +1,11 @@
class MapExportService < Struct.new(:user, :map)
# frozen_string_literal: true
class MapExportService
attr_reader :user, :map
def initialize(user, map)
@user = user
@map = map
end
def json
# marshal_dump turns OpenStruct into a Hash
{

View file

@ -1,3 +1,4 @@
# frozen_string_literal: true
class Perm
# e.g. Perm::ISSIONS
ISSIONS = [:commons, :public, :private].freeze

View file

@ -1,3 +1,4 @@
# frozen_string_literal: true
class WebhookService
def self.publish!(webhook:, event:)
return false unless webhook.event_types.include? event.kind

View file

@ -94,7 +94,7 @@
<p>As a valued beta tester, you have the ability to invite your peers, colleagues and collaborators onto the platform.</p>
<p>Below is a personal invite link containing your unique access code, which can be used multiple times.</p>
<div id="joinCodesBox">
<p class="joinCodes"><%= determine_invite_link %>
<p class="joinCodes"><%= invite_link() %>
<button class="button" onclick="Metamaps.GlobalUI.shareInvite('<%= @invite_link %>');">COPY INVITE LINK!</button>
</div>

View file

@ -1,29 +1,34 @@
<% @metacodes = user_metacodes() %>
<%= form_for Topic.new, url: topics_url, remote: true do |form| %>
<div class="openMetacodeSwitcher openLightbox" data-open="switchMetacodes">
<div class="tooltipsAbove">Switch Metacodes</div>
</div>
<div class="pinCarousel">
<div class="tooltipsAbove helpPin">Pin Open</div>
<div class="tooltipsAbove helpUnpin">Unpin</div>
</div>
<div id="metacodeImg">
<% @metacodes = user_metacodes() %>
<% set = get_metacodeset() %>
<% @metacodes.each do |metacode| %>
<img class="cloudcarousel" width="40" height="40" src="<%= asset_path metacode.icon %>" alt="<%= metacode.name %>" title="<%= metacode.name %>" data-id="<%= metacode.id %>" />
<% end %>
</div>
<%= form.text_field :name, :maxlength => 140, :placeholder => "title..." %>
<div id="metacodeImgTitle"></div>
<div class="clearfloat"></div>
<script>
<% @metacodes.each do |metacode| %>
<% if !set %>
Metamaps.Create.selectedMetacodes.push("<%= metacode.id %>");
Metamaps.Create.newSelectedMetacodes.push("<%= metacode.id %>");
Metamaps.Create.selectedMetacodeNames.push("<%= metacode.name %>");
Metamaps.Create.newSelectedMetacodeNames.push("<%= metacode.name %>");
<% end %>
<% end %>
</script>
<script>
<% @metacodes.each do |metacode| %>
<% if !metacodeset() %>
Metamaps.Create.selectedMetacodes.push("<%= metacode.id %>");
Metamaps.Create.newSelectedMetacodes.push("<%= metacode.id %>");
Metamaps.Create.selectedMetacodeNames.push("<%= metacode.name %>");
Metamaps.Create.newSelectedMetacodeNames.push("<%= metacode.name %>");
<% end %>
<% end %>
</script>
<% end %>

View file

@ -1,3 +1,4 @@
# frozen_string_literal: true
# This file is used by Rack-based servers to start the application.
require ::File.expand_path('../config/environment', __FILE__)

View file

@ -1,3 +1,4 @@
# frozen_string_literal: true
require_relative 'boot'
require 'csv'

View file

@ -1,3 +1,4 @@
# frozen_string_literal: true
require 'rubygems'
require 'rails/commands/server'

View file

@ -1,3 +1,4 @@
# frozen_string_literal: true
# Load the Rails application.
require_relative 'application'

View file

@ -1,3 +1,4 @@
# frozen_string_literal: true
Metamaps::Application.configure do
# Settings specified here will take precedence over those in config/application.rb

View file

@ -1,3 +1,4 @@
# frozen_string_literal: true
Rails.application.configure do
# Settings specified here will take precedence over those in config/application.rb

View file

@ -1,3 +1,4 @@
# frozen_string_literal: true
Metamaps::Application.configure do
# Settings specified here will take precedence over those in config/application.rb

View file

@ -1,4 +1,7 @@
# frozen_string_literal: true
$codes = []
if ActiveRecord::Base.connection.data_source_exists? 'users'
$codes = ActiveRecord::Base.connection.execute('SELECT code FROM users').map { |user| user['code'] }
$codes = ActiveRecord::Base.connection
.execute('SELECT code FROM users')
.map { |user| user['code'] }
end

Some files were not shown because too many files have changed in this diff Show more