From afbffe37d52e98b5660105f589503c9ab671f1ca Mon Sep 17 00:00:00 2001 From: chris2tof Date: Tue, 20 Dec 2011 14:35:28 +0800 Subject: [PATCH] Users can now be saved with sub_roles --- app/assets/javascripts/user_form.js | 6 ++- app/controllers/admin/roles_controller.rb | 2 - app/controllers/admin/users_controller.rb | 13 +----- app/models/user/attribute_field.rb | 2 +- app/models/user/attribute_value.rb | 4 ++ app/models/user/sub_role.rb | 2 +- app/models/user/user.rb | 10 ++++- app/views/admin/users/_form.html.erb | 26 +++++------ app/views/admin/users/_info.html.erb | 40 +++++++++++++++++ .../admin/users/_show_attribute.html.erb | 2 +- app/views/admin/users/_show_sub_role.html.erb | 4 ++ app/views/admin/users/_sub_role.html.erb | 37 ++++++++++++++++ .../admin/users/_sub_role_selector.html.erb | 4 ++ .../admin/users/_user_attribute.html.erb | 44 ------------------- app/views/admin/users/show.html.erb | 11 ++--- lib/tasks/dev.rake | 6 +-- 16 files changed, 125 insertions(+), 88 deletions(-) create mode 100644 app/views/admin/users/_info.html.erb create mode 100644 app/views/admin/users/_show_sub_role.html.erb create mode 100644 app/views/admin/users/_sub_role.html.erb create mode 100644 app/views/admin/users/_sub_role_selector.html.erb delete mode 100644 app/views/admin/users/_user_attribute.html.erb diff --git a/app/assets/javascripts/user_form.js b/app/assets/javascripts/user_form.js index 168f5437..183d8270 100644 --- a/app/assets/javascripts/user_form.js +++ b/app/assets/javascripts/user_form.js @@ -1,6 +1,10 @@ $(document).ready(function(){ - $('#active_role').change(function(){ + $('#user_role_id').change(function(){ $('#roles .attributes_block').hide(); $('#'+this.value+'_block').toggle(); }); + + $('.sub_role_select').change(function(){ + $('#sub_role_' + this.value).toggle(); + }); }); \ No newline at end of file diff --git a/app/controllers/admin/roles_controller.rb b/app/controllers/admin/roles_controller.rb index f0321368..65c86f36 100644 --- a/app/controllers/admin/roles_controller.rb +++ b/app/controllers/admin/roles_controller.rb @@ -26,14 +26,12 @@ class Admin::RolesController < ApplicationController end def create - puts params.to_yaml @attribute = Role.new(params[:role]) @attribute.save redirect_to :action => :index end def update - puts params.to_yaml @attribute = Role.find(params[:id]) @attribute.update_attributes(params[:role]) respond_to do |format| diff --git a/app/controllers/admin/users_controller.rb b/app/controllers/admin/users_controller.rb index b8df3312..f7135beb 100644 --- a/app/controllers/admin/users_controller.rb +++ b/app/controllers/admin/users_controller.rb @@ -19,16 +19,9 @@ class Admin::UsersController < ApplicationController end def create + puts params.to_yaml @user = User.new(params[:user]) if @user.save - @user.infos.each do |info| - info.save - info.attribute_values.each(&:save) - end - @user.roles.each do |role| - role.save - role.attribute_values.each(&:save) - end flash[:notice] = t('admin.create_success_user') redirect_to :action => :index else @@ -66,10 +59,6 @@ class Admin::UsersController < ApplicationController else @user = User.find(params[:id]) @user.destroy - @user.attributes.each do |ua| - ua.attribute_values.each(&:destroy) - ua.destroy - end end redirect_to :action => :index diff --git a/app/models/user/attribute_field.rb b/app/models/user/attribute_field.rb index 7ea385a9..42bbf910 100644 --- a/app/models/user/attribute_field.rb +++ b/app/models/user/attribute_field.rb @@ -14,7 +14,7 @@ class AttributeField has_one :i18n_variable, :as => :language_value, :autosave => true, :dependent => :destroy has_many :attribute_values - validates_uniqueness_of :key + # validates_uniqueness_of :key # Convert the string list_options into an array def select_list_options=(var) diff --git a/app/models/user/attribute_value.rb b/app/models/user/attribute_value.rb index 8b15bf3d..47c84085 100644 --- a/app/models/user/attribute_value.rb +++ b/app/models/user/attribute_value.rb @@ -8,4 +8,8 @@ class AttributeValue belongs_to :attribute_field belongs_to :user + def method_missing(field) + self[field] + end + end diff --git a/app/models/user/sub_role.rb b/app/models/user/sub_role.rb index 63b8cf92..cdefb5f5 100644 --- a/app/models/user/sub_role.rb +++ b/app/models/user/sub_role.rb @@ -1,7 +1,7 @@ class SubRole < Attribute belongs_to :role - has_many :users + has_and_belongs_to_many :users # Get an sub_role from key def self.get_sub_role_from_key(key) diff --git a/app/models/user/user.rb b/app/models/user/user.rb index a06233d1..10b874ad 100644 --- a/app/models/user/user.rb +++ b/app/models/user/user.rb @@ -12,7 +12,7 @@ class User has_many :attribute_values, :autosave => true, :dependent => :destroy belongs_to :role - belongs_to :sub_role + has_and_belongs_to_many :sub_roles accepts_nested_attributes_for :attribute_values, :allow_destroy => true def name @@ -27,7 +27,13 @@ class User end def get_attribute_values - @attribute_values ||= self.attribute_values.to_a + @attribute_values ||= self.attribute_values + end + + def get_value_from_field_id(field_id) + values = get_attribute_values + value = values.detect {|value| value.attribute_field_id == field_id} rescue nil + value ? value : self.attribute_values.build end end diff --git a/app/views/admin/users/_form.html.erb b/app/views/admin/users/_form.html.erb index 5c57284b..602afc2d 100644 --- a/app/views/admin/users/_form.html.erb +++ b/app/views/admin/users/_form.html.erb @@ -16,32 +16,26 @@ -<% @infos.each do |ua| %> -
-

<%= ua.i18n_variable[I18n.locale] %>

-
- <%= render :partial => 'attribute', - :object => (@user.infos.detect {|u| u.key == ua.key } || @user.infos.build), - :locals => {:ua => ua, :type => 'info'} %> -
-<% end -%> + +<%= render :partial => 'info', :collection => @infos, :locals => {:f => f} %> +
- <% @roles.each do |ua| %> - + <% content_for :page_specific_javascript do %> <%= javascript_include_tag "user_form" %> <% end -%> diff --git a/app/views/admin/users/_info.html.erb b/app/views/admin/users/_info.html.erb new file mode 100644 index 00000000..a7d44ea9 --- /dev/null +++ b/app/views/admin/users/_info.html.erb @@ -0,0 +1,40 @@ +
+

<%= info.i18n_variable[I18n.locale] %>

+
+
+ + + + + <% @site_valid_locales.each do |locale| %> + + <% end %> + + + + <% info.get_enabled_attribute_fields.each do |attr| %> + <%= f.fields_for :attribute_values, @user.get_value_from_field_id(attr.id) do |f| %> + + + <% if attr["locale"] && attr["markup"] == 'text_field' %> + <% @site_valid_locales.each do |locale| %> + + <% end %> + <% else %> + + <% end -%> + <%= f.hidden_field :attribute_field_id, :value => attr.id %> + <%= f.hidden_field :key, :value => attr.key %> + + <% end %> + <% end %> + +
<%= t("admin.key") %><%= I18nVariable.first(:conditions => {:key => locale})[I18n.locale] %>
<%= attr.i18n_variable[I18n.locale] %><%= f.text_field locale %>> + <% if attr["markup"] == "text_field" %> + <%= f.text_field :non_locale %> + <% elsif attr["markup"] == "select" %> + <%= f.select :non_locale, options_for_select(attr["options"], value['non_locale']) %> + <% end -%> +
+
+
\ No newline at end of file diff --git a/app/views/admin/users/_show_attribute.html.erb b/app/views/admin/users/_show_attribute.html.erb index 22f89b93..ec33f84b 100644 --- a/app/views/admin/users/_show_attribute.html.erb +++ b/app/views/admin/users/_show_attribute.html.erb @@ -7,7 +7,7 @@ <% end %> - <% a.get_enabled_attribute_fields.each do |attr| %> + <% show_attribute.get_enabled_attribute_fields.each do |attr| %> <%= attr.i18n_variable[I18n.locale] %> <% @site_valid_locales.each do |locale| %> diff --git a/app/views/admin/users/_show_sub_role.html.erb b/app/views/admin/users/_show_sub_role.html.erb new file mode 100644 index 00000000..22d8b758 --- /dev/null +++ b/app/views/admin/users/_show_sub_role.html.erb @@ -0,0 +1,4 @@ +
+

<%= show_sub_role.i18n_variable[I18n.locale] %>

+ <%= render :partial => 'show_attribute', :object => show_sub_role %> +
\ No newline at end of file diff --git a/app/views/admin/users/_sub_role.html.erb b/app/views/admin/users/_sub_role.html.erb new file mode 100644 index 00000000..f55b8b48 --- /dev/null +++ b/app/views/admin/users/_sub_role.html.erb @@ -0,0 +1,37 @@ + \ No newline at end of file diff --git a/app/views/admin/users/_sub_role_selector.html.erb b/app/views/admin/users/_sub_role_selector.html.erb new file mode 100644 index 00000000..6d7924ca --- /dev/null +++ b/app/views/admin/users/_sub_role_selector.html.erb @@ -0,0 +1,4 @@ +<% role.sub_roles.each do |sub_role| %> + <%= sub_role.i18n_variable[I18n.locale] %> + <%= check_box_tag "user[sub_role_ids][]", sub_role.id, @user.sub_role_ids.include?(sub_role.id), :class => 'sub_role_select' %> +<% end %> \ No newline at end of file diff --git a/app/views/admin/users/_user_attribute.html.erb b/app/views/admin/users/_user_attribute.html.erb deleted file mode 100644 index 65225ce5..00000000 --- a/app/views/admin/users/_user_attribute.html.erb +++ /dev/null @@ -1,44 +0,0 @@ -<%= fields_for "user[#{type}s]", attribute, :index => nil do |f| %> - <%= f.hidden_field :key, :value => ua.key %> - <%= f.hidden_field :id unless attribute.new_record? %> - -
- - - - - <% @site_valid_locales.each do |locale| %> - - <% end %> - - - - <% ua.get_enabled_attribute_fields.each do |attr| %> - <% value = attribute.attribute_values.detect{|v| v.key == attr.key } %> - <%= hidden_field_tag "user[#{type}s][][attribute_values][][id]", value.id rescue nil %> - <%= hidden_field_tag "user[#{type}s][][attribute_values][][key]", attr.key %> - - - <% if attr["locale"] && attr["markup"] == 'text_field' %> - <%= hidden_field_tag "user[#{type}s][][attribute_values][][non_locale]", nil %> - <% @site_valid_locales.each do |locale| %> - - <% end -%> - <% else %> - - <% end -%> - - <% end %> - -
<%= t("admin.key") %><%= I18nVariable.first(:conditions => {:key => locale})[I18n.locale] %>
<%= attr.i18n_variable[I18n.locale] %> - <%= text_field_tag "user[#{type}s][][attribute_values][][#{locale}]", (value[locale] rescue nil) %> - > - <% if attr["markup"] == "text_field" %> - <%= text_field_tag "user[#{type}s][][attribute_values][][non_locale]", (value['non_locale'] rescue nil) %> - <% elsif attr["markup"] == "select" %> - <%= select_tag "user[#{type}s][][attribute_values][][non_locale]", options_for_select(attr["options"], value['non_locale']) %> - <% end -%> -
-
- -<% end %> diff --git a/app/views/admin/users/show.html.erb b/app/views/admin/users/show.html.erb index 2e8221b8..bbada72c 100644 --- a/app/views/admin/users/show.html.erb +++ b/app/views/admin/users/show.html.erb @@ -5,24 +5,25 @@
- <% if @user.avatar? %> - <%= image_tag(@user.avatar.thumb.url) %> + <% if @user.avatar? %> + <%= image_tag(@user.avatar.thumb.url) %> <% else %> - <%= image_tag "person.png" %> + <%= image_tag "person.png" %> <% end %> +
<% @infos.each do |a| %>

<%= a.i18n_variable[I18n.locale] %>


- <%= render :partial => 'show_attribute', :locals => {:a => a} %> + <%= render :partial => 'show_attribute', :object => a %>
<% end -%>

<%= @user.role.i18n_variable[I18n.locale] %>


- <%#= render :partial => 'show_attribute', :locals => {:a => active_role, :object => @user.get_role_from_key(active_role.key)} %> + <%= render :partial => 'show_sub_role', :collection => @user.sub_roles %>
<%= link_to t(:edit), edit_admin_user_path(@user) %> diff --git a/lib/tasks/dev.rake b/lib/tasks/dev.rake index cb376f9c..e397e202 100644 --- a/lib/tasks/dev.rake +++ b/lib/tasks/dev.rake @@ -46,7 +46,7 @@ namespace :dev do sr_1_1 = r_1.sub_roles.build( :key => 'good_teacher', :i18n_variable => var_1_1, :built_in => true ) sr_1_1.attribute_fields.build( :key => 'field', :locale => true, :i18n_variable => var_1_1_1, :markup => 'text_field', :list_options => [], :built_in => true ) sr_1_1.attribute_fields.build( :key => 'department', :locale => true, :i18n_variable => var_1_1_2, :markup => 'text_field', :list_options => [], :built_in => true ) - sr_1_2 = r_1.sub_roles.build( :key => 'bad_teacher', :i18n_variable => var_1_1 ) + sr_1_2 = r_1.sub_roles.build( :key => 'bad_teacher', :i18n_variable => var_1_2 ) sr_1_2.attribute_fields.build( :key => 'field', :locale => true, :i18n_variable => var_1_2_1, :markup => 'text_field', :list_options => [] ) sr_1_2.attribute_fields.build( :key => 'department', :locale => true, :i18n_variable => var_1_2_2, :markup => 'text_field', :list_options => [] ) sr_1_2.attribute_fields.build( :key => 'speciality', :locale => true, :i18n_variable => var_1_2_3, :markup => 'text_field', :list_options => [] ) @@ -124,7 +124,7 @@ namespace :dev do - user = User.create( :email => 'chris@rulingcom.com', :password => 'password', :password_confirmation => 'password', :admin => true, :role_id => r_1.id, :sub_role_id => sr_1_2.id ) + user = User.create( :email => 'chris@rulingcom.com', :password => 'password', :password_confirmation => 'password', :admin => true, :role_id => r_1.id, :sub_role_ids => [sr_1_2.id] ) AttributeValue.create( :user => user, :attribute_field => i_1.attribute_fields[0], :key => 'first_name', :en => 'Christophe', :zh_tw => '順發' ) AttributeValue.create( :user_id => user.id, :attribute_field_id => i_1.attribute_fields[1].id, :key => 'last_name', :en => 'Vilayphiou', :zh_tw => '林' ) AttributeValue.create( :user_id => user.id, :attribute_field_id => sr_1_2.attribute_fields[0].id, :key => 'field', :en => 'Computer Architecture', :zh_tw => '計算機系統結構' ) @@ -132,7 +132,7 @@ namespace :dev do AttributeValue.create( :user_id => user.id, :attribute_field_id => sr_1_2.attribute_fields[2].id, :key => 'speciality', :en => 'HSR', :zh_tw => '高鐵' ) - user = User.create( :email => 'matt@rulingcom.com', :password => 'password', :password_confirmation => 'password', :admin => true, :role_id => r_2.id, :sub_role_id => sr_2_1.id ) + user = User.create( :email => 'matt@rulingcom.com', :password => 'password', :password_confirmation => 'password', :admin => true, :role_id => r_2.id, :sub_role_ids => [sr_2_1.id ] ) AttributeValue.create( :user_id => user.id, :attribute_field_id => i_1.attribute_fields[0].id, :key => 'first_name', :en => 'Matt', :zh_tw => '儒淵' ) AttributeValue.create( :user_id => user.id, :attribute_field_id => i_1.attribute_fields[1].id, :key => 'last_name', :en => 'Fu', :zh_tw => '傅' ) AttributeValue.create( :user_id => user.id, :attribute_field_id => sr_2_1.attribute_fields[0].id, :key => 'major', :en => 'Information management', :zh_tw => '信息化管理' )