Tidy up user preferences controller
This commit is contained in:
parent
083a8bb043
commit
c809f79912
3 changed files with 73 additions and 60 deletions
|
@ -6,39 +6,8 @@ class UserPreferenceController < ApplicationController
|
||||||
before_filter :require_allow_write_prefs, :except => [:read_one, :read]
|
before_filter :require_allow_write_prefs, :except => [:read_one, :read]
|
||||||
around_filter :api_call_handle_error
|
around_filter :api_call_handle_error
|
||||||
|
|
||||||
def read_one
|
##
|
||||||
pref = UserPreference.find(@user.id, params[:preference_key])
|
# return all the preferences as an XML document
|
||||||
|
|
||||||
render :text => pref.v.to_s, :content_type => "text/plain"
|
|
||||||
rescue ActiveRecord::RecordNotFound => ex
|
|
||||||
render :text => 'OH NOES! PREF NOT FOUND!', :status => :not_found
|
|
||||||
end
|
|
||||||
|
|
||||||
def update_one
|
|
||||||
begin
|
|
||||||
pref = UserPreference.find(@user.id, params[:preference_key])
|
|
||||||
pref.v = request.raw_post.chomp
|
|
||||||
pref.save
|
|
||||||
rescue ActiveRecord::RecordNotFound
|
|
||||||
pref = UserPreference.new
|
|
||||||
pref.user = @user
|
|
||||||
pref.k = params[:preference_key]
|
|
||||||
pref.v = request.raw_post.chomp
|
|
||||||
pref.save
|
|
||||||
end
|
|
||||||
|
|
||||||
render :nothing => true, :content_type => "text/plain"
|
|
||||||
end
|
|
||||||
|
|
||||||
def delete_one
|
|
||||||
UserPreference.find(@user.id, params[:preference_key]).delete
|
|
||||||
|
|
||||||
render :nothing => true, :content_type => "text/plain"
|
|
||||||
rescue ActiveRecord::RecordNotFound => ex
|
|
||||||
render :text => "param: #{params[:preference_key]} not found", :status => :not_found
|
|
||||||
end
|
|
||||||
|
|
||||||
# print out all the preferences as a big xml block
|
|
||||||
def read
|
def read
|
||||||
doc = OSM::API.new.get_xml_doc
|
doc = OSM::API.new.get_xml_doc
|
||||||
|
|
||||||
|
@ -54,43 +23,70 @@ class UserPreferenceController < ApplicationController
|
||||||
render :text => doc.to_s, :content_type => "text/xml"
|
render :text => doc.to_s, :content_type => "text/xml"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
##
|
||||||
|
# return the value for a single preference
|
||||||
|
def read_one
|
||||||
|
pref = UserPreference.find(@user.id, params[:preference_key])
|
||||||
|
|
||||||
|
render :text => pref.v.to_s, :content_type => "text/plain"
|
||||||
|
end
|
||||||
|
|
||||||
# update the entire set of preferences
|
# update the entire set of preferences
|
||||||
def update
|
def update
|
||||||
|
old_preferences = @user.preferences.reduce({}) do |preferences,preference|
|
||||||
|
preferences[preference.k] = preference
|
||||||
|
preferences
|
||||||
|
end
|
||||||
|
|
||||||
|
new_preferences = {}
|
||||||
|
|
||||||
doc = XML::Parser.string(request.raw_post).parse
|
doc = XML::Parser.string(request.raw_post).parse
|
||||||
|
|
||||||
prefs = []
|
|
||||||
|
|
||||||
keyhash = {}
|
|
||||||
|
|
||||||
doc.find('//preferences/preference').each do |pt|
|
doc.find('//preferences/preference').each do |pt|
|
||||||
pref = UserPreference.new
|
if preference = old_preferences.delete(pt["k"])
|
||||||
|
preference.v = pt["v"]
|
||||||
unless keyhash[pt['k']].nil? # already have that key
|
elsif new_preferences.include?(pt["k"])
|
||||||
render :text => 'OH NOES! CAN HAS UNIQUE KEYS?', :status => :not_acceptable, :content_type => "text/plain"
|
raise OSM::APIDuplicatePreferenceError.new(pt["k"])
|
||||||
return
|
else
|
||||||
|
preference = @user.preferences.build(:k => pt["k"], :v => pt["v"])
|
||||||
end
|
end
|
||||||
|
|
||||||
keyhash[pt['k']] = 1
|
new_preferences[preference.k] = preference
|
||||||
|
|
||||||
pref.k = pt['k']
|
|
||||||
pref.v = pt['v']
|
|
||||||
pref.user_id = @user.id
|
|
||||||
prefs << pref
|
|
||||||
end
|
end
|
||||||
|
|
||||||
if prefs.size > 150
|
old_preferences.each_value do |preference|
|
||||||
render :text => 'Too many preferences', :status => :request_entity_too_large, :content_type => "text/plain"
|
preference.delete
|
||||||
return
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# kill the existing ones
|
new_preferences.each_value do |preference|
|
||||||
UserPreference.delete_all(['user_id = ?', @user.id])
|
preference.save!
|
||||||
|
|
||||||
# save the new ones
|
|
||||||
prefs.each do |pref|
|
|
||||||
pref.save!
|
|
||||||
end
|
end
|
||||||
|
|
||||||
render :nothing => true, :content_type => "text/plain"
|
render :nothing => true, :content_type => "text/plain"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
##
|
||||||
|
# update the value of a single preference
|
||||||
|
def update_one
|
||||||
|
begin
|
||||||
|
pref = UserPreference.find(@user.id, params[:preference_key])
|
||||||
|
rescue ActiveRecord::RecordNotFound
|
||||||
|
pref = UserPreference.new
|
||||||
|
pref.user = @user
|
||||||
|
pref.k = params[:preference_key]
|
||||||
|
end
|
||||||
|
|
||||||
|
pref.v = request.raw_post.chomp
|
||||||
|
pref.save!
|
||||||
|
|
||||||
|
render :nothing => true, :content_type => "text/plain"
|
||||||
|
end
|
||||||
|
|
||||||
|
##
|
||||||
|
# delete a single preference
|
||||||
|
def delete_one
|
||||||
|
UserPreference.find(@user.id, params[:preference_key]).delete
|
||||||
|
|
||||||
|
render :nothing => true, :content_type => "text/plain"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
19
lib/osm.rb
19
lib/osm.rb
|
@ -298,6 +298,23 @@ module OSM
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# raised when a two preferences have a duplicate key string.
|
||||||
|
class APIDuplicatePreferenceError < APIError
|
||||||
|
def initialize(key)
|
||||||
|
@key = key
|
||||||
|
end
|
||||||
|
|
||||||
|
attr_reader :key
|
||||||
|
|
||||||
|
def status
|
||||||
|
:bad_request
|
||||||
|
end
|
||||||
|
|
||||||
|
def to_s
|
||||||
|
"Duplicate preferences with key #{@key}"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
# Helper methods for going to/from mercator and lat/lng.
|
# Helper methods for going to/from mercator and lat/lng.
|
||||||
class Mercator
|
class Mercator
|
||||||
include Math
|
include Math
|
||||||
|
@ -497,7 +514,7 @@ module OSM
|
||||||
country = "GB" if country == "UK"
|
country = "GB" if country == "UK"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
return country.upcase
|
return country.upcase
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -120,9 +120,9 @@ class UserPreferenceControllerTest < ActionController::TestCase
|
||||||
content "<osm><preferences><preference k='key' v='value'/><preference k='key' v='newer_value'/></preferences></osm>"
|
content "<osm><preferences><preference k='key' v='value'/><preference k='key' v='newer_value'/></preferences></osm>"
|
||||||
put :update
|
put :update
|
||||||
end
|
end
|
||||||
assert_response :not_acceptable
|
assert_response :bad_request
|
||||||
assert_equal "text/plain", @response.content_type
|
assert_equal "text/plain", @response.content_type
|
||||||
assert_equal "OH NOES! CAN HAS UNIQUE KEYS?", @response.body
|
assert_equal "Duplicate preferences with key key", @response.body
|
||||||
assert_equal "new_value", UserPreference.find(1, "key").v
|
assert_equal "new_value", UserPreference.find(1, "key").v
|
||||||
|
|
||||||
# try a put with invalid content
|
# try a put with invalid content
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue