1 # Update and read user preferences, which are arbitrayr key/val pairs
3 class UserPreferencesController < ApplicationController
4 skip_before_action :verify_authenticity_token
5 before_action :authorize
9 around_action :api_call_handle_error
12 # return all the preferences as an XML document
14 doc = OSM::API.new.get_xml_doc
16 prefs = current_user.preferences
18 el1 = XML::Node.new "preferences"
21 el1 << pref.to_xml_node
25 render :xml => doc.to_s
29 # return the value for a single preference
31 pref = UserPreference.find([current_user.id, params[:preference_key]])
33 render :plain => pref.v.to_s
36 # update the entire set of preferences
38 old_preferences = current_user.preferences.each_with_object({}) do |preference, preferences|
39 preferences[preference.k] = preference
44 doc = XML::Parser.string(request.raw_post, :options => XML::Parser::Options::NOERROR).parse
46 doc.find("//preferences/preference").each do |pt|
47 if preference = old_preferences.delete(pt["k"])
48 preference.v = pt["v"]
49 elsif new_preferences.include?(pt["k"])
50 raise OSM::APIDuplicatePreferenceError, pt["k"]
52 preference = current_user.preferences.build(:k => pt["k"], :v => pt["v"])
55 new_preferences[preference.k] = preference
58 old_preferences.each_value(&:delete)
60 new_preferences.each_value(&:save!)
66 # update the value of a single preference
69 pref = UserPreference.find([current_user.id, params[:preference_key]])
70 rescue ActiveRecord::RecordNotFound
71 pref = UserPreference.new
72 pref.user = current_user
73 pref.k = params[:preference_key]
76 pref.v = request.raw_post.chomp
83 # delete a single preference
85 UserPreference.find([current_user.id, params[:preference_key]]).delete