Users can now be saved with sub_roles

This commit is contained in:
chris2tof committed 2011-12-20 14:35:28 +08:00
1 parent 4108ebf06d
commit afbffe37d5
16 files changed
+125 -88

No files matched your search

+5 -1
View File
@@ -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();
});
});
@@ -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|
+1 -12
View File
@@ -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
+1 -1
View File
@@ -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)
+4
View File
@@ -8,4 +8,8 @@ class AttributeValue
belongs_to :attribute_field
belongs_to :user
def method_missing(field)
self[field]
end
end
+1 -1
View File
@@ -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)
+8 -2
View File
@@ -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
+10 -16
View File
@@ -16,32 +16,26 @@
</label>
<label>
<span><%= t('admin.role') %>:</span>
<%= f.select :active_role, @roles.sort_by{|role| role.key }.collect {|role| [ role.i18n_variable[I18n.locale], role.key ]}, :selected => (@user.active_role || 'student') %>
<%= f.select :role_id, @roles.sort_by{|role| role.key }.collect {|role| [ role.i18n_variable[I18n.locale], role.id ]}, :selected => (@user.role_id || Role.get_role_from_key('student')) %>
</label>
</div>
<% @infos.each do |ua| %>
<div id="basic_block" class="attributes_block <%= @class %>">
<h2><%= ua.i18n_variable[I18n.locale] %></h2>
<hr>
<%= render :partial => 'attribute',
:object => (@user.infos.detect {|u| u.key == ua.key } || @user.infos.build),
:locals => {:ua => ua, :type => 'info'} %>
</div>
<% end -%>
<%= render :partial => 'info', :collection => @infos, :locals => {:f => f} %>
<div id='roles'>
<% @roles.each do |ua| %>
<div id="<%= ua.key %>_block" class="attributes_block <%= @class %>" <%= raw(' style="display: none;"') unless ((@user.active_role.eql?(ua.key) rescue nil) || (ua.key.eql?('student') && @user.new_record?)) %>>
<h2><%= ua.i18n_variable[I18n.locale] %></h2>
<% @roles.each do |role| %>
<div id="<%= role.id %>_block" class="attributes_block <%= @class %>" <%= raw(' style="display: none;"') unless ((@user.role.id.eql?(role.id) rescue nil) || (role.key.eql?('student') && @user.new_record?)) %>>
<h2><%= role.i18n_variable[I18n.locale] %></h2>
<hr>
<%= render :partial => 'attribute',
:object => (@user.roles.detect {|u| u.key == ua.key } || @user.roles.build),
:locals => {:ua => ua, :type => 'role'} %>
<%= render :partial => 'sub_role_selector', :locals => {:f => f, :role => role} %>
<%= render :partial => 'sub_role', :collection => role.sub_roles, :locals => {:f => f, :role => role} %>
</div>
<% end -%>
</div>
<% content_for :page_specific_javascript do %>
<%= javascript_include_tag "user_form" %>
<% end -%>
+40
View File
@@ -0,0 +1,40 @@
<div id="basic_block" class="attributes_block <%= @class %>">
<h2><%= info.i18n_variable[I18n.locale] %></h2>
<hr>
<div class="info_input">
<table border="0" cellspacing="0" cellpadding="0">
<thead class="list_head">
<tr>
<td><%= t("admin.key") %></td>
<% @site_valid_locales.each do |locale| %>
<td><%= I18nVariable.first(:conditions => {:key => locale})[I18n.locale] %></td>
<% end %>
</tr>
</thead>
<tbody>
<% info.get_enabled_attribute_fields.each do |attr| %>
<%= f.fields_for :attribute_values, @user.get_value_from_field_id(attr.id) do |f| %>
<tr class="list_item">
<td><%= attr.i18n_variable[I18n.locale] %></td>
<% if attr["locale"] && attr["markup"] == 'text_field' %>
<% @site_valid_locales.each do |locale| %>
<td><%= f.text_field locale %></td>
<% end %>
<% else %>
<td colspan=<%= @site_valid_locales.size %>>
<% 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 -%>
</td>
<% end -%>
<%= f.hidden_field :attribute_field_id, :value => attr.id %>
<%= f.hidden_field :key, :value => attr.key %>
</tr>
<% end %>
<% end %>
</tbody>
</table>
</div>
</div>
@@ -7,7 +7,7 @@
<% end %>
</thead>
<tbody>
<% a.get_enabled_attribute_fields.each do |attr| %>
<% show_attribute.get_enabled_attribute_fields.each do |attr| %>
<tr class="list_item">
<td><%= attr.i18n_variable[I18n.locale] %></td>
<% @site_valid_locales.each do |locale| %>
@@ -0,0 +1,4 @@
<div id="sub_role_<%= show_sub_role.id %>" class="info_input">
<h2><%= show_sub_role.i18n_variable[I18n.locale] %></h2>
<%= render :partial => 'show_attribute', :object => show_sub_role %>
</div>
+37
View File
@@ -0,0 +1,37 @@
<div id="sub_role_<%= sub_role.id %>" class="info_input" <%= raw(' style="display: none;"') unless @user.sub_role_ids.include?(sub_role.id) %>>
<h2><%= sub_role.i18n_variable[I18n.locale] %></h2>
<table border="0" cellspacing="0" cellpadding="0">
<thead class="list_head">
<tr>
<td><%= t("admin.key") %></td>
<% @site_valid_locales.each do |locale| %>
<td><%= I18nVariable.first(:conditions => {:key => locale})[I18n.locale] %></td>
<% end %>
</tr>
</thead>
<tbody>
<% sub_role.get_enabled_attribute_fields.each do |attr| %>
<%= f.fields_for :attribute_values, @user.get_value_from_field_id(attr.id) do |f| %>
<tr class="list_item">
<td><%= attr.i18n_variable[I18n.locale] %></td>
<% if attr["locale"] && attr["markup"] == 'text_field' %>
<% @site_valid_locales.each do |locale| %>
<td><%= f.text_field locale %></td>
<% end %>
<% else %>
<td colspan=<%= @site_valid_locales.size %>>
<% 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 -%>
</td>
<% end -%>
<%= f.hidden_field :attribute_field_id, :value => attr.id %>
<%= f.hidden_field :key, :value => attr.key %>
</tr>
<% end %>
<% end %>
</tbody>
</table>
</div>
@@ -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 %>
@@ -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? %>
<div class="info_input">
<table border="0" cellspacing="0" cellpadding="0">
<thead class="list_head">
<tr>
<td><%= t("admin.key") %></td>
<% @site_valid_locales.each do |locale| %>
<td><%= I18nVariable.first(:conditions => {:key => locale})[I18n.locale] %></td>
<% end %>
</tr>
</thead>
<tbody>
<% 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 %>
<tr class="list_item">
<td><%= attr.i18n_variable[I18n.locale] %></td>
<% if attr["locale"] && attr["markup"] == 'text_field' %>
<%= hidden_field_tag "user[#{type}s][][attribute_values][][non_locale]", nil %>
<% @site_valid_locales.each do |locale| %>
<td>
<%= text_field_tag "user[#{type}s][][attribute_values][][#{locale}]", (value[locale] rescue nil) %>
</td>
<% end -%>
<% else %>
<td colspan=<%= @site_valid_locales.size %>>
<% 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 -%>
</td>
<% end -%>
</tr>
<% end %>
</tbody>
</table>
</div>
<% end %>
+6 -5
View File
@@ -5,24 +5,25 @@
<div id="profile" class="clear">
<div id="person">
<% 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 %>
<label><span><%= @user.email %></span></label>
</div>
<% @infos.each do |a| %>
<div id="basic_block" class="attributes_block <%= @class %>">
<h2><%= a.i18n_variable[I18n.locale] %></h2>
<hr>
<%= render :partial => 'show_attribute', :locals => {:a => a} %>
<%= render :partial => 'show_attribute', :object => a %>
</div>
<% end -%>
<div id="<%= @user.role.key %>_block" class="attributes_block <%= @class %>">
<h2><%= @user.role.i18n_variable[I18n.locale] %></h2>
<hr>
<%#= 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 %>
</div>
<div class="button_bar">
<%= link_to t(:edit), edit_admin_user_path(@user) %>