add page themes for admins

This commit is contained in:
呱呱呱 2023-08-23 17:16:03 +08:00
parent 471fbcace4
commit beeb12c336
21 changed files with 320 additions and 17 deletions

View File

@ -0,0 +1,3 @@
// Place all the behaviors and hooks related to the matching controller here.
// All this logic will automatically be available in application.js.

View File

@ -0,0 +1,3 @@
// Place all the styles related to the admins/page_themes controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/

View File

@ -0,0 +1,61 @@
class Admins::PageThemesController < Admins::Topic::BaseController
before_action :finder_page_theme, only: [:edit, :update, :destroy]
def index
params[:sort_by] = params[:sort_by].presence || 'created_at'
params[:sort_direction] = params[:sort_direction].presence || 'desc'
page_themes = Admins::PageThemesQuery.call(params)
@page_themes = paginate page_themes
end
def show
render 'edit'
end
def edit
end
def create
@page_theme = PageTheme.new update_params
if @page_theme.save
save_image_file(params[:image_url], @page_theme)
redirect_to admins_page_themes_path
flash[:success] = "新增主题成功"
else
redirect_to admins_page_themes_path
flash[:danger] = "新增主题失败: #{@page_theme.errors.messages.values.flatten.join(',')}"
end
end
def destroy
if @page_theme.destroy
redirect_to admins_page_themes_path
flash[:success] = "删除主题成功"
else
redirect_to admins_page_themes_path
flash[:danger] = "删除主题失败"
end
end
def new
@page_theme = PageTheme.new
end
def update
@page_theme.update(update_params)
flash[:success] = '保存成功'
render 'edit'
end
private
def finder_page_theme
@page_theme = PageTheme.find(params[:id])
end
def update_params
params.require(:page_theme).permit(:language_frame, :name, :cate, :image_url, :clone_url)
end
end

View File

@ -0,0 +1,2 @@
module Admins::PageThemesHelper
end

View File

@ -1,20 +1,27 @@
# == Schema Information
#
# Table name: ignores
# user_id
# student_name
# school
# profession
# location
# grade
# phone
# mail
# created_on
# is_delete
# prove_attachment_id
# cancel_count
# round
# Table name: glcc_registration_task
#
# id :integer not null, primary key
# reg_id :integer not null
# task_name :string(255)
# task_desc :text(16777215)
# task_difficulty :integer
# task_url :string(1000)
# task_reward :string(255)
# tutor_name :string(255)
# tutor_mail :string(255)
# tutor_phone :string(255)
# created_on :datetime
# is_delete :boolean default("0"), not null
# sort_no :integer default("0")
# locked :boolean default("0")
# round :integer default("1"), not null
# check_status :boolean default("0")
#
# Indexes
#
# idx_glcc_reg_id (reg_id)
#
class GlccRegistrationTask < ActiveRecord::Base

18
app/models/page_theme.rb Normal file
View File

@ -0,0 +1,18 @@
# == Schema Information
#
# Table name: page_themes
#
# id :integer not null, primary key
# name :string(255) not null
# language_frame :integer default("0")
# image_url :string(255)
# clone_url :string(255) not null
# order_index :integer default("0")
# created_at :datetime not null
# updated_at :datetime not null
#
class PageTheme < ApplicationRecord
enum language_frame: { hugo: 0, jeklly: 1, hexo: 2}
validates :name, presence: {message: "主题名不能为空"}, uniqueness: {message: "主题名已存在",scope: :language_frame}
end

View File

@ -120,6 +120,7 @@ class Project < ApplicationRecord
has_many :issues, dependent: :destroy
# has_many :user_grades, dependent: :destroy
has_many :attachments, as: :container, dependent: :destroy
has_one :page, dependent: :destroy
has_one :project_score, dependent: :destroy
has_many :versions, -> { order("versions.created_on DESC, versions.name DESC") }, dependent: :destroy
has_many :praise_treads, as: :praise_tread_object, dependent: :destroy

View File

@ -0,0 +1,17 @@
class Admins::PageThemesQuery < ApplicationQuery
include CustomSortable
attr_reader :params
sort_columns :created_at, default_by: :created_at, default_direction: :desc
def initialize(params)
@params = params
end
def call
language_frame = params[:language_frame].blank? ? [0..99] : params[:language_frame]
page_themes = PageTheme.where(language_frame: language_frame)
custom_sort(page_themes, params[:sort_by], params[:sort_direction])
end
end

