Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct naming classes (collections) in modules #22

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/dynamic_sitemaps/generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def sitemap(*args, &block)
def sitemap_for(collection, options = {}, &block)
raise ArgumentError, "The collection given to `sitemap_for` must respond to #find_each. This is for performance. Use `Model.scoped` to get an ActiveRecord relation that responds to #find_each." unless collection.respond_to?(:find_each)

name = options.delete(:name) || collection.model_name.to_s.underscore.pluralize.to_sym
name = options.delete(:name) || collection.model_name.demodulize.to_s.underscore.pluralize.to_sym
options[:collection] = collection

sitemap(name, options, &block)
Expand Down Expand Up @@ -122,4 +122,4 @@ def folder(*args)
end
end
end
end
end
5 changes: 5 additions & 0 deletions test/dummy/app/models/blog.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module Blog
def self.table_name_prefix
'blog_'
end
end
7 changes: 7 additions & 0 deletions test/dummy/app/models/blog/post.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Blog::Post < ActiveRecord::Base
attr_accessible :title

def to_param
title || id.to_s
end
end
2 changes: 2 additions & 0 deletions test/dummy/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@
resources :products do
resources :comments
end

resources :blog_posts
end
9 changes: 9 additions & 0 deletions test/dummy/db/migrate/20140420132943_create_blog_posts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class CreateBlogPosts < ActiveRecord::Migration
def change
create_table :blog_posts do |t|
t.string :title

t.timestamps
end
end
end
8 changes: 7 additions & 1 deletion test/dummy/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20130211190343) do
ActiveRecord::Schema.define(:version => 20140420132943) do

create_table "blog_posts", :force => true do |t|
t.string "title"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end

create_table "products", :force => true do |t|
t.boolean "featured", :default => false
Expand Down
7 changes: 7 additions & 0 deletions test/dummy/test/fixtures/blog/posts.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html

one:
title: MyString

two:
title: MyString
7 changes: 7 additions & 0 deletions test/dummy/test/unit/blog/post_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# require 'test_helper'

# class Blog::PostTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
# end
31 changes: 31 additions & 0 deletions test/generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,37 @@ class GeneratorTest < ActiveSupport::TestCase
end
end

test "basic sitemap with model in namespace" do
2.times { Blog::Post.create }

DynamicSitemaps.generate_sitemap do
host "www.mytest.com"

sitemap :site do
url root_url
end

sitemap_for Blog::Post.scoped do |product|
url product
url product_comments_url(product)
end

end

doc = open_sitemap
assert_equal "http://www.mytest.com/sitemaps/site.xml", doc.xpath("sitemapindex/sitemap/loc").first.text

doc = open_sitemap(Rails.root.join("public", "sitemaps", "posts.xml"))
urls = doc.xpath("urlset/url")
assert_equal 4, urls.count


urls[0].tap do |url|
assert_equal "http://www.mytest.com/blog_posts/1", url.xpath("loc").text
end

end

private

# Opens a sitemap file using Nokogiri::XML and removes namespaces by default.
Expand Down