friendly_id 5.2.4 → 5.4.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 19856ab4f2e02ba7eed649beef7728be7eae0a58
4
- data.tar.gz: bcf94acb37ff90b70916a96a34a2035570490128
2
+ SHA256:
3
+ metadata.gz: 21ddfb374efbecb41ddb5164a5f0fbb4df5838edfcb13a8e7d8d6caa1b378f34
4
+ data.tar.gz: 363955e55e33b9b15d59099f1b6ac00d88f32e0b3835f1dfd6fcf718aad7ac7f
5
5
  SHA512:
6
- metadata.gz: 41b11ab959bcf15522a627431421bdb4542fd8768ee1695d6bf68d2351f479c4cd49e6f42e9b46a8f6aac0557bf199b8ad58582c02b049624204e12283740643
7
- data.tar.gz: 221d38007de3e624191d6905bd0430dc90bce1a9b1212cc20e8bb9bfbd06aa60fc8e0e44c74ec52b9d424210806683ed39446bd623eae09cc07d0542f58db0e6
6
+ metadata.gz: d966763a10e683b815681576ef71c720caeafee88c49f866f912cae1a2aae12a33fd00bfc93f2d0eb7204ae2b91c514613edf34996119519613452b0ce0cdfb1
7
+ data.tar.gz: f8e8522f7f4b1c8a32bc8fd58c719d8376beb3325f2adfbca0554f7ec63a2742d5ab5b7128bbf2fa3f106075f5f89af590843c36bf3924d975674bada3673ca9
@@ -0,0 +1,2 @@
1
+ ҺO�WL[R���F���,����%�2Z�*��-���&,�ɳdxڢ�7�ҏ��͹�֛���”.�ʚ�\ZGh�탧?�T�q�x7G�P.�'�Z����!����=�@W��6f�\B����#X�Y*M�Kv�ػ��U��=-���齤Jq�hR_�v1��(<�}��S6��e[Q<F
2
+ �������&^EN�u�x���,.C���*��M�z�Y��s�ho��I�#M
@@ -0,0 +1,3 @@
1
+ ,o*��۬���w�S�^�턚�j�sn���嚣%b1�UV�0���d�(��$\Q�F�/�P������IӣD'�
2
+ s�H=�������9�N�`�Qd<�'�|OQ��{sY������^a�+5H���m����DSZ0�i*����xM��'hN,Y�� �� *Xq�AgD���x���YJ(0Z��`MlZ=��1�tG ��rV�%�\�m�������'D�'<{8�u���:��W���š��y�o��#@CC�a���&�=�M=I�sI`%����n��]|�)�~`�/_i��{L� �
3
+ �l��ѹ��2�p�7��x�ޥ}������~|���F�� %5r�����s�A��b8M��8�#[$B#���j&b�
@@ -0,0 +1 @@
1
+ github: parndt
@@ -0,0 +1,17 @@
1
+ # Number of days of inactivity before an issue becomes stale
2
+ daysUntilStale: 84
3
+ # Number of days of inactivity before a stale issue is closed
4
+ daysUntilClose: 7
5
+ # Issues with these labels will never be considered stale
6
+ exemptLabels:
7
+ - pinned
8
+ - security
9
+ # Label to use when marking an issue as stale
10
+ staleLabel: stale
11
+ # Comment to post when marking an issue as stale. Set to `false` to disable
12
+ markComment: >
13
+ This issue has been automatically marked as stale because it has not had
14
+ recent activity. It will be closed if no further activity occurs. Thank you
15
+ for your contributions.
16
+ # Comment to post when closing a stale issue. Set to `false` to disable
17
+ closeComment: false
@@ -0,0 +1,49 @@
1
+ name: CI
2
+
3
+ on:
4
+ push:
5
+ branches:
6
+ - 'master'
7
+ pull_request:
8
+ jobs:
9
+ test:
10
+ strategy:
11
+ matrix:
12
+ database: [ mysql, postgresql ]
13
+ gemfile: [ '6.0', '5.2' ]
14
+ ruby: [ '2.7', '2.6', '2.5' ]
15
+ fail-fast: false
16
+ runs-on: ubuntu-latest
17
+ name: ${{ matrix.ruby }} ${{ matrix.database }} rails-${{ matrix.gemfile }}
18
+ steps:
19
+ - uses: actions/checkout@v2
20
+ - run: sudo apt-get update && sudo apt-get install libpq-dev postgresql-client libmysqlclient-dev mysql-client libsqlite3-dev -y
21
+ - uses: ruby/setup-ruby@v1
22
+ with:
23
+ bundler-cache: true
24
+ ruby-version: ${{ matrix.ruby }}
25
+ - run: bundle exec rake db:create db:up
26
+ - run: bundle exec rake test
27
+
28
+ env:
29
+ BUNDLE_GEMFILE: gemfiles/Gemfile.rails-${{ matrix.gemfile }}.rb
30
+ CI: true
31
+ COVERALLS: true
32
+ DB: ${{ matrix.database }}
33
+ MYSQL_PASSWORD: root
34
+ PGHOST: localhost
35
+ PGPORT: 5432
36
+ PGUSER: postgres
37
+ RAILS_ENV: test
38
+
39
+ services:
40
+ postgres:
41
+ image: postgres:11.5
42
+ ports: ["5432:5432"]
43
+ options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
44
+ mysql:
45
+ image: mysql:5.7
46
+ ports: ["3306:3306"]
47
+ options: --health-cmd "mysqladmin ping" --health-interval 10s --health-timeout 5s --health-retries 5
48
+ env:
49
+ MYSQL_ROOT_PASSWORD: root
@@ -3,6 +3,41 @@
3
3
  We would like to think our many [contributors](https://github.com/norman/friendly_id/graphs/contributors) for
4
4
  suggestions, ideas and improvements to FriendlyId.
5
5
 
6
+ ## Unreleased
7
+
8
+ ## 5.4.2 (2021-01-07)
9
+
10
+ * Fix: Set slug before save if needed ([#948](https://github.com/norman/friendly_id/pull/948))
11
+ * Revert "Make `first_by_friendly_id` case insensitive using `downcase`" ([#951](https://github.com/norman/friendly_id/pull/951))
12
+
13
+ ## 5.4.1 (2020-11-06)
14
+
15
+ * Fix unexpected `:slug` error on valid, unpersisted model ([#952](https://github.com/norman/friendly_id/pull/952))
16
+
17
+ ## 5.4.0 (2020-08-14)
18
+
19
+ * Fix Ruby 2.7 keyword params deprecation warning ([#939](https://github.com/norman/friendly_id/pull/939))
20
+ * Fix Slug Validation in `unset_slug_if_invalid` ([#938](https://github.com/norman/friendly_id/pull/938))
21
+ * Make `first_by_friendly_id` case insensitive using `downcase` ([#787](https://github.com/norman/friendly_id/pull/787))
22
+ * Use `destroy_all` rather than `delete_all` when creating historical slugs ([#924](https://github.com/norman/friendly_id/pull/924))
23
+ * Set required ruby version to `>= 2.1.0` ([#923](https://github.com/norman/friendly_id/pull/923))
24
+ * Avoid using deprecated `update_attributes` ([#922](https://github.com/norman/friendly_id/pull/922))
25
+
26
+ ## 5.3.0 (2019-09-25)
27
+
28
+ * Record history when scope changes but slug does not ([#916](https://github.com/norman/friendly_id/pull/916))
29
+ * Add support for Rails 6 ([#897](https://github.com/norman/friendly_id/pull/897))
30
+
31
+ ## 5.2.5 (2018-12-30)
32
+
33
+ * Pass all possible parameters to ActiveRecord::RecordNotFound.new when raising the exception ([#890](https://github.com/norman/friendly_id/pull/890))
34
+ * Use composite index for queries by sluggable ([#882](https://github.com/norman/friendly_id/pull/882))
35
+ * Scoped: generate new slug if scope changed ([#878](https://github.com/norman/friendly_id/pull/878))
36
+ * Fix History + SequentiallySlugged issues ([#877](https://github.com/norman/friendly_id/pull/877))
37
+ * Support scoped with STI ([#745](https://github.com/norman/friendly_id/pull/745))
38
+ * Fix exists? to behave the same as find for numeric slugs ([#875](https://github.com/norman/friendly_id/pull/875))
39
+ * Remove dirty tracking code from to_param ([#867](https://github.com/norman/friendly_id/pull/867))
40
+
6
41
  ## 5.2.4 (2018-04-24)
7
42
 
8
43
  * Fix compatibility with Rails versions 4.0 -> 5.2. ([#863](https://github.com/norman/friendly_id/pull/863)).
data/Gemfile CHANGED
@@ -2,9 +2,12 @@ source 'https://rubygems.org'
2
2
 
3
3
  gemspec
4
4
 
5
+ gem 'rake'
6
+
5
7
  group :development, :test do
6
8
  platforms :ruby do
7
9
  gem 'byebug'
10
+ gem 'pry'
8
11
  end
9
12
 
10
13
  platforms :jruby do
data/README.md CHANGED
@@ -1,10 +1,10 @@
1
- [![Build Status](https://travis-ci.org/norman/friendly_id.svg)](https://travis-ci.org/norman/friendly_id)
1
+ [![Build Status](https://github.com/norman/friendly_id/workflows/CI/badge.svg)](https://github.com/norman/friendly_id/actions)
2
2
  [![Code Climate](https://codeclimate.com/github/norman/friendly_id.svg)](https://codeclimate.com/github/norman/friendly_id)
3
- [![Inline docs](http://inch-ci.org/github/norman/friendly_id.svg?branch=master)](http://inch-ci.org/github/norman/friendly_id)
3
+ [![Inline docs](https://inch-ci.org/github/norman/friendly_id.svg?branch=master)](https://inch-ci.org/github/norman/friendly_id)
4
4
 
5
5
  # FriendlyId
6
6
 
7
- **For the most complete, user-friendly documentation, see the [FriendlyId Guide](http://norman.github.io/friendly_id/file.Guide.html).**
7
+ **For the most complete, user-friendly documentation, see the [FriendlyId Guide](https://norman.github.io/friendly_id/file.Guide.html).**
8
8
 
9
9
  FriendlyId is the "Swiss Army bulldozer" of slugging and permalink plugins for
10
10
  Active Record. It lets you create pretty URLs and work with human-friendly
@@ -12,16 +12,16 @@ strings as if they were numeric ids.
12
12
 
13
13
  With FriendlyId, it's easy to make your application use URLs like:
14
14
 
15
- http://example.com/states/washington
15
+ https://example.com/states/washington
16
16
 
17
17
  instead of:
18
18
 
19
- http://example.com/states/4323454
19
+ https://example.com/states/4323454
20
20
 
21
21
 
22
22
  ## Getting Help
23
23
 
24
- Ask questions on [Stack Overflow](http://stackoverflow.com/questions/tagged/friendly-id)
24
+ Ask questions on [Stack Overflow](https://stackoverflow.com/questions/tagged/friendly-id)
25
25
  using the "friendly-id" tag, and for bugs have a look at [the bug section](https://github.com/norman/friendly_id#bugs)
26
26
 
27
27
  ## FriendlyId Features
@@ -39,23 +39,29 @@ FriendlyId offers many advanced features, including:
39
39
  Add this line to your application's Gemfile:
40
40
 
41
41
  ```ruby
42
- gem 'friendly_id', '~> 5.1.0' # Note: You MUST use 5.0.0 or greater for Rails 4.0+
42
+ gem 'friendly_id', '~> 5.4.0'
43
43
  ```
44
44
 
45
+ Note: You MUST use 5.0.0 or greater for Rails 4.0+.
46
+
45
47
  And then execute:
46
48
 
47
49
  ```shell
48
50
  bundle install
49
51
  ```
50
52
 
51
- Generate the friendly configuration file
53
+ Add a `slug` column to the desired table (e.g. `Users`)
54
+ ```shell
55
+ rails g migration AddSlugToUsers slug:uniq
56
+ ```
57
+
58
+ Generate the friendly configuration file and a new migration
52
59
 
53
60
  ```shell
54
61
  rails generate friendly_id
55
62
  ```
56
63
 
57
- >Temp solution for Rails 5.1+ : Before running the migration, go into the generated migration file and specify the Rails version:
58
- `class CreateFriendlyIdSlugs < ActiveRecord::Migration[5.1]`
64
+ Note: You can delete the `CreateFriendlyIdSlugs` migration if you won't use the slug history feature. ([Read more](https://norman.github.io/friendly_id/FriendlyId/History.html))
59
65
 
60
66
  Run the migration scripts
61
67
 
@@ -91,7 +97,7 @@ User.create! name: "Joe Schmoe"
91
97
  You can then access the user show page using the URL http://localhost:3000/users/joe-schmoe.
92
98
 
93
99
 
94
- If you're adding FriendlyId to an existing app and need to generate slugs for
100
+ If you're adding FriendlyId to an existing app and need to generate slugs for
95
101
  existing users, do this from the console, runner, or add a Rake task:
96
102
 
97
103
  ```ruby
@@ -101,7 +107,7 @@ User.find_each(&:save)
101
107
  ## Bugs
102
108
 
103
109
  Please report them on the [Github issue
104
- tracker](http://github.com/norman/friendly_id/issues) for this project.
110
+ tracker](https://github.com/norman/friendly_id/issues) for this project.
105
111
 
106
112
  If you have a bug to report, please include the following information:
107
113
 
@@ -113,8 +119,8 @@ If you have a bug to report, please include the following information:
113
119
  If you are able to, it helps even more if you can fork FriendlyId on Github,
114
120
  and add a test that reproduces the error you are experiencing.
115
121
 
116
- For more info on how to report bugs, please see [this
117
- article](http://yourbugreportneedsmore.info/).
122
+ For more inspiration on how to report bugs, please see [this
123
+ article](https://www.chiark.greenend.org.uk/~sgtatham/bugs.html).
118
124
 
119
125
  ## Thanks and Credits
120
126
 
@@ -127,7 +133,7 @@ volunteers](https://github.com/norman/friendly_id/contributors).
127
133
 
128
134
  ## License
129
135
 
130
- Copyright (c) 2008-2016 Norman Clarke and contributors, released under the MIT
136
+ Copyright (c) 2008-2020 Norman Clarke and contributors, released under the MIT
131
137
  license.
132
138
 
133
139
  Permission is hereby granted, free of charge, to any person obtaining a copy of
data/Rakefile CHANGED
@@ -71,7 +71,7 @@ namespace :db do
71
71
  driver = FriendlyId::Test::Database.driver
72
72
  config = FriendlyId::Test::Database.config[driver]
73
73
  commands = {
74
- "mysql" => "mysql -u #{config['username']} -e 'create database #{config["database"]};' >/dev/null",
74
+ "mysql" => "mysql -h #{config['host']} -P #{config['port']} -u #{config['username']} --password=#{config['password']} -e 'create database #{config["database"]};' >/dev/null",
75
75
  "postgres" => "psql -c 'create database #{config['database']};' -U #{config['username']} >/dev/null"
76
76
  }
77
77
  %x{#{commands[driver] || true}}
@@ -83,7 +83,7 @@ namespace :db do
83
83
  driver = FriendlyId::Test::Database.driver
84
84
  config = FriendlyId::Test::Database.config[driver]
85
85
  commands = {
86
- "mysql" => "mysql -u #{config['username']} -e 'drop database #{config["database"]};' >/dev/null",
86
+ "mysql" => "mysql -h #{config['host']} -P #{config['port']} -u #{config['username']} --password=#{config['password']} -e 'drop database #{config["database"]};' >/dev/null",
87
87
  "postgres" => "psql -c 'drop database #{config['database']};' -U #{config['username']} >/dev/null"
88
88
  }
89
89
  %x{#{commands[driver] || true}}
@@ -0,0 +1,25 @@
1
+ -----BEGIN CERTIFICATE-----
2
+ MIIEMjCCApqgAwIBAgIBATANBgkqhkiG9w0BAQsFADAjMSEwHwYDVQQDDBhnZW1z
3
+ L0RDPXAvREM9YXJuZHQvREM9aW8wHhcNMjAwNTEwMjIxOTQ2WhcNMjEwNTEwMjIx
4
+ OTQ2WjAjMSEwHwYDVQQDDBhnZW1zL0RDPXAvREM9YXJuZHQvREM9aW8wggGiMA0G
5
+ CSqGSIb3DQEBAQUAA4IBjwAwggGKAoIBgQDT+JzHYPGMYJt9ct2DCsbIymn1uJJp
6
+ HnDkQESfsGe40jTC90oF2iVbVOkaacNc1N3CSWUZvZjuygUuS86P6/kpBILGdO2+
7
+ bkXXKtfGC2YGGx9TdNLpCb4925vQHvdFeKXGpQDZdDw1SNC6zraZou47CvOE1cl2
8
+ Bp+1QMZuGRZ4+5CzOEWDWurjqce3O1jUEbyBB7z5H0h/YEaxfXipxhL1Dhi0sgkH
9
+ qP/e6SxzifdifdZCksJFQ06a1ji9hJY6eM23qbv/aaluVHAZSVBAQBS7rYniLo+N
10
+ G4vpFhoubQO5u8UluUtCaPUpI/qOvVcSaZn3ZkzlMwC8b1RwAeXBQmtFE2wnrv2i
11
+ ovTwoN7rHchwhgaHbkuFh4Wr92wGbrWL7J+X8rWKk1f8RF4kvtNE/NA6YrkxTpVh
12
+ QMyDmekt7rTxvcq2NneLGroWIUVCx/JID+Jw492LKQ6Sl1/P2TRzdEDtqZAZL0gt
13
+ xlWeMUfGG2D/gLnhs5qnaFaWQwGTmBnTgHcCAwEAAaNxMG8wCQYDVR0TBAIwADAL
14
+ BgNVHQ8EBAMCBLAwHQYDVR0OBBYEFEqtAyQVxPgKsrgoTQ1YmaIu/fmvMBoGA1Ud
15
+ EQQTMBGBD2dlbXNAcC5hcm5kdC5pbzAaBgNVHRIEEzARgQ9nZW1zQHAuYXJuZHQu
16
+ aW8wDQYJKoZIhvcNAQELBQADggGBALu2HM50B8xqlAXkCwavJDvWWtV9pG1igFUg
17
+ friZRWprUQ5nTaNmAd8p8qbJQwaIK2gt+DfYWfB9LtKnQTfbhLRBbmJ7zYw8LjKY
18
+ PwCs4RWjDAiuyCO3ppfsz+1bsMUXPLgWlaUkXsUy3nr2NruEFTO9zu3wGYQQ93Tt
19
+ vYSHOnP35UB4QjsjNrOO7FBaQfy6O909PP+GnVcJ62s9c26voJz63RSolwY7Jydw
20
+ XUlG68jjJKSoDHRzVTmNB7sX8rs8P2kvYkpIUXPHyls3mWBWjBWbdEYWESZrxI2x
21
+ dS7jY3AnfqhvsWra2pSREb2IDqPnJrHVOejnEI/zuuufUxLwDx3AC6SMdsyWkZ7V
22
+ 9OmLt2rg75Sct6h2220lO5ySqYtqAXuOMBDGv5L0zLalx1g8LACA7uILTKVWh8B8
23
+ Hsej0MQ3drCB1eA4c9OXdCUQJnY2aLTq3uNvTbZvoTgWK55eq3KLBJ4zzoKZ4tBX
24
+ /HIFI/fEwYlI1Ji3oikUrHkc4rWgaQ==
25
+ -----END CERTIFICATE-----
@@ -8,13 +8,12 @@ Gem::Specification.new do |s|
8
8
  s.email = ["norman@njclarke.com", "p@arndt.io"]
9
9
  s.homepage = "https://github.com/norman/friendly_id"
10
10
  s.summary = "A comprehensive slugging and pretty-URL plugin."
11
- s.rubyforge_project = "friendly_id"
12
11
  s.files = `git ls-files`.split("\n")
13
12
  s.test_files = `git ls-files -- {test}/*`.split("\n")
14
13
  s.require_paths = ["lib"]
15
14
  s.license = 'MIT'
16
15
 
17
- s.required_ruby_version = '>= 1.9.3'
16
+ s.required_ruby_version = '>= 2.1.0'
18
17
 
19
18
  s.add_dependency 'activerecord', '>= 4.0.0'
20
19
 
@@ -32,4 +31,9 @@ FriendlyId is the "Swiss Army bulldozer" of slugging and permalink plugins for
32
31
  Active Record. It lets you create pretty URLs and work with human-friendly
33
32
  strings as if they were numeric ids.
34
33
  EOM
34
+
35
+ s.cert_chain = [File.expand_path('certs/parndt.pem', __dir__)]
36
+ if $PROGRAM_NAME =~ /gem\z/ && ARGV.include?('build') && ARGV.include?(__FILE__)
37
+ s.signing_key = File.expand_path('~/.ssh/gem-private_key.pem')
38
+ end
35
39
  end
@@ -8,8 +8,8 @@ gem 'railties', '~> 5.2.0'
8
8
  # Database Configuration
9
9
  group :development, :test do
10
10
  platforms :jruby do
11
- gem 'activerecord-jdbcmysql-adapter', git: 'https://github.com/jruby/activerecord-jdbc-adapter', branch: 'master'
12
- gem 'activerecord-jdbcpostgresql-adapter', git: 'https://github.com/jruby/activerecord-jdbc-adapter', branch: 'master'
11
+ gem 'activerecord-jdbcmysql-adapter', '~> 51.1'
12
+ gem 'activerecord-jdbcpostgresql-adapter', '~> 51.1'
13
13
  gem 'kramdown'
14
14
  end
15
15
 
@@ -2,22 +2,21 @@ source 'https://rubygems.org'
2
2
 
3
3
  gemspec path: '../'
4
4
 
5
- gem 'activerecord', '~> 4.2.1'
6
- gem 'railties', '~> 4.2.1'
7
- gem 'i18n', '~> 0.7.0'
5
+ gem 'activerecord', '~> 6.0.0'
6
+ gem 'railties', '~> 6.0.0'
8
7
 
9
8
  # Database Configuration
10
9
  group :development, :test do
11
10
  platforms :jruby do
12
- gem 'activerecord-jdbcmysql-adapter', '~> 1.3.14'
13
- gem 'activerecord-jdbcpostgresql-adapter', '~> 1.3.14'
11
+ gem 'activerecord-jdbcmysql-adapter', '~> 51.1'
12
+ gem 'activerecord-jdbcpostgresql-adapter', '~> 51.1'
14
13
  gem 'kramdown'
15
14
  end
16
15
 
17
16
  platforms :ruby, :rbx do
18
17
  gem 'sqlite3'
19
- gem 'mysql2', '~> 0.4.10'
20
- gem 'pg', '~> 0.15'
18
+ gem 'mysql2'
19
+ gem 'pg'
21
20
  gem 'redcarpet'
22
21
  end
23
22
 
@@ -159,8 +159,9 @@ often better and easier to use {FriendlyId::Slugged slugs}.
159
159
  #
160
160
  # @option options [Symbol,Module] :use The addon or name of an addon to use.
161
161
  # By default, FriendlyId provides {FriendlyId::Slugged :slugged},
162
- # {FriendlyId::History :history}, {FriendlyId::Reserved :reserved}, and
163
- # {FriendlyId::Scoped :scoped}, and {FriendlyId::SimpleI18n :simple_i18n}.
162
+ # {FriendlyId::Reserved :finders}, {FriendlyId::History :history},
163
+ # {FriendlyId::Reserved :reserved}, {FriendlyId::Scoped :scoped}, and
164
+ # {FriendlyId::SimpleI18n :simple_i18n}.
164
165
  #
165
166
  # @option options [Array] :reserved_words Available when using `:reserved`,
166
167
  # which is loaded by default. Sets an array of words banned for use as
@@ -261,12 +262,7 @@ often better and easier to use {FriendlyId::Slugged slugs}.
261
262
  # Either the friendly_id, or the numeric id cast to a string.
262
263
  def to_param
263
264
  if friendly_id_config.routes == :friendly
264
- if attribute_changed?(friendly_id_config.query_field)
265
- diff = changes[friendly_id_config.query_field]
266
- diff.first || diff.second
267
- else
268
- friendly_id.presence.to_param || super
269
- end
265
+ friendly_id.presence.to_param || super
270
266
  else
271
267
  super
272
268
  end
@@ -25,6 +25,7 @@ module FriendlyId
25
25
  attr_accessor :routes
26
26
 
27
27
  def initialize(model_class, values = nil)
28
+ @base = nil
28
29
  @model_class = model_class
29
30
  @defaults = {}
30
31
  @modules = []
@@ -47,8 +48,8 @@ module FriendlyId
47
48
  #
48
49
  # @param [#to_s,Module] modules Arguments should be Modules, or symbols or
49
50
  # strings that correspond with the name of an addon to use with FriendlyId.
50
- # By default FriendlyId provides `:slugged`, `:history`, `:simple_i18n`,
51
- # and `:scoped`.
51
+ # By default FriendlyId provides `:slugged`, `:finders`, `:history`,
52
+ # `:reserved`, `:simple_i18n`, and `:scoped`.
52
53
  def use(*modules)
53
54
  modules.to_a.flatten.compact.map do |object|
54
55
  mod = get_module(object)
@@ -20,24 +20,26 @@ module FriendlyId
20
20
  return super if args.count != 1 || id.unfriendly_id?
21
21
  first_by_friendly_id(id).tap {|result| return result unless result.nil?}
22
22
  return super if potential_primary_key?(id)
23
- raise ActiveRecord::RecordNotFound, "can't find record with friendly id: #{id.inspect}"
23
+
24
+ raise_not_found_exception(id)
24
25
  end
25
26
 
26
27
  # Returns true if a record with the given id exists.
27
28
  def exists?(conditions = :none)
28
- return super unless conditions.friendly_id?
29
- exists_by_friendly_id?(conditions)
29
+ return super if conditions.unfriendly_id?
30
+ return true if exists_by_friendly_id?(conditions)
31
+ super
30
32
  end
31
33
 
32
34
  # Finds exclusively by the friendly id, completely bypassing original
33
35
  # `find`.
34
36
  # @raise ActiveRecord::RecordNotFound
35
37
  def find_by_friendly_id(id)
36
- first_by_friendly_id(id) or raise ActiveRecord::RecordNotFound, "can't find record with friendly id: #{id.inspect}"
38
+ first_by_friendly_id(id) or raise raise_not_found_exception(id)
37
39
  end
38
40
 
39
41
  def exists_by_friendly_id?(id)
40
- where(friendly_id_config.query_field => id).exists?
42
+ where(friendly_id_config.query_field => parse_friendly_id(id)).exists?
41
43
  end
42
44
 
43
45
  private
@@ -57,7 +59,49 @@ module FriendlyId
57
59
  end
58
60
 
59
61
  def first_by_friendly_id(id)
60
- find_by(friendly_id_config.query_field => id)
62
+ find_by(friendly_id_config.query_field => parse_friendly_id(id))
63
+ end
64
+
65
+ # Parse the given value to make it suitable for use as a slug according to
66
+ # your application's rules.
67
+ #
68
+ # This method is not intended to be invoked directly; FriendlyId uses it
69
+ # internally to process a slug into string to use as a finder.
70
+ #
71
+ # However, if FriendlyId's default slug parsing doesn't suit your needs,
72
+ # you can override this method in your model class to control exactly how
73
+ # slugs are generated.
74
+ #
75
+ # ### Example
76
+ #
77
+ # class Person < ActiveRecord::Base
78
+ # extend FriendlyId
79
+ # friendly_id :name_and_location
80
+ #
81
+ # def name_and_location
82
+ # "#{name} from #{location}"
83
+ # end
84
+ #
85
+ # # Use default slug, but lower case
86
+ # # If `id` is "Jane-Doe" or "JANE-DOE", this finds data by "jane-doe"
87
+ # def parse_friendly_id(slug)
88
+ # super.downcase
89
+ # end
90
+ # end
91
+ #
92
+ # @param [#to_s] value The slug to be parsed.
93
+ # @return The parsed slug, which is not modified by default.
94
+ def parse_friendly_id(value)
95
+ value
96
+ end
97
+
98
+ def raise_not_found_exception(id)
99
+ message = "can't find record with friendly id: #{id.inspect}"
100
+ if ActiveRecord.version < Gem::Version.create('5.0')
101
+ raise ActiveRecord::RecordNotFound.new(message)
102
+ else
103
+ raise ActiveRecord::RecordNotFound.new(message, name, friendly_id_config.query_field, id)
104
+ end
61
105
  end
62
106
 
63
107
  end