slugs 1.3.2 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (67) hide show
  1. checksums.yaml +4 -4
  2. data/MIT-LICENSE +1 -1
  3. data/Rakefile +5 -0
  4. data/lib/slugs.rb +36 -4
  5. data/lib/slugs/concern.rb +50 -0
  6. data/lib/slugs/configuration.rb +7 -0
  7. data/lib/slugs/extensions/action_dispatch/generator.rb +24 -0
  8. data/lib/slugs/extensions/action_dispatch/optimized_url_helper.rb +20 -0
  9. data/lib/slugs/extensions/active_record/base.rb +19 -0
  10. data/lib/slugs/railtie.rb +9 -1
  11. data/lib/slugs/version.rb +1 -1
  12. data/test/dummy/Rakefile +1 -2
  13. data/test/dummy/app/assets/javascripts/application.js +2 -2
  14. data/test/dummy/app/assets/stylesheets/application.css +6 -4
  15. data/test/dummy/app/models/category.rb +7 -0
  16. data/test/dummy/app/models/product.rb +8 -0
  17. data/test/dummy/app/models/shop.rb +5 -0
  18. data/test/dummy/app/models/user.rb +5 -0
  19. data/test/dummy/app/views/layouts/application.html.erb +9 -11
  20. data/test/dummy/bin/bundle +0 -0
  21. data/test/dummy/bin/rails +1 -1
  22. data/test/dummy/bin/rake +0 -0
  23. data/test/dummy/bin/setup +29 -0
  24. data/test/dummy/config.ru +1 -1
  25. data/test/dummy/config/application.rb +3 -1
  26. data/test/dummy/config/boot.rb +1 -1
  27. data/test/dummy/config/database.yml +4 -22
  28. data/test/dummy/config/database.yml.travis +12 -0
  29. data/test/dummy/config/environment.rb +1 -1
  30. data/test/dummy/config/environments/development.rb +14 -2
  31. data/test/dummy/config/environments/production.rb +20 -25
  32. data/test/dummy/config/environments/test.rb +8 -10
  33. data/test/dummy/config/initializers/assets.rb +11 -0
  34. data/test/dummy/config/initializers/cookies_serializer.rb +3 -0
  35. data/test/dummy/config/initializers/mime_types.rb +1 -2
  36. data/test/dummy/config/initializers/session_store.rb +1 -1
  37. data/test/dummy/config/initializers/slugs.rb +5 -0
  38. data/test/dummy/config/routes.rb +2 -53
  39. data/test/dummy/config/secrets.yml +22 -0
  40. data/test/dummy/db/migrate/20161016174020_create_users.rb +9 -0
  41. data/test/dummy/db/migrate/20161016174126_create_shops.rb +8 -0
  42. data/test/dummy/db/migrate/20161016174202_create_products.rb +10 -0
  43. data/test/dummy/db/migrate/20161016174225_create_categories.rb +9 -0
  44. data/test/dummy/db/schema.rb +21 -26
  45. data/test/dummy/log/development.log +144 -0
  46. data/test/dummy/log/test.log +4190 -0
  47. data/test/dummy/public/404.html +20 -11
  48. data/test/dummy/public/422.html +20 -11
  49. data/test/dummy/public/500.html +19 -10
  50. data/test/records_test.rb +15 -103
  51. data/test/routes_test.rb +17 -0
  52. data/test/test_helper.rb +4 -12
  53. metadata +49 -54
  54. data/lib/slugs/active_record/base.rb +0 -82
  55. data/lib/slugs/active_record/finders.rb +0 -24
  56. data/lib/slugs/active_record/non_translatable.rb +0 -26
  57. data/lib/slugs/active_record/translatable.rb +0 -49
  58. data/test/dummy/README.rdoc +0 -28
  59. data/test/dummy/app/models/simple.rb +0 -5
  60. data/test/dummy/app/models/translatable.rb +0 -7
  61. data/test/dummy/app/models/translatable_translation.rb +0 -8
  62. data/test/dummy/app/models/without.rb +0 -2
  63. data/test/dummy/config/initializers/secret_token.rb +0 -13
  64. data/test/dummy/db/migrate/20130819183013_create_simples.rb +0 -11
  65. data/test/dummy/db/migrate/20130819183047_create_withouts.rb +0 -9
  66. data/test/dummy/db/migrate/20130819183119_create_translatables.rb +0 -8
  67. data/test/dummy/db/migrate/20130819183257_create_translatable_translations.rb +0 -15
