From 86e1817e29dd11d77e23758670870cc2edad89ba Mon Sep 17 00:00:00 2001 From: Wojciech Todryk Date: Sun, 24 Jul 2011 15:56:47 +0200 Subject: [PATCH] prefs model added --- app/controllers/application_controller.rb | 10 +++++----- app/controllers/messages_controller.rb | 1 + app/controllers/user_controller.rb | 11 +++++++++-- app/models/prefs.rb | 17 +++++++++++++++++ app/models/user.rb | 1 + config/defaults.yml | 2 ++ db/migrate/20110724125806_create_prefs.rb | 14 ++++++++++++++ .../20110724134917_add_params_to_prefs.rb | 11 +++++++++++ db/schema.rb | 12 +++++++++++- test/fixtures/prefs.yml | 9 +++++++++ test/unit/prefs_test.rb | 8 ++++++++ themes/olive/views/layouts/application.html.erb | 2 -- 12 files changed, 88 insertions(+), 10 deletions(-) create mode 100644 app/models/prefs.rb create mode 100644 db/migrate/20110724125806_create_prefs.rb create mode 100644 db/migrate/20110724134917_add_params_to_prefs.rb create mode 100644 test/fixtures/prefs.yml create mode 100644 test/unit/prefs_test.rb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 004d67d..3a62219 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -8,22 +8,22 @@ class ApplicationController < ActionController::Base protected 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 def theme_resolver if @current_user.nil? - @defaults['theme'] + $defaults['theme'] else - @current_user.theme + @current_user.prefs.theme end end def set_locale if @current_user.nil? - I18n.locale = @defaults['locale'] || I18n.default_locale + I18n.locale = $defaults['locale'] || I18n.default_locale else - I18n.locale = @current_user.locale || I18n.default_locale + I18n.locale = @current_user.prefs.locale || I18n.default_locale end end diff --git a/app/controllers/messages_controller.rb b/app/controllers/messages_controller.rb index 3ddfcfb..e5dcda9 100644 --- a/app/controllers/messages_controller.rb +++ b/app/controllers/messages_controller.rb @@ -4,6 +4,7 @@ class MessagesController < ApplicationController theme :theme_resolver def index + end end diff --git a/app/controllers/user_controller.rb b/app/controllers/user_controller.rb index a4cfe5b..4fecac5 100644 --- a/app/controllers/user_controller.rb +++ b/app/controllers/user_controller.rb @@ -17,9 +17,15 @@ class UserController < ApplicationController if user.nil? redirect_to :action => 'unknown' else - auten = false + 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 flash[:error] = t(:login_failure) redirect_to :action => 'login' @@ -49,6 +55,7 @@ class UserController < ApplicationController @user.save @server.user_id = @user.id @server.save + Prefs.create_default(@user.id) flash[:notice] = t(:setup_done) redirect_to :action => 'login' else diff --git a/app/models/prefs.rb b/app/models/prefs.rb new file mode 100644 index 0000000..35cee51 --- /dev/null +++ b/app/models/prefs.rb @@ -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 diff --git a/app/models/user.rb b/app/models/user.rb index 87710b9..76ed4ec 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -3,4 +3,5 @@ class User < ActiveRecord::Base validates_presence_of :first_name,:last_name validates_uniqueness_of :email has_many :servers, :dependent => :destroy + has_one :prefs, :dependent => :destroy end diff --git a/config/defaults.yml b/config/defaults.yml index 99b94e0..86d7b41 100644 --- a/config/defaults.yml +++ b/config/defaults.yml @@ -1,2 +1,4 @@ theme: olive locale: en +msgs_per_page: 20 +msg_send_type: html diff --git a/db/migrate/20110724125806_create_prefs.rb b/db/migrate/20110724125806_create_prefs.rb new file mode 100644 index 0000000..f6c6564 --- /dev/null +++ b/db/migrate/20110724125806_create_prefs.rb @@ -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 diff --git a/db/migrate/20110724134917_add_params_to_prefs.rb b/db/migrate/20110724134917_add_params_to_prefs.rb new file mode 100644 index 0000000..4c6c323 --- /dev/null +++ b/db/migrate/20110724134917_add_params_to_prefs.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index 3c49415..fe23a1f 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,17 @@ # # 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| t.string "name" diff --git a/test/fixtures/prefs.yml b/test/fixtures/prefs.yml new file mode 100644 index 0000000..dfc7203 --- /dev/null +++ b/test/fixtures/prefs.yml @@ -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 diff --git a/test/unit/prefs_test.rb b/test/unit/prefs_test.rb new file mode 100644 index 0000000..ec6ef1a --- /dev/null +++ b/test/unit/prefs_test.rb @@ -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 diff --git a/themes/olive/views/layouts/application.html.erb b/themes/olive/views/layouts/application.html.erb index c9c94a1..65c9dd9 100755 --- a/themes/olive/views/layouts/application.html.erb +++ b/themes/olive/views/layouts/application.html.erb @@ -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('style') %> - <%=javascript_include_tag current_theme_javascript_path('global') %> - <%=javascript_include_tag current_theme_javascript_path('webmail') %> <%=javascript_include_tag :defaults %>