Update preview with file upload

This commit is contained in:
manson 2014-06-23 16:36:04 +08:00
parent cdc2a79222
commit 39b8b1f17a
6 changed files with 85 additions and 20 deletions

View File

@ -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?

View File

@ -4,6 +4,7 @@ class Bulletin
include OrbitModel::Status
include OrbitModel::Impression
# encoding: utf-8
include OrbitTag::Taggable
include OrbitCategory::Categorizable
include Slug

View File

@ -1,3 +1,4 @@
# encoding: utf-8
class BulletinFile
include Mongoid::Document

View File

@ -1,3 +1,4 @@
# encoding: utf-8
require 'uri'
class BulletinLink

View File

@ -298,7 +298,7 @@
<h3><%= t(:preview) %></h3>
</div>
<div class="modal-body">
<iframe src=""></iframe>
<iframe id="preview-iframe" src=""></iframe>
</div>
<div class="modal-footer">
<a href="#" class="btn" data-dismiss="modal"><%= t(:close) %></a>
@ -354,22 +354,33 @@
$('#button_for_preview').click(function(){
var method = $('.main-forms input[name="_method"]').val();
$('.main-forms input[name="_method"]').val("post");
for ( instance in CKEDITOR.instances )
CKEDITOR.instances[instance].updateElement();
var formData = new FormData( $('.main-forms')[0] );
formData.append("preview_type", ( (method==undefined) ? "new" : "edit" ));
formData.append("bulletin_id", '<%= @bulletin.id.to_s %>');
$.ajax({
type: "post",
url: '<%= admin_announcement_preview_path %>',
data : $('.main-forms').serialize()
data : formData,
processData: false,
contentType: false
}).done(function(data){
$('.modal-body iframe').attr('src',data);
$('#show_preview .modal').modal();
$('#show_preview .modal').height(function() {
return $(window).height() * 0.7;
});
var slug = data.split('/')[(data.split('/').length-1)];
$('#preview-iframe').on('load', function(){
$.get('/admin/announcement/destroy_preview/'+slug,function(data){
});
});
});
$('.main-forms input[name="_method"]').val(method);
return false;
});

View File

@ -5,6 +5,7 @@ Rails.application.routes.draw do
scope "(:locale)", locale: Regexp.new(locales.join("|")) do
namespace :admin do
post 'announcement/preview', to: 'announcements#preview'
get 'announcement/destroy_preview/:slug_title-:uid', to: 'announcements#destroy_preview'
resources :announcements
end