@@ -1,82 +0,0 @@
1
- module Slugs
2
- module ActiveRecord
3
- module Base
4
- extend ActiveSupport::Concern
5
-
6
- def to_param
7
- if self.class.sluggable?
8
- if slug_changed?
9
- slug_was
10
- else
11
- slug
12
- end
13
- else
14
- super
15
- end
16
- end
17
-
18
- protected
19
-
20
- def generate_slug
21
- unless slug_changed?
22
- options = self.class.slug
23
- case options
24
- when Symbol
25
- value = send(options)
26
- when Array
27
- value = options.each.map{ |p| send(p) }.join(' ')
28
- else
29
- value = options.call(self) if options.respond_to? :call
30
- end
31
- value = value.to_s.parameterize
32
- if value.present? and value != slug
33
- previous_value = self.class.unscoped.find_previous_slug(value)
34
- if previous_value.present?
35
- index = previous_value.match(/#{value}-([0-9]+)$/)
36
- if index.present?
37
- value << "-#{index[1].to_i + 1}"
38
- else
39
- value << '-1'
40
- end
41
- end
42
- self.slug = value
43
- end
44
- end
45
- end
46
-
47
- module ClassMethods
48
-
49
- attr_accessor :slug
50
-
51
- def inherited(subclass)
52
- subclass.slug = slug
53
- super
54
- end
55
-
56
- def sluggable?
57
- slug.present?
58
- end
59
-
60
- def has_slug(*args, &block)
61
- unless sluggable?
62
- if try(:translatable?)
63
- include Slugs::ActiveRecord::Translatable
64
- attr_translatable :slug
65
- before_validation :generate_slugs
66
- else
67
- include Slugs::ActiveRecord::NonTranslatable
68
- before_validation :generate_slug
69
- end
70
- include Slugs::ActiveRecord::Finders
71
- if block_given?
72
- self.slug = block
73
- else
74
- self.slug = args.size == 1 ? args[0] : args
75
- end
76
- end
77
- end
78
-
79
- end
80
- end
81
- end
82
- end
@@ -1,24 +0,0 @@
1
- module Slugs
2
- module ActiveRecord
3
- module Finders
4
- extend ActiveSupport::Concern
5
- module ClassMethods
6
-
7
- def slugged
8
- all.extending do
9
-
10
- def find(*args)
11
- find_by_slug args.first
12
- end
13
-
14
- def exists?(*args)
15
- exists_by_slug? args.first
16
- end
17
-
18
- end
19
- end
20
-
21
- end
22
- end
23
- end
24
- end
@@ -1,26 +0,0 @@
1
- module Slugs
2
- module ActiveRecord
3
- module NonTranslatable
4
- extend ActiveSupport::Concern
5
- module ClassMethods
6
-
7
- def find_previous_slug(slug)
8
- where(
9
- 'slug LIKE ? OR slug = ?', "#{slug}-%", slug
10
- ).order(
11
- 'LENGTH(slug) DESC, slug DESC'
12
- ).map(&:slug).select{ |r| r =~ /^#{slug}(-\d+)?$/ }.first
13
- end
14
-
15
- def find_by_slug(id)
16
- find_by slug: id
17
- end
18
-
19
- def exists_by_slug?(id)
20
- exists? slug: id
21
- end
22
-
23
- end
24
- end
25
- end
26
- end
@@ -1,49 +0,0 @@
1
- module Slugs
2
- module ActiveRecord
3
- module Translatable
4
- extend ActiveSupport::Concern
5
-
6
- protected
7
-
8
- def generate_slugs
9
- old_locale = current_locale
10
- I18n.available_locales.each do |locale|
11
- I18n.locale = locale
12
- with_locale locale
13
- generate_slug
14
- end
15
- I18n.locale = old_locale
16
- with_locale old_locale
17
- end
18
-
19
- module ClassMethods
20
-
21
- def find_previous_slug(slug)
22
- t = reflect_on_association(:translations)
23
- joins(
24
- "INNER JOIN #{t.table_name} t ON t.#{t.foreign_key} = #{table_name}.#{t.active_record_primary_key}"
25
- ).where(
26
- "(t.slug LIKE ? OR t.slug = ?) AND t.locale = ?", "#{slug}-%", slug, I18n.locale
27
- ).order(
28
- 'LENGTH(t.slug) DESC, t.slug DESC'
29
- ).map(&:slug).select{ |r| r =~ /^#{slug}(-\d+)?$/ }.first
30
- end
31
-
32
- def find_by_slug(id)
33
- t = reflect_on_association(:translations)
34
- joins(
35
- "INNER JOIN #{t.table_name} t ON t.#{t.foreign_key} = #{table_name}.#{t.active_record_primary_key}"
36
- ).where(
37
- "t.slug = ? AND t.locale = ?", id, I18n.locale
38
- ).readonly(false).first
39
- end
40
-
41
- def exists_by_slug?(id)
42
- t = reflect_on_association(:translations)
43
- joins(:translations).exists? t.table_name.to_sym => { slug: id, locale: I18n.locale }
44
- end
45
-
46
- end
47
- end
48
- end
49
- end
@@ -1,28 +0,0 @@
1
- == README
2
-
3
- This README would normally document whatever steps are necessary to get the
4
- application up and running.
5
-
6
- Things you may want to cover:
7
-
8
- * Ruby version
9
-
10
- * System dependencies
11
-
12
- * Configuration
13
-
14
- * Database creation
15
-
16
- * Database initialization
17
-
18
- * How to run the test suite
19
-
20
- * Services (job queues, cache servers, search engines, etc.)
21
-
22
- * Deployment instructions
23
-
24
- * ...
25
-
26
-
27
- Please feel free to use a different markup language if you do not plan to run
28
- <tt>rake doc:app</tt>.
@@ -1,5 +0,0 @@
1
- class Simple < ActiveRecord::Base
2
-
3
- has_slug :name
4
-
5
- end
@@ -1,7 +0,0 @@
1
- class Translatable < ActiveRecord::Base
2
-
3
- attr_translatable :name, :age
4
-
5
- has_slug :name
6
-
7
- end
@@ -1,8 +0,0 @@
1
- class TranslatableTranslation < ActiveRecord::Base
2
-
3
- belongs_to :translatables
4
-
5
- validates :locale, presence: true
6
- validates :translatable_id, uniqueness: { scope: :locale }
7
-
8
- end
@@ -1,2 +0,0 @@
1
- class Without < ActiveRecord::Base
2
- end
@@ -1,13 +0,0 @@
1
- # Be sure to restart your server when you modify this file.
2
-
3
- # Your secret key is used for verifying the integrity of signed cookies.
4
- # If you change this key, all old signed cookies will become invalid!
5
-
6
- # Make sure the secret is at least 30 characters and all random,
7
- # no regular words or you'll be exposed to dictionary attacks.
8
- # You can use `rake secret` to generate a secure secret key.
9
-
10
- # Make sure your secret_key_base is kept private
11
- # if you're sharing your code publicly.
12
- Dummy::Application.config.secret_token = '9c3fdbe4ad05cd861c28b88f90272c74a5ee9bbc4d7aaa9ca9685303726f61228bac98672426242abe35c78e3b37517702862a399e65bf75a78d00ac9585e0b5'
13
- Dummy::Application.config.secret_key_base = '9c3fdbe4ad05cd861c28b88f90272c74a5ee9bbc4d7aaa9ca9685303726f61228bac98672426242abe35c78e3b37517702862a399e65bf75a78d00ac9585e0b5'
@@ -1,11 +0,0 @@
1
- class CreateSimples < ActiveRecord::Migration
2
- def change
3
- create_table :simples do |t|
4
- t.string :name
5
- t.integer :age
6
- t.string :slug
7
-
8
- t.timestamps
9
- end
10
- end
11
- end
@@ -1,9 +0,0 @@
1
- class CreateWithouts < ActiveRecord::Migration
2
- def change
3
- create_table :withouts do |t|
4
- t.string :name
5
-
6
- t.timestamps
7
- end
8
- end
9
- end
@@ -1,8 +0,0 @@
1
- class CreateTranslatables < ActiveRecord::Migration
2
- def change
3
- create_table :translatables do |t|
4
-
5
- t.timestamps
6
- end
7
- end
8
- end
@@ -1,15 +0,0 @@
1
- class CreateTranslatableTranslations < ActiveRecord::Migration
2
- def change
3
- create_table :translatable_translations do |t|
4
- t.integer :translatable_id
5
- t.string :locale
6
- t.string :name
7
- t.integer :age
8
- t.string :slug
9
-
10
- t.timestamps
11
- end
12
- add_index :translatable_translations, :translatable_id
13
- add_index :translatable_translations, :locale
14
- end
15
- end