From 39b8b1f17adf3293b3f6886b09a501998e669174 Mon Sep 17 00:00:00 2001 From: manson Date: Mon, 23 Jun 2014 16:36:04 +0800 Subject: [PATCH] Update preview with file upload --- .../admin/announcements_controller.rb | 80 +++++++++++++++---- app/models/bulletin.rb | 1 + app/models/bulletin_file.rb | 1 + app/models/bulletin_link.rb | 1 + app/views/admin/announcements/_form.html.erb | 21 +++-- config/routes.rb | 1 + 6 files changed, 85 insertions(+), 20 deletions(-) diff --git a/app/controllers/admin/announcements_controller.rb b/app/controllers/admin/announcements_controller.rb index ade9b8b..c5811cf 100644 --- a/app/controllers/admin/announcements_controller.rb +++ b/app/controllers/admin/announcements_controller.rb @@ -62,34 +62,84 @@ class Admin::AnnouncementsController < OrbitAdminController def delete if params[:ids] - bulletins = Bulletin.any_in(:uid => params[:ids]).destroy_all + Bulletin.any_in(:uid => params[:ids]).destroy_all end redirect_to "/admin/announcements" end def preview - bulletin_data = bulletin_params - - unless bulletin_data['bulletin_files_attributes'].nil? - bulletin_data['bulletin_files_attributes'].each do |key, value| - bulletin_data['bulletin_files_attributes'][key].delete('id') - end - end - - unless bulletin_data['bulletin_links_attributes'].nil? - bulletin_data['bulletin_links_attributes'].each do |key, value| - bulletin_data['bulletin_links_attributes'][key].delete('id') + if params['preview_type'].eql?('edit') + bulletin_data = bulletin_params + org_bulletin = Bulletin.find(params['bulletin_id']) + bulletin = org_bulletin.clone + bulletin.generate_uid + bulletin.bulletin_files = [] + bulletin.bulletin_links = [] + if bulletin_data['image'].blank? + bulletin.image = org_bulletin.image end + + if !bulletin_data['bulletin_files_attributes'].blank? + bulletin_data['bulletin_files_attributes'].each do |key, bulletin_file| + next if !bulletin_file['_destroy'].blank? + file = nil + if bulletin_file['id'].blank? + file = BulletinFile.new(bulletin_file) + file.bulletin_id = bulletin.id + file.save + else + file = BulletinFile.find(bulletin_file['id']).clone + file.bulletin_id = bulletin.id + bulletin_file.delete('id') + bulletin_file.delete('_destroy') + file.update_attributes(bulletin_file) + end + + file.save + bulletin.bulletin_files << file + end + end + + if !bulletin_data['bulletin_links_attributes'].blank? + bulletin_data['bulletin_links_attributes'].each do |key, bulletin_link| + next if !bulletin_link['_destroy'].blank? + + if bulletin_link['id'].blank? + link = BulletinLink.new(bulletin_link) + link.bulletin_id = bulletin.id + else + link = BulletinLink.find(bulletin_link['id']).clone + link.bulletin_id = bulletin.id + bulletin_link.delete('id') + bulletin_link.delete('_destroy') + link.update_attributes(bulletin_link) + end + + link.save + bulletin.bulletin_links << link + end + end + + bulletin_data.delete('bulletin_files_attributes') + bulletin_data.delete('bulletin_links_attributes') + bulletin.update_attributes(bulletin_data) + else + bulletin = Bulletin.new(bulletin_params) end - bulletin = Bulletin.new(bulletin_data) bulletin.is_preview = true - bulletin.create_user_id = current_user.id - bulletin.update_user_id = current_user.id bulletin.save render :text=>page_for_bulletin(bulletin) end + def destroy_preview + bulletin = Bulletin.find_by(:uid=>params['uid']) + if bulletin.is_preview + bulletin.destroy + end + render :json=>{'destroy'=>bulletin.id.to_s} + end + def build_email(bulletin) if bulletin.email_sent and !bulletin.email_addresses.blank? if bulletin.email.nil? diff --git a/app/models/bulletin.rb b/app/models/bulletin.rb index 199cb42..f1e5e82 100644 --- a/app/models/bulletin.rb +++ b/app/models/bulletin.rb @@ -4,6 +4,7 @@ class Bulletin include OrbitModel::Status include OrbitModel::Impression + # encoding: utf-8 include OrbitTag::Taggable include OrbitCategory::Categorizable include Slug diff --git a/app/models/bulletin_file.rb b/app/models/bulletin_file.rb index d121bd2..dabe73a 100644 --- a/app/models/bulletin_file.rb +++ b/app/models/bulletin_file.rb @@ -1,3 +1,4 @@ +# encoding: utf-8 class BulletinFile include Mongoid::Document diff --git a/app/models/bulletin_link.rb b/app/models/bulletin_link.rb index 755dea8..6283857 100644 --- a/app/models/bulletin_link.rb +++ b/app/models/bulletin_link.rb @@ -1,3 +1,4 @@ +# encoding: utf-8 require 'uri' class BulletinLink diff --git a/app/views/admin/announcements/_form.html.erb b/app/views/admin/announcements/_form.html.erb index d238637..ef4cd52 100644 --- a/app/views/admin/announcements/_form.html.erb +++ b/app/views/admin/announcements/_form.html.erb @@ -298,7 +298,7 @@

<%= t(:preview) %>