forked from Trustie/forgeplus
新增:模型关联关系以及控制器方法
This commit is contained in:
parent
f1d880057f
commit
af303ac023
|
|
@ -0,0 +1,56 @@
|
|||
class Users::ResidentExpertsController < Users::BaseController
|
||||
def index
|
||||
@resident_experts = ResidentExpert.approved.includes(:user)
|
||||
@resident_experts = @resident_experts.where(expert_category: params[:expert_category]) if params[:expert_category].present?
|
||||
@resident_experts = @resident_experts.where("name like ? or department like ?", "%#{params[:search]}%", "%#{params[:search]}%") if params[:keyword].present?
|
||||
@resident_expert_category_list = @resident_experts.group(:expert_category).count
|
||||
@resident_expert_memos_count = @resident_experts.count("users.memos_count")
|
||||
@resident_experts = @resident_experts.order(created_at: :desc)
|
||||
@resident_experts = paginate(@resident_experts)
|
||||
end
|
||||
|
||||
def current
|
||||
@resident_expert = ResidentExpert.find_by(user_id: current_user.id)
|
||||
@resident_expert = @resident_expert.includes(:user)
|
||||
end
|
||||
|
||||
def create
|
||||
@resident_expert = ResidentExpert.new(resident_expert_params)
|
||||
@resident_expert.user = current_user
|
||||
if @resident_expert.save
|
||||
@resident_expert.resident_expert_actions.create(action_type: ResidentExpertAction::action_types[:approved], action_name: '提交申请' )
|
||||
@resident_expert.resident_expert_actions.create(action_type: ResidentExpertAction::action_types[:approved], action_name: '进入审核队列' )
|
||||
@resident_expert.resident_expert_actions.create(action_type: ResidentExpertAction::action_types[:pending], action_name: '管理员审核中', action_description: "预计 1~3 个工作日内完成" )
|
||||
@resident_expert.resident_expert_actions.create(action_type: ResidentExpertAction::action_types[:todo], action_name: '展示上线', action_description: "待审核通过后自动上线" )
|
||||
render_ok
|
||||
else
|
||||
render_error(@resident_expert.errors.full_messages)
|
||||
end
|
||||
end
|
||||
|
||||
def update
|
||||
@resident_expert = ResidentExpert.find_by(user_id: current_user.id)
|
||||
@resident_expert.attributes = resident_expert_params
|
||||
@resident_expert.status = ResidentExpert::statuses[:pending]
|
||||
if @resident_expert.save
|
||||
@resident_expert.resident_expert_actions.destroy_all
|
||||
@resident_expert.resident_expert_actions.create(action_type: ResidentExpertAction::action_types[:approved], action_name: '提交申请' )
|
||||
@resident_expert.resident_expert_actions.create(action_type: ResidentExpertAction::action_types[:approved], action_name: '进入审核队列' )
|
||||
@resident_expert.resident_expert_actions.create(action_type: ResidentExpertAction::action_types[:pending], action_name: '管理员审核中', action_description: "预计 1~3 个工作日内完成" )
|
||||
@resident_expert.resident_expert_actions.create(action_type: ResidentExpertAction::action_types[:todo], action_name: '展示上线', action_description: "待审核通过后自动上线" )
|
||||
render_ok
|
||||
else
|
||||
render_error(@resident_expert.errors.full_messages)
|
||||
end
|
||||
end
|
||||
|
||||
def actions
|
||||
@resident_expert = ResidentExpert.find_by(user_id: current_user.id)
|
||||
@resident_expert_actions = @resident_expert.resident_expert_actions
|
||||
end
|
||||
|
||||
private
|
||||
def resident_expert_params
|
||||
params.require(:resident_expert).permit(:name, :gender, :expert_category, :academic_degree, :department, :title, :territory_area1, :territory_area2, :territory_area3, :introduction)
|
||||
end
|
||||
end
|
||||
|
|
@ -1,2 +1,13 @@
|
|||
class ResidentExpert < ApplicationRecord
|
||||
|
||||
belongs_to :user
|
||||
has_many :resident_expert_actions, dependent: :destroy
|
||||
|
||||
enum gender: { male: 0, female: 1, secret: 2 }
|
||||
enum expert_category: { academic: 0, industry: 1, engineering: 2, senior: 3 }
|
||||
enum academic_degree: { bachelor: 0, master: 1, doctor: 2 }
|
||||
enum status: { pending: 0, approved: 1, rejected: 2 }
|
||||
|
||||
validates :user_id, presence: true, uniqueness: true
|
||||
validates :name, :gender, :expert_category, :academic_degree, :department, :title, :territory_area1, :territory_area2, :introduction, :status, presence: true
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,2 +1,3 @@
|
|||
class ResidentExpertAction < ApplicationRecord
|
||||
belongs_to :resident_expert
|
||||
end
|
||||
|
|
|
|||
|
|
@ -380,6 +380,12 @@ Rails.application.routes.draw do
|
|||
end
|
||||
|
||||
scope module: :users do
|
||||
resources :resident_experts do
|
||||
collection do
|
||||
get :current
|
||||
get :actions
|
||||
end
|
||||
end
|
||||
get 'template_message_settings', to: 'template_message_settings#current_setting'
|
||||
post 'template_message_settings/update_setting', to: 'template_message_settings#update_setting'
|
||||
resources :system_notification_histories, only: [:create]
|
||||
|
|
|
|||
Loading…
Reference in New Issue