diff --git a/app/controllers/panel/users_controller.rb b/app/controllers/panel/users_controller.rb index b74263a2..d0b177df 100644 --- a/app/controllers/panel/users_controller.rb +++ b/app/controllers/panel/users_controller.rb @@ -21,6 +21,14 @@ class Panel::UsersController < ApplicationController def create @user = User.new(params[:user]) if @user.save + @user.user_infos.each do |user_info| + user_info.save + user_info.attribute_values.each(&:save) + end + @user.user_roles.each do |user_role| + user_role.save + user_role.attribute_values.each(&:save) + end flash[:notice] = t('panel.create_success_user') redirect_to :action => :index else diff --git a/app/models/attribute_value.rb b/app/models/attribute_value.rb new file mode 100644 index 00000000..877431f0 --- /dev/null +++ b/app/models/attribute_value.rb @@ -0,0 +1,10 @@ +class AttributeValue + + include Mongoid::Document + include Mongoid::Timestamps + + field :key + + belongs_to :user_attribute + +end diff --git a/app/models/user.rb b/app/models/user.rb index a86890de..5e047415 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -10,8 +10,8 @@ class User field :admin, :type => Boolean, :default => true field :active_roles, :type => Array - embeds_many :user_roles - embeds_many :user_infos + has_many :user_roles + has_many :user_infos before_save :clean_active_roles # Update or create the user_role records @@ -26,6 +26,18 @@ class User end end + # Update or create the user_info records + def user_infos=(*attrs) + attrs[0].each do |infos| + if infos[:id].blank? + user_infos.build(infos) + else + user_info = user_infos.detect {|a| a.id.to_s == infos[:id].to_s } + user_info.update_attributes(infos) + end + end + end + # Get an user_info from model key def get_info_from_model_key(key) self.user_infos.detect {|a| a.key.to_s == key.to_s } diff --git a/app/models/user_attribute.rb b/app/models/user_attribute.rb new file mode 100644 index 00000000..002ec4be --- /dev/null +++ b/app/models/user_attribute.rb @@ -0,0 +1,21 @@ +class UserAttribute + + include Mongoid::Document + include Mongoid::Timestamps + + field :key + has_many :attribute_values + + # Update or create the attribute_value records + def attributes=(*args) + args[0].each do |value| + if value[:id].blank? + attribute_values.build(value) + else + attribute_value = attribute_values.detect {|a| a.id.to_s == value[:id].to_s } + attribute_value.update_attributes(value) + end + end + end + +end diff --git a/app/models/user_info.rb b/app/models/user_info.rb index 55252311..f216c83a 100644 --- a/app/models/user_info.rb +++ b/app/models/user_info.rb @@ -1,10 +1,5 @@ -class UserInfo +class UserInfo < UserAttribute - include Mongoid::Document - include Mongoid::Timestamps - - field :key - - embedded_in :user, :inverse_of => :user_infos + belongs_to :user end diff --git a/app/models/user_role.rb b/app/models/user_role.rb index 6bd6ccf1..1bc23e93 100644 --- a/app/models/user_role.rb +++ b/app/models/user_role.rb @@ -1,10 +1,5 @@ -class UserRole +class UserRole < UserAttribute - include Mongoid::Document - include Mongoid::Timestamps - - field :key - - embedded_in :user, :inverse_of => :user_roles + belongs_to :user end diff --git a/app/views/panel/users/_user_attribute.html.erb b/app/views/panel/users/_user_attribute.html.erb index a31c3100..a0005aee 100644 --- a/app/views/panel/users/_user_attribute.html.erb +++ b/app/views/panel/users/_user_attribute.html.erb @@ -13,20 +13,24 @@ <% ua.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 %>