Users can now be saved with sub_roles
This commit is contained in:
parent
4108ebf06d
commit
afbffe37d5
|
@ -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|
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -8,4 +8,8 @@ class AttributeValue
|
|||
belongs_to :attribute_field
|
||||
belongs_to :user
|
||||
|
||||
def method_missing(field)
|
||||
self[field]
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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 -%>
|
||||
|
|
|
@ -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>
|
|
@ -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 %>
|
|
@ -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) %>
|
||||
|
|
|
@ -46,7 +46,7 @@ namespace :dev do
|
|||
sr_1_1 = r_1.sub_roles.build( :key => 'good_teacher', :i18n_variable => var_1_1, :built_in => true )
|
||||
sr_1_1.attribute_fields.build( :key => 'field', :locale => true, :i18n_variable => var_1_1_1, :markup => 'text_field', :list_options => [], :built_in => true )
|
||||
sr_1_1.attribute_fields.build( :key => 'department', :locale => true, :i18n_variable => var_1_1_2, :markup => 'text_field', :list_options => [], :built_in => true )
|
||||
sr_1_2 = r_1.sub_roles.build( :key => 'bad_teacher', :i18n_variable => var_1_1 )
|
||||
sr_1_2 = r_1.sub_roles.build( :key => 'bad_teacher', :i18n_variable => var_1_2 )
|
||||
sr_1_2.attribute_fields.build( :key => 'field', :locale => true, :i18n_variable => var_1_2_1, :markup => 'text_field', :list_options => [] )
|
||||
sr_1_2.attribute_fields.build( :key => 'department', :locale => true, :i18n_variable => var_1_2_2, :markup => 'text_field', :list_options => [] )
|
||||
sr_1_2.attribute_fields.build( :key => 'speciality', :locale => true, :i18n_variable => var_1_2_3, :markup => 'text_field', :list_options => [] )
|
||||
|
@ -124,7 +124,7 @@ namespace :dev do
|
|||
|
||||
|
||||
|
||||
user = User.create( :email => 'chris@rulingcom.com', :password => 'password', :password_confirmation => 'password', :admin => true, :role_id => r_1.id, :sub_role_id => sr_1_2.id )
|
||||
user = User.create( :email => 'chris@rulingcom.com', :password => 'password', :password_confirmation => 'password', :admin => true, :role_id => r_1.id, :sub_role_ids => [sr_1_2.id] )
|
||||
AttributeValue.create( :user => user, :attribute_field => i_1.attribute_fields[0], :key => 'first_name', :en => 'Christophe', :zh_tw => '順發' )
|
||||
AttributeValue.create( :user_id => user.id, :attribute_field_id => i_1.attribute_fields[1].id, :key => 'last_name', :en => 'Vilayphiou', :zh_tw => '林' )
|
||||
AttributeValue.create( :user_id => user.id, :attribute_field_id => sr_1_2.attribute_fields[0].id, :key => 'field', :en => 'Computer Architecture', :zh_tw => '計算機系統結構' )
|
||||
|
@ -132,7 +132,7 @@ namespace :dev do
|
|||
AttributeValue.create( :user_id => user.id, :attribute_field_id => sr_1_2.attribute_fields[2].id, :key => 'speciality', :en => 'HSR', :zh_tw => '高鐵' )
|
||||
|
||||
|
||||
user = User.create( :email => 'matt@rulingcom.com', :password => 'password', :password_confirmation => 'password', :admin => true, :role_id => r_2.id, :sub_role_id => sr_2_1.id )
|
||||
user = User.create( :email => 'matt@rulingcom.com', :password => 'password', :password_confirmation => 'password', :admin => true, :role_id => r_2.id, :sub_role_ids => [sr_2_1.id ] )
|
||||
AttributeValue.create( :user_id => user.id, :attribute_field_id => i_1.attribute_fields[0].id, :key => 'first_name', :en => 'Matt', :zh_tw => '儒淵' )
|
||||
AttributeValue.create( :user_id => user.id, :attribute_field_id => i_1.attribute_fields[1].id, :key => 'last_name', :en => 'Fu', :zh_tw => '傅' )
|
||||
AttributeValue.create( :user_id => user.id, :attribute_field_id => sr_2_1.attribute_fields[0].id, :key => 'major', :en => 'Information management', :zh_tw => '信息化管理' )
|
||||
|
|
Loading…
Reference in New Issue