friendly_id-globalize 1.0.0.alpha2 → 1.0.0.alpha3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c240c7dc5d34b3afb7338ae100c7202f04d73e96
4
- data.tar.gz: f5655d526ff831df27bad81a74699c824123ffb0
3
+ metadata.gz: 26844163b66ff103307b8f064989dd81cd75dde5
4
+ data.tar.gz: 8fa22695b5243a5a024477d47f82864305c00691
5
5
  SHA512:
6
- metadata.gz: a6b36a173d89897737d18071f84a55eddeeb9edf1afaf61cec54b703a9a2975e1e65ab632486a5873bd707ab994a366c2d6105bbe15fb1008c8b455de332f5a9
7
- data.tar.gz: 07d69a9ec4616a7ba1da6f744cae485b6c1bb7a75abd5b379334492992f1d0a829de35b3dd78454f87193663488b00dcaf77f3d5ef5636fc1ac64b967c50a53b
6
+ metadata.gz: d33a80f8d66e16a97169a66230f7042ef4767adb3b7ee7425205aa1fe969026c0774f2f093d4f5207927ce6de4d440916a9c1554d34a7fabce070d507a3131fb
7
+ data.tar.gz: 24eff53635369319398cb77547f6edd11a7b7a70531504b9e7d95d99c7c9d81af84dba24a66b0e6fb0f3b8a1210af79122ec9238fbedc451a693f1dc320a8479
data/README.md CHANGED
@@ -3,6 +3,12 @@
3
3
  [Globalize](https://github.com/globalize/globalize) support for
4
4
  [FriendlyId](https://github.com/norman/friendly_id).
5
5
 
6
+ ### Installation
7
+ ```ruby
8
+ gem 'friendly_id-globalize'
9
+ rails generate friendly_id_globalize
10
+ ```
11
+
6
12
  ### Translating Slugs Using Globalize
7
13
  The `FriendlyId::Globalize Globalize` module lets you use
8
14
  [Globalize](https://github.com/globalize/globalize) to translate slugs. This
@@ -16,7 +16,7 @@ Gem::Specification.new do |s|
16
16
 
17
17
  s.required_ruby_version = '>= 1.9.3'
18
18
 
19
- s.add_dependency 'friendly_id', '~> 5.1.0', '< 6.0'
19
+ s.add_dependency 'friendly_id', '>= 5.2.0', '< 6.0'
20
20
  s.add_development_dependency 'minitest'
21
21
  s.add_development_dependency 'yard'
22
22
  s.add_development_dependency "globalize"
@@ -1,5 +1,5 @@
1
1
  module FriendlyId
2
2
  module Globalize
3
- VERSION = '1.0.0.alpha2'
3
+ VERSION = '1.0.0.alpha3'
4
4
  end
5
5
  end
@@ -0,0 +1,142 @@
1
+ module FriendlyId
2
+
3
+ =begin
4
+
5
+ ## History: Avoiding 404's When Slugs Change
6
+
7
+ FriendlyId's {FriendlyId::History History} module adds the ability to store a
8
+ log of a model's slugs, so that when its friendly id changes, it's still
9
+ possible to perform finds by the old id.
10
+
11
+ The primary use case for this is avoiding broken URLs.
12
+
13
+ ### Setup
14
+
15
+ In order to use this module, you must add a table to your database schema to
16
+ store the slug records. FriendlyId provides a generator for this purpose:
17
+
18
+ rails generate friendly_id_globalize
19
+ rake db:migrate
20
+
21
+ This will add a table named `friendly_id_slugs`, used by the {FriendlyId::Slug}
22
+ model.
23
+
24
+ ### Considerations
25
+
26
+ Because recording slug history requires creating additional database records,
27
+ this module has an impact on the performance of the associated model's `create`
28
+ method.
29
+
30
+ ### Example
31
+
32
+ class Post < ActiveRecord::Base
33
+ extend FriendlyId
34
+ friendly_id :title, :use => :history
35
+ end
36
+
37
+ class PostsController < ApplicationController
38
+
39
+ before_filter :find_post
40
+
41
+ ...
42
+
43
+ def find_post
44
+ @post = Post.find params[:id]
45
+
46
+ # If an old id or a numeric id was used to find the record, then
47
+ # the request path will not match the post_path, and we should do
48
+ # a 301 redirect that uses the current friendly id.
49
+ if request.path != post_path(@post)
50
+ return redirect_to @post, :status => :moved_permanently
51
+ end
52
+ end
53
+ end
54
+ =end
55
+ module History
56
+
57
+ def self.setup(model_class)
58
+ model_class.instance_eval do
59
+ friendly_id_config.use :slugged
60
+ friendly_id_config.finder_methods = FriendlyId::History::FinderMethods
61
+ if friendly_id_config.uses? :finders
62
+ relation.class.send(:include, friendly_id_config.finder_methods)
63
+ if ActiveRecord::VERSION::MAJOR == 4 && ActiveRecord::VERSION::MINOR == 2
64
+ model_class.send(:extend, friendly_id_config.finder_methods)
65
+ end
66
+ end
67
+ end
68
+ end
69
+
70
+ # Configures the model instance to use the History add-on.
71
+ def self.included(model_class)
72
+ model_class.class_eval do
73
+ has_many :slugs, -> {order("#{Slug.quoted_table_name}.id DESC")}, {
74
+ :as => :sluggable,
75
+ :dependent => :destroy,
76
+ :class_name => Slug.to_s
77
+ }
78
+ after_save :create_slug
79
+ end
80
+ end
81
+
82
+ module FinderMethods
83
+ include ::FriendlyId::FinderMethods
84
+
85
+ def exists_by_friendly_id?(id)
86
+ joins(:slugs, :translations).where(translation_class.arel_table[friendly_id_config.query_field].eq(id)).exists? || joins(:slugs).where(slug_history_clause(id)).exists?
87
+ end
88
+
89
+ private
90
+
91
+ def first_by_friendly_id(id)
92
+ matching_record = where(friendly_id_config.query_field => id).first
93
+ matching_record || slug_table_record(id)
94
+ end
95
+
96
+ def slug_table_record(id)
97
+ select(quoted_table_name + '.*').joins(:slugs).where(slug_history_clause(id)).order(Slug.arel_table[:id].desc).first
98
+ end
99
+
100
+ def slug_history_clause(id)
101
+ Slug.arel_table[:sluggable_type].eq(base_class.to_s).and(Slug.arel_table[:slug].eq(id)).and(Slug.arel_table[:locale].eq(::Globalize.locale))
102
+ end
103
+ end
104
+
105
+ private
106
+
107
+ # If we're updating, don't consider historic slugs for the same record
108
+ # to be conflicts. This will allow a record to revert to a previously
109
+ # used slug.
110
+ def scope_for_slug_generator
111
+ relation = super
112
+ return relation if new_record?
113
+ relation = relation.merge(Slug.where('sluggable_id <> ?', id))
114
+ if friendly_id_config.uses?(:scoped)
115
+ relation = relation.where(Slug.arel_table[:scope].eq(serialized_scope))
116
+ end
117
+ relation
118
+ end
119
+
120
+ def create_slug
121
+ translations.map(&:locale).each do |locale|
122
+ ::Globalize.with_locale(locale) { super_create_slug(locale) }
123
+ end
124
+ end
125
+
126
+ def super_create_slug(locale)
127
+ return unless friendly_id
128
+ return if slugs.where(locale: locale).first.try(:slug) == friendly_id
129
+ # Allow reversion back to a previously used slug
130
+ relation = slugs.where(slug: friendly_id, locale: locale)
131
+ if friendly_id_config.uses?(:scoped)
132
+ relation = relation.where(:scope => serialized_scope)
133
+ end
134
+ relation.delete_all
135
+ slugs.create! do |record|
136
+ record.slug = friendly_id
137
+ record.locale = locale
138
+ record.scope = serialized_scope if friendly_id_config.uses?(:scoped)
139
+ end
140
+ end
141
+ end
142
+ end
@@ -0,0 +1,11 @@
1
+ class AddLocaleToFriendlyIdSlugs < ActiveRecord::Migration
2
+ def change
3
+ add_column :friendly_id_slugs, :locale, :string, length: 2, null: :false, after: :scope
4
+
5
+ remove_index :friendly_id_slugs, [:slug, :sluggable_type]
6
+ add_index :friendly_id_slugs, [:slug, :sluggable_type, :locale], length: { slug: 140, sluggable_type: 50, locale: 2 }
7
+ remove_index :friendly_id_slugs, [:slug, :sluggable_type, :scope]
8
+ add_index :friendly_id_slugs, [:slug, :sluggable_type, :scope, :locale], length: { slug: 70, sluggable_type: 50, scope: 70, locale: 2 }, unique: true, name: :index_friendly_id_slugs_uniqueness
9
+ add_index :friendly_id_slugs, :locale
10
+ end
11
+ end
@@ -0,0 +1,20 @@
1
+ require 'rails/generators'
2
+ require "rails/generators/active_record"
3
+
4
+ # This generator adds a migration for the {FriendlyId::History
5
+ # FriendlyId::History} addon.
6
+ class FriendlyIdGlobalizeGenerator < ActiveRecord::Generators::Base
7
+ # ActiveRecord::Generators::Base inherits from Rails::Generators::NamedBase which requires a NAME parameter for the
8
+ # new table name. Our generator always uses 'friendly_id_slugs', so we just set a random name here.
9
+ argument :name, type: :string, default: 'random_name'
10
+
11
+ class_option :'skip-migration', :type => :boolean, :desc => "Don't generate a migration for the slugs table"
12
+
13
+ source_root File.expand_path('../../friendly_id', __FILE__)
14
+
15
+ # Copies the migration template to db/migrate.
16
+ def copy_files
17
+ return if options['skip-migration']
18
+ migration_template 'migration.rb', 'db/migrate/add_locale_to_friendly_id_slugs.rb'
19
+ end
20
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: friendly_id-globalize
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0.alpha2
4
+ version: 1.0.0.alpha3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Norman Clarke
@@ -9,68 +9,68 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-07-23 00:00:00.000000000 Z
12
+ date: 2017-04-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: friendly_id
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  requirements:
18
- - - "~>"
18
+ - - '>='
19
19
  - !ruby/object:Gem::Version
20
- version: 5.1.0
21
- - - "<"
20
+ version: 5.2.0
21
+ - - <
22
22
  - !ruby/object:Gem::Version
23
23
  version: '6.0'
24
24
  type: :runtime
25
25
  prerelease: false
26
26
  version_requirements: !ruby/object:Gem::Requirement
27
27
  requirements:
28
- - - "~>"
28
+ - - '>='
29
29
  - !ruby/object:Gem::Version
30
- version: 5.1.0
31
- - - "<"
30
+ version: 5.2.0
31
+ - - <
32
32
  - !ruby/object:Gem::Version
33
33
  version: '6.0'
34
34
  - !ruby/object:Gem::Dependency
35
35
  name: minitest
36
36
  requirement: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ">="
38
+ - - '>='
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  type: :development
42
42
  prerelease: false
43
43
  version_requirements: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ">="
45
+ - - '>='
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
48
  - !ruby/object:Gem::Dependency
49
49
  name: yard
50
50
  requirement: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ">="
52
+ - - '>='
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
57
  version_requirements: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - ">="
59
+ - - '>='
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0'
62
62
  - !ruby/object:Gem::Dependency
63
63
  name: globalize
64
64
  requirement: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - ">="
66
+ - - '>='
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
69
  type: :development
70
70
  prerelease: false
71
71
  version_requirements: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - ">="
73
+ - - '>='
74
74
  - !ruby/object:Gem::Version
75
75
  version: '0'
76
76
  description: Adds Globalize support to the FriendlyId gem.
@@ -81,8 +81,8 @@ executables: []
81
81
  extensions: []
82
82
  extra_rdoc_files: []
83
83
  files:
84
- - ".gitignore"
85
- - ".yardopts"
84
+ - .gitignore
85
+ - .yardopts
86
86
  - Gemfile
87
87
  - MIT-LICENSE
88
88
  - README.md
@@ -90,6 +90,9 @@ files:
90
90
  - friendly_id-globalize.gemspec
91
91
  - lib/friendly_id/globalize.rb
92
92
  - lib/friendly_id/globalize/version.rb
93
+ - lib/friendly_id/history.rb
94
+ - lib/friendly_id/migration.rb
95
+ - lib/generators/friendly_id_globalize_generator.rb
93
96
  - test/globalize_test.rb
94
97
  homepage: http://github.com/norman/friendly_id-globalize
95
98
  licenses:
@@ -101,19 +104,18 @@ require_paths:
101
104
  - lib
102
105
  required_ruby_version: !ruby/object:Gem::Requirement
103
106
  requirements:
104
- - - ">="
107
+ - - '>='
105
108
  - !ruby/object:Gem::Version
106
109
  version: 1.9.3
107
110
  required_rubygems_version: !ruby/object:Gem::Requirement
108
111
  requirements:
109
- - - ">"
112
+ - - '>'
110
113
  - !ruby/object:Gem::Version
111
114
  version: 1.3.1
112
115
  requirements: []
113
116
  rubyforge_project:
114
- rubygems_version: 2.4.6
117
+ rubygems_version: 2.0.14.1
115
118
  signing_key:
116
119
  specification_version: 4
117
120
  summary: Globalize support for FriendlyId.
118
121
  test_files: []
119
- has_rdoc: