Modifications of the user model
This commit is contained in:
		
							parent
							
								
									1909edbe2a
								
							
						
					
					
						commit
						509c106b3e
					
				| 
						 | 
				
			
			@ -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
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -0,0 +1,10 @@
 | 
			
		|||
class AttributeValue
 | 
			
		||||
  
 | 
			
		||||
  include Mongoid::Document
 | 
			
		||||
  include Mongoid::Timestamps
 | 
			
		||||
  
 | 
			
		||||
  field :key
 | 
			
		||||
  
 | 
			
		||||
  belongs_to :user_attribute
 | 
			
		||||
  
 | 
			
		||||
end
 | 
			
		||||
| 
						 | 
				
			
			@ -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 }
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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
 | 
			
		||||
| 
						 | 
				
			
			@ -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
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -13,20 +13,24 @@
 | 
			
		|||
    </tr>
 | 
			
		||||
    
 | 
			
		||||
    <% 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 %>
 | 
			
		||||
      <tr>
 | 
			
		||||
        <td><%= attr.i18n_variable[I18n.locale] %></td>
 | 
			
		||||
        <% if attr["locale"] && attr["markup"] == 'text_field' %>
 | 
			
		||||
					<%= hidden_field_tag "user[user_#{type}s][][attributes][][non_locale]", nil %>
 | 
			
		||||
          <% @site_valid_locales.each do |locale| %>
 | 
			
		||||
            <td>
 | 
			
		||||
              <%= text_field_tag "user[user_#{type}s][][#{attr.key}_#{locale}]", (user_attribute["#{attr.key}_#{locale}"] rescue nil) %>
 | 
			
		||||
              <%= text_field_tag "user[user_#{type}s][][attributes][][#{locale}]", (value[locale] rescue nil) %>
 | 
			
		||||
            </td>
 | 
			
		||||
          <% end -%> 
 | 
			
		||||
        <% else %>
 | 
			
		||||
          <td colspan=<%= @site_valid_locales.size %>>
 | 
			
		||||
            <% if attr["markup"] == "text_field"  %>
 | 
			
		||||
              <%= text_field_tag "user[user_#{type}s][][#{attr.key}]", (user_attribute[attr.key] rescue nil) %>
 | 
			
		||||
              <%= text_field_tag "user[user_#{type}s][][attributes][][non_locale]", (value['non_locale'] rescue nil) %>
 | 
			
		||||
            <% elsif attr["markup"] == "select" %>
 | 
			
		||||
              <%= select_tag "user[user_#{type}s][][#{attr.key}]", options_for_select(attr["options"], user_attribute[attr.key]) %>
 | 
			
		||||
              <%= select_tag "user[user_#{type}s][][attributes][][non_locale]", options_for_select(attr["options"], value['non_locale']) %>
 | 
			
		||||
            <% end -%>
 | 
			
		||||
          </td>        
 | 
			
		||||
        <% end -%>
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue