add rough user attributes API

This commit is contained in:
Wen-Tien Chang 2010-03-08 17:14:59 +08:00
parent 29ee4585b2
commit a40b117fbe
6 changed files with 32 additions and 11 deletions

View File

@ -13,6 +13,7 @@ class Panel::UsersController < ApplicationController
def new def new
@user = User.new @user = User.new
@user_attributes = UserAttribute.all
end end
def create def create
@ -26,6 +27,7 @@ class Panel::UsersController < ApplicationController
def edit def edit
@user = User.find(params[:id]) @user = User.find(params[:id])
@user_attributes = UserAttribute.all
end end
def update def update

View File

@ -3,6 +3,16 @@ class User
devise :authenticatable, :recoverable, :rememberable, :trackable devise :authenticatable, :recoverable, :rememberable, :trackable
key :use_attributes # key :use_attributes
def method_missing(method)
safe_read(method)
end
def safe_read(key)
self[key]
rescue MongoMapper::KeyNotFound
return ""
end
end end

View File

@ -2,7 +2,7 @@ class UserAttribute
include MongoMapper::Document include MongoMapper::Document
key :ident, String key :key, String
key_i18n :name, String key_i18n :name, String
key_i18n :attrs, Array key_i18n :attrs, Array

View File

@ -1,7 +1,13 @@
<p> <% @user_attributes.each do |ua| %>
<%= label_tag :name %> <h3><%= ua.name %></h3>
<%= text_field_tag "user[name]" %>
</p> <% ua.attrs.each do |attr| %>
<p>
<%= f.label attr["name"] %>
<%= f.send(attr["type"], "#{ua.key}_#{attr["key"]}" )%>
</p>
<% end -%>
<% end -%>
<p> <p>
<%= f.label :email %> <%= f.label :email %>

View File

@ -17,7 +17,7 @@
<% @users.each do |user| %> <% @users.each do |user| %>
<tr> <tr>
<td><%= user.use_attributes.join(",") %></td> <td><%= user.use_attributes.join(",") %></td>
<td><%#= user.name %></td> <td><%= user.name %></td>
<td><%= user.email %></td> <td><%= user.email %></td>
<td> <td>
<%= link_to 'Show', panel_user_path(user) %> <%= link_to 'Show', panel_user_path(user) %>

View File

@ -2,10 +2,13 @@ namespace :dev do
task :build => :environment do task :build => :environment do
User.delete_all User.delete_all
UserAttribute.delete_all
User.create!( :email => 'ihower@rulingcom.com', :password => 'password', :password_confirmation => 'password', :use_attributes => ["teacher"] ) User.create!( :email => 'ihower@rulingcom.com', :password => 'password', :password_confirmation => 'password', :use_attributes => ["teacher"] )
UserAttribute.create!( :ident => "teacher", :name => "Teacher", :attrs => [ "name" => { :type => 'text_field', :width => 100 }, UserAttribute.create!( :key => "teacher", :name => "Teacher", :attrs => [ { :name => "Name", :key => "name", :type => "text_field" }, { :name => "Title", :key => "title", :type => 'text_field' } ] )
"title" => { :type => 'text_field', :width => 100 } ] )
UserAttribute.create!( :key => "staff", :name => "Staff", :attrs => [ { :name => "Name", :key => "name", :type => "text_field" }, { :name => "Duty", :key => "duty", :type => 'text_field' } ] )
[Announcement, Item, Snippet, Layout].each { |m| m.delete_all } [Announcement, Item, Snippet, Layout].each { |m| m.delete_all }
Layout.create!( :name => 'root', :description => 'root', :content_zh_tw => File.open("#{RAILS_ROOT}/lib/template/root.layout.zh_tw").read, Layout.create!( :name => 'root', :description => 'root', :content_zh_tw => File.open("#{RAILS_ROOT}/lib/template/root.layout.zh_tw").read,