1 # Update and read user preferences, which are arbitrayr key/val pairs
2 class UserPreferenceController < ApplicationController
3 skip_before_filter :verify_authenticity_token
4 before_filter :authorize
5 before_filter :require_allow_read_prefs, :only => [:read_one, :read]
6 before_filter :require_allow_write_prefs, :except => [:read_one, :read]
7 around_filter :api_call_handle_error
10 pref = UserPreference.find(@user.id, params[:preference_key])
12 render :text => pref.v.to_s, :content_type => "text/plain"
13 rescue ActiveRecord::RecordNotFound => ex
14 render :text => 'OH NOES! PREF NOT FOUND!', :status => :not_found
19 pref = UserPreference.find(@user.id, params[:preference_key])
20 pref.v = request.raw_post.chomp
22 rescue ActiveRecord::RecordNotFound
23 pref = UserPreference.new
25 pref.k = params[:preference_key]
26 pref.v = request.raw_post.chomp
30 render :nothing => true, :content_type => "text/plain"
34 UserPreference.find(@user.id, params[:preference_key]).delete
36 render :nothing => true, :content_type => "text/plain"
37 rescue ActiveRecord::RecordNotFound => ex
38 render :text => "param: #{params[:preference_key]} not found", :status => :not_found
41 # print out all the preferences as a big xml block
43 doc = OSM::API.new.get_xml_doc
45 prefs = @user.preferences
47 el1 = XML::Node.new 'preferences'
50 el1 << pref.to_xml_node
54 render :text => doc.to_s, :content_type => "text/xml"
57 # update the entire set of preferences
59 doc = XML::Parser.string(request.raw_post).parse
65 doc.find('//preferences/preference').each do |pt|
66 pref = UserPreference.new
68 unless keyhash[pt['k']].nil? # already have that key
69 render :text => 'OH NOES! CAN HAS UNIQUE KEYS?', :status => :not_acceptable, :content_type => "text/plain"
77 pref.user_id = @user.id
82 render :text => 'Too many preferences', :status => :request_entity_too_large, :content_type => "text/plain"
86 # kill the existing ones
87 UserPreference.delete_all(['user_id = ?', @user.id])
94 render :nothing => true, :content_type => "text/plain"