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 %>