From 5cb2194f5e6541caf29aabbb579635c9a9a1baae Mon Sep 17 00:00:00 2001 From: Paul Mackay Date: Tue, 10 Jan 2017 11:39:17 +0000 Subject: [PATCH 1/5] #1027: Add sitemap.xml generation --- app/controllers/sitemap_controller.rb | 11 +++++++++++ app/views/sitemap/index.xml.haml | 18 ++++++++++++++++++ config/routes.rb | 2 ++ spec/features/consumer/sitemap_spec.rb | 12 ++++++++++++ 4 files changed, 43 insertions(+) create mode 100644 app/controllers/sitemap_controller.rb create mode 100644 app/views/sitemap/index.xml.haml create mode 100644 spec/features/consumer/sitemap_spec.rb diff --git a/app/controllers/sitemap_controller.rb b/app/controllers/sitemap_controller.rb new file mode 100644 index 0000000000..f5ce44ceca --- /dev/null +++ b/app/controllers/sitemap_controller.rb @@ -0,0 +1,11 @@ +class SitemapController < ApplicationController + layout nil + + def index + headers['Content-Type'] = 'application/xml' + @pages = ['shops', 'map', 'producers', 'groups'] + @enterprises = Enterprise.is_hub + @groups = EnterpriseGroup.all + respond_to :xml + end +end diff --git a/app/views/sitemap/index.xml.haml b/app/views/sitemap/index.xml.haml new file mode 100644 index 0000000000..3c07cc6ebc --- /dev/null +++ b/app/views/sitemap/index.xml.haml @@ -0,0 +1,18 @@ +!!! XML +%urlset{xmlns: "http://www.sitemaps.org/schemas/sitemap/0.9"} + - for page in @pages + %url + %loc= root_url + page + %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) + %lastmod= enterprise.updated_at.strftime('%Y-%m-%d') + %changefreq yearly 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 From fc400741b4491d2041ba30d32c0727563fedf2ac Mon Sep 17 00:00:00 2001 From: Paul Mackay Date: Tue, 10 Jan 2017 11:44:57 +0000 Subject: [PATCH 2/5] Remove lastmod for groups --- app/views/sitemap/index.xml.haml | 1 - 1 file changed, 1 deletion(-) diff --git a/app/views/sitemap/index.xml.haml b/app/views/sitemap/index.xml.haml index 3c07cc6ebc..f961b75899 100644 --- a/app/views/sitemap/index.xml.haml +++ b/app/views/sitemap/index.xml.haml @@ -14,5 +14,4 @@ - for group in @groups %url %loc= group_url(group) - %lastmod= enterprise.updated_at.strftime('%Y-%m-%d') %changefreq yearly From d80f080af55997aa2ddfb5ddcda7326f5ac7de1b Mon Sep 17 00:00:00 2001 From: Paul Mackay Date: Thu, 12 Jan 2017 10:18:30 +0000 Subject: [PATCH 3/5] #1027: Use url helpers for basic pages Set groups change frequency to monthly. --- app/controllers/sitemap_controller.rb | 2 +- app/views/sitemap/index.xml.haml | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/app/controllers/sitemap_controller.rb b/app/controllers/sitemap_controller.rb index f5ce44ceca..103b6e93ee 100644 --- a/app/controllers/sitemap_controller.rb +++ b/app/controllers/sitemap_controller.rb @@ -3,7 +3,7 @@ class SitemapController < ApplicationController def index headers['Content-Type'] = 'application/xml' - @pages = ['shops', 'map', 'producers', 'groups'] + @page_urls = [shops_url, map_url, producers_url, groups_url] @enterprises = Enterprise.is_hub @groups = EnterpriseGroup.all respond_to :xml diff --git a/app/views/sitemap/index.xml.haml b/app/views/sitemap/index.xml.haml index f961b75899..dac6032a15 100644 --- a/app/views/sitemap/index.xml.haml +++ b/app/views/sitemap/index.xml.haml @@ -1,8 +1,8 @@ !!! XML %urlset{xmlns: "http://www.sitemaps.org/schemas/sitemap/0.9"} - - for page in @pages + - for page_url in @page_urls %url - %loc= root_url + page + %loc= page_url %changefreq monthly - for enterprise in @enterprises @@ -14,4 +14,4 @@ - for group in @groups %url %loc= group_url(group) - %changefreq yearly + %changefreq monthly From 651626eb4ffd220f0a2d71cb2f0e8f56b20c541e Mon Sep 17 00:00:00 2001 From: Paul Mackay Date: Sat, 14 Jan 2017 11:47:21 +0000 Subject: [PATCH 4/5] #1365: Remove /test dir as it is not used --- test/fixtures/.gitkeep | 0 test/functional/.gitkeep | 0 test/integration/.gitkeep | 0 test/performance/browsing_test.rb | 12 ------------ test/test_helper.rb | 13 ------------- test/unit/.gitkeep | 0 6 files changed, 25 deletions(-) delete mode 100644 test/fixtures/.gitkeep delete mode 100644 test/functional/.gitkeep delete mode 100644 test/integration/.gitkeep delete mode 100644 test/performance/browsing_test.rb delete mode 100644 test/test_helper.rb delete mode 100644 test/unit/.gitkeep 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 From 017916b1933e4d6cd0b09acbca6bb8c87d7b1eb4 Mon Sep 17 00:00:00 2001 From: Rob Harrington Date: Thu, 5 Jan 2017 12:17:24 +1100 Subject: [PATCH 5/5] Enterprise distributing_products scope uses INNER JOINS instead of OUTER JOINS --- app/models/enterprise.rb | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) 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