Built-in user_attribute_models

This commit is contained in:
chris2tof 2011-05-02 12:07:31 +08:00
parent 509c106b3e
commit 83bbd77d96
14 changed files with 78 additions and 20 deletions

View File

@ -34,8 +34,10 @@ class Admin::UserInfoModelsController < ApplicationController
def update
@user_attribute_model = UserInfoModel.find(params[:id])
@user_attribute_model.update_attributes(params[:user_info_model])
redirect_to :action => :index
respond_to do |format|
format.html { redirect_to :action => :index }
format.js { render 'admin/user_attribute_models/toggle_enable' }
end
end
def destroy

View File

@ -35,7 +35,10 @@ class Admin::UserRoleModelsController < ApplicationController
@user_attribute_model = UserRoleModel.find(params[:id])
@user_attribute_model.update_attributes(params[:user_role_model])
redirect_to :action => :index
respond_to do |format|
format.html { redirect_to :action => :index }
format.js { render 'admin/user_attribute_models/toggle_enable' }
end
end
def destroy

View File

@ -9,13 +9,12 @@ class Panel::UsersController < ApplicationController
def show
@user = User.find(params[:id])
@user_info_models = UserInfoModel.all.entries
get_info_role_models
end
def new
@user = User.new
@user_info_models = UserInfoModel.all.entries
@user_role_models = UserRoleModel.all.entries
get_info_role_models
end
def create
@ -38,8 +37,7 @@ class Panel::UsersController < ApplicationController
def edit
@user = User.find(params[:id])
@user_info_models = UserInfoModel.all.entries
@user_role_models = UserRoleModel.all.entries
get_info_role_models
end
def update
@ -56,8 +54,7 @@ class Panel::UsersController < ApplicationController
flash[:notice] = t('panel.update_success_user')
redirect_to :action => :index
else
@user_info_models = UserInfoModel.all.entries
@user_role_models = UserRoleModel.all.entries
get_info_role_models
render :action => :edit
end
end
@ -73,4 +70,11 @@ class Panel::UsersController < ApplicationController
redirect_to :action => :index
end
protected
def get_info_role_models
@user_info_models = UserInfoModel.excludes('disabled' => true)
@user_role_models = UserRoleModel.excludes('disabled' => true)
end
end

View File

@ -8,6 +8,8 @@ class AttributeModel
field :markup
field :locale, :type => Boolean
field :list_options, :type => Array
field :built_in, :type => Boolean, :default => false
field :disabled, :type => Boolean, :default => false
embedded_in :user_attribute_model, :inverse_of => :attribute_models
validates_uniqueness_of :key
@ -47,5 +49,13 @@ class AttributeModel
def should_destroy?
should_destroy.to_i == 1 rescue nil
end
def is_built_in?
self.built_in
end
def is_disabled?
self.disabled
end
end

View File

@ -5,6 +5,8 @@ class UserAttributeModel
field :key
field :i18n_variable_id, :type => BSON::ObjectId, :index => true
field :built_in, :type => Boolean, :default => false
field :disabled, :type => Boolean, :default => false
embeds_many :attribute_models
@ -46,6 +48,18 @@ class UserAttributeModel
@i18n_variable ||= I18nVariable.find(self.i18n_variable_id) rescue nil
end
def is_built_in?
self.built_in
end
def is_disabled?
self.disabled
end
def get_enabled_attribute_models
self.attribute_models.excludes('disabled' => true)
end
protected
# Destroy the i18n_variable for each attribute_models if marked to destroy

View File

@ -20,8 +20,17 @@
<% if attribute_model.new_record? %>
<a href="#" class="remove">(<%= t(:delete) %>)</a>
<% else %>
<a href="#" class="remove_existing_record">(<%= t(:delete) %>)</a>
<%= hidden_field_tag "user_#{@attribute}_model[attribute_models][][should_destroy]", nil , :class => 'should_destroy' %>
<% if attribute_model.is_built_in? %>
<a href="#" class="change_built_in" id="<%= attribute_model.id %>">
<span style="display:<%= attribute_model.is_disabled? ? 'none' : 'inline' %>"><%= t(:disable) %></span>
<span style="display:<%= attribute_model.is_disabled? ? 'inline' : 'none' %>"><%= t(:enable) %></span>
</a>
<%= hidden_field_tag "user_#{@attribute}_model[attribute_models][][disabled]", attribute_model.is_disabled? , :class => 'built_in_state' %>
<% else %>
<a href="#" class="remove_existing_record">(<%= t(:delete) %>)</a>
<%= hidden_field_tag "user_#{@attribute}_model[attribute_models][][should_destroy]", nil , :class => 'should_destroy' %>
<% end %>
<%= f.hidden_field :id %>
<%= f.hidden_field :key %>
<% end %>

View File

