<NME> pages_controller.rb
<BEF> module Refinery
  class PagesController < ::ApplicationController
    include Pages::RenderOptions

    before_action :find_page, :set_canonical
    before_action :error_404, unless: :current_user_can_view_page?

    # Save whole Page after delivery
    after_action :write_cache?

    # This action is usually accessed with the root path, normally '/'
    def home
      if page.link_url.present? && page.link_url != "/"
        redirect_to page.link_url, status: 301 and return
      end
      render_with_templates?
    end

    # This action can be accessed normally, or as nested pages.
    # Assuming a page named "mission" that is a child of "about",
    # you can access the pages with the following URLs:
    #
    #   GET /pages/about
    #   GET /about
    #
    #   GET /pages/mission
    #   GET /about/mission
        elsif page.link_url.present?
          redirect_to page.link_url
        else
          if requested_friendly_id != page.friendly_id
            redirect_to refinery.url_for(page.url), :status => 301
          else
            render_with_templates?
        redirect_to refinery.url_for(page.url), status: 301 and return
      end

      render_with_templates?
    end

  protected

    def requested_friendly_id
      if ::Refinery::Pages.scope_slug_by_parent
        # Pick out last path component, or id if present
        "#{params[:path]}/#{params[:id]}".split('/').last
      else
        # Remove leading and trailing slashes in path, but leave internal
        # ones for global slug scoping
        params[:path].to_s.gsub(%r{\A/+}, '').presence || params[:id]
      end
    end
      page.skip_to_first_child && first_live_child
    end

    def current_user_can_view_page?
      page.live? || current_refinery_user_can_access?("refinery_pages")
    end
    end

    def current_user_can_view_page?
      page.live? || current_refinery_user_can_access?("refinery_pages")
    end

    def current_refinery_user_can_access?(plugin)
      admin? && authorisation_manager.allow?(:plugin, plugin)
    end

    def first_live_child
      page.children.order('lft ASC').live.first
    end

    def find_page(fallback_to_404 = true)
      @page ||= action_page_finder.call(params) if action_has_page_finder?
      @page || (error_404 if fallback_to_404)
    end

    alias_method :page, :find_page

    def set_canonical
      @canonical = refinery.url_for @page.canonical if @page.present?
    end

    def write_cache?
      # Don't cache the page with the site bar showing.
      if Refinery::Pages.cache_pages_full && !authorisation_manager.allow?(:read, :site_bar)
        cache_page(response.body, File.join('', 'refinery', 'cache', 'pages', request.path).to_s)
      end
    end

    private
    def action_has_page_finder?
      Finders.const_defined? action_name.classify
    end

    def action_page_finder
      Finders.const_get action_name.classify
    end

    module Finders
      class Home
        def self.call(_params)
          Refinery::Page.find_by link_url: Refinery::Pages.home_page_path
        end
      end

      class Show
        def self.call(params)
          Refinery::Page.friendly.find_by_path_or_id params[:path], params[:id]
        end
      end
    end
  end
end

<MSG> Merge remote-tracking branch 'keram/fix_locale_redirects'

<DFF> @@ -28,7 +28,7 @@ module Refinery
         elsif page.link_url.present?
           redirect_to page.link_url
         else
-          if requested_friendly_id != page.friendly_id
+          if should_redirect_to_friendly_url?
             redirect_to refinery.url_for(page.url), :status => 301
           else
             render_with_templates?
@@ -53,6 +53,10 @@ module Refinery
       page.skip_to_first_child && first_live_child
     end
 
+    def should_redirect_to_friendly_url?
+      requested_friendly_id != page.friendly_id || params[:path].present? && params[:path].match(page.root.slug).nil?
+    end
+
     def current_user_can_view_page?
       page.live? || current_refinery_user_can_access?("refinery_pages")
     end

