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 def delete
if params[:ids] if params[:ids]
bulletins = Bulletin.any_in(:uid => params[:ids]).destroy_all Bulletin.any_in(:uid => params[:ids]).destroy_all
end end
redirect_to "/admin/announcements" redirect_to "/admin/announcements"
end end
def preview def preview
if params['preview_type'].eql?('edit')
bulletin_data = bulletin_params 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
unless bulletin_data['bulletin_files_attributes'].nil? if !bulletin_data['bulletin_files_attributes'].blank?
bulletin_data['bulletin_files_attributes'].each do |key, value| bulletin_data['bulletin_files_attributes'].each do |key, bulletin_file|
bulletin_data['bulletin_files_attributes'][key].delete('id') 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
end end
unless bulletin_data['bulletin_links_attributes'].nil? if !bulletin_data['bulletin_links_attributes'].blank?
bulletin_data['bulletin_links_attributes'].each do |key, value| bulletin_data['bulletin_links_attributes'].each do |key, bulletin_link|
bulletin_data['bulletin_links_attributes'][key].delete('id') 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
end end
bulletin = Bulletin.new(bulletin_data) 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.is_preview = true bulletin.is_preview = true
bulletin.create_user_id = current_user.id
bulletin.update_user_id = current_user.id
bulletin.save bulletin.save
render :text=>page_for_bulletin(bulletin) render :text=>page_for_bulletin(bulletin)
end 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) def build_email(bulletin)
if bulletin.email_sent and !bulletin.email_addresses.blank? if bulletin.email_sent and !bulletin.email_addresses.blank?
if bulletin.email.nil? if bulletin.email.nil?

View File

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

View File

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

View File

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

View File

@ -298,7 +298,7 @@
<h3><%= t(:preview) %></h3> <h3><%= t(:preview) %></h3>
</div> </div>
<div class="modal-body"> <div class="modal-body">
<iframe src=""></iframe> <iframe id="preview-iframe" src=""></iframe>
</div> </div>
<div class="modal-footer"> <div class="modal-footer">
<a href="#" class="btn" data-dismiss="modal"><%= t(:close) %></a> <a href="#" class="btn" data-dismiss="modal"><%= t(:close) %></a>
@ -358,18 +358,29 @@
for ( instance in CKEDITOR.instances ) for ( instance in CKEDITOR.instances )
CKEDITOR.instances[instance].updateElement(); 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({ $.ajax({
type: "post", type: "post",
url: '<%= admin_announcement_preview_path %>', url: '<%= admin_announcement_preview_path %>',
data : $('.main-forms').serialize() data : formData,
processData: false,
contentType: false
}).done(function(data){ }).done(function(data){
$('.modal-body iframe').attr('src',data); $('.modal-body iframe').attr('src',data);
$('#show_preview .modal').modal(); $('#show_preview .modal').modal();
$('#show_preview .modal').height(function() { $('#show_preview .modal').height(function() {
return $(window).height() * 0.7; 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); $('.main-forms input[name="_method"]').val(method);
return false; return false;
}); });

View File

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