-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor Refinery::Page#url and friends.
* Add replacement message to deprecated Refinery::Page#url_marketable method.
- Loading branch information
Showing
3 changed files
with
155 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
module Refinery | ||
module Pages | ||
class Url | ||
|
||
class Localised < Url | ||
def self.handle?(page) | ||
page.link_url.present? | ||
end | ||
|
||
def url | ||
current_url = page.link_url | ||
|
||
if current_url =~ %r{^/} && | ||
Refinery::I18n.current_frontend_locale != Refinery::I18n.default_frontend_locale | ||
current_url = "/#{Refinery::I18n.current_frontend_locale}#{current_url}" | ||
end | ||
|
||
current_url | ||
end | ||
end | ||
|
||
class Marketable < Url | ||
def self.handle?(page) | ||
Refinery::Pages.marketable_urls | ||
end | ||
|
||
def url | ||
url_hash = base_url_hash.merge(:path => page.nested_url, :id => nil) | ||
with_locale_param(url_hash) | ||
end | ||
end | ||
|
||
class Normal < Url | ||
def self.handle?(page) | ||
page.to_param.present? | ||
end | ||
|
||
def url | ||
url_hash = base_url_hash.merge(:path => nil, :id => page.to_param) | ||
with_locale_param(url_hash) | ||
end | ||
end | ||
|
||
def self.build(page) | ||
klass = [ Localised, Marketable, Normal ].detect { |d| d.handle?(page) } || self | ||
klass.new(page).url | ||
end | ||
|
||
def initialize(page) | ||
@page = page | ||
end | ||
|
||
def url | ||
raise NotImplementedError | ||
end | ||
|
||
private | ||
|
||
attr_reader :page | ||
|
||
def with_locale_param(url_hash) | ||
if (locale = Refinery::I18n.current_frontend_locale) != ::I18n.locale | ||
url_hash.update :locale => locale if locale | ||
end | ||
url_hash | ||
end | ||
|
||
def base_url_hash | ||
{ :controller => '/refinery/pages', :action => 'show', :only_path => true } | ||
end | ||
|
||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
require "spec_helper" | ||
|
||
module Refinery | ||
module Pages | ||
describe Url::Localised do | ||
describe ".handle?" do | ||
it "returns true if link_url is present" do | ||
page = stub(:page, :link_url => "/") | ||
Url::Localised.handle?(page).should be_true | ||
end | ||
end | ||
|
||
describe "#url" do | ||
let(:page) { stub(:page, :link_url => "/test") } | ||
|
||
context "when current frontend locale != default frontend locale" do | ||
it "returns link_url prefixed with current frontend locale" do | ||
Refinery::I18n.stub(:current_frontend_locale).and_return(:lv) | ||
Refinery::I18n.stub(:default_frontend_locale).and_return(:en) | ||
Url::Localised.new(page).url.should eq("/lv/test") | ||
end | ||
end | ||
|
||
context "when current frontend locale == default frontend locale" do | ||
it "returns unaltered link_url" do | ||
Refinery::I18n.stub(:current_frontend_locale).and_return(:en) | ||
Refinery::I18n.stub(:default_frontend_locale).and_return(:en) | ||
Url::Localised.new(page).url.should eq("/test") | ||
end | ||
end | ||
end | ||
end | ||
|
||
describe Url::Marketable do | ||
describe ".handle?" do | ||
it "returns true if marketable_url config is set to true" do | ||
page = stub(:page) | ||
Refinery::Pages.stub(:marketable_url).and_return(true) | ||
Url::Marketable.handle?(page).should be_true | ||
end | ||
end | ||
|
||
describe "#url" do | ||
it "returns hash" do | ||
page = stub(:page, :nested_url => "test") | ||
Url::Marketable.new(page).url.should eq({ | ||
:controller => "/refinery/pages", :action => "show", :only_path => true, | ||
:path => "test", :id => nil | ||
}) | ||
end | ||
end | ||
end | ||
|
||
describe Url::Normal do | ||
describe ".handle?" do | ||
it "returns true if to_param is present" do | ||
page = stub(:page, :to_param => "test") | ||
Url::Normal.handle?(page).should be_true | ||
end | ||
end | ||
|
||
describe "#url" do | ||
it "returns hash" do | ||
page = stub(:page, :to_param => "test") | ||
Url::Normal.new(page).url.should eq({ | ||
:controller => "/refinery/pages", :action => "show", :only_path => true, | ||
:path => nil, :id => "test" | ||
}) | ||
end | ||
end | ||
end | ||
end | ||
end |