-
-
Notifications
You must be signed in to change notification settings - Fork 196
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
[#2482] Fix translated public body sort #2619
Merged
mysociety-pusher
merged 3 commits into
develop
from
2482-fix-translated-public-body-sort
Aug 6, 2015
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,66 @@ | ||
# -*- encoding : utf-8 -*- | ||
# | ||
# Public: Class to check whether the current database supports collation for | ||
# a given language. Prefer the class method .supports? rather than creating a | ||
# new instance. | ||
class DatabaseCollation | ||
DEFAULT_CONNECTION = ActiveRecord::Base.connection | ||
MINIMUM_POSTGRESQL_VERSION = 90112 | ||
|
||
attr_reader :connection | ||
|
||
# Public: Does the connected database support collation in the given locale? | ||
# Delegates to an instance configured with the DEFAULT_CONNECTION. See | ||
# DatabaseCollation#supports? for more documentation. | ||
def self.supports?(locale) | ||
instance.supports?(locale) | ||
end | ||
|
||
def self.instance | ||
@instance ||= new | ||
end | ||
|
||
def initialize(connection = DEFAULT_CONNECTION) | ||
@connection = connection | ||
end | ||
|
||
# Public: Does the connected database support collation in the given locale? | ||
# | ||
# locale - String locale name | ||
# | ||
# Examples | ||
# | ||
# database.supports? 'en_GB' | ||
# # => true | ||
# database.supports? 'es' | ||
# # => false | ||
# | ||
# Returns a Boolean | ||
def supports?(locale) | ||
exist? && supported_collations.include?(locale) | ||
end | ||
|
||
private | ||
|
||
def exist? | ||
postgresql? && postgresql_version >= MINIMUM_POSTGRESQL_VERSION | ||
end | ||
|
||
def postgresql? | ||
adapter_name == 'PostgreSQL' | ||
end | ||
|
||
def postgresql_version | ||
@postgresql_version ||= connection.send(:postgresql_version) if postgresql? | ||
end | ||
|
||
def supported_collations | ||
@supported_collations ||= connection. | ||
execute(%q(SELECT collname FROM pg_collation;)). | ||
map { |row| row['collname'] } | ||
end | ||
|
||
def adapter_name | ||
@adapter_name ||= connection.adapter_name | ||
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
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,101 @@ | ||
# -*- encoding : utf-8 -*- | ||
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') | ||
|
||
describe DatabaseCollation do | ||
|
||
describe '.supports?' do | ||
|
||
it 'delegates to an instance of the class' do | ||
collation = double | ||
DatabaseCollation.stub(:instance).and_return(collation) | ||
collation.should_receive(:supports?).with('en_GB') | ||
DatabaseCollation.supports?('en_GB') | ||
end | ||
|
||
end | ||
|
||
describe '.instance' do | ||
|
||
it 'creates a new instance' do | ||
expect(DatabaseCollation.instance).to be_instance_of(DatabaseCollation) | ||
end | ||
|
||
it 'caches the instance' do | ||
expect(DatabaseCollation.instance).to equal(DatabaseCollation.instance) | ||
end | ||
|
||
it 'configures the instance with the default connection' do | ||
expect(DatabaseCollation.instance.connection). | ||
to equal(DatabaseCollation::DEFAULT_CONNECTION) | ||
end | ||
|
||
end | ||
|
||
describe :new do | ||
|
||
it 'defaults to the ActiveRecord::Base connection' do | ||
expect(DatabaseCollation.new.connection). | ||
to eq(ActiveRecord::Base.connection) | ||
end | ||
|
||
it 'allows a connection to be specified' do | ||
mock_connection = double | ||
expect(DatabaseCollation.new(mock_connection).connection). | ||
to eq(mock_connection) | ||
end | ||
|
||
end | ||
|
||
describe :supports? do | ||
|
||
it 'does not support collation if the database is not postgresql' do | ||
database = DatabaseCollation. | ||
new(mock_connection(:adapter_name => 'MySQL')) | ||
expect(database.supports?('en_GB')).to be_false | ||
end | ||
|
||
it 'does not support collation if the postgresql version is too old' do | ||
database = DatabaseCollation. | ||
new(mock_connection(:postgresql_version => 90111)) | ||
expect(database.supports?('en_GB')).to be_false | ||
end | ||
|
||
it 'does not support collation if the collation does not exist' do | ||
database = DatabaseCollation.new(mock_connection) | ||
expect(database.supports?('es')).to be_false | ||
end | ||
|
||
it 'supports collation if the collation exists' do | ||
database = DatabaseCollation.new(mock_connection) | ||
expect(database.supports?('en_GB')).to be_true | ||
end | ||
|
||
end | ||
|
||
end | ||
|
||
def mock_connection(connection_double_opts = {}) | ||
# Connection must be PostgreSQL 90112 or greater | ||
default_double_opts = { :adapter_name => 'PostgreSQL', | ||
:postgresql_version => 90112 } | ||
|
||
connection_double_opts = default_double_opts.merge(connection_double_opts) | ||
|
||
connection = double('ActiveRecord::FakeConnection', connection_double_opts) | ||
|
||
installed_collations = [ | ||
{ "collname" => "default" }, | ||
{ "collname" => "C" }, | ||
{ "collname" => "POSIX" }, | ||
{ "collname" => "C.UTF-8" }, | ||
{ "collname" => "en_GB" }, | ||
{ "collname" => "en_GB.utf8" } | ||
] | ||
|
||
connection. | ||
stub(:execute). | ||
with(%q(SELECT collname FROM pg_collation;)). | ||
and_return(installed_collations) | ||
|
||
connection | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could do with an encoding line