From a40b117fbec4f8a31562d32f01b5c933f039b408 Mon Sep 17 00:00:00 2001 From: Wen-Tien Chang Date: Mon, 8 Mar 2010 17:14:59 +0800 Subject: [PATCH] add rough user attributes API --- app/controllers/panel/users_controller.rb | 2 ++ app/models/user.rb | 14 ++++++++++++-- app/models/user_attribute.rb | 2 +- app/views/panel/users/_form.html.erb | 14 ++++++++++---- app/views/panel/users/index.html.erb | 2 +- lib/tasks/dev.rake | 9 ++++++--- 6 files changed, 32 insertions(+), 11 deletions(-) diff --git a/app/controllers/panel/users_controller.rb b/app/controllers/panel/users_controller.rb index 1a0123b2..4fd14c49 100644 --- a/app/controllers/panel/users_controller.rb +++ b/app/controllers/panel/users_controller.rb @@ -13,6 +13,7 @@ class Panel::UsersController < ApplicationController def new @user = User.new + @user_attributes = UserAttribute.all end def create @@ -26,6 +27,7 @@ class Panel::UsersController < ApplicationController def edit @user = User.find(params[:id]) + @user_attributes = UserAttribute.all end def update diff --git a/app/models/user.rb b/app/models/user.rb index 3cd89648..478f2588 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -3,6 +3,16 @@ class User 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 \ No newline at end of file diff --git a/app/models/user_attribute.rb b/app/models/user_attribute.rb index 92e8aba3..005dfecf 100644 --- a/app/models/user_attribute.rb +++ b/app/models/user_attribute.rb @@ -2,7 +2,7 @@ class UserAttribute include MongoMapper::Document - key :ident, String + key :key, String key_i18n :name, String key_i18n :attrs, Array diff --git a/app/views/panel/users/_form.html.erb b/app/views/panel/users/_form.html.erb index 65a98419..1be9e60e 100644 --- a/app/views/panel/users/_form.html.erb +++ b/app/views/panel/users/_form.html.erb @@ -1,7 +1,13 @@ -

- <%= label_tag :name %> - <%= text_field_tag "user[name]" %> -

+<% @user_attributes.each do |ua| %> +

<%= ua.name %>

+ + <% ua.attrs.each do |attr| %> +

+ <%= f.label attr["name"] %> + <%= f.send(attr["type"], "#{ua.key}_#{attr["key"]}" )%> +

+ <% end -%> +<% end -%>

<%= f.label :email %> diff --git a/app/views/panel/users/index.html.erb b/app/views/panel/users/index.html.erb index 0065539a..9187c6e7 100644 --- a/app/views/panel/users/index.html.erb +++ b/app/views/panel/users/index.html.erb @@ -17,7 +17,7 @@ <% @users.each do |user| %> <%= user.use_attributes.join(",") %> - <%#= user.name %> + <%= user.name %> <%= user.email %> <%= link_to 'Show', panel_user_path(user) %> diff --git a/lib/tasks/dev.rake b/lib/tasks/dev.rake index 71809b86..5069986c 100644 --- a/lib/tasks/dev.rake +++ b/lib/tasks/dev.rake @@ -2,11 +2,14 @@ namespace :dev do task :build => :environment do User.delete_all + UserAttribute.delete_all + 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 }, - "title" => { :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' } ] ) + + 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 } Layout.create!( :name => 'root', :description => 'root', :content_zh_tw => File.open("#{RAILS_ROOT}/lib/template/root.layout.zh_tw").read, :content_en => File.open("#{RAILS_ROOT}/lib/template/root.layout.en").read)