slugs 2.0.0 → 2.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 +4 -4
- data/README.md +16 -11
- data/lib/generators/slugs/install_generator.rb +15 -0
- data/lib/generators/slugs/templates/configuration.rb +7 -0
- data/lib/slugs/version.rb +1 -1
- data/test/dummy/config/initializers/slugs.rb +2 -0
- data/test/dummy/log/test.log +72 -0
- data/test/generator_test.rb +19 -0
- metadata +5 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 21b9bbbfe8b2a44f63ffb7edf8eaa22fe97cf6ce
|
4
|
+
data.tar.gz: 41e5ec36704f1c403c4cb62030cdbc83dc4e8fb9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1096e6bdcdb42b39622b0a8175c9c2fe7928b1d29e09fd2c4eedf5d1b59745dc871d653f4279cf7277a935632e935ee13dabd573c684034ea4bb6839fd465bc1
|
7
|
+
data.tar.gz: 072aa30a88b10abad83b03a6b45fb65fc383af4d1c3a9e7495822d7d8778fe13e3f8d727b73a020d439f5347d203a19c4fe24536373ec800f5d4006a0185ac81
|
data/README.md
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
|
6
6
|
# Slugs
|
7
7
|
|
8
|
-
|
8
|
+
Manages slugs for records with minimal efford in rails.
|
9
9
|
|
10
10
|
## Install
|
11
11
|
|
@@ -21,6 +21,11 @@ $ bundle
|
|
21
21
|
|
22
22
|
## Configuration
|
23
23
|
|
24
|
+
Generate the slugs configuration file:
|
25
|
+
```
|
26
|
+
bundle exec rails g slugs:install
|
27
|
+
```
|
28
|
+
|
24
29
|
Add the slug column to the tables of the models you want to have slugs:
|
25
30
|
```ruby
|
26
31
|
t.string :slug
|
@@ -28,10 +33,17 @@ t.string :slug
|
|
28
33
|
|
29
34
|
Update your db:
|
30
35
|
```
|
31
|
-
rake db:migrate
|
36
|
+
bundle exec rake db:migrate
|
32
37
|
```
|
33
38
|
|
34
|
-
|
39
|
+
Configure the proc to decide which records will be slugged:
|
40
|
+
```ruby
|
41
|
+
Slugs.configure do |config|
|
42
|
+
config.use_slug_proc = Proc.new do |record, params|
|
43
|
+
params[:controller] != 'admin'
|
44
|
+
end
|
45
|
+
end
|
46
|
+
```
|
35
47
|
|
36
48
|
## Usage
|
37
49
|
|
@@ -47,18 +59,11 @@ To concatenate the value of multiple fields:
|
|
47
59
|
has_slug :prop1, :prop2, :prop3
|
48
60
|
```
|
49
61
|
|
50
|
-
If you need a very custom slug you can use a lambda, proc or block:
|
51
|
-
```ruby
|
52
|
-
has_slug proc { |record| "#{record.prop}-custom" }
|
53
|
-
```
|
54
|
-
|
55
62
|
To find a record by slug:
|
56
63
|
```ruby
|
57
|
-
Model.
|
64
|
+
Model.find_by slug: 'slug'
|
58
65
|
```
|
59
66
|
|
60
|
-
NOTE: All the path and url helpers will start using the slug by default.
|
61
|
-
|
62
67
|
## Credits
|
63
68
|
|
64
69
|
This gem is maintained and funded by [mmontossi](https://github.com/mmontossi).
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'rails/generators/base'
|
2
|
+
|
3
|
+
module Cronjobs
|
4
|
+
module Generators
|
5
|
+
class InstallGenerator < Rails::Generators::Base
|
6
|
+
|
7
|
+
source_root File.expand_path('../templates', __FILE__)
|
8
|
+
|
9
|
+
def create_definitions_file
|
10
|
+
copy_file 'configuration.rb', 'config/initializers/slugs.rb'
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
data/lib/slugs/version.rb
CHANGED
data/test/dummy/log/test.log
CHANGED
@@ -4188,3 +4188,75 @@ RoutesTest: test_optimized_url_helper
|
|
4188
4188
|
[1m[36m (0.2ms)[0m [1mSELECT COUNT(*) FROM "shops" WHERE "shops"."slug" = $1 AND ("shops"."id" != $2)[0m [["slug", "guitar-shop"], ["id", 234]]
|
4189
4189
|
[1m[35m (0.1ms)[0m RELEASE SAVEPOINT active_record_1
|
4190
4190
|
[1m[36m (0.2ms)[0m [1mROLLBACK[0m
|
4191
|
+
[1m[36mActiveRecord::SchemaMigration Load (0.3ms)[0m [1mSELECT "schema_migrations".* FROM "schema_migrations"[0m
|
4192
|
+
[1m[35m (0.3ms)[0m BEGIN
|
4193
|
+
------------------------------------
|
4194
|
+
GeneratorsTest: test_file_generation
|
4195
|
+
------------------------------------
|
4196
|
+
[1m[36m (0.2ms)[0m [1mROLLBACK[0m
|
4197
|
+
[1m[35m (0.1ms)[0m BEGIN
|
4198
|
+
----------------------
|
4199
|
+
RecordsTest: test_save
|
4200
|
+
----------------------
|
4201
|
+
[1m[36m (0.1ms)[0m [1mSAVEPOINT active_record_1[0m
|
4202
|
+
[1m[35mSQL (0.4ms)[0m INSERT INTO "users" ("first_name", "last_name", "slug") VALUES ($1, $2, $3) RETURNING "id" [["first_name", "Zakk"], ["last_name", "Wylde"], ["slug", "zakk-wylde"]]
|
4203
|
+
[1m[36m (0.3ms)[0m [1mSELECT COUNT(*) FROM "users" WHERE "users"."slug" = $1 AND ("users"."id" != $2)[0m [["slug", "zakk-wylde"], ["id", 48]]
|
4204
|
+
[1m[35m (0.1ms)[0m RELEASE SAVEPOINT active_record_1
|
4205
|
+
[1m[36m (0.1ms)[0m [1mSAVEPOINT active_record_1[0m
|
4206
|
+
[1m[35mSQL (0.2ms)[0m UPDATE "users" SET "slug" = $1 WHERE "users"."id" = $2 [["slug", "yngwie-malmsteen"], ["id", 48]]
|
4207
|
+
[1m[36m (0.2ms)[0m [1mSELECT COUNT(*) FROM "users" WHERE "users"."slug" = $1 AND ("users"."id" != $2)[0m [["slug", "yngwie-malmsteen"], ["id", 48]]
|
4208
|
+
[1m[35m (0.1ms)[0m RELEASE SAVEPOINT active_record_1
|
4209
|
+
[1m[36m (0.2ms)[0m [1mROLLBACK[0m
|
4210
|
+
[1m[35m (0.1ms)[0m BEGIN
|
4211
|
+
----------------------------
|
4212
|
+
RecordsTest: test_uniqueness
|
4213
|
+
----------------------------
|
4214
|
+
[1m[36m (0.2ms)[0m [1mSAVEPOINT active_record_1[0m
|
4215
|
+
[1m[35mSQL (0.3ms)[0m INSERT INTO "shops" ("name", "slug") VALUES ($1, $2) RETURNING "id" [["name", "Guitar Shop"], ["slug", "guitar-shop"]]
|
4216
|
+
[1m[36m (0.2ms)[0m [1mSELECT COUNT(*) FROM "shops" WHERE "shops"."slug" = $1 AND ("shops"."id" != $2)[0m [["slug", "guitar-shop"], ["id", 235]]
|
4217
|
+
[1m[35m (0.1ms)[0m RELEASE SAVEPOINT active_record_1
|
4218
|
+
[1m[36m (0.1ms)[0m [1mSAVEPOINT active_record_1[0m
|
4219
|
+
[1m[35mSQL (0.2ms)[0m INSERT INTO "shops" ("name", "slug") VALUES ($1, $2) RETURNING "id" [["name", "Guitar Shop"], ["slug", "guitar-shop"]]
|
4220
|
+
[1m[36m (0.2ms)[0m [1mSELECT COUNT(*) FROM "shops" WHERE "shops"."slug" = $1 AND ("shops"."id" != $2)[0m [["slug", "guitar-shop"], ["id", 236]]
|
4221
|
+
[1m[35mSQL (0.2ms)[0m UPDATE "shops" SET "slug" = 'guitar-shop-236' WHERE "shops"."id" = $1 [["id", 236]]
|
4222
|
+
[1m[36m (0.1ms)[0m [1mRELEASE SAVEPOINT active_record_1[0m
|
4223
|
+
[1m[35mShop Load (0.3ms)[0m SELECT "shops".* FROM "shops" ORDER BY "shops"."id" DESC LIMIT 1
|
4224
|
+
[1m[36m (0.2ms)[0m [1mSAVEPOINT active_record_1[0m
|
4225
|
+
[1m[35mSQL (0.3ms)[0m INSERT INTO "categories" ("name", "shop_id", "slug") VALUES ($1, $2, $3) RETURNING "id" [["name", "Gibson"], ["shop_id", 236], ["slug", "gibson"]]
|
4226
|
+
[1m[36m (0.2ms)[0m [1mSELECT COUNT(*) FROM "categories" WHERE "categories"."shop_id" = $1 AND "categories"."slug" = $2 AND ("categories"."id" != $3)[0m [["shop_id", 236], ["slug", "gibson"], ["id", 98]]
|
4227
|
+
[1m[35m (0.2ms)[0m RELEASE SAVEPOINT active_record_1
|
4228
|
+
[1m[36m (0.2ms)[0m [1mSAVEPOINT active_record_1[0m
|
4229
|
+
[1m[35mSQL (0.2ms)[0m INSERT INTO "categories" ("name", "shop_id", "slug") VALUES ($1, $2, $3) RETURNING "id" [["name", "Gibson"], ["shop_id", 236], ["slug", "gibson"]]
|
4230
|
+
[1m[36m (0.2ms)[0m [1mSELECT COUNT(*) FROM "categories" WHERE "categories"."shop_id" = $1 AND "categories"."slug" = $2 AND ("categories"."id" != $3)[0m [["shop_id", 236], ["slug", "gibson"], ["id", 99]]
|
4231
|
+
[1m[35mSQL (0.2ms)[0m UPDATE "categories" SET "slug" = 'gibson-99' WHERE "categories"."id" = $1 [["id", 99]]
|
4232
|
+
[1m[36m (0.1ms)[0m [1mRELEASE SAVEPOINT active_record_1[0m
|
4233
|
+
[1m[35mCategory Load (0.2ms)[0m SELECT "categories".* FROM "categories" ORDER BY "categories"."id" DESC LIMIT 1
|
4234
|
+
[1m[36m (0.1ms)[0m [1mSAVEPOINT active_record_1[0m
|
4235
|
+
[1m[35mSQL (0.2ms)[0m INSERT INTO "products" ("name", "shop_id", "category_id", "slug") VALUES ($1, $2, $3, $4) RETURNING "id" [["name", "Les Paul"], ["shop_id", 236], ["category_id", 99], ["slug", "les-paul"]]
|
4236
|
+
[1m[36m (0.2ms)[0m [1mSELECT COUNT(*) FROM "products" WHERE "products"."shop_id" = $1 AND "products"."category_id" = $2 AND "products"."slug" = $3 AND ("products"."id" != $4)[0m [["shop_id", 236], ["category_id", 99], ["slug", "les-paul"], ["id", 94]]
|
4237
|
+
[1m[35m (0.1ms)[0m RELEASE SAVEPOINT active_record_1
|
4238
|
+
[1m[36m (0.1ms)[0m [1mSAVEPOINT active_record_1[0m
|
4239
|
+
[1m[35mSQL (0.2ms)[0m INSERT INTO "products" ("name", "shop_id", "category_id", "slug") VALUES ($1, $2, $3, $4) RETURNING "id" [["name", "Les Paul"], ["shop_id", 236], ["category_id", 99], ["slug", "les-paul"]]
|
4240
|
+
[1m[36m (0.2ms)[0m [1mSELECT COUNT(*) FROM "products" WHERE "products"."shop_id" = $1 AND "products"."category_id" = $2 AND "products"."slug" = $3 AND ("products"."id" != $4)[0m [["shop_id", 236], ["category_id", 99], ["slug", "les-paul"], ["id", 95]]
|
4241
|
+
[1m[35mSQL (0.3ms)[0m UPDATE "products" SET "slug" = 'les-paul-95' WHERE "products"."id" = $1 [["id", 95]]
|
4242
|
+
[1m[36m (0.1ms)[0m [1mRELEASE SAVEPOINT active_record_1[0m
|
4243
|
+
[1m[35mProduct Load (0.2ms)[0m SELECT "products".* FROM "products" ORDER BY "products"."id" DESC LIMIT 1
|
4244
|
+
[1m[36m (0.2ms)[0m [1mROLLBACK[0m
|
4245
|
+
[1m[35m (0.1ms)[0m BEGIN
|
4246
|
+
--------------------------
|
4247
|
+
RoutesTest: test_generator
|
4248
|
+
--------------------------
|
4249
|
+
[1m[36m (0.1ms)[0m [1mSAVEPOINT active_record_1[0m
|
4250
|
+
[1m[35mSQL (0.2ms)[0m INSERT INTO "shops" ("name", "slug") VALUES ($1, $2) RETURNING "id" [["name", "Guitar Shop"], ["slug", "guitar-shop"]]
|
4251
|
+
[1m[36m (0.2ms)[0m [1mSELECT COUNT(*) FROM "shops" WHERE "shops"."slug" = $1 AND ("shops"."id" != $2)[0m [["slug", "guitar-shop"], ["id", 237]]
|
4252
|
+
[1m[35m (0.2ms)[0m RELEASE SAVEPOINT active_record_1
|
4253
|
+
[1m[36m (0.2ms)[0m [1mROLLBACK[0m
|
4254
|
+
[1m[35m (0.1ms)[0m BEGIN
|
4255
|
+
-------------------------------------
|
4256
|
+
RoutesTest: test_optimized_url_helper
|
4257
|
+
-------------------------------------
|
4258
|
+
[1m[36m (0.2ms)[0m [1mSAVEPOINT active_record_1[0m
|
4259
|
+
[1m[35mSQL (0.2ms)[0m INSERT INTO "shops" ("name", "slug") VALUES ($1, $2) RETURNING "id" [["name", "Guitar Shop"], ["slug", "guitar-shop"]]
|
4260
|
+
[1m[36m (0.2ms)[0m [1mSELECT COUNT(*) FROM "shops" WHERE "shops"."slug" = $1 AND ("shops"."id" != $2)[0m [["slug", "guitar-shop"], ["id", 238]]
|
4261
|
+
[1m[35m (0.2ms)[0m RELEASE SAVEPOINT active_record_1
|
4262
|
+
[1m[36m (0.1ms)[0m [1mROLLBACK[0m
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'rails/generators'
|
3
|
+
require 'generators/slugs/install_generator'
|
4
|
+
|
5
|
+
class GeneratorsTest < Rails::Generators::TestCase
|
6
|
+
|
7
|
+
tests Cronjobs::Generators::InstallGenerator
|
8
|
+
destination Rails.root.join('tmp')
|
9
|
+
|
10
|
+
teardown do
|
11
|
+
FileUtils.rm_rf destination_root
|
12
|
+
end
|
13
|
+
|
14
|
+
test 'file generation' do
|
15
|
+
run_generator
|
16
|
+
assert_file 'config/initializers/slugs.rb'
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: slugs
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0.
|
4
|
+
version: 2.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- mmontossi
|
@@ -54,6 +54,8 @@ files:
|
|
54
54
|
- MIT-LICENSE
|
55
55
|
- README.md
|
56
56
|
- Rakefile
|
57
|
+
- lib/generators/slugs/install_generator.rb
|
58
|
+
- lib/generators/slugs/templates/configuration.rb
|
57
59
|
- lib/slugs.rb
|
58
60
|
- lib/slugs/concern.rb
|
59
61
|
- lib/slugs/configuration.rb
|
@@ -108,6 +110,7 @@ files:
|
|
108
110
|
- test/dummy/public/422.html
|
109
111
|
- test/dummy/public/500.html
|
110
112
|
- test/dummy/public/favicon.ico
|
113
|
+
- test/generator_test.rb
|
111
114
|
- test/records_test.rb
|
112
115
|
- test/routes_test.rb
|
113
116
|
- test/test_helper.rb
|
@@ -182,6 +185,7 @@ test_files:
|
|
182
185
|
- test/dummy/public/500.html
|
183
186
|
- test/dummy/public/favicon.ico
|
184
187
|
- test/dummy/Rakefile
|
188
|
+
- test/generator_test.rb
|
185
189
|
- test/records_test.rb
|
186
190
|
- test/routes_test.rb
|
187
191
|
- test/test_helper.rb
|