View File

@ -0,0 +1,61 @@
<div class="modal fade page-themes-change-modal" tabindex="-1" role="dialog" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title"><%= type == "create" ? "新增" : "编辑" %></h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<%= simple_form_for(@page_theme, url: admins_page_themes_path(@page_theme)) do |p| %>
<div class="modal-body">
<div class="form-group">
<label>
建站工具
</label>
<% state_options = [['hugo', "hugo"], ['jeklly', "jeklly"],['hexo',"hexo"]] %>
<%= select_tag('page_theme[language_frame]', options_for_select(state_options), class: 'form-control') %>
</div>
<div class="form-group">
<label>
主题名 <span class="ml10 color-orange mr20">*</span>
</label>
<%= p.text_field :name,class: "form-control input-lg",required: true%>
</div>
<div class="form-group">
<label>
仓库url
</label>
<%= p.text_field :clone_url, class: "form-control",placeholder: ""%>
</div>
<div class="form-group">
<label>
排序等级
</label>
<%= p.number_field :order_index, class: "form-control",placeholder: ""%>
</div>
<div class="logo-item">
<% logo_img = @page_theme.image_url %>
<div class="logo-item-left mr-3 <%= logo_img ? 'has-img' : '' %>">
<img class="logo-item-img nav-logo-img" src="<%= logo_img %>" style="<%= logo_img.present? ? '' : 'display: none' %>"/>
<%= file_field_tag(:image_url, accept: 'image/png,image/jpg,image/jpeg',style: "display: none", value: params[:image]) %>
<label for="image_url" class="logo-item-upload" data-toggle="tooltip" data-title="选择图片"></label>
</div>
<div class="logo-item-right">
<div class="logo-item-title flex-1">logo</div>
<div>格式PNG、JPG</div>
<div>尺寸高度38px以内宽等比例缩放</div>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
<%= p.submit "确认", class: "btn btn-primary submit-btn" %>
</div>
<% end %>
</div>
</div>
</div>

View File

@ -0,0 +1,33 @@
<table class="table table-hover text-center subject-list-table">
<thead class="thead-light">
<tr>
<th width="5%">序号</th>
<th width="10%">建站工具</th>
<th width="20%">主题名</th>
<th width="20%">图片</th>
<th width="25%">仓库url</th>
<th width="20%">操作</th>
</tr>
</thead>
<tbody>
<% if page_themes.present? %>
<% page_themes.each_with_index do |banner, index| %>
<tr class="page-theme-item-<%= banner.id %>">
<td><%= list_index_no((params[:page] || 1).to_i, index) %></td>
<td><%= banner.language_frame %></td>
<td><%= banner.name %></td>
<td><img style="width:150px" src="<%= banner.image_url %>" /></td>
<td><a href="<%= banner.clone_url %>" target="_blank"><%= banner.clone_url %></a> </td>
<td class="action-container">
<%= link_to "编辑", edit_admins_page_theme_path(banner), remote: true, class: "action" %>
<%= link_to "删除", admins_page_theme_path(banner), method: :delete, data:{confirm: "确认删除的吗?"}, class: "action" %>
</td>
</tr>
<% end %>
<% else %>
<%= render 'admins/shared/no_data_for_table' %>
<% end %>
</tbody>
</table>
<%= render partial: 'admins/shared/paginate', locals: { objects: page_themes } %>

View File

@ -0,0 +1,18 @@
$("#page-themes-modals").html("<%= j render(partial: 'admins/page_themes/form_modal', locals: {type: 'update'}) %>")
$(".page-themes-change-modal").modal('show');
$('.logo-item-left').on("change", 'input[type="file"]', function () {
var $fileInput = $(this);
var file = this.files[0];
var imageType = /image.*/;
if (file && file.type.match(imageType)) {
var reader = new FileReader();
reader.onload = function () {
var $box = $fileInput.parent();
$box.find('img').attr('src', reader.result).css('display', 'block');
$box.addClass('has-img');
};
reader.readAsDataURL(file);
} else {
}
});

View File

