From 2ccc2c9b39f779842c829f8cadd9f462157e910d Mon Sep 17 00:00:00 2001 From: chris Date: Tue, 1 Feb 2011 10:46:26 +0800 Subject: [PATCH] add user_info --- .../admin/translations_controller.rb | 4 +- .../admin/user_attribute_models_controller.rb | 30 +++++------ app/controllers/panel/users_controller.rb | 6 +-- app/models/attribute_attr_model.rb | 50 ------------------- app/models/user.rb | 45 +++++++++-------- app/models/user_attribute.rb | 9 ---- app/models/user_attribute_model.rb | 41 +++------------ .../_edit_user_attribute_model.html.erb | 8 +-- .../_user_attribute_model.html.erb | 8 +-- app/views/admin/translations/edit.html.erb | 6 +-- app/views/admin/translations/index.html.erb | 6 +-- .../_attribute_attr_model.html.erb | 18 +++---- .../user_attribute_models/_form.html.erb | 18 +++---- .../admin/user_attribute_models/edit.html.erb | 6 +-- .../user_attribute_models/index.html.erb | 18 +++---- .../admin/user_attribute_models/new.html.erb | 8 +-- app/views/layouts/admin.html.erb | 2 +- .../panel/users/_attribute_attr.html.erb | 6 +-- app/views/panel/users/_form.html.erb | 18 +++---- .../panel/users/_user_attribute.html.erb | 14 +++--- app/views/panel/users/show.html.erb | 8 +-- 21 files changed, 123 insertions(+), 206 deletions(-) delete mode 100644 app/models/attribute_attr_model.rb delete mode 100644 app/models/user_attribute.rb diff --git a/app/controllers/admin/translations_controller.rb b/app/controllers/admin/translations_controller.rb index 23a512e5..f4e5cead 100644 --- a/app/controllers/admin/translations_controller.rb +++ b/app/controllers/admin/translations_controller.rb @@ -69,8 +69,8 @@ class Admin::TranslationsController < ApplicationController result << var if var.document_class.eql?('language') result end - @user_attribute_model_i18n_variables = @i18n_variables.inject([]) do |result, var| - result << var if var.document_class.eql?('UserAttributeModel') + @user_role_model_i18n_variables = @i18n_variables.inject([]) do |result, var| + result << var if var.document_class.eql?('UserRoleModel') result end end diff --git a/app/controllers/admin/user_attribute_models_controller.rb b/app/controllers/admin/user_attribute_models_controller.rb index 18117c44..7455d0e3 100644 --- a/app/controllers/admin/user_attribute_models_controller.rb +++ b/app/controllers/admin/user_attribute_models_controller.rb @@ -1,46 +1,46 @@ -class Admin::UserAttributeModelsController < ApplicationController +class Admin::UserRoleModelsController < ApplicationController layout "admin" before_filter :authenticate_user! before_filter :is_admin? def index - @user_attribute_models = UserAttributeModel.all.entries + @user_role_models = UserRoleModel.all.entries end def show - @user_attribute_model = UserAttributeModel.find(params[:id]) + @user_role_model = UserRoleModel.find(params[:id]) end def new - @user_attribute_model = UserAttributeModel.new - session[:last_page] = get_go_back || admin_user_attribute_models_url + @user_role_model = UserRoleModel.new + session[:last_page] = get_go_back || admin_user_role_models_url end def edit - @user_attribute_model = UserAttributeModel.find(params[:id]) - @i18n_variable = @user_attribute_model.i18n_variable - session[:last_page] = get_go_back || admin_user_attribute_models_url + @user_role_model = UserRoleModel.find(params[:id]) + @i18n_variable = @user_role_model.i18n_variable + session[:last_page] = get_go_back || admin_user_role_models_url end def create - @user_attribute_model = UserAttributeModel.new(params[:user_attribute_model]) - @user_attribute_model.save + @user_role_model = UserRoleModel.new(params[:user_role_model]) + @user_role_model.save redirect_to :action => :index end def update - @user_attribute_model = UserAttributeModel.find(params[:id]) - @user_attribute_model.update_attributes(params[:user_attribute_model]) + @user_role_model = UserRoleModel.find(params[:id]) + @user_role_model.update_attributes(params[:user_role_model]) redirect_to :action => :index end def destroy - @user_attribute_model = UserAttributeModel.find(params[:id]) - @user_attribute_model.destroy + @user_role_model = UserRoleModel.find(params[:id]) + @user_role_model.destroy - @user_attribute_model.destroy_i18n_variables + @user_role_model.destroy_i18n_variables redirect_to :action => :index end diff --git a/app/controllers/panel/users_controller.rb b/app/controllers/panel/users_controller.rb index 0b481413..27c196dc 100644 --- a/app/controllers/panel/users_controller.rb +++ b/app/controllers/panel/users_controller.rb @@ -13,7 +13,7 @@ class Panel::UsersController < ApplicationController def new @user = User.new - @user_attribute_models = UserAttributeModel.all.entries + @user_role_models = UserRoleModel.all.entries session[:last_page] = get_go_back || panel_users_url end @@ -28,7 +28,7 @@ class Panel::UsersController < ApplicationController def edit @user = User.find(params[:id]) - @user_attribute_models = UserAttributeModel.all.entries + @user_role_models = UserRoleModel.all.entries session[:last_page] = get_go_back || panel_users_url end @@ -42,7 +42,7 @@ class Panel::UsersController < ApplicationController if @user.update_attributes(params[:user]) redirect_to :action => :index else - @user_attribute_models = UserAttributeModel.all.entries + @user_role_models = UserRoleModel.all.entries render :action => :edit end end diff --git a/app/models/attribute_attr_model.rb b/app/models/attribute_attr_model.rb deleted file mode 100644 index 8e26d274..00000000 --- a/app/models/attribute_attr_model.rb +++ /dev/null @@ -1,50 +0,0 @@ -class AttributeAttrModel - - include Mongoid::Document - - field :key - field :i18n_variable_id, :type => BSON::ObjectId, :index => true - field :markup - field :locale, :type => Boolean - field :list_options, :type => Array - - embedded_in :user_attribute_model, :inverse_of => :attribute_attr_models - validates_uniqueness_of :key - - # Destroy the i18n_variable - def destroy_i18n_variable - self.i18n_variable.destroy rescue nil - end - - # Get the i18n_variable - def i18n_variable - @i18n_variable ||= I18nVariable.find(self.i18n_variable_id) rescue nil - end - - # Update or create the i18n_variable record - def i18n_variable=(attr) - if self.i18n_variable_id - self.i18n_variable.update_attributes(attr) - else - var = I18nVariable.new(attr.merge({:key => self.key, :document_class => self.class, :parent_id => self.user_attribute_model.i18n_variable_id})) - var.save - self.i18n_variable_id = var.id - end - end - - # Convert the string list_options into an array - def select_list_options=(var) - self.list_options = var.gsub(' ', '').split(',') - end - - # Convert the array list_options into a string - def select_list_options - self.list_options.to_a.join(', ') - end - - # Check if the attribute_attr is set to be destroyed - def should_destroy? - should_destroy.to_i == 1 rescue nil - end - -end diff --git a/app/models/user.rb b/app/models/user.rb index e0b2a42d..35280cb8 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -9,46 +9,47 @@ class User field :admin, :type => Boolean, :default => true field :active_attributes, :type => Array - embeds_many :user_attributes + embeds_one :user_info + embeds_many :user_roles before_update :clean_active_attributes - # Update or create the user_attribute records - def user_attributes=(*attrs) - attrs[0].each do |attributes| - if attributes[:id].blank? - user_attributes.build(attributes) + # Update or create the user_role records + def user_roles=(*attrs) + attrs[0].each do |roles| + if roles[:id].blank? + user_roles.build(roles) else - user_attribute = user_attributes.detect {|a| a.id.to_s == attributes[:id].to_s } - user_attribute.update_attributes(attributes) + user_role = user_roles.detect {|a| a.id.to_s == roles[:id].to_s } + user_role.update_roles(roles) end end end - # Get an user_attribute from model key - def get_attribute_from_model_key(key) - self.user_attributes.detect {|a| a.key.to_s == key.to_s } + # Get an user_role from model key + def get_role_from_model_key(key) + self.user_roles.detect {|a| a.key.to_s == key.to_s } end - # Get the active user_attribute_models - def get_active_attribute_models - self.active_attributes.map{ |attr| get_attribute_model(attr) } rescue [] + # Get the active user_role_models + def get_active_role_models + self.active_roles.map{ |role| get_role_model(role) } rescue [] end - # Get an user_attribute_model from key - def get_attribute_model(key) - UserAttributeModel.first(:conditions => {:key => key}) + # Get an user_role_model from key + def get_role_model(key) + UserRoleModel.first(:conditions => {:key => key}) end - # Get the active attributes names or default to '-' - def get_attributes - (self.active_attributes.nil? || self.active_attributes.empty?) ? '-' : self.active_attributes.map{|attr| I18nVariable.first(:conditions => {:key => attr})[I18n.locale] rescue attr}.join(' / ') + # Get the active roles names or default to '-' + def get_roles + (self.active_roles.nil? || self.active_roles.empty?) ? '-' : self.active_roles.map{|role| I18nVariable.first(:conditions => {:key => role})[I18n.locale] rescue role}.join(' / ') end protected # Remove empty values - def clean_active_attributes - self.active_attributes.delete('') if self.active_attributes + def clean_active_roles + self.active_roles.delete('') if self.active_roles end end diff --git a/app/models/user_attribute.rb b/app/models/user_attribute.rb deleted file mode 100644 index 94c7e251..00000000 --- a/app/models/user_attribute.rb +++ /dev/null @@ -1,9 +0,0 @@ -class UserAttribute - - include Mongoid::Document - - field :key - - embedded_in :user, :inverse_of => :user_attributes - -end \ No newline at end of file diff --git a/app/models/user_attribute_model.rb b/app/models/user_attribute_model.rb index eb102a0a..2ba0cacd 100644 --- a/app/models/user_attribute_model.rb +++ b/app/models/user_attribute_model.rb @@ -5,53 +5,28 @@ class UserAttributeModel field :key field :i18n_variable_id, :type => BSON::ObjectId, :index => true - embeds_many :attribute_attr_models + embeds_many :attribute_models after_update :destroy_attrs - # Update or create the attribute_attr_model records - def attribute_attr_models=(*attrs) + # Update or create the attribute_model records + def attribute_models=(*attrs) attrs[0].each do |attributes| if attributes[:id].blank? - attribute_attr_models.build(attributes) + attribute_models.build(attributes) else - attribute_attr_model = attribute_attr_models.detect {|a| a.id.to_s == attributes[:id].to_s } - attribute_attr_model.update_attributes(attributes) + attribute_model = attribute_models.detect {|a| a.id.to_s == attributes[:id].to_s } + attribute_model.update_attributes(attributes) end end end - # Destroy the i18n_variables - def destroy_i18n_variables - self.i18n_variable.destroy rescue nil - self.attribute_attr_models.each do |attr| - attr.destroy_i18n_variable - end - end - - # Update or create the i18n_variable record - def i18n_variable=(attr) - if self.i18n_variable_id - self.i18n_variable.update_attributes(attr) rescue nil - else - var = I18nVariable.new(attr.merge({:key => self.key, :document_class => self.class})) - var.save - self.i18n_variable_id = var.id - end - end - - # Get the i18n_variable - def i18n_variable - @i18n_variable ||= I18nVariable.find(self.i18n_variable_id) rescue nil - end - protected - # Destroy the attribute_attr_models if marked to destroy + # Destroy the i18n_variable for each attribute_models if marked to destroy def destroy_attrs - attribute_attr_models.each do |a| + attribute_models.each do |a| if a.should_destroy? - a.destroy a.destroy_i18n_variable end end diff --git a/app/views/admin/translations/_edit_user_attribute_model.html.erb b/app/views/admin/translations/_edit_user_attribute_model.html.erb index 829d1c11..899ff745 100644 --- a/app/views/admin/translations/_edit_user_attribute_model.html.erb +++ b/app/views/admin/translations/_edit_user_attribute_model.html.erb @@ -1,11 +1,11 @@ <% @vars = @i18n_variables.inject([]) do |result, var| - result << var if var.parent_id.to_s.eql?(edit_user_attribute_model.id.to_s) + result << var if var.parent_id.to_s.eql?(edit_user_role_model.id.to_s) result end %>
- <%= edit_user_attribute_model.key %> + <%= edit_user_role_model.key %> <%#= render :partial => 'action_bar' %> @@ -16,9 +16,9 @@ end %> <% end %> - + <% @site_valid_locales.each do |locale| %> - + <% end %> diff --git a/app/views/admin/translations/_user_attribute_model.html.erb b/app/views/admin/translations/_user_attribute_model.html.erb index 1d7f0eca..b326021a 100644 --- a/app/views/admin/translations/_user_attribute_model.html.erb +++ b/app/views/admin/translations/_user_attribute_model.html.erb @@ -1,11 +1,11 @@ <% @vars = @i18n_variables.inject([]) do |result, var| - result << var if var.parent_id.to_s.eql?(user_attribute_model.id.to_s) + result << var if var.parent_id.to_s.eql?(user_role_model.id.to_s) result end %>
- <%= user_attribute_model.key %> + <%= user_role_model.key %> <%#= render :partial => 'action_bar' %>
<%= edit_user_attribute_model.key %><%= edit_user_role_model.key %><%= text_field_tag "i18n_variables[#{edit_user_attribute_model.id}][#{locale}]", edit_user_attribute_model[locale] %><%= text_field_tag "i18n_variables[#{edit_user_role_model.id}][#{locale}]", edit_user_role_model[locale] %>
@@ -16,9 +16,9 @@ end %> <% end %> - + <% @site_valid_locales.each do |locale| %> - + <% end %> diff --git a/app/views/admin/translations/edit.html.erb b/app/views/admin/translations/edit.html.erb index 3151d972..d3ca2971 100644 --- a/app/views/admin/translations/edit.html.erb +++ b/app/views/admin/translations/edit.html.erb @@ -10,13 +10,13 @@ <% end %> - <% if @user_attribute_model_i18n_variables.size > 0 %> + <% if @user_role_role_i18n_variables.size > 0 %>
- <%= t('admin.user_attribute') %> + <%= t('admin.user_role') %> <%#= render :partial => 'action_bar' %> -
<%= render :partial => 'edit_user_attribute_model', :collection => @user_attribute_model_i18n_variables %>
+
<%= render :partial => 'edit_user_role_model', :collection => @user_role_model_i18n_variables %>
<% end %> diff --git a/app/views/admin/translations/index.html.erb b/app/views/admin/translations/index.html.erb index dac5457f..5b24ec02 100644 --- a/app/views/admin/translations/index.html.erb +++ b/app/views/admin/translations/index.html.erb @@ -42,13 +42,13 @@ <% end %> -<% if @user_attribute_model_i18n_variables.size > 0 %> +<% if @user_role_model_i18n_variables.size > 0 %>
- <%= t('admin.user_attribute') %> + <%= t('admin.user_role') %> <%#= render :partial => 'action_bar' %> -
<%= render :partial => 'user_attribute_model', :collection => @user_attribute_model_i18n_variables %>
+
<%= render :partial => 'user_role_model', :collection => @user_role_model_i18n_variables %>
<% end %> diff --git a/app/views/admin/user_attribute_models/_attribute_attr_model.html.erb b/app/views/admin/user_attribute_models/_attribute_attr_model.html.erb index 1a8ddea7..34f572fd 100644 --- a/app/views/admin/user_attribute_models/_attribute_attr_model.html.erb +++ b/app/views/admin/user_attribute_models/_attribute_attr_model.html.erb @@ -1,27 +1,27 @@ -<%= fields_for 'user_attribute_model[attribute_attr_models][]', attribute_attr_model, :index => nil do |f| %> +<%= fields_for 'user_role_model[attribute_models][]', attribute_model, :index => nil do |f| %> - + <% @site_valid_locales.each do |locale| %> - + <% end %> - + <% @site_valid_locales.each do |locale| %> <% end %>
<%= user_attribute_model.key %><%= user_role_model.key %><%= user_attribute_model[locale] %><%= user_role_model[locale] %>
<%= attribute_attr_model.new_record? ? (f.text_field :key, {:style => "width:130px"}) : attribute_attr_model.key %><%= attribute_model.new_record? ? (f.text_field :key, {:style => "width:130px"}) : attribute_model.key %> - <%= check_box_tag "locale[]", value = '', attribute_attr_model.locale.nil? ? true : attribute_attr_model.locale %> - <%= hidden_field_tag "user_attribute_model[attribute_attr_models][][locale]", attribute_attr_model.locale.nil? ? true : attribute_attr_model.locale %> + <%= check_box_tag "locale[]", value = '', attribute_model.locale.nil? ? true : attribute_model.locale %> + <%= hidden_field_tag "user_model[attribute_models][][locale]", attribute_model.locale.nil? ? true : attribute_model.locale %> <%= text_field_tag "user_attribute_model[attribute_attr_models][][i18n_variable][#{locale}]", (attribute_attr_model.i18n_variable[locale] rescue nil), :style => "width:130px" %><%= text_field_tag "user_role_model[attribute_models][][i18n_variable][#{locale}]", (attribute_model.i18n_variable[locale] rescue nil), :style => "width:130px" %> <%= f.select :markup, LIST[:markups], {}, {:style => "width:90px"} %> -
> +
> <%= t('admin.options') %>: - <%= text_field_tag "user_attribute_model[attribute_attr_models][][select_list_options]", attribute_attr_model.select_list_options, :style => "width:130px" %> + <%= text_field_tag "user_role_model[attribute_models][][select_list_options]", attribute_model.select_list_options, :style => "width:130px" %>
- <% if attribute_attr_model.new_record? %> + <% if attribute_model.new_record? %> (<%= t(:delete) %>) <% else %> (<%= t(:delete) %>) - <%= hidden_field_tag "user_attribute_model[attribute_attr_models][][should_destroy]", nil , :class => 'should_destroy' %> + <%= hidden_field_tag "user_role_model[attribute_models][][should_destroy]", nil , :class => 'should_destroy' %> <%= 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 7fa6f952..e32d6d11 100644 --- a/app/views/admin/user_attribute_models/_form.html.erb +++ b/app/views/admin/user_attribute_models/_form.html.erb @@ -6,21 +6,21 @@ <% end %>
<%= is_new ? (f.text_field :key, {:style => "width:130px"}) : @user_attribute_model.key %><%= is_new ? (f.text_field :key, {:style => "width:130px"}) : @user_role_model.key %> - <%= text_field_tag "user_attribute_model[i18n_variable][#{locale}]", (@i18n_variable[locale] if @i18n_variable), :style => "width:130px" %> + <%= text_field_tag "user_role_model[i18n_variable][#{locale}]", (@i18n_variable[locale] if @i18n_variable), :style => "width:130px" %>
-
+
<%= t('admin.attributes')%> - +
@@ -30,7 +30,7 @@ - <%= render :partial => 'attribute_attr_model', :collection => @user_attribute_model.attribute_attr_models %> + <%= render :partial => 'attribute_model', :collection => @user_role_model.attribute_models %>
<%= t('admin.key') %> <%= t('admin.multilingual') %><%= t('admin.type')%>
(<%= t(:add) %>) @@ -40,15 +40,15 @@ <% content_for :page_specific_javascript do %> diff --git a/app/views/panel/users/_user_attribute.html.erb b/app/views/panel/users/_user_attribute.html.erb index 90e9960f..8c5db101 100644 --- a/app/views/panel/users/_user_attribute.html.erb +++ b/app/views/panel/users/_user_attribute.html.erb @@ -1,34 +1,34 @@