From 7552fdd6d0e5b0ec049f4426298ebab6678f349b Mon Sep 17 00:00:00 2001 From: Harry Bomrah Date: Mon, 22 Sep 2014 16:32:30 +0800 Subject: [PATCH] added approve method to announcements --- .../admin/announcements_controller.rb | 14 ++++++++++++- app/controllers/announcements_controller.rb | 12 ++++++++--- app/helpers/admin/announcements_helper.rb | 20 +++++++++++++++++++ app/models/bulletin.rb | 2 ++ app/views/admin/announcements/_index.html.erb | 5 +++++ config/locales/en.yml | 1 + config/locales/zh_tw.yml | 1 + config/routes.rb | 1 + 8 files changed, 52 insertions(+), 4 deletions(-) diff --git a/app/controllers/admin/announcements_controller.rb b/app/controllers/admin/announcements_controller.rb index d3a1296..0ef6610 100644 --- a/app/controllers/admin/announcements_controller.rb +++ b/app/controllers/admin/announcements_controller.rb @@ -3,6 +3,7 @@ class Admin::AnnouncementsController < OrbitAdminController include Admin::AnnouncementsHelper before_action ->(module_app = @app_title) { set_variables module_app } before_action :set_bulletin, only: [:edit, :destroy] + before_action :load_access_level def initialize super @@ -41,15 +42,26 @@ class Admin::AnnouncementsController < OrbitAdminController bulletin_params['bulletin_links_attributes'].delete(idx.to_s) if link['url'].blank? end end - + bulletin = Bulletin.new(bulletin_params) bulletin.create_user_id = current_user.id bulletin.update_user_id = current_user.id + if user_can_approve? + bulletin.approved = true + end bulletin.save build_email(bulletin) redirect_to params['referer_url'] end + def approve_bulletin + id = params[:id] + bulletin = Bulletin.find(id) + bulletin.approved = true + bulletin.save + redirect_to admin_announcements_path + end + def edit if can_edit_or_delete?(@bulletin) @tags = @module_app.tags diff --git a/app/controllers/announcements_controller.rb b/app/controllers/announcements_controller.rb index 1c421aa..59d331c 100644 --- a/app/controllers/announcements_controller.rb +++ b/app/controllers/announcements_controller.rb @@ -1,7 +1,7 @@ class AnnouncementsController < ApplicationController def index - announcements = Bulletin.where(:is_preview.in=>[false,nil]).can_display.order_by(:created_at=>'desc').filter_by_categories.filter_by_tags(OrbitHelper.params['tags']) + announcements = Bulletin.where(:is_preview.in=>[false,nil]).can_display.is_approved.order_by(:created_at=>'desc').filter_by_categories.filter_by_tags(OrbitHelper.params['tags']) anns = announcements.collect do |a| statuses = a.statuses_with_classname.collect do |status| @@ -49,7 +49,7 @@ class AnnouncementsController < ApplicationController end def widget - announcements = Bulletin.where(:is_preview.in=>[false,nil]).can_display.order_by(:created_at=>'desc').filter_by_widget_categories.filter_by_tags(OrbitHelper.widget_tags) + announcements = Bulletin.where(:is_preview.in=>[false,nil]).can_display.is_approved.order_by(:created_at=>'desc').filter_by_widget_categories.filter_by_tags(OrbitHelper.widget_tags) anns = announcements.collect do |a| statuses = a.statuses_with_classname.collect do |status| @@ -94,7 +94,7 @@ class AnnouncementsController < ApplicationController end def widget1 - announcements = Bulletin.where(:is_preview.in=>[false,nil]).can_display.order_by(:created_at=>'desc').filter_by_widget_categories.filter_by_tags(OrbitHelper.widget_tags) + announcements = Bulletin.where(:is_preview.in=>[false,nil]).can_display.is_approved.order_by(:created_at=>'desc').filter_by_widget_categories.filter_by_tags(OrbitHelper.widget_tags) anns = announcements.collect do |a| statuses = a.statuses_with_classname.collect do |status| @@ -144,6 +144,12 @@ class AnnouncementsController < ApplicationController url_to_edit = OrbitHelper.user_can_edit?(announcement) ? "/admin/announcements/#{announcement.id.to_s}/edit" : "" + access_level = OrbitHelper.user_access_level? + + if !announcement.approved && (access_level != "manager" && access_level != "admin") + return {} + end + tags = announcement.tags.map{|tag| { "tag" => tag.name , "url" => OrbitHelper.page_for_tag(tag) diff --git a/app/helpers/admin/announcements_helper.rb b/app/helpers/admin/announcements_helper.rb index 9a46c72..cf3ace5 100644 --- a/app/helpers/admin/announcements_helper.rb +++ b/app/helpers/admin/announcements_helper.rb @@ -24,4 +24,24 @@ module Admin::AnnouncementsHelper ann_page = pages.first if ann_page.nil? request.protocol+(request.host_with_port+ann_page.url+'/'+bulletin.to_param).gsub('//','/') rescue "/" end + + def load_access_level + if current_user.is_admin? + @access_level = "admin" + elsif current_user.is_manager?(@module_app) + @access_level = "manager" + end + end + + def user_can_approve? + case @access_level + when "admin" + return true + when "manager" + return true + else + return false + end + end + end diff --git a/app/models/bulletin.rb b/app/models/bulletin.rb index 4267639..7f48cbb 100644 --- a/app/models/bulletin.rb +++ b/app/models/bulletin.rb @@ -18,6 +18,7 @@ class Bulletin field :postdate , :type => DateTime, :default => Time.now field :deadline , :type => DateTime field :rss2_sn + field :approved, :type => Boolean, :default => false field :is_preview, :type => Boolean, :default => false field :email_id @@ -37,6 +38,7 @@ class Bulletin before_destroy :destroy_email scope :can_display, ->{where(:is_hidden=>false).any_of({:postdate.lt=>Time.now, :deadline.gt=>Time.now},{:postdate.lt=>Time.now, :deadline=>nil}).order_by([:is_top, :desc])} + scope :is_approved, ->{where(:approved => true)} def update_user User.find(update_user_id) rescue nil diff --git a/app/views/admin/announcements/_index.html.erb b/app/views/admin/announcements/_index.html.erb index c433d3b..57af791 100644 --- a/app/views/admin/announcements/_index.html.erb +++ b/app/views/admin/announcements/_index.html.erb @@ -20,6 +20,8 @@ <% if b.expired? %> <%= b.title %> <%= t(:expired) %> + <% elsif !b.approved? %> + <%= b.title %> <%= t(:pending) %> <% else %> <%= b.title %> <% end %> @@ -29,6 +31,9 @@ <% if can_edit_or_delete?(b) %>
  • <%= t(:edit) %>
  • <%= t(:delete_) %>
  • + <% if !b.approved && user_can_approve? %> +
  • <%= t("announcement.approve") %>
  • + <% end %> <% end %> diff --git a/config/locales/en.yml b/config/locales/en.yml index f6c8192..ab5c5ba 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -8,6 +8,7 @@ en: sub_title: Sub Title category: Category add_new: Add New + approve: Approve all_articles: All Articles announcement: Announcement approval_setting: Approval Setting diff --git a/config/locales/zh_tw.yml b/config/locales/zh_tw.yml index a32222f..261d1ab 100644 --- a/config/locales/zh_tw.yml +++ b/config/locales/zh_tw.yml @@ -8,6 +8,7 @@ zh_tw: sub_title: 副標題 category: 類別 add_new: 新建 + approve: 通過 all_articles: 文章列表 announcement: 公告 approval_setting: 審核設定 diff --git a/config/routes.rb b/config/routes.rb index 2d86021..319d041 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -6,6 +6,7 @@ Rails.application.routes.draw do namespace :admin do post 'announcement/preview', to: 'announcements#preview' get 'announcement/destroy_preview/:slug_title-:uid', to: 'announcements#destroy_preview' + get 'announcement/approve_bulletin', to: 'announcements#approve_bulletin' resources :announcements end