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.index_by(&:k)
32 doc = XML::Parser.string(request.raw_post, :options => XML::Parser::Options::NOERROR).parse
34 doc.find("//preferences/preference").each do |pt|
35 if preference = old_preferences.delete(pt["k"])
36 preference.v = pt["v"]
37 elsif new_preferences.include?(pt["k"])
38 raise OSM::APIDuplicatePreferenceError, pt["k"]
40 preference = current_user.preferences.build(:k => pt["k"], :v => pt["v"])
43 new_preferences[preference.k] = preference
46 old_preferences.each_value(&:delete)
48 new_preferences.each_value(&:save!)
54 # update the value of a single preference
57 pref = UserPreference.find([current_user.id, params[:preference_key]])
58 rescue ActiveRecord::RecordNotFound
59 pref = UserPreference.new
60 pref.user = current_user
61 pref.k = params[:preference_key]
64 pref.v = request.raw_post.chomp
71 # delete a single preference
73 UserPreference.find([current_user.id, params[:preference_key]]).delete