From e62df9011782d6e7d6c17c40e53d939bf8442a3c Mon Sep 17 00:00:00 2001 From: chris Date: Tue, 8 Feb 2011 10:07:40 +0800 Subject: [PATCH] user can't delete self and flash_messages --- app/controllers/panel/users_controller.rb | 10 +++++++-- app/helpers/application_helper.rb | 20 +++++++++++++++++ app/views/admin/items/index.html.erb | 2 ++ app/views/admin/layouts/index.html.erb | 2 ++ app/views/panel/users/_form.html.erb | 2 +- app/views/panel/users/index.html.erb | 27 +++++++++++++---------- config/locales/en.yml | 3 +++ config/locales/zh_tw.yml | 3 +++ public/stylesheets/main.css | 14 +++++++++++- 9 files changed, 67 insertions(+), 16 deletions(-) diff --git a/app/controllers/panel/users_controller.rb b/app/controllers/panel/users_controller.rb index 1e67c11b..d0fcdf6c 100644 --- a/app/controllers/panel/users_controller.rb +++ b/app/controllers/panel/users_controller.rb @@ -22,6 +22,7 @@ class Panel::UsersController < ApplicationController def create @user = User.new(params[:user]) if @user.save + flash[:notice] = t('panel.create_success_user') redirect_to :action => :index else render :action => :new @@ -43,6 +44,7 @@ class Panel::UsersController < ApplicationController @user.avatar = params[:file] if params[:file] if @user.update_attributes(params[:user]) + flash[:notice] = t('panel.update_success_user') redirect_to :action => :index else @user_info_models = UserInfoModel.all.entries @@ -52,8 +54,12 @@ class Panel::UsersController < ApplicationController end def destroy - @user = User.find(params[:id]) - @user.destroy + if params[:id].eql?(session['warden.user.user.key'][1].to_s) + flash[:error] = t('panel.cant_delete_self') + else + @user = User.find(params[:id]) + @user.destroy + end redirect_to :action => :index end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index cfda53e6..2e6db454 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -1,11 +1,31 @@ module ApplicationHelper + + FLASH_NOTICE_KEYS = [:error, :notice, :warning] def colorize_in_use_locale(locale) @site_in_use_locales.include?(locale)? 'green' : 'red' end + def flash_messages + return unless messages = flash.keys.select{|k| FLASH_NOTICE_KEYS.include?(k)} + formatted_messages = messages.map do |type| + content_tag :div, :class => type.to_s do + message_for_item(flash[type], flash["#{type}_item".to_sym]) + end + end + raw(formatted_messages.join) + end + def link_back link_to t('back'), session[:last_page] end + def message_for_item(message, item = nil) + if item.is_a?(Array) + message % link_to(*item) + else + message % item + end + end + end diff --git a/app/views/admin/items/index.html.erb b/app/views/admin/items/index.html.erb index 5f0f56ed..40217fcb 100644 --- a/app/views/admin/items/index.html.erb +++ b/app/views/admin/items/index.html.erb @@ -12,6 +12,8 @@ <% end -%> +<%= flash_messages %> +

<%= t('admin.list_items') %>: <%= show_parent_items_link unless @parent_item.nil? %>

<% if !Layout.exist_one? %> diff --git a/app/views/admin/layouts/index.html.erb b/app/views/admin/layouts/index.html.erb index c7270cb6..b0634d3a 100644 --- a/app/views/admin/layouts/index.html.erb +++ b/app/views/admin/layouts/index.html.erb @@ -4,6 +4,8 @@ <% end -%> +<%= flash_messages %> +

<%= t('admin.list_layouts') %>

diff --git a/app/views/panel/users/_form.html.erb b/app/views/panel/users/_form.html.erb index 1034c8c2..86afefe6 100644 --- a/app/views/panel/users/_form.html.erb +++ b/app/views/panel/users/_form.html.erb @@ -32,7 +32,7 @@ <% end %> -<%= hidden_field_tag :active_roles, @user.active_roles.size > 0 %> +<%= hidden_field_tag :active_roles, (@user.active_roles.size > 0 rescue nil ) %> <% if @user_role_models %>
@@ -14,15 +16,16 @@ -<% @users.each do |user| %> - - - - - + + + + - -<% end %> + <%= link_to t(:delete), panel_user_path(user), :confirm => t('sure?'), :method => :delete %> + + + <% end %>
<%= t('panel.action') %>
<%= user.get_roles %><%= user[:name] %><%= user.email %> + <% @users.each do |user| %> +
<%= user.get_roles %><%= user[:name] %><%= user.email %> <%= link_to t(:show), panel_user_path(user) %> | <%= link_to t(:edit), edit_panel_user_path(user) %> | - <%= link_to t(:delete), panel_user_path(user), :method => :delete %>
diff --git a/config/locales/en.yml b/config/locales/en.yml index b659af5e..5ac19eea 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -97,6 +97,8 @@ en: panel: action: Action + cant_delete_self: You can not delete yourself. + create_success_user: User was successfully created. email: Email home: Home list_users: Users list @@ -105,5 +107,6 @@ en: new_user: New user infos: Information roles: Roles + update_success_user: User was successfully updated. user: User user_panel: User panel diff --git a/config/locales/zh_tw.yml b/config/locales/zh_tw.yml index f6495949..b5fb50cb 100644 --- a/config/locales/zh_tw.yml +++ b/config/locales/zh_tw.yml @@ -94,6 +94,8 @@ zh_tw: panel: action: 行動 + cant_delete_self: 您不可以刪除自己。 + create_success_user: 用戶已成功創建。 email: Email home: 首頁 list_users: 使用清單 @@ -102,6 +104,7 @@ zh_tw: new_user: 新增使用 infos: 資料 roles: 角色 + update_success_user: 用戶已成功更新。 user: 用戶 user_panel: 用戶面板 diff --git a/public/stylesheets/main.css b/public/stylesheets/main.css index 01bf8ab0..e6a5601f 100644 --- a/public/stylesheets/main.css +++ b/public/stylesheets/main.css @@ -9,4 +9,16 @@ ul.list li{ padding:.5em 0; list-style:none; border-bottom:1px solid #ccc; - } \ No newline at end of file + } + +.error{ + color:red; + } + +.notice, .message{ + color:green; + } + +.warning{ + color:orange; + }