require_dependency "educode_sales/application_controller" module EducodeSales class IdeasController < ApplicationController before_action :find_idea, only: [:edit, :destroy, :detail, :history, :update] def index respond_to do |format| format.html do staff_ids = EducodeSales::Idea.all.pluck(:staff_id) creator_ids = EducodeSales::Idea.all.pluck(:creator_id) @creator_arr = EducodeSales::Staff.joins(:user).where(id: creator_ids).pluck("concat(users.lastname,users.firstname)", :id) @staff_arr = EducodeSales::Staff.joins(:user).where(id: staff_ids).pluck("concat(users.lastname,users.firstname)", :id) end format.json do @ideas = Idea.not_deleted if params[:q] && params[:q][:created_at].present? date = params[:q][:created_at].split(" - ") @ideas = @ideas.where("educode_sales_ideas.created_at >= ? AND educode_sales_ideas.created_at <= ?", date[0] + " 00:00:00", date[1] + " 23:59:59") end if params[:q].present? && params[:q][:name].present? @ideas = @ideas.where("educode_sales_ideas.name like ?", "%#{params[:q][:name]}%") end if params[:q].present? && params[:q][:school].present? @ideas = @ideas.left_joins(:school).where("schools.name like ?", "%#{params[:q][:school]}%") end if params[:q].present? && params[:q][:creator_id].present? @ideas = @ideas.where("educode_sales_ideas.creator_id = ?", params[:q][:creator_id].to_i) end if params[:q].present? && params[:q][:staff_id].present? @ideas = @ideas.where("educode_sales_ideas.staff_id = ?", params[:q][:staff_id].to_i) end if params[:q].present? && params[:q][:status].present? @ideas = @ideas.where(status: params[:q][:status]) end if params[:q].present? && params[:q][:types].present? @ideas = @ideas.where(types: params[:q][:types]) end if params[:q].present? && params[:q][:model].present? @ideas = @ideas.where(model: params[:q][:model]) end if params[:q].present? && params[:q][:history_type].present? if params[:q][:history_type].to_i == 0 @ideas = @ideas.where("educode_sales_ideas.history_type=#{params[:q][:history_type]} or educode_sales_ideas.history_type is null ") else @ideas = @ideas.where(history_type: params[:q][:history_type]) end end if params[:q].present? && params[:q][:level].present? @ideas = @ideas.where(level: params[:q][:level]) end @ideas = @ideas.page(params[:page]).per(params[:limit]) end end end def new gon.department = { value: '', name: '' } gon.value = '' render layout: false end def create idea = Idea.new(idea_params) idea.school_id = Department.find_by_id(idea.department_id)&.school_id idea.creator = current_user idea.save render_success end def detail render layout: false end def history render layout: false end def update @idea.assign_attributes(idea_params) @idea.school_id = Department.find_by_id(@idea.department_id)&.school_id check_changes @idea.save render_success end def destroy @idea.soft_destroy(current_user.id) render_success end def edit gon.department = { value: @idea&.department_id, name: "#{@idea&.department&.school&.name}-#{@idea&.department&.name}" } gon.value = @idea.department_id render layout: false end private def idea_params params.permit(:name, :level, :staff_id, :status, :types, :model, :hardware, :project, :money, :end_time, :content, :department_id, :manager_name, :manager_phone) end def find_idea @idea = Idea.find(params[:id]) end def check_changes unless @idea.new_record? history = [] arr = @idea.attributes.except(:creator_id, :id, :history_type, :deleter_id, :is_deleted, :deleted_at, :history_record, :created_at, :updated_at).keys arr.each do |attr| if @idea.send("#{attr}_changed?") old_value, new_value = @idea.send("#{attr}_was"), @idea.send(attr) res = @idea.save_history(attr, old_value, new_value) history << res if res end end @idea.idea_histories.create(content: history, staff: current_user) if history.present? end end end end