From cb7769ea4f53cd38ea29a7c196114e2a334ad6bc Mon Sep 17 00:00:00 2001 From: manson Date: Tue, 17 Jun 2014 18:46:52 +0800 Subject: [PATCH] Basic preview function --- .../admin/announcements_controller.rb | 25 ++++++++++- app/controllers/announcements_controller.rb | 9 ++-- app/models/bulletin.rb | 1 + app/views/admin/announcements/_form.html.erb | 43 ++++++++++++++++++- config/routes.rb | 1 + 5 files changed, 72 insertions(+), 7 deletions(-) diff --git a/app/controllers/admin/announcements_controller.rb b/app/controllers/admin/announcements_controller.rb index 4df744f..f66fb73 100644 --- a/app/controllers/admin/announcements_controller.rb +++ b/app/controllers/admin/announcements_controller.rb @@ -15,7 +15,7 @@ class Admin::AnnouncementsController < OrbitAdminController @filter_fields = filter_fields(@categories, @tags) @table_fields = [:status, :category, :title, :start_date, :end_date, :last_modified] - @bulletins = Bulletin.order_by(sort).with_categories(filters("category")).with_tags(filters("tag")).with_status(filters("status")).page(params[:page]).per(10) + @bulletins = Bulletin.where(:is_preview=>false).order_by(sort).with_categories(filters("category")).with_tags(filters("tag")).with_status(filters("status")).page(params[:page]).per(10) if request.xhr? render :partial => "index" @@ -67,6 +67,29 @@ class Admin::AnnouncementsController < OrbitAdminController 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') + end + 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 build_email(bulletin) if bulletin.email_sent and !bulletin.email_addresses.blank? if bulletin.email.nil? diff --git a/app/controllers/announcements_controller.rb b/app/controllers/announcements_controller.rb index 1185236..e4bb168 100644 --- a/app/controllers/announcements_controller.rb +++ b/app/controllers/announcements_controller.rb @@ -1,7 +1,7 @@ class AnnouncementsController < ApplicationController def index - announcements = Bulletin.can_display.order_by(:created_at=>'desc').filter_by_categories + announcements = Bulletin.where(:is_preview=>false).can_display.order_by(:created_at=>'desc').filter_by_categories anns = announcements.collect do |a| statuses = a.statuses.collect do |status| { @@ -28,7 +28,7 @@ class AnnouncementsController < ApplicationController end def widget - announcements = Bulletin.can_display.order_by(:created_at=>'desc').filter_by_widget_categories + announcements = Bulletin.where(:is_preview=>false).can_display.order_by(:created_at=>'desc').filter_by_widget_categories anns = announcements.collect do |a| statuses = a.statuses.collect do |status| { @@ -61,6 +61,7 @@ class AnnouncementsController < ApplicationController files = announcement.bulletin_files.map{|file| { "file_url" => file.file.url, "file_title" => (file.title.blank? ? File.basename(file.file.path) : file.title) } } rescue [] links = announcement.bulletin_links.map{|link| { "link_url" => link.url, "link_title" => (link.title.blank? ? link.url : link.title) } } rescue [] update_user = announcement.update_user.member_profile.name rescue "" + # announcement.destroy if announcement.is_preview { "tags" => tags, "bulletin_files" => files, @@ -72,8 +73,8 @@ class AnnouncementsController < ApplicationController "body" =>announcement.text, "image" => announcement.image.url }, - "impressionist" => announcement + "impressionist" => (announcement.is_preview ? nil : announcement) } - end + end end diff --git a/app/models/bulletin.rb b/app/models/bulletin.rb index 7abca1f..199cb42 100644 --- a/app/models/bulletin.rb +++ b/app/models/bulletin.rb @@ -17,6 +17,7 @@ class Bulletin field :postdate , :type => DateTime, :default => Time.now field :deadline , :type => DateTime field :rss2_sn + field :is_preview, :type => Boolean, :default => false field :email_id field :email_sent, :type => Boolean, :default => false diff --git a/app/views/admin/announcements/_form.html.erb b/app/views/admin/announcements/_form.html.erb index b5cabe5..d238637 100644 --- a/app/views/admin/announcements/_form.html.erb +++ b/app/views/admin/announcements/_form.html.erb @@ -7,7 +7,6 @@ <%= javascript_include_tag "lib/bootstrap-fileupload" %> <%= javascript_include_tag "lib/bootstrap-datetimepicker" %> <%= javascript_include_tag "lib/datetimepicker/datetimepicker.js" %> - <%= javascript_include_tag "lib/modal-preview" %> <%= javascript_include_tag "lib/file-type" %> <%= javascript_include_tag "lib/module-area" %> <% end %> @@ -288,13 +287,30 @@
<%= f.submit t('submit'), class: 'btn btn-primary' %> - <%= button_tag t("preview"), id: "button_for_preview", name: "commit", class: 'btn post_preview', type: :button, url: admin_announcements_path %> + <%= button_tag t("preview"), id: "button_for_preview", name: "commit", class: 'btn', type: :button %> <%= link_to t('cancel'), admin_announcements_path, :class=>"btn" %>
+ + + + <% content_for :page_specific_javascript do %> <% end %> \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index c06cd51..76ede2f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -4,6 +4,7 @@ Rails.application.routes.draw do scope "(:locale)", locale: Regexp.new(locales.join("|")) do namespace :admin do + post 'announcement/preview', to: 'announcements#preview' resources :announcements end