-# Update and read user preferences, which are arbitrayr key/val pairs
+# Update and read user preferences, which are arbitrary key/val pairs
module Api
- class UserPreferencesController < ApplicationController
- skip_before_action :verify_authenticity_token
+ class UserPreferencesController < ApiController
+ before_action :check_api_writable, :only => [:update_all, :update, :destroy]
before_action :authorize
authorize_resource
- around_action :api_call_handle_error
+ before_action :set_request_formats
##
- # return all the preferences as an XML document
- def read
- doc = OSM::API.new.get_xml_doc
+ # return all the preferences
+ def index
+ @user_preferences = current_user.preferences
- prefs = current_user.preferences
-
- el1 = XML::Node.new "preferences"
-
- prefs.each do |pref|
- el1 << pref.to_xml_node
+ respond_to do |format|
+ format.xml
+ format.json
end
-
- doc.root << el1
- render :xml => doc.to_s
end
##
# return the value for a single preference
- def read_one
+ def show
pref = UserPreference.find([current_user.id, params[:preference_key]])
render :plain => pref.v.to_s
end
# update the entire set of preferences
- def update
- old_preferences = current_user.preferences.each_with_object({}) do |preference, preferences|
- preferences[preference.k] = preference
- end
+ def update_all
+ old_preferences = current_user.preferences.index_by(&:k)
new_preferences = {}
##
# update the value of a single preference
- def update_one
+ def update
begin
pref = UserPreference.find([current_user.id, params[:preference_key]])
rescue ActiveRecord::RecordNotFound
pref.k = params[:preference_key]
end
- pref.v = request.raw_post.chomp
+ pref.v = request.raw_post.chomp.force_encoding("UTF-8")
pref.save!
render :plain => ""
##
# delete a single preference
- def delete_one
+ def destroy
UserPreference.find([current_user.id, params[:preference_key]]).delete
render :plain => ""