From: Andy Allan Date: Wed, 20 Nov 2019 15:31:46 +0000 (+0100) Subject: Move user preference XML generation to a view X-Git-Tag: live~2957^2 X-Git-Url: https://git.openstreetmap.org./rails.git/commitdiff_plain/8ad88b9ddc2f5aad1d9302bb6c2398c39cbfd2e6 Move user preference XML generation to a view --- diff --git a/app/controllers/api/user_preferences_controller.rb b/app/controllers/api/user_preferences_controller.rb index 9b117a4d9..83e28bc4e 100644 --- a/app/controllers/api/user_preferences_controller.rb +++ b/app/controllers/api/user_preferences_controller.rb @@ -10,18 +10,9 @@ module Api ## # return all the preferences as an XML document def index - doc = OSM::API.new.get_xml_doc + @user_preferences = current_user.preferences - prefs = current_user.preferences - - el1 = XML::Node.new "preferences" - - prefs.each do |pref| - el1 << pref.to_xml_node - end - - doc.root << el1 - render :xml => doc.to_s + render :formats => [:xml] end ## diff --git a/app/models/user_preference.rb b/app/models/user_preference.rb index 583ced3c5..37bab38b0 100644 --- a/app/models/user_preference.rb +++ b/app/models/user_preference.rb @@ -18,13 +18,4 @@ class UserPreference < ActiveRecord::Base validates :user, :presence => true, :associated => true validates :k, :v, :length => 1..255, :characters => true - - # Turn this Node in to an XML Node without the wrapper. - def to_xml_node - el1 = XML::Node.new "preference" - el1["k"] = k - el1["v"] = v - - el1 - end end diff --git a/app/views/api/user_preferences/_user_preference.xml.builder b/app/views/api/user_preferences/_user_preference.xml.builder new file mode 100644 index 000000000..ae830bed3 --- /dev/null +++ b/app/views/api/user_preferences/_user_preference.xml.builder @@ -0,0 +1,6 @@ +attrs = { + "k" => user_preference.k, + "v" => user_preference.v +} + +xml.preference(attrs) diff --git a/app/views/api/user_preferences/index.xml.builder b/app/views/api/user_preferences/index.xml.builder new file mode 100644 index 000000000..0a852b269 --- /dev/null +++ b/app/views/api/user_preferences/index.xml.builder @@ -0,0 +1,7 @@ +xml.instruct! + +xml.osm(OSM::API.new.xml_root_attributes) do |osm| + osm.preferences do |preferences| + preferences << (render(@user_preferences) || "") + end +end