diff --git a/app/controllers/sitemap_controller.rb b/app/controllers/sitemap_controller.rb new file mode 100644 index 0000000000..103b6e93ee --- /dev/null +++ b/app/controllers/sitemap_controller.rb @@ -0,0 +1,11 @@ +class SitemapController < ApplicationController + layout nil + + def index + headers['Content-Type'] = 'application/xml' + @page_urls = [shops_url, map_url, producers_url, groups_url] + @enterprises = Enterprise.is_hub + @groups = EnterpriseGroup.all + respond_to :xml + end +end diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index 840477185f..e9acbd51f6 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -163,10 +163,18 @@ class Enterprise < ActiveRecord::Base } scope :distributing_products, lambda { |products| - with_distributed_products_outer.with_order_cycles_and_exchange_variants_outer. - where('product_distributions.product_id IN (?) OR spree_variants.product_id IN (?)', products, products). - select('DISTINCT enterprises.*') + # TODO: remove this when we pull out product distributions + pds = joins("INNER JOIN product_distributions ON product_distributions.distributor_id = enterprises.id"). + where("product_distributions.product_id IN (?)", products).select('DISTINCT enterprises.id') + + exs = joins("INNER JOIN exchanges ON (exchanges.receiver_id = enterprises.id AND exchanges.incoming = 'f')"). + joins('INNER JOIN exchange_variants ON (exchange_variants.exchange_id = exchanges.id)'). + joins('INNER JOIN spree_variants ON (spree_variants.id = exchange_variants.variant_id)'). + where('spree_variants.product_id IN (?)', products).select('DISTINCT enterprises.id') + + where(id: pds | exs) } + scope :managed_by, lambda { |user| if user.has_spree_role?('admin') scoped diff --git a/app/views/sitemap/index.xml.haml b/app/views/sitemap/index.xml.haml new file mode 100644 index 0000000000..dac6032a15 --- /dev/null +++ b/app/views/sitemap/index.xml.haml @@ -0,0 +1,17 @@ +!!! XML +%urlset{xmlns: "http://www.sitemaps.org/schemas/sitemap/0.9"} + - for page_url in @page_urls + %url + %loc= page_url + %changefreq monthly + + - for enterprise in @enterprises + %url + %loc= enterprise_shop_url(enterprise) + %lastmod= enterprise.updated_at.strftime('%Y-%m-%d') + %changefreq weekly + + - for group in @groups + %url + %loc= group_url(group) + %changefreq monthly diff --git a/config/routes.rb b/config/routes.rb index 69319ae84a..607e6308a2 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -169,6 +169,8 @@ Openfoodnetwork::Application.routes.draw do end end + get 'sitemap.xml', to: 'sitemap#index', defaults: { format: 'xml' } + # Mount Spree's routes mount Spree::Core::Engine, :at => '/' diff --git a/spec/features/consumer/sitemap_spec.rb b/spec/features/consumer/sitemap_spec.rb new file mode 100644 index 0000000000..6ec913de32 --- /dev/null +++ b/spec/features/consumer/sitemap_spec.rb @@ -0,0 +1,12 @@ +require 'spec_helper' + +feature 'sitemap' do + let(:enterprise) { create(:distributor_enterprise) } + let!(:group) { create(:enterprise_group, enterprises: [enterprise], on_front_page: true) } + + it "renders sitemap" do + visit '/sitemap.xml' + expect(page).to have_content enterprise_shop_url(enterprise) + expect(page).to have_content group_url(group) + end +end diff --git a/test/fixtures/.gitkeep b/test/fixtures/.gitkeep deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/test/functional/.gitkeep b/test/functional/.gitkeep deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/test/integration/.gitkeep b/test/integration/.gitkeep deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/test/performance/browsing_test.rb b/test/performance/browsing_test.rb deleted file mode 100644 index 3fea27b916..0000000000 --- a/test/performance/browsing_test.rb +++ /dev/null @@ -1,12 +0,0 @@ -require 'test_helper' -require 'rails/performance_test_help' - -class BrowsingTest < ActionDispatch::PerformanceTest - # Refer to the documentation for all available options - # self.profile_options = { :runs => 5, :metrics => [:wall_time, :memory] - # :output => 'tmp/performance', :formats => [:flat] } - - def test_homepage - get '/' - end -end diff --git a/test/test_helper.rb b/test/test_helper.rb deleted file mode 100644 index 7d57a78563..0000000000 --- a/test/test_helper.rb +++ /dev/null @@ -1,13 +0,0 @@ -ENV["RAILS_ENV"] = "test" -require_relative '../config/environment' -require 'rails/test_help' - -class ActiveSupport::TestCase - # Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in alphabetical order. - # - # Note: You'll currently still have to declare fixtures explicitly in integration tests - # -- they do not yet inherit this setting - fixtures :all - - # Add more helper methods to be used by all tests here... -end diff --git a/test/unit/.gitkeep b/test/unit/.gitkeep deleted file mode 100644 index e69de29bb2..0000000000