Skip to content

Commit

Permalink
fixed issue locomotivecms#567
Browse files Browse the repository at this point in the history
  • Loading branch information
did committed Feb 2, 2013
1 parent 2cc34a3 commit b90ccb7
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 2 deletions.
16 changes: 16 additions & 0 deletions app/models/locomotive/extensions/page/templatized.rb
Expand Up @@ -102,6 +102,22 @@ def fetch_target_entry(permalink)
target_klass.find_by_permalink(permalink)
end

# Find all the ordered entries of the target klass filtered or not
# by the conditions passed in parameter.
#
# @param [ Hash ] conditions The conditions used to filter the entries (optional)
#
# @return [ Object ] The documents
#
def fetch_target_entries(conditions = {})
if self.target_klass_name =~ /^Locomotive::Entry([a-z0-9]+)$/
@content_type ||= self.site.content_types.find($1)
@content_type.ordered_entries(conditions)
else
[]
end
end

protected

def get_templatized_from_parent
Expand Down
4 changes: 2 additions & 2 deletions app/views/locomotive/public/sitemaps/show.xml.builder
Expand Up @@ -9,9 +9,9 @@ xml.urlset "xmlns" => "http://www.sitemaps.org/schemas/sitemap/0.9" do
@pages.each do |page|
if not page.index_or_not_found?
if page.templatized?
page.content_type.entries.visible.each do |c|
page.fetch_target_entries(_visible: true).each do |c|
xml.url do
xml.loc public_page_url(page, { :content => c })
xml.loc public_page_url(page, { content: c })
xml.lastmod c.updated_at.to_date.to_s('%Y-%m-%d')
xml.priority 0.9
end
Expand Down
74 changes: 74 additions & 0 deletions features/public/sitemap.feature
@@ -0,0 +1,74 @@
Feature: Sitemap
As a designer
I want to have an automatically generated sitemap

Background:
Given I have the site: "test site" set up

Scenario: Simple sitemap
Given a page named "about" with the template:
"""
<html>
<body>About us</body>
</html>
"""
When I go to the sitemap
Then the response status should be 200
And I should see the following xml output:
"""
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>http://test.example.com:9886</loc>
<priority>1.0</priority>
</url>
<url>
<loc>http://test.example.com:9886/about</loc>
<lastmod>:now</lastmod>
<priority>0.9</priority>
</url>
</urlset>
"""

Scenario: Templatized page
Given I have a custom model named "Articles" with
| label | type | required |
| Title | string | true |
| Body | text | true |
And I have entries for "Articles" with
| title | body |
| Hello world | Lorem ipsum |
| Lorem ipsum | Lorem ipsum... |
And a templatized page for the "Articles" model and with the template:
"""
<html>
<body>Template for an article</body>
</html>
"""
When I go to the sitemap
Then the response status should be 200
And I should see the following xml output:
"""
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>http://test.example.com:9886</loc>
<priority>1.0</priority>
</url>
<url>
<loc>http://test.example.com:9886/articles</loc>
<lastmod>:now</lastmod>
<priority>0.9</priority>
</url>
<url>
<loc>http://test.example.com:9886/articles/hello-world</loc>
<lastmod>:now</lastmod>
<priority>0.9</priority>
</url>
<url>
<loc>http://test.example.com:9886/articles/lorem-ipsum</loc>
<lastmod>:now</lastmod>
<priority>0.9</priority>
</url>
</urlset>
"""
11 changes: 11 additions & 0 deletions features/step_definitions/more_web_steps.rb
Expand Up @@ -50,10 +50,21 @@
# pending # express the regexp above with the code you wish you had
end

Then /^the response status should be (\d+)$/ do |status|
page.status_code.should == status.to_i
end

Then /^it returns a (\d+) error page$/ do |code|
page.status_code.should == code.to_i
end

Then /^I should see the following xml output:$/ do |xml_output|
xml_output.gsub!(':now', Date.today.to_s)
response = Hash.from_xml(page.source)
expected = Hash.from_xml(xml_output)
expected.diff(response).should == {}
end

def wait_for_ajax(&block)
start_time = Time.now
while Time.now < start_time + Capybara.default_wait_time
Expand Down
9 changes: 9 additions & 0 deletions features/step_definitions/page_steps.rb
Expand Up @@ -36,6 +36,15 @@ def new_content_page(page_slug, page_contents, template = nil)
@page.save!
end

Given /^a templatized page for the "(.*?)" model and with the template:$/ do |model_name, template|
content_type = Locomotive::ContentType.where(name: model_name).first
parent = create_content_page(content_type.slug, '', '')
@page = @site.pages.new(parent: parent, title: "Template for #{model_name}", published: true,
templatized: true, target_klass_name: content_type.entries_class_name,
raw_template: template)
@page.save!
end

# change the title
When /^I change the page title to "([^"]*)"$/ do |page_title|
page.evaluate_script "window.prompt = function() { return '#{page_title}'; }"
Expand Down
2 changes: 2 additions & 0 deletions features/support/paths.rb
Expand Up @@ -37,6 +37,8 @@ def path_to(page_name)
when /the "(.*)" model edition page/
content_type = Locomotive::Site.first.content_types.where(:name => $1).first
edit_content_type_path(content_type)
when /the sitemap/
"/sitemap.xml"

# Add more mappings here.
# Here is an example that pulls values out of the Regexp:
Expand Down

0 comments on commit b90ccb7

Please sign in to comment.