Commit 0431641d

mo khan <mo@mokhan.ca>
2014-08-06 23:27:33
use absolute path helper instead of url_for when generating site map.
1 parent 00ed396
Changed files (4)
app/controllers/sitemap_controller.rb
@@ -1,10 +1,10 @@
 class SitemapController < ApplicationController
   layout nil
+  respond_to :xml
 
   def index
     @creations = Creation.all
     @tutorials = Tutorial.all
-    @base_url = "https://#{request.host_with_port}"
     headers['Content-Type'] = 'application/xml'
     expires_in(1.hour)
     respond_to do |format|
app/views/sitemap/index.xml.erb
@@ -2,16 +2,16 @@
 <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
   xmlns:image="http://www.sitemaps.org/schemas/sitemap-image/1.1"
   xmlns:video="http://www.sitemaps.org/schemas/sitemap-video/1.1">
-<% @creations.each do |creation| %>
+<% @creations.find_each do |creation| %>
     <url>
-      <loc><%="#{@base_url}#{url_for(creation)}"%></loc>
+      <loc><%= creation_url(creation) %></loc>
       <lastmod><%= creation.updated_at.strftime('%Y-%m-%dT%H:%M-06:00') %></lastmod>
       <priority>0.5</priority>
     </url>
 <% end %>
-<% @tutorials.each do |tutorial| %>
+<% @tutorials.find_each do |tutorial| %>
     <url>
-      <loc><%="#{@base_url}#{url_for(tutorial)}"%></loc>
+      <loc><%= tutorial_url(tutorial) %></loc>
       <lastmod><%= tutorial.updated_at.strftime('%Y-%m-%dT%H:%M-06:00') %></lastmod>
       <priority>0.5</priority>
     </url>
spec/controllers/sitemap_controller_spec.rb
@@ -2,13 +2,16 @@ require "rails_helper"
 
 describe SitemapController do
   describe "#index" do
+    render_views
+
     let!(:cake) { create(:cake) }
     let!(:tutorial) { create(:tutorial) }
 
-    it "returns the most recent cakes" do
+    it "returns the correct headers and body" do
       get :index, format: :xml
-      puts "BODY: #{response.body.inspect}"
+      expect(response).to be_success
       expect(response.body).to_not be_empty
+      expect(response.content_type).to eql('application/xml')
     end
   end
 end
spec/factories.rb
@@ -12,8 +12,15 @@ FactoryGirl.define do
     story 'whats the story morning glory?'
     user { FactoryGirl.create(:user) }
     association :category
-    #image { File.new(File.join( Rails.root, 'spec/fixtures/images/example.png')) }
   end
+
+  factory :cake, class: Creation do
+    name { Faker::Name.name }
+    story 'whats the story morning glory?'
+    association :user
+    association :category
+  end
+
   factory :favorite do
     user { FactoryGirl.create(:user) }
     creation { FactoryGirl.create(:creation) }