@ -0,0 +1,21 @@
<% define_admin_breadcrumbs do %>
<% add_admin_breadcrumb('主题管理') %>
<% end %>
<div class="box search-form-container project-list-form">
<%= form_tag(admins_page_themes_path, method: :get, class: 'form-inline search-form flex-1', remote: true) do %>
<div class="form-group mr-2">
<label for="language_frame">建站工具:</label>
<% state_options = [['全部',nil], ['hugo', 0], ['jeklly', 1],['hexo',2]] %>
<%= select_tag(:language_frame, options_for_select(state_options), class: 'form-control') %>
</div>
<%= submit_tag('搜索', class: 'btn btn-primary ml-3', 'data-disable-with': '搜索中...') %>
<% end %>
<%= link_to "新增", new_admins_page_theme_path, remote: true, class: "btn btn-primary pull-right", "data-disabled-with":"...新增" %>
</div>
<div class="box admin-list-container page-themes-list-container">
<%= render partial: 'admins/page_themes/list', locals: { page_themes: @page_themes } %>
</div>
<div id="page-themes-modals">
</div>

View File

@ -0,0 +1 @@
$('.page-themes-list-container').html("<%= j( render partial: 'admins/page_themes/list', locals: { page_themes: @page_themes } ) %>");

View File

@ -0,0 +1,18 @@
$("#page-themes-modals").html("<%= j render(partial: 'admins/page_themes/form_modal', locals: {type: 'create'}) %>")
$(".page-themes-change-modal").modal('show');
$('.logo-item-left').on("change", 'input[type="file"]', function () {
var $fileInput = $(this);
var file = this.files[0];
var imageType = /image.*/;
if (file && file.type.match(imageType)) {
var reader = new FileReader();
reader.onload = function () {
var $box = $fileInput.parent();
$box.find('img').attr('src', reader.result).css('display', 'block');
$box.addClass('has-img');
};
reader.readAsDataURL(file);
} else {
}
});

View File

@ -32,6 +32,7 @@
<%= sidebar_item_group('#pages-submenu', '站点管理', icon: 'cogs') do %>
<li><%= sidebar_item(admins_identity_verifications_path, '身份审核列表', icon: 'user', controller: 'admins-identity_verifications') %></li>
<li><%= sidebar_item(admins_site_pages_path, '用户站点列表', icon: 'sitemap', controller: 'admins-site_pages') %></li>
<li><%= sidebar_item(admins_page_themes_path, '站点主题配置', icon: 'cogs', controller: 'admins-page_themes') %></li>
<% end %>
</li>

View File

@ -29,8 +29,8 @@
<% end %>
</td>
<td class="text-left">
<%= link_to "/#{site_page.user.login}/#{site_page.project.identifier}", target: '_blank' do %>
<%= overflow_hidden_span site_page.project.name, width: 100 %>
<%= link_to "/#{site_page.user.login}/#{site_page.project.try(:identifier)}", target: '_blank' do %>
<%= overflow_hidden_span site_page.project.try(:name), width: 100 %>
<% end %>
</td>
<td><%= display_text(site_page.state == true ? "正常" : "已关闭") %></td>

View File

@ -815,6 +815,7 @@ Rails.application.routes.draw do
resources :users_rank, only: [:index]
resources :identity_verifications
resources :site_pages
resources :page_themes
resources :projects_rank, only: [:index]
resources :sites
resources :edu_settings

View File

@ -0,0 +1,12 @@
class CreatePageThemes < ActiveRecord::Migration[5.2]
def change
create_table :page_themes do |t|
t.string :name, null:false
t.integer :language_frame, default:0
t.string :image_url
t.string :clone_url, null:false
t.integer :order_index,default: 0
t.timestamps
end
end
end

View File

@ -0,0 +1,5 @@
require 'rails_helper'
RSpec.describe Admins::PageThemesController, type: :controller do
end

View File

@ -0,0 +1,15 @@
require 'rails_helper'
# Specs in this file have access to a helper object that includes
# the Admins::PageThemesHelper. For example:
#
# describe Admins::PageThemesHelper do
# describe "string concat" do
# it "concats two strings with spaces" do
# expect(helper.concat_strings("this","that")).to eq("this that")
# end
# end
# end
RSpec.describe Admins::PageThemesHelper, type: :helper do
pending "add some examples to (or delete) #{__FILE__}"
end

View File

@ -0,0 +1,5 @@
require 'rails_helper'
RSpec.describe PageTheme, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
end