]> git.openstreetmap.org Git - rails.git/blob - app/controllers/preferences_controller.rb
Make preferences page editable without clicking 'Edit Preferences'
[rails.git] / app / controllers / preferences_controller.rb
1 class PreferencesController < ApplicationController
2   layout "site"
3
4   before_action :authorize_web
5   before_action :set_locale
6
7   authorize_resource :class => false
8
9   before_action :check_database_readable
10   before_action :check_database_writable, :only => [:update]
11
12   def show; end
13
14   def update
15     current_user.languages = params[:user][:languages].split(",")
16
17     current_user.preferred_editor = if params[:user][:preferred_editor] == "default"
18                                       nil
19                                     else
20                                       params[:user][:preferred_editor]
21                                     end
22
23     success = current_user.save
24
25     if params[:site_color_scheme]
26       site_color_scheme_preference = current_user.preferences.find_or_create_by(:k => "site.color_scheme")
27       success &= site_color_scheme_preference.update(:v => params[:site_color_scheme])
28     end
29
30     if params[:map_color_scheme]
31       map_color_scheme_preference = current_user.preferences.find_or_create_by(:k => "map.color_scheme")
32       success &= map_color_scheme_preference.update(:v => params[:map_color_scheme])
33     end
34
35     if success
36       # Use a partial so that it is rendered during the next page load in the correct language.
37       flash[:notice] = { :partial => "preferences/update_success_flash" }
38       redirect_to preferences_path
39     else
40       flash.now[:error] = t ".failure"
41       render :show
42     end
43   end
44 end