Move user preference XML generation to a view
authorAndy Allan <git@gravitystorm.co.uk>
Wed, 20 Nov 2019 15:31:46 +0000 (16:31 +0100)
committerAndy Allan <git@gravitystorm.co.uk>
Wed, 20 Nov 2019 15:39:23 +0000 (16:39 +0100)
app/controllers/api/user_preferences_controller.rb
app/models/user_preference.rb
app/views/api/user_preferences/_user_preference.xml.builder [new file with mode: 0644]
app/views/api/user_preferences/index.xml.builder [new file with mode: 0644]

index 9b117a4..83e28bc 100644 (file)
@@ -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
 
     ##
index 583ced3..37bab38 100644 (file)
@@ -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 <osm> 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 (file)
index 0000000..ae830be
--- /dev/null
@@ -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 (file)
index 0000000..0a852b2
--- /dev/null
@@ -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