@ -52,6 +52,12 @@
$(this).next('.should_destroy').attr('value', 1);
$(this).parent().parent().hide();
});
$('#field_set_attributes a.change_built_in').click(function(){
$(this).children().toggle();
var $am = $(this).next('.built_in_state')
$am.attr('value', ($am.attr('value') == "false") ? "true" : "false");
});
$('.select_mulitlingual input').live('click', function(){
$(this).next().attr('value', this.checked);

View File

@ -19,8 +19,14 @@
<td><%= user_attribute_model.key %></td>
<td>
<%= link_to t(:edit), eval("edit_admin_user_#{@attribute}_model_path(user_attribute_model)") %> |
<%= link_to t(:delete), eval("admin_user_#{@attribute}_model_path(user_attribute_model)"), :confirm => t('sure?'), :method => :delete %>
</td>
<% if user_attribute_model.is_built_in? %>
<%= link_to t(:disable), eval("admin_user_#{@attribute}_model_path(user_attribute_model, :authenticity_token => form_authenticity_token, :user_#{@attribute}_model => {:disabled => true})"), :remote => true, :method => :put, :id => "disable_#{user_attribute_model.id}", :style => "display:#{user_attribute_model.is_disabled? ? 'none' : 'inline'}" %>
<%= link_to t(:enable), eval("admin_user_#{@attribute}_model_path(user_attribute_model, :authenticity_token => form_authenticity_token, :user_#{@attribute}_model => {:disabled => false})"), :remote => true, :method => :put, :id => "enable_#{user_attribute_model.id}", :style => "display:#{user_attribute_model.is_disabled? ? 'inline' : 'none'}" %>
<% else %>
<%= link_to t(:delete), eval("admin_user_#{@attribute}_model_path(user_attribute_model)"), :confirm => t('sure?'), :method => :delete %>
<% end %>
</td>
</tr>
<% end %>
</table>

View File

@ -0,0 +1,2 @@
$("#enable_<%= @user_attribute_model.id %>").toggle();
$("#disable_<%= @user_attribute_model.id %>").toggle();

View File

@ -7,6 +7,7 @@
<%= yield :page_specific_link %>
<%= stylesheet_link_tag "easy", "main", "devise", :media => "screen, projection" %>
<%= stylesheet_link_tag "easyprint", :media => "print" %>
<%= javascript_include_tag :defaults %>
<!--[if IE]>
<%= stylesheet_link_tag "ie", :media => "screen, projection" %>
<![endif]-->

View File

@ -7,6 +7,7 @@
<%= yield :page_specific_link %>
<%= stylesheet_link_tag "easy", "main", "devise", :media => "screen, projection" %>
<%= stylesheet_link_tag "easyprint", :media => "print" %>
<%= javascript_include_tag :defaults %>
<!--[if IE]>
<%= stylesheet_link_tag "ie", :media => "screen, projection" %>
<![endif]-->

View File

@ -1,7 +1,7 @@
<fieldset>
<legend><%= am.i18n_variable[I18n.locale] %></legend>
<table>
<% am.attribute_models.each do |attr| %>
<% am.get_enabled_attribute_models.each do |attr| %>
<tr>
<td><%= attr.i18n_variable[I18n.locale] %></td>
<td>

View File

@ -12,7 +12,7 @@
<% end %>
</tr>
<% ua.attribute_models.each do |attr| %>
<% ua.get_enabled_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 %>

View File

@ -32,15 +32,15 @@ namespace :dev do
var_11 = I18nVariable.create!( :document_class => 'Page', :key => 'about', :en => 'About', :zh_tw => '關於我們' )
var_12 = I18nVariable.create!( :document_class => 'Link', :key => 'google', :en => 'Google', :zh_tw => 'Google' )
urm_1 = UserRoleModel.new( :key => 'teacher', :i18n_variable_id => var_1.id )
urm_1.attribute_models.build( :key => 'discipline', :locale => true, :i18n_variable_id => var_2.id, :markup => 'text_field', :list_options => [] )
urm_1.attribute_models.build( :key => 'department', :locale => true, :i18n_variable_id => var_3.id, :markup => 'text_field', :list_options => [] )
urm_1 = UserRoleModel.new( :key => 'teacher', :i18n_variable_id => var_1.id, :built_in => true )
urm_1.attribute_models.build( :key => 'discipline', :locale => true, :i18n_variable_id => var_2.id, :markup => 'text_field', :list_options => [], :built_in => true )
urm_1.attribute_models.build( :key => 'department', :locale => true, :i18n_variable_id => var_3.id, :markup => 'text_field', :list_options => [], :built_in => true )
urm_1.save!
urm_2 = UserRoleModel.new( :key => 'student', :i18n_variable_id => var_4.id )
urm_2.attribute_models.build( :key => 'department', :locale => true, :i18n_variable_id => var_5.id, :markup => 'text_field', :list_options => [] )
urm_2.attribute_models.build( :key => 'major', :locale => true, :i18n_variable_id => var_6.id, :markup => 'text_field', :list_options => [] )
urm_2.save!
uim_1 = UserInfoModel.new( :key => 'profile', :i18n_variable_id => var_7.id )
uim_1 = UserInfoModel.new( :key => 'profile', :i18n_variable_id => var_7.id, :built_in => true )
uim_1.attribute_models.build( :key => 'family_name', :locale => true, :i18n_variable_id => var_8.id, :markup => 'text_field', :list_options => [] )
uim_1.attribute_models.build( :key => 'first_name', :locale => true, :i18n_variable_id => var_9.id, :markup => 'text_field', :list_options => [] )
uim_1.save!