From 83bbd77d96e6f064ce12c431fd881533c74d75d6 Mon Sep 17 00:00:00 2001 From: chris2tof Date: Mon, 2 May 2011 12:07:31 +0800 Subject: [PATCH] Built-in user_attribute_models --- .../admin/user_info_models_controller.rb | 6 ++++-- .../admin/user_role_models_controller.rb | 5 ++++- app/controllers/panel/users_controller.rb | 18 +++++++++++------- app/models/attribute_model.rb | 10 ++++++++++ app/models/user_attribute_model.rb | 14 ++++++++++++++ .../_attribute_model.html.erb | 13 +++++++++++-- .../admin/user_attribute_models/_form.html.erb | 6 ++++++ .../admin/user_attribute_models/index.html.erb | 10 ++++++++-- .../user_attribute_models/toggle_enable.js.erb | 2 ++ app/views/layouts/admin.html.erb | 1 + app/views/layouts/panel.html.erb | 1 + .../panel/users/_show_user_attribute.html.erb | 2 +- app/views/panel/users/_user_attribute.html.erb | 2 +- lib/tasks/dev.rake | 8 ++++---- 14 files changed, 78 insertions(+), 20 deletions(-) create mode 100644 app/views/admin/user_attribute_models/toggle_enable.js.erb diff --git a/app/controllers/admin/user_info_models_controller.rb b/app/controllers/admin/user_info_models_controller.rb index 5c9c0613..7660d699 100644 --- a/app/controllers/admin/user_info_models_controller.rb +++ b/app/controllers/admin/user_info_models_controller.rb @@ -34,8 +34,10 @@ class Admin::UserInfoModelsController < ApplicationController def update @user_attribute_model = UserInfoModel.find(params[:id]) @user_attribute_model.update_attributes(params[:user_info_model]) - - redirect_to :action => :index + respond_to do |format| + format.html { redirect_to :action => :index } + format.js { render 'admin/user_attribute_models/toggle_enable' } + end end def destroy diff --git a/app/controllers/admin/user_role_models_controller.rb b/app/controllers/admin/user_role_models_controller.rb index 90f2070d..b4145a8f 100644 --- a/app/controllers/admin/user_role_models_controller.rb +++ b/app/controllers/admin/user_role_models_controller.rb @@ -35,7 +35,10 @@ class Admin::UserRoleModelsController < ApplicationController @user_attribute_model = UserRoleModel.find(params[:id]) @user_attribute_model.update_attributes(params[:user_role_model]) - redirect_to :action => :index + respond_to do |format| + format.html { redirect_to :action => :index } + format.js { render 'admin/user_attribute_models/toggle_enable' } + end end def destroy diff --git a/app/controllers/panel/users_controller.rb b/app/controllers/panel/users_controller.rb index d0b177df..c0a2f526 100644 --- a/app/controllers/panel/users_controller.rb +++ b/app/controllers/panel/users_controller.rb @@ -9,13 +9,12 @@ class Panel::UsersController < ApplicationController def show @user = User.find(params[:id]) - @user_info_models = UserInfoModel.all.entries + get_info_role_models end def new @user = User.new - @user_info_models = UserInfoModel.all.entries - @user_role_models = UserRoleModel.all.entries + get_info_role_models end def create @@ -38,8 +37,7 @@ class Panel::UsersController < ApplicationController def edit @user = User.find(params[:id]) - @user_info_models = UserInfoModel.all.entries - @user_role_models = UserRoleModel.all.entries + get_info_role_models end def update @@ -56,8 +54,7 @@ class Panel::UsersController < ApplicationController flash[:notice] = t('panel.update_success_user') redirect_to :action => :index else - @user_info_models = UserInfoModel.all.entries - @user_role_models = UserRoleModel.all.entries + get_info_role_models render :action => :edit end end @@ -73,4 +70,11 @@ class Panel::UsersController < ApplicationController redirect_to :action => :index end + protected + + def get_info_role_models + @user_info_models = UserInfoModel.excludes('disabled' => true) + @user_role_models = UserRoleModel.excludes('disabled' => true) + end + end diff --git a/app/models/attribute_model.rb b/app/models/attribute_model.rb index aad944b5..5cc11aa2 100644 --- a/app/models/attribute_model.rb +++ b/app/models/attribute_model.rb @@ -8,6 +8,8 @@ class AttributeModel field :markup field :locale, :type => Boolean field :list_options, :type => Array + field :built_in, :type => Boolean, :default => false + field :disabled, :type => Boolean, :default => false embedded_in :user_attribute_model, :inverse_of => :attribute_models validates_uniqueness_of :key @@ -47,5 +49,13 @@ class AttributeModel def should_destroy? should_destroy.to_i == 1 rescue nil end + + def is_built_in? + self.built_in + end + + def is_disabled? + self.disabled + end end diff --git a/app/models/user_attribute_model.rb b/app/models/user_attribute_model.rb index b55e38a7..a70c9f85 100644 --- a/app/models/user_attribute_model.rb +++ b/app/models/user_attribute_model.rb @@ -5,6 +5,8 @@ class UserAttributeModel field :key field :i18n_variable_id, :type => BSON::ObjectId, :index => true + field :built_in, :type => Boolean, :default => false + field :disabled, :type => Boolean, :default => false embeds_many :attribute_models @@ -46,6 +48,18 @@ class UserAttributeModel @i18n_variable ||= I18nVariable.find(self.i18n_variable_id) rescue nil end + def is_built_in? + self.built_in + end + + def is_disabled? + self.disabled + end + + def get_enabled_attribute_models + self.attribute_models.excludes('disabled' => true) + end + protected # Destroy the i18n_variable for each attribute_models if marked to destroy diff --git a/app/views/admin/user_attribute_models/_attribute_model.html.erb b/app/views/admin/user_attribute_models/_attribute_model.html.erb index 88098505..e8ad4854 100644 --- a/app/views/admin/user_attribute_models/_attribute_model.html.erb +++ b/app/views/admin/user_attribute_models/_attribute_model.html.erb @@ -20,8 +20,17 @@ <% if attribute_model.new_record? %> (<%= t(:delete) %>) <% else %> - (<%= t(:delete) %>) - <%= hidden_field_tag "user_#{@attribute}_model[attribute_models][][should_destroy]", nil , :class => 'should_destroy' %> + + <% if attribute_model.is_built_in? %> + + <%= t(:disable) %> + <%= t(:enable) %> + + <%= hidden_field_tag "user_#{@attribute}_model[attribute_models][][disabled]", attribute_model.is_disabled? , :class => 'built_in_state' %> + <% else %> + (<%= t(:delete) %>) + <%= hidden_field_tag "user_#{@attribute}_model[attribute_models][][should_destroy]", nil , :class => 'should_destroy' %> + <% end %> <%= f.hidden_field :id %> <%= f.hidden_field :key %> <% end %> diff --git a/app/views/admin/user_attribute_models/_form.html.erb b/app/views/admin/user_attribute_models/_form.html.erb index 94c92615..3b8a0b5f 100644 --- a/app/views/admin/user_attribute_models/_form.html.erb +++ b/app/views/admin/user_attribute_models/_form.html.erb @@ -52,6 +52,12 @@ $(this).next('.should_destroy').attr('value', 1); $(this).parent().parent().hide(); }); + + $('#field_set_attributes a.change_built_in').click(function(){ + $(this).children().toggle(); + var $am = $(this).next('.built_in_state') + $am.attr('value', ($am.attr('value') == "false") ? "true" : "false"); + }); $('.select_mulitlingual input').live('click', function(){ $(this).next().attr('value', this.checked); diff --git a/app/views/admin/user_attribute_models/index.html.erb b/app/views/admin/user_attribute_models/index.html.erb index 4e758f17..fed40700 100644 --- a/app/views/admin/user_attribute_models/index.html.erb +++ b/app/views/admin/user_attribute_models/index.html.erb @@ -19,8 +19,14 @@ <%= user_attribute_model.key %> <%= link_to t(:edit), eval("edit_admin_user_#{@attribute}_model_path(user_attribute_model)") %> | - <%= link_to t(:delete), eval("admin_user_#{@attribute}_model_path(user_attribute_model)"), :confirm => t('sure?'), :method => :delete %> - + <% if user_attribute_model.is_built_in? %> + <%= link_to t(:disable), eval("admin_user_#{@attribute}_model_path(user_attribute_model, :authenticity_token => form_authenticity_token, :user_#{@attribute}_model => {:disabled => true})"), :remote => true, :method => :put, :id => "disable_#{user_attribute_model.id}", :style => "display:#{user_attribute_model.is_disabled? ? 'none' : 'inline'}" %> + <%= link_to t(:enable), eval("admin_user_#{@attribute}_model_path(user_attribute_model, :authenticity_token => form_authenticity_token, :user_#{@attribute}_model => {:disabled => false})"), :remote => true, :method => :put, :id => "enable_#{user_attribute_model.id}", :style => "display:#{user_attribute_model.is_disabled? ? 'inline' : 'none'}" %> + <% else %> + <%= link_to t(:delete), eval("admin_user_#{@attribute}_model_path(user_attribute_model)"), :confirm => t('sure?'), :method => :delete %> + <% end %> + + <% end %> diff --git a/app/views/admin/user_attribute_models/toggle_enable.js.erb b/app/views/admin/user_attribute_models/toggle_enable.js.erb new file mode 100644 index 00000000..a78a00db --- /dev/null +++ b/app/views/admin/user_attribute_models/toggle_enable.js.erb @@ -0,0 +1,2 @@ +$("#enable_<%= @user_attribute_model.id %>").toggle(); +$("#disable_<%= @user_attribute_model.id %>").toggle(); \ No newline at end of file diff --git a/app/views/layouts/admin.html.erb b/app/views/layouts/admin.html.erb index 9f7106f6..f2c21d31 100644 --- a/app/views/layouts/admin.html.erb +++ b/app/views/layouts/admin.html.erb @@ -7,6 +7,7 @@ <%= yield :page_specific_link %> <%= stylesheet_link_tag "easy", "main", "devise", :media => "screen, projection" %> <%= stylesheet_link_tag "easyprint", :media => "print" %> + <%= javascript_include_tag :defaults %> diff --git a/app/views/layouts/panel.html.erb b/app/views/layouts/panel.html.erb index 58e834e2..93551afb 100644 --- a/app/views/layouts/panel.html.erb +++ b/app/views/layouts/panel.html.erb @@ -7,6 +7,7 @@ <%= yield :page_specific_link %> <%= stylesheet_link_tag "easy", "main", "devise", :media => "screen, projection" %> <%= stylesheet_link_tag "easyprint", :media => "print" %> + <%= javascript_include_tag :defaults %> diff --git a/app/views/panel/users/_show_user_attribute.html.erb b/app/views/panel/users/_show_user_attribute.html.erb index 1ff9fe45..261fab9a 100644 --- a/app/views/panel/users/_show_user_attribute.html.erb +++ b/app/views/panel/users/_show_user_attribute.html.erb @@ -1,7 +1,7 @@
<%= am.i18n_variable[I18n.locale] %> - <% am.attribute_models.each do |attr| %> + <% am.get_enabled_attribute_models.each do |attr| %> - <% ua.attribute_models.each do |attr| %> + <% ua.get_enabled_attribute_models.each do |attr| %> <% value = user_attribute.attribute_values.detect{|v| v.key == attr.key } %> <%= hidden_field_tag "user[user_#{type}s][][attributes][][id]", value.id rescue nil %> <%= hidden_field_tag "user[user_#{type}s][][attributes][][key]", attr.key %> diff --git a/lib/tasks/dev.rake b/lib/tasks/dev.rake index 012b6478..21b1880b 100644 --- a/lib/tasks/dev.rake +++ b/lib/tasks/dev.rake @@ -32,15 +32,15 @@ namespace :dev do var_11 = I18nVariable.create!( :document_class => 'Page', :key => 'about', :en => 'About', :zh_tw => '關於我們' ) var_12 = I18nVariable.create!( :document_class => 'Link', :key => 'google', :en => 'Google', :zh_tw => 'Google' ) - urm_1 = UserRoleModel.new( :key => 'teacher', :i18n_variable_id => var_1.id ) - urm_1.attribute_models.build( :key => 'discipline', :locale => true, :i18n_variable_id => var_2.id, :markup => 'text_field', :list_options => [] ) - urm_1.attribute_models.build( :key => 'department', :locale => true, :i18n_variable_id => var_3.id, :markup => 'text_field', :list_options => [] ) + urm_1 = UserRoleModel.new( :key => 'teacher', :i18n_variable_id => var_1.id, :built_in => true ) + urm_1.attribute_models.build( :key => 'discipline', :locale => true, :i18n_variable_id => var_2.id, :markup => 'text_field', :list_options => [], :built_in => true ) + urm_1.attribute_models.build( :key => 'department', :locale => true, :i18n_variable_id => var_3.id, :markup => 'text_field', :list_options => [], :built_in => true ) urm_1.save! urm_2 = UserRoleModel.new( :key => 'student', :i18n_variable_id => var_4.id ) urm_2.attribute_models.build( :key => 'department', :locale => true, :i18n_variable_id => var_5.id, :markup => 'text_field', :list_options => [] ) urm_2.attribute_models.build( :key => 'major', :locale => true, :i18n_variable_id => var_6.id, :markup => 'text_field', :list_options => [] ) urm_2.save! - uim_1 = UserInfoModel.new( :key => 'profile', :i18n_variable_id => var_7.id ) + uim_1 = UserInfoModel.new( :key => 'profile', :i18n_variable_id => var_7.id, :built_in => true ) uim_1.attribute_models.build( :key => 'family_name', :locale => true, :i18n_variable_id => var_8.id, :markup => 'text_field', :list_options => [] ) uim_1.attribute_models.build( :key => 'first_name', :locale => true, :i18n_variable_id => var_9.id, :markup => 'text_field', :list_options => [] ) uim_1.save!
<%= attr.i18n_variable[I18n.locale] %> diff --git a/app/views/panel/users/_user_attribute.html.erb b/app/views/panel/users/_user_attribute.html.erb index a0005aee..7eeb9b15 100644 --- a/app/views/panel/users/_user_attribute.html.erb +++ b/app/views/panel/users/_user_attribute.html.erb @@ -12,7 +12,7 @@ <% end %>