From 3ae4072b5d58e5dc6516e734b49cedcdee2cb12b Mon Sep 17 00:00:00 2001 From: Connor Turland Date: Sun, 5 Feb 2017 06:28:10 +0000 Subject: [PATCH] add ability to keep metacode focus --- app/controllers/users_controller.rb | 12 +++++++++++- app/helpers/application_helper.rb | 19 ++++++++++++++++++- app/models/mapping.rb | 4 ---- app/models/user_preference.rb | 3 ++- app/policies/user_policy.rb | 4 ++++ config/routes.rb | 1 + 6 files changed, 36 insertions(+), 7 deletions(-) diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 1defb323..7aff655d 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true class UsersController < ApplicationController - before_action :require_user, only: [:edit, :update, :updatemetacodes] + before_action :require_user, only: [:edit, :update, :updatemetacodes, :update_metacode_focus] respond_to :html, :json @@ -92,6 +92,16 @@ class UsersController < ApplicationController end end + # PUT /user/update_metacode_focus + def update_metacode_focus + @user = current_user + @user.settings.metacode_focus = params[:value] + @user.save + respond_to do |format| + format.json { render json: { success: "success" }} + end + end + private def user_params diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index e630e72d..7d0f4f68 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -22,7 +22,24 @@ module ApplicationHelper else Metacode.where(id: @m).to_a end - @metacodes.sort! { |m1, m2| m2.name.downcase <=> m1.name.downcase }.rotate!(-1) + + focus_code = user_metacode() + if focus_code != nil && @metacodes.index{|m| m.id == focus_code.id} == nil + @metacodes.push(focus_code) + end + + @metacodes + .sort! { |m1, m2| m2.name.downcase <=> m1.name.downcase } + + if focus_code != nil + @metacodes.rotate!(@metacodes.index{|m| m.id == focus_code.id}) + else + @metacodes.rotate!(-1) + end + end + + def user_metacode + current_user.settings.metacode_focus ? Metacode.find(current_user.settings.metacode_focus.to_i) : nil end def user_most_used_metacodes diff --git a/app/models/mapping.rb b/app/models/mapping.rb index 348a5e92..2f74e7ee 100644 --- a/app/models/mapping.rb +++ b/app/models/mapping.rb @@ -8,10 +8,6 @@ class Mapping < ApplicationRecord belongs_to :user belongs_to :updated_by, class_name: 'User' - validates :xloc, presence: true, - unless: proc { |m| m.mappable_type == 'Synapse' } - validates :yloc, presence: true, - unless: proc { |m| m.mappable_type == 'Synapse' } validates :map, presence: true validates :mappable, presence: true diff --git a/app/models/user_preference.rb b/app/models/user_preference.rb index cb200d3c..9ea37532 100644 --- a/app/models/user_preference.rb +++ b/app/models/user_preference.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true class UserPreference - attr_accessor :metacodes + attr_accessor :metacodes, :metacode_focus def initialize array = [] @@ -15,5 +15,6 @@ class UserPreference end end @metacodes = array + @metacode_focus = array[0] end end diff --git a/app/policies/user_policy.rb b/app/policies/user_policy.rb index 943200e8..4de64c57 100644 --- a/app/policies/user_policy.rb +++ b/app/policies/user_policy.rb @@ -28,6 +28,10 @@ class UserPolicy < ApplicationPolicy update? end + def update_metacode_focus? + update? + end + # API action def current? user == record diff --git a/config/routes.rb b/config/routes.rb index e365cab5..5abf5668 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -105,6 +105,7 @@ Metamaps::Application.routes.draw do end end post 'user/updatemetacodes', to: 'users#updatemetacodes', as: :updatemetacodes + post 'user/update_metacode_focus', to: 'users#update_metacode_focus' namespace :api, path: '/api', default: { format: :json } do namespace :v2, path: '/v2' do