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 def update
@user_attribute_model = UserInfoModel.find(params[:id]) @user_attribute_model = UserInfoModel.find(params[:id])
@user_attribute_model.update_attributes(params[:user_info_model]) @user_attribute_model.update_attributes(params[:user_info_model])
respond_to do |format|
redirect_to :action => :index format.html { redirect_to :action => :index }
format.js { render 'admin/user_attribute_models/toggle_enable' }
end
end end
def destroy def destroy

View File

@ -35,7 +35,10 @@ class Admin::UserRoleModelsController < ApplicationController
@user_attribute_model = UserRoleModel.find(params[:id]) @user_attribute_model = UserRoleModel.find(params[:id])
@user_attribute_model.update_attributes(params[:user_role_model]) @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 end
def destroy def destroy

View File

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

View File

@ -8,6 +8,8 @@ class AttributeModel
field :markup field :markup
field :locale, :type => Boolean field :locale, :type => Boolean
field :list_options, :type => Array 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 embedded_in :user_attribute_model, :inverse_of => :attribute_models
validates_uniqueness_of :key validates_uniqueness_of :key
@ -48,4 +50,12 @@ class AttributeModel
should_destroy.to_i == 1 rescue nil should_destroy.to_i == 1 rescue nil
end end
def is_built_in?
self.built_in
end
def is_disabled?
self.disabled
end
end end

View File

@ -5,6 +5,8 @@ class UserAttributeModel
field :key field :key
field :i18n_variable_id, :type => BSON::ObjectId, :index => true 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 embeds_many :attribute_models
@ -46,6 +48,18 @@ class UserAttributeModel
@i18n_variable ||= I18nVariable.find(self.i18n_variable_id) rescue nil @i18n_variable ||= I18nVariable.find(self.i18n_variable_id) rescue nil
end 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 protected
# Destroy the i18n_variable for each attribute_models if marked to destroy # Destroy the i18n_variable for each attribute_models if marked to destroy

View File

@ -20,8 +20,17 @@
<% if attribute_model.new_record? %> <% if attribute_model.new_record? %>
<a href="#" class="remove">(<%= t(:delete) %>)</a> <a href="#" class="remove">(<%= t(:delete) %>)</a>
<% else %> <% else %>
<% 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> <a href="#" class="remove_existing_record">(<%= t(:delete) %>)</a>
<%= hidden_field_tag "user_#{@attribute}_model[attribute_models][][should_destroy]", nil , :class => 'should_destroy' %> <%= hidden_field_tag "user_#{@attribute}_model[attribute_models][][should_destroy]", nil , :class => 'should_destroy' %>
<% end %>
<%= f.hidden_field :id %> <%= f.hidden_field :id %>
<%= f.hidden_field :key %> <%= f.hidden_field :key %>
<% end %> <% end %>

View File

@ -53,6 +53,12 @@
$(this).parent().parent().hide(); $(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(){ $('.select_mulitlingual input').live('click', function(){
$(this).next().attr('value', this.checked); $(this).next().attr('value', this.checked);
}); });

View File

@ -19,7 +19,13 @@
<td><%= user_attribute_model.key %></td> <td><%= user_attribute_model.key %></td>
<td> <td>
<%= link_to t(:edit), eval("edit_admin_user_#{@attribute}_model_path(user_attribute_model)") %> | <%= link_to t(:edit), eval("edit_admin_user_#{@attribute}_model_path(user_attribute_model)") %> |
<% 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 %> <%= link_to t(:delete), eval("admin_user_#{@attribute}_model_path(user_attribute_model)"), :confirm => t('sure?'), :method => :delete %>
<% end %>
</td> </td>
</tr> </tr>
<% end %> <% end %>

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

View File

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

View File

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

View File

@ -12,7 +12,7 @@
<% end %> <% end %>
</tr> </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 } %> <% 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][][id]", value.id rescue nil %>
<%= hidden_field_tag "user[user_#{type}s][][attributes][][key]", attr.key %> <%= 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_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' ) 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 = 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 => [] ) 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 => [] ) 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_1.save!
urm_2 = UserRoleModel.new( :key => 'student', :i18n_variable_id => var_4.id ) 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 => '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.attribute_models.build( :key => 'major', :locale => true, :i18n_variable_id => var_6.id, :markup => 'text_field', :list_options => [] )
urm_2.save! 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 => '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.attribute_models.build( :key => 'first_name', :locale => true, :i18n_variable_id => var_9.id, :markup => 'text_field', :list_options => [] )
uim_1.save! uim_1.save!