{'commit': 'diff --git a/pages/app/controllers/refinery/pages_controller.rb b/pages/app/controllers/refinery/pages_controller.rb\nindex d46bce08f..6bef7e0a3 100644\n--- a/pages/app/controllers/refinery/pages_controller.rb\n+++ b/pages/app/controllers/refinery/pages_controller.rb\n@@ -28,7 +28,7 @@ module Refinery\n         elsif page.link_url.present?\n           redirect_to page.link_url\n         else\n-          if requested_friendly_id != page.friendly_id\n+          if should_redirect_to_friendly_url?\n             redirect_to refinery.url_for(page.url), :status => 301\n           else\n             render_with_templates?\n@@ -53,6 +53,10 @@ module Refinery\n       page.skip_to_first_child && first_live_child\n     end\n \n+    def should_redirect_to_friendly_url?\n+      requested_friendly_id != page.friendly_id || params[:path].present? && params[:path].match(page.root.slug).nil?\n+    end\n+\n     def current_user_can_view_page?\n       page.live? || current_refinery_user_can_access?("refinery_pages")\n     end\ndiff --git a/pages/spec/requests/refinery/pages_spec.rb b/pages/spec/requests/refinery/pages_spec.rb\nindex 640c52767..37bfc1d94 100644\n--- a/pages/spec/requests/refinery/pages_spec.rb\n+++ b/pages/spec/requests/refinery/pages_spec.rb\n@@ -298,5 +298,74 @@ module Refinery\n         end\n       end\n     end\n+\n+    context "with multiple locales" do\n+\n+      describe "redirects" do\n+        let(:en_page_title) { \'News\' }\n+        let(:en_page_slug) { \'news\' }\n+        let(:ru_page_title) { \'Новости\' }\n+        let(:ru_page_slug) { \'новости\' }\n+        let(:ru_page_slug_encoded) { \'%D0%BD%D0%BE%D0%B2%D0%BE%D1%81%D1%82%D0%B8\' }\n+        let!(:news_page) do\n+          Refinery::I18n.stub(:frontend_locales).and_return([:en, :ru])\n+\n+          _page = Globalize.with_locale(:en) {\n+            Page.create :title => en_page_title\n+          }\n+          Globalize.with_locale(:ru) do\n+            _page.title = ru_page_title\n+            _page.save\n+          end\n+\n+          _page\n+        end\n+\n+        it "should remove default locale from path" do\n+          visit "/en/#{en_page_slug}"\n+\n+          current_path.should == "/#{en_page_slug}"\n+        end\n+\n+        it "should redirect to default locale slug" do\n+          visit "/#{ru_page_slug_encoded}"\n+\n+          current_path.should == "/#{en_page_slug}"\n+        end\n+\n+        it "should redirect to second locale slug" do\n+          visit "/ru/#{en_page_slug}"\n+\n+          current_path.should == "/ru/#{ru_page_slug_encoded}"\n+        end\n+\n+        describe "nested page" do\n+          let(:nested_page_title) { \'2012\' }\n+          let(:nested_page_slug) { \'2012\' }\n+\n+          let!(:nested_page) do\n+            Refinery::I18n.stub(:frontend_locales).and_return([:en, :ru])\n+\n+            _page = Globalize.with_locale(:en) {\n+              news_page.children.create :title => nested_page_title\n+            }\n+\n+            Globalize.with_locale(:ru) do\n+              _page.title = nested_page_title\n+              _page.save\n+            end\n+\n+            _page\n+          end\n+\n+          it "should redirect to localized url" do\n+            visit "/ru/#{en_page_slug}/#{nested_page_slug}"\n+\n+            current_path.should == "/ru/#{ru_page_slug_encoded}/#{nested_page_slug}"\n+          end\n+        end\n+      end\n+\n+    end\n   end\n end\n', 'commit_subject': "Merge remote-tracking branch 'keram/fix_locale_redirects'", 'repo_name': 'refinery/refinerycms', 'committer': {'name': 'Philip Arndt', 'email': '05447a56ef8d61f6725eec433ebbace12c6c37bc@gmail.com', 'time_sec': '1352610943', 'tz_offset': '-360', 'date': {'seconds': '1352610943'}}, 'commit_tree': '61f8cf71532dcae639f8857d24daf5121059156b', 'commit_message': "Merge remote-tracking branch 'keram/fix_locale_redirects'\n", 'language_name': 'JavaScript', 'license': 'mit', 'hunks': '@@ -28,7 +28,7 @@ module Refinery\n         elsif page.link_url.present?\n           redirect_to page.link_url\n         else\n-          if requested_friendly_id != page.friendly_id\n+          if should_redirect_to_friendly_url?\n             redirect_to refinery.url_for(page.url), :status => 301\n           else\n             render_with_templates?\n@@ -53,6 +53,10 @@ module Refinery\n       page.skip_to_first_child && first_live_child\n     end\n \n+    def should_redirect_to_friendly_url?\n+      requested_friendly_id != page.friendly_id || params[:path].present? && params[:path].match(page.root.slug).nil?\n+    end\n+\n     def current_user_can_view_page?\n       page.live? || current_refinery_user_can_access?("refinery_pages")\n     end\n', 'addition_count': 5, 'deletion_count': 1, 'src_file': 'a/pages/app/controllers/refinery/pages_controller.rb', 'tgt_file': 'b/pages/app/controllers/refinery/pages_controller.rb', 'file_extension': '.rb', 'before_file': 'module Refinery\n  class PagesController < ::ApplicationController\n    include Pages::RenderOptions\n\n    before_action :find_page, :set_canonical\n    before_action :error_404, unless: :current_user_can_view_page?\n\n    # Save whole Page after delivery\n    after_action :write_cache?\n\n    # This action is usually accessed with the root path, normally \'/\'\n    def home\n      if page.link_url.present? && page.link_url != "/"\n        redirect_to page.link_url, status: 301 and return\n      end\n      render_with_templates?\n    end\n\n    # This action can be accessed normally, or as nested pages.\n    # Assuming a page named "mission" that is a child of "about",\n    # you can access the pages with the following URLs:\n    #\n    #   GET /pages/about\n    #   GET /about\n    #\n    #   GET /pages/mission\n    #   GET /about/mission\n        elsif page.link_url.present?\n          redirect_to page.link_url\n        else\n          if requested_friendly_id != page.friendly_id\n            redirect_to refinery.url_for(page.url), :status => 301\n          else\n            render_with_templates?\n        redirect_to refinery.url_for(page.url), status: 301 and return\n      end\n\n      render_with_templates?\n    end\n\n  protected\n\n    def requested_friendly_id\n      if ::Refinery::Pages.scope_slug_by_parent\n        # Pick out last path component, or id if present\n        "#{params[:path]}/#{params[:id]}".split(\'/\').last\n      else\n        # Remove leading and trailing slashes in path, but leave internal\n        # ones for global slug scoping\n        params[:path].to_s.gsub(%r{\\A/+}, \'\').presence || params[:id]\n      end\n    end\n      page.skip_to_first_child && first_live_child\n    end\n\n    def current_user_can_view_page?\n      page.live? || current_refinery_user_can_access?("refinery_pages")\n    end\n    end\n\n    def current_user_can_view_page?\n      page.live? || current_refinery_user_can_access?("refinery_pages")\n    end\n\n    def current_refinery_user_can_access?(plugin)\n      admin? && authorisation_manager.allow?(:plugin, plugin)\n    end\n\n    def first_live_child\n      page.children.order(\'lft ASC\').live.first\n    end\n\n    def find_page(fallback_to_404 = true)\n      @page ||= action_page_finder.call(params) if action_has_page_finder?\n      @page || (error_404 if fallback_to_404)\n    end\n\n    alias_method :page, :find_page\n\n    def set_canonical\n      @canonical = refinery.url_for @page.canonical if @page.present?\n    end\n\n    def write_cache?\n      # Don\'t cache the page with the site bar showing.\n      if Refinery::Pages.cache_pages_full && !authorisation_manager.allow?(:read, :site_bar)\n        cache_page(response.body, File.join(\'\', \'refinery\', \'cache\', \'pages\', request.path).to_s)\n      end\n    end\n\n    private\n    def action_has_page_finder?\n      Finders.const_defined? action_name.classify\n    end\n\n    def action_page_finder\n      Finders.const_get action_name.classify\n    end\n\n    module Finders\n      class Home\n        def self.call(_params)\n          Refinery::Page.find_by link_url: Refinery::Pages.home_page_path\n        end\n      end\n\n      class Show\n        def self.call(params)\n          Refinery::Page.friendly.find_by_path_or_id params[:path], params[:id]\n        end\n      end\n    end\n  end\nend\n', 'message': "Merge remote-tracking branch 'keram/fix_locale_redirects'\n", 'author_name': 'Philip Arndt', 'lang': 'rb'}