ruling_template/app/controllers/admin/ruling_templates_controller.rb

101 lines
2.6 KiB
Ruby

class Admin::RulingTemplatesController < OrbitAdminController
def index
@templates = RTemplate.all.desc(:created_at).page(params[:page]).per(10)
end
def new
@template = RTemplate.new
end
def edit
@template = RTemplate.find(params[:id])
end
def create
template = RTemplate.new(template_params)
template.save
redirect_to upload_files_admin_ruling_template_path(template.id)
end
def update
template = RTemplate.find(params[:id])
template.update_attributes(template_params)
template.save
redirect_to upload_files_admin_ruling_template_path(template.id)
end
def show
@template = RTemplate.find(params[:id])
render :layout => false
end
def upload_files
@template = RTemplate.find(params[:id])
end
def upload_image
template = RTemplate.find(params[:id])
image = TemplateImageFile.new(template_image_params)
image.order = (template.template_image_files.max(:order) + 1 rescue 0)
image.r_template = template
image.save!
render :json => {"files" => [{"thumbnail_url"=>image.image_file.thumb.url}]}.to_json
end
def change_image_order
image_ids = params[:images]
image_ids.each_with_index do |img, idx|
image = TemplateImageFile.find(img)
image.order = idx
image.save
end
render :json => {"success" => true}.to_json
end
def delete_image
image = TemplateImageFile.find(params[:id])
image.destroy
render :json => {"success" => true}.to_json
end
def upload_temp_file
filepath = params[:path]
file = params[:psdfile]
File.open(filepath,"ab"){ |f| f.write(file.read) }
render :json => {"success" => true}.to_json
end
def get_temp_dir_name
dirname = Digest::MD5.hexdigest(Time.now.to_s)
directory = "public/ruling_template/#{dirname}"
FileUtils.mkdir_p(directory) unless File.exists?(directory)
path_to_file = "#{directory}/#{params[:filename]}"
File.open(path_to_file,"w")
template = RTemplate.find(params[:id])
if !template.template_psd_file.nil? && !template.template_psd_file.psd_zip.url.nil?
template.template_psd_file.destroy
end
psd = TemplatePsdFile.new
psd.psd_zip = Rails.root.join(path_to_file).open
psd.r_template = template
psd.save
FileUtils.remove_dir(directory)
render :json => {"psd_id" => psd.id.to_s, "path" => psd.psd_zip.path}.to_json
end
def destroy
template = RTemplate.find(params[:id])
template.destroy
redirect_to :back
end
private
def template_params
params.require(:r_template).permit!
end
def template_image_params
params.require(:template_image_file).permit!
end
end