From 4d022d501a50fbcff83307da828bf4f51cf86004 Mon Sep 17 00:00:00 2001 From: viletyy Date: Wed, 24 Nov 2021 14:38:18 +0800 Subject: [PATCH] add: settings personal add common --- Gemfile | 2 +- Gemfile.lock | 7 +-- app/controllers/settings_controller.rb | 51 +++++++++++++++++ app/models/site.rb | 79 ++++++++++++++++++++++++++ app/views/settings/show.json.jbuilder | 9 +++ public/react/build | 2 +- 6 files changed, 144 insertions(+), 6 deletions(-) create mode 100644 app/models/site.rb mode change 160000 => 120000 public/react/build diff --git a/Gemfile b/Gemfile index 83963a31..2f8c34ed 100644 --- a/Gemfile +++ b/Gemfile @@ -1,7 +1,7 @@ source 'https://gems.ruby-china.com' git_source(:github) { |repo| "https://github.com/#{repo}.git" } -ruby '2.3.7' +# ruby '2.3.7' gem 'rails', '~> 5.2.0' gem 'mysql2', '>= 0.4.4', '< 0.6.0' diff --git a/Gemfile.lock b/Gemfile.lock index 9190d340..99e73988 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -182,7 +182,9 @@ GEM mime-types (3.3.1) mime-types-data (~> 3.2015) mime-types-data (3.2019.1009) - mimemagic (0.3.4) + mimemagic (0.3.10) + nokogiri (~> 1) + rake mini_mime (1.0.2) mini_portile2 (2.4.0) minitest (5.14.0) @@ -505,8 +507,5 @@ DEPENDENCIES web-console (>= 3.3.0) wkhtmltopdf-binary -RUBY VERSION - ruby 2.3.7p456 - BUNDLED WITH 2.1.4 diff --git a/app/controllers/settings_controller.rb b/app/controllers/settings_controller.rb index bb031c7b..37fc47a6 100644 --- a/app/controllers/settings_controller.rb +++ b/app/controllers/settings_controller.rb @@ -2,5 +2,56 @@ class SettingsController < ApplicationController def show @old_projects_url = nil @old_projects_url = "https://www.trustie.net/users/#{current_user.try(:login)}/projects" if User.current.logged? + get_add_menu + get_common_menu + get_personal_menu + end + + private + def get_add_menu + @add = [] + Site.add.select(:id, :name, :url, :key).to_a.map(&:serializable_hash).each do |site| + hash = {} + site.each {|k, v| + hash.merge!("#{k}": get_site_url(k, v)) + } + @add << hash + end + end + + def get_common_menu + @common = {} + Site.common.select(:url, :key).each do |site| + next if site["url"].to_s.include?("current_user") && !User.current.logged? + @common.merge!("#{site["key"]}": append_http(reset_site_url(site["url"]))) + end + end + + def get_personal_menu + @personal = [] + if User.current.logged? + Site.personal.select(:id, :name, :url, :key).to_a.map(&:serializable_hash).each do |site| + hash = {} + site.each {|k, v| + hash.merge!("#{k}": get_site_url(k, v)) + } + @personal << hash + end + end + end + + def get_site_url(key, value) + key.to_s === "url" ? append_http(reset_site_url(value)) : reset_site_url(value) + end + + def reset_site_url(url) + return url unless url.to_s.include?("current_user") + + split_arr = url.split('current_user') + split_arr.length > 1 ? split_arr.join(current_user&.login) : (split_arr << current_user&.login).join('') + end + + def append_http(url) + url.to_s.start_with?("http") ? url : [request.protocol, request.host_with_port, url].join('') end end diff --git a/app/models/site.rb b/app/models/site.rb new file mode 100644 index 00000000..af5e7816 --- /dev/null +++ b/app/models/site.rb @@ -0,0 +1,79 @@ +# == Schema Information +# +# Table name: sites +# +# id :integer not null, primary key +# name :string(255) +# url :string(255) +# key :string(255) +# site_type :integer +# created_at :datetime not null +# updated_at :datetime not null +# + +class Site < ApplicationRecord + # add: 添加类链接 + # personal: 个人名下类链接, + # common: 普通链接 + enum site_type: { add: 0, personal: 1, common: 2 } + + scope :by_search, -> (keyword){ where("name LIKE :keyword OR url LIKE :keyword", keyword: "%#{strip_param(keyword)}%") unless strip_param(keyword).blank? } + scope :by_site_type, -> (site_type){ where(site_type: strip_param(site_type)) unless strip_param(site_type).blank? } + + def self.set_default_menu + set_add_menu! + set_personal_menu! + set_common_menu! + end + + def self.has_notice_menu? + self.common.where(key: 'notice').present? + end + + private + def self.set_add_menu! + adds= [ + {name: '新建项目', key: 'add_mirror_project', url: '/projects/mirror/new'}, + {name: '导入项目', key: 'add_common', url: '/projects/deposit/new'}, + {name: '新建组织', key: 'add_r', url: '/organize/new'}] + + adds.each { |ele| + Site.find_or_create_by(key: ele[:key]) do |site| + site.name = ele[:name] + site.url = ele[:url] + site.site_type = Site.site_types[:add] + end + } + end + + def self.set_personal_menu! + personals = [ + {name: '个人中心', key: 'my_page', url: '/users/current_user'}, + {name: '我的组织', key: 'my_organizes', url: '/users/current_user/organizes'} + ] + + personals.each { |ele| + Site.find_or_create_by(key: ele[:key]) do |site| + site.name = ele[:name] + site.url = ele[:url] + site.site_type = Site.site_types[:personal] + end + } + end + + def self.set_common_menu! + commons = [ + {name: '通知', key: 'notice', url: '/users/current_user/user_messages'}, + {name: '找回密码', key: 'lost_password', url: '/account/lost_password'}, + {name: '注册', key: 'register', url: '/login?login=false'} + ] + + commons.each { |ele| + Site.find_or_create_by(key: ele[:key]) do |site| + site.name = ele[:name] + site.url = ele[:url] + site.site_type = Site.site_types[:common] + end + } + end +end diff --git a/app/views/settings/show.json.jbuilder b/app/views/settings/show.json.jbuilder index 8f818b47..58af296d 100644 --- a/app/views/settings/show.json.jbuilder +++ b/app/views/settings/show.json.jbuilder @@ -48,4 +48,13 @@ json.setting do json.old_projects_url @old_projects_url + json.add do + json.array! @add + end + + json.personal do + json.array! @personal + end + + json.common @common end \ No newline at end of file diff --git a/public/react/build b/public/react/build deleted file mode 160000 index 447edde1..00000000 --- a/public/react/build +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 447edde157092e65f05b36bc41c472659c482c6a diff --git a/public/react/build b/public/react/build new file mode 120000 index 00000000..ae03ce74 --- /dev/null +++ b/public/react/build @@ -0,0 +1 @@ +/Users/virus/work/keda/forumbuild \ No newline at end of file