1 # Update and read user preferences, which are arbitrayr key/val pairs
3 class UserPreferencesController < ApiController
4 before_action :authorize
8 around_action :api_call_handle_error
11 # return all the preferences as an XML document
13 @user_preferences = current_user.preferences
15 render :formats => [:xml]
19 # return the value for a single preference
21 pref = UserPreference.find([current_user.id, params[:preference_key]])
23 render :plain => pref.v.to_s
26 # update the entire set of preferences
28 old_preferences = current_user.preferences.each_with_object({}) do |preference, preferences|
29 preferences[preference.k] = preference
34 doc = XML::Parser.string(request.raw_post, :options => XML::Parser::Options::NOERROR).parse
36 doc.find("//preferences/preference").each do |pt|
37 if preference = old_preferences.delete(pt["k"])
38 preference.v = pt["v"]
39 elsif new_preferences.include?(pt["k"])
40 raise OSM::APIDuplicatePreferenceError, pt["k"]
42 preference = current_user.preferences.build(:k => pt["k"], :v => pt["v"])
45 new_preferences[preference.k] = preference
48 old_preferences.each_value(&:delete)
50 new_preferences.each_value(&:save!)
56 # update the value of a single preference
59 pref = UserPreference.find([current_user.id, params[:preference_key]])
60 rescue ActiveRecord::RecordNotFound
61 pref = UserPreference.new
62 pref.user = current_user
63 pref.k = params[:preference_key]
66 pref.v = request.raw_post.chomp
73 # delete a single preference
75 UserPreference.find([current_user.id, params[:preference_key]]).delete