activerecord-collation 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: b08951bb81767158f2bb2182f468a658c763879c00f03a3173f7ed7fdb82dac4
4
+ data.tar.gz: 63d6d457800220ab6962bf1d201febdcdfa1f4d71128fb4ed7db2a3c28109a48
5
+ SHA512:
6
+ metadata.gz: f0c2107d45f7d6f5878494e91716e96f4853c6403c8c423eec5b0c61f1156567609417179ec4b2a39b6ef577a18466c0f9d6414b06b893eb4086e2159493e776
7
+ data.tar.gz: 85bdcf26363009c077c105eac437ee6cd0d5f8710a5f927f66c3b9e247c919278017c40dd02dd31e0d7ff246e701593ead7b93f3bcff90031c9c415447917a73
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright © Fingertips <manfred@fngtps.com>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,55 @@
1
+ # Active Record – Collations
2
+
3
+ Allows basic management of ICU collations in Postgres.
4
+
5
+ ```ruby
6
+ class UpdateNaturalDeCollation < ActiveRecord::Migration[8.1]
7
+ def change
8
+ drop_collation(:natural_de)
9
+ create_collation(
10
+ :natural_de,
11
+ 'de-u-kn-true-ks-level1-ka-shifted-kv-symbol'
12
+ )
13
+ end
14
+ end
15
+ ```
16
+
17
+ See [Postgres collation documentation](https://www.postgresql.org/docs/current/collation.html) on how to write ICU locale strings.
18
+
19
+ ## Setup
20
+
21
+ Add `activerecord-collations` to your dependencies. If you also want the `collate` class method on your model you also include the collation module:
22
+
23
+ ```ruby
24
+ class ApplicationRecord < ActiveRecord::Base
25
+ self.abstract_class = true
26
+
27
+ include ActiveRecord::Collation
28
+ end
29
+ ```
30
+
31
+ The collate method creates an ARel expression for the column with a certain collation.
32
+
33
+ ```ruby
34
+ scope :ordered, -> { order(collate(:title, "natural_#{I18n.locale}") }
35
+ scope :ordered_de, -> { order(collate(:title, :natural_de)) }
36
+ ```
37
+
38
+ Alternatively you can build it yourself:
39
+
40
+ ```ruby
41
+ Arel::Nodes::InfixOperation.new(
42
+ 'COLLATE',
43
+ arel_table[:title],
44
+ Arel.sql('natural_de')
45
+ )
46
+ ```
47
+
48
+ ## Testing
49
+
50
+ The Dummy application uses the gem so you can load the schema and run migrations there:
51
+
52
+ ```
53
+ cd test/dummy
54
+ RAILS_ENV=test rake db:migrate
55
+ ```
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveRecord
4
+ module Collation
5
+ class Railtie < Rails::Railtie # :nodoc:
6
+ initializer 'active_record.collation' do
7
+ ActiveSupport.on_load(:active_record_postgresqladapter) do
8
+ ActiveRecord::ConnectionAdapters::PostgreSQL::SchemaDumper.include(
9
+ ActiveRecord::Collation::SchemaDumper
10
+ )
11
+ ActiveSupport.on_load(:active_record) do
12
+ ActiveRecord::ConnectionAdapters::PostgreSQL::SchemaStatements.include(
13
+ ActiveRecord::Collation::SchemaStatements
14
+ )
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveRecord
4
+ module Collation
5
+ # Overrides the PostgresAdapter schema dumper to include collations.
6
+ module SchemaDumper
7
+ extend ActiveSupport::Concern
8
+
9
+ def collations(stream)
10
+ collations = @connection.collations
11
+ return unless collations.any?
12
+
13
+ stream.puts
14
+ collations.each do |row|
15
+ stream.puts(
16
+ " create_collation(#{row['collname'].inspect}, #{row['colllocale'].inspect})"
17
+ )
18
+ end
19
+ end
20
+
21
+ def trailer(stream)
22
+ collations(stream)
23
+ super
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveRecord
4
+ module Collation
5
+ # Defines methods to create, drop, and inspect collations on a schema in Postgres.
6
+ module SchemaStatements
7
+ def create_collation(name, icu_locale)
8
+ execute(
9
+ 'CREATE COLLATION IF NOT EXISTS ' \
10
+ "#{name} (provider = icu, locale = #{quote(icu_locale)})"
11
+ )
12
+ end
13
+
14
+ def drop_collation(name)
15
+ execute(
16
+ "DROP COLLATION IF EXISTS #{name}"
17
+ )
18
+ end
19
+
20
+ def collations
21
+ internal_exec_query(<<~SQL, 'SCHEMA')
22
+ SELECT
23
+ pg_collation.collname,
24
+ pg_collation.colllocale
25
+ FROM pg_collation
26
+ JOIN pg_namespace
27
+ ON pg_collation.collnamespace = pg_namespace.oid AND pg_namespace.nspname = 'public'
28
+ SQL
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveRecord
4
+ module Collation
5
+ VERSION = '0.0.1'
6
+ end
7
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveRecord
4
+ # Implements tools to manage and use collations in Postgres.
5
+ module Collation
6
+ extend ActiveSupport::Concern
7
+
8
+ class_methods do
9
+ def collate(column, collation)
10
+ Arel::Nodes::InfixOperation.new('COLLATE', arel_table[column], Arel.sql(collation.to_s))
11
+ end
12
+ end
13
+ end
14
+ end
15
+
16
+ require 'active_record/collation/schema_dumper'
17
+ require 'active_record/collation/schema_statements'
18
+ require 'active_record/collation/version'
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_record/collation'
4
+ require 'active_record/collation/railtie'
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activerecord-collation
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Manfred Stienstra
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: activerecord
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: '0'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - ">="
24
+ - !ruby/object:Gem::Version
25
+ version: '0'
26
+ description: " Adds methods like create_collation to manage collations in your Ruby
27
+ on Rials project.\n"
28
+ email:
29
+ - manfred@fngtps.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - LICENSE
35
+ - README.md
36
+ - lib/active_record/collation.rb
37
+ - lib/active_record/collation/railtie.rb
38
+ - lib/active_record/collation/schema_dumper.rb
39
+ - lib/active_record/collation/schema_statements.rb
40
+ - lib/active_record/collation/version.rb
41
+ - lib/activerecord-collation.rb
42
+ homepage: https://github.com/Fingertips/activerecord-collation
43
+ licenses:
44
+ - MIT
45
+ metadata:
46
+ rubygems_mfa_required: 'true'
47
+ rdoc_options: []
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.3'
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ requirements: []
61
+ rubygems_version: 3.7.0
62
+ specification_version: 4
63
+ summary: Collation support for Active Record.
64
+ test_files: []