require_dependency "educode_sales/application_controller" module EducodeSales class PlacesController < ApplicationController def index respond_to do |format| authorize! :read, Place format.html do end format.json do @places = EducodeSales::Place.all if params[:q].present? && params[:q][:name].present? @places = @places.where("name like ?", "%#{params[:q][:name]}%") end @places = @places.page(params[:page]).per(params[:limit]) @x = Common.find_by(extras: EducodeSales::Common::XTYPE)&.id @stage_ids = Common.where(clazz: '商机阶段', name: ['已中标','已签单','已验收','回款中', '服务中','已结束']).pluck(:id) end end end def new # gon.area_ids = @place.area_ids gon.areas = Common.where(clazz: 'area').map { |d| { value: d.id, title: d.name } } #.unshift({value: -1, title: '全国'}) render layout: false end def edit @place = Place.find(params[:id]) gon.area_ids = @place.area_ids gon.areas = Common.where(clazz: 'area').map { |d| { value: d.id, title: d.name } } #.unshift({value: -1, title: '全国'}) render layout: false end def create place = @current_admin.places.new(name: params[:name]) commons = [] params[:area_ids].each do |d| commons << Common.find(d) end place.areas = commons if place.save render_success else render_failure place end end def update place = Place.find(params[:id]) place.assign_attributes(name: params[:name]) commons = [] params[:area_ids].each do |d| commons << Common.find(d) end place.areas = commons if place.save render_success else render_failure place end end def destroy place = Place.find(params[:id]) place.destroy render_success rescue ActiveRecord::DeleteRestrictionError => e render_failure '渠道下已有关联数据产生,暂不能删除' end end end