prefs model added

This commit is contained in:
Wojciech Todryk 2011-07-24 15:56:47 +02:00
parent da03add37b
commit 86e1817e29
12 changed files with 88 additions and 10 deletions

View file

@ -8,22 +8,22 @@ class ApplicationController < ActionController::Base
protected protected
def load_defaults def load_defaults
@defaults = YAML::load(File.open(Rails.root.join('config','defaults.yml'))) $defaults ||= YAML::load(File.open(Rails.root.join('config','defaults.yml')))
end end
def theme_resolver def theme_resolver
if @current_user.nil? if @current_user.nil?
@defaults['theme'] $defaults['theme']
else else
@current_user.theme @current_user.prefs.theme
end end
end end
def set_locale def set_locale
if @current_user.nil? if @current_user.nil?
I18n.locale = @defaults['locale'] || I18n.default_locale I18n.locale = $defaults['locale'] || I18n.default_locale
else else
I18n.locale = @current_user.locale || I18n.default_locale I18n.locale = @current_user.prefs.locale || I18n.default_locale
end end
end end

View file

@ -4,6 +4,7 @@ class MessagesController < ApplicationController
theme :theme_resolver theme :theme_resolver
def index def index
end end
end end

View file

@ -17,9 +17,15 @@ class UserController < ApplicationController
if user.nil? if user.nil?
redirect_to :action => 'unknown' redirect_to :action => 'unknown'
else else
auten = false auten = true
if auten == true if auten == true
session[:user_id] = user.id
if session["return_to"]
redirect_to(session["return_to"])
session["return_to"] = nil
else
redirect_to :controller=> "messages",:action=>"index"
end
else else
flash[:error] = t(:login_failure) flash[:error] = t(:login_failure)
redirect_to :action => 'login' redirect_to :action => 'login'
@ -49,6 +55,7 @@ class UserController < ApplicationController
@user.save @user.save
@server.user_id = @user.id @server.user_id = @user.id
@server.save @server.save
Prefs.create_default(@user.id)
flash[:notice] = t(:setup_done) flash[:notice] = t(:setup_done)
redirect_to :action => 'login' redirect_to :action => 'login'
else else

17
app/models/prefs.rb Normal file
View file

@ -0,0 +1,17 @@
class Prefs < ActiveRecord::Base
validates_presence_of :theme,:locale
has_one :user
protected
def self.create_default(user_id)
Prefs.create(:user_id => user_id,
:theme => $defaults['theme'],
:locale => $defaults['locale'],
:msgs_per_page => $defaults['msgs_per_page'],
:msg_send_type => $defaults['msg_send_type']
)
end
end

View file

@ -3,4 +3,5 @@ class User < ActiveRecord::Base
validates_presence_of :first_name,:last_name validates_presence_of :first_name,:last_name
validates_uniqueness_of :email validates_uniqueness_of :email
has_many :servers, :dependent => :destroy has_many :servers, :dependent => :destroy
has_one :prefs, :dependent => :destroy
end end

View file

@ -1,2 +1,4 @@
theme: olive theme: olive
locale: en locale: en
msgs_per_page: 20
msg_send_type: html

View file

@ -0,0 +1,14 @@
class CreatePrefs < ActiveRecord::Migration
def self.up
create_table :prefs do |t|
t.string :theme
t.string :locale
t.references :user
t.timestamps
end
end
def self.down
drop_table :prefs
end
end

View file

@ -0,0 +1,11 @@
class AddParamsToPrefs < ActiveRecord::Migration
def self.up
add_column :prefs, :msgs_per_page, :string
add_column :prefs, :msg_send_type, :string
end
def self.down
remove_column :prefs, :msg_send_type
remove_column :prefs, :msgs_per_page
end
end

View file

@ -10,7 +10,17 @@
# #
# It's strongly recommended to check this file into your version control system. # It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20110723153214) do ActiveRecord::Schema.define(:version => 20110724134917) do
create_table "prefs", :force => true do |t|
t.string "theme"
t.string "locale"
t.integer "user_id"
t.datetime "created_at"
t.datetime "updated_at"
t.string "msgs_per_page"
t.string "msg_send_type"
end
create_table "servers", :force => true do |t| create_table "servers", :force => true do |t|
t.string "name" t.string "name"

9
test/fixtures/prefs.yml vendored Normal file
View file

@ -0,0 +1,9 @@
# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
one:
theme: MyString
locale: MyString
two:
theme: MyString
locale: MyString

8
test/unit/prefs_test.rb Normal file
View file

@ -0,0 +1,8 @@
require 'test_helper'
class PrefsTest < ActiveSupport::TestCase
# Replace this with your real tests.
test "the truth" do
assert true
end
end

View file

@ -8,8 +8,6 @@ PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
<%=stylesheet_link_tag current_theme_stylesheet_path('base') %> <%=stylesheet_link_tag current_theme_stylesheet_path('base') %>
<%=stylesheet_link_tag current_theme_stylesheet_path('style') %> <%=stylesheet_link_tag current_theme_stylesheet_path('style') %>
<%=javascript_include_tag current_theme_javascript_path('global') %>
<%=javascript_include_tag current_theme_javascript_path('webmail') %>
<%=javascript_include_tag :defaults %> <%=javascript_include_tag :defaults %>