acts_as_permalink 1.0.3 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +7 -2
- data/lib/acts_as_permalink/version.rb +1 -1
- data/lib/acts_as_permalink.rb +13 -3
- data/lib/config.rb +2 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6d5b611f96f9ccb7d5173c770274bf567f84b286
|
4
|
+
data.tar.gz: 7b061e771348291f3231592651e98486be9ae760
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e048b3627e8d731bf372b62b5bf418d7a15470d2d7835ad2e78a1cc5842f921f138d5af07295f8fc2a631e35ae33023ed99436780af366a167006791114ad5fb
|
7
|
+
data.tar.gz: 168bf287c8b6ab73e33ed730e03d4cff7b4a6563153787c6f5899069e511717ad240a186355a1749d4e6fab34ba257ed3591ba9400fd287f07c577c202e4194d
|
data/README.md
CHANGED
@@ -51,12 +51,15 @@ The `title` and `permalink` fields can be overridden with the following options:
|
|
51
51
|
to: :permalink # Name of the column where the permalink will be stored
|
52
52
|
max_length: 60 # Maximum number of characters the permalink will be
|
53
53
|
underscore: false # Prefer using the `_` character as a replacement over the default `-`
|
54
|
+
scope: nil # Make the permalink unique scoped to a particular attribute
|
54
55
|
|
55
|
-
So, for example you have want to store your permalink in a column called `path_name` and you want to generate your permalink using first and last name, and you want to restrict it to 40 characters, your model would look like:
|
56
|
+
So, for example you have want to store your permalink in a column called `path_name` and you want to generate your permalink using first and last name, and you want to restrict it to 40 characters, and scope the permalink by organization, your model would look like:
|
56
57
|
|
57
58
|
```ruby
|
58
59
|
class User < ActiveRecord::Base
|
59
|
-
acts_as_permalink from: :full_name, to: :path, max_length: 40
|
60
|
+
acts_as_permalink from: :full_name, to: :path, max_length: 40, scope: :organization_id
|
61
|
+
|
62
|
+
belongs_to :organization
|
60
63
|
|
61
64
|
def full_name
|
62
65
|
[first_name, last_name].join(" ")
|
@@ -74,6 +77,8 @@ $ bundle exec rspec
|
|
74
77
|
|
75
78
|
## Changelog
|
76
79
|
|
80
|
+
* 1.1.0 -- Allow the option to `scope: :column_id` for uniquness.
|
81
|
+
|
77
82
|
* 1.0.3 -- Update tests to use DatabaseCleaner, and bump some dependency versions.
|
78
83
|
|
79
84
|
* 1.0.2 -- Use `ActiveSupport::Inflector.transliterate` to convert accented letters to simple ASCII letters.
|
data/lib/acts_as_permalink.rb
CHANGED
@@ -11,9 +11,15 @@ module Acts
|
|
11
11
|
self.acts_as_permalink_config = Acts::Permalink::Config.new(options)
|
12
12
|
|
13
13
|
before_validation :update_permalink, on: :create
|
14
|
-
validates_uniqueness_of self.acts_as_permalink_config.to
|
15
14
|
attr_readonly self.acts_as_permalink_config.to
|
16
15
|
|
16
|
+
if self.acts_as_permalink_config.scope
|
17
|
+
validates self.acts_as_permalink_config.to, uniqueness: {scope: self.acts_as_permalink_config.scope}
|
18
|
+
else
|
19
|
+
validates self.acts_as_permalink_config.to, uniqueness: true
|
20
|
+
end
|
21
|
+
|
22
|
+
|
17
23
|
include Acts::Permalink::InstanceMethods
|
18
24
|
end
|
19
25
|
end
|
@@ -35,15 +41,19 @@ module Acts
|
|
35
41
|
text = [self.class.base_class.to_s, rand(10000)].join(config.separator) if text.blank?
|
36
42
|
text = text.to_permalink(separator: config.separator, max_length: config.max_length)
|
37
43
|
|
44
|
+
# scope it if one is present
|
45
|
+
conditions = {}
|
46
|
+
conditions[config.scope] = self.public_send(config.scope) if config.scope.present?
|
47
|
+
|
38
48
|
# Attempt to find the object by the permalink, and if so there is a collision and we need to de-collision it
|
39
|
-
if self.class.base_class.where(config.to => text).
|
49
|
+
if self.class.base_class.where(conditions.merge(config.to => text)).any?
|
40
50
|
candidate_text = nil
|
41
51
|
|
42
52
|
# This will fail if you have a million records with the same name
|
43
53
|
(1..999999).each do |num|
|
44
54
|
suffix = "#{ config.separator }#{ num }"
|
45
55
|
candidate_text = [text[0...(config.max_length - suffix.length)], suffix].join("")
|
46
|
-
break unless self.class.base_class.where(config.to => candidate_text).
|
56
|
+
break unless self.class.base_class.where(conditions.merge(config.to => candidate_text)).any?
|
47
57
|
end
|
48
58
|
|
49
59
|
text = candidate_text
|
data/lib/config.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
module Acts
|
2
2
|
module Permalink
|
3
3
|
class Config
|
4
|
-
attr_reader :to, :from, :separator, :max_length
|
4
|
+
attr_reader :to, :from, :separator, :max_length, :scope
|
5
5
|
|
6
6
|
def initialize(options={})
|
7
7
|
@config = options.with_indifferent_access
|
@@ -9,6 +9,7 @@ module Acts
|
|
9
9
|
@to = @config[:to].try(:to_sym) || :permalink
|
10
10
|
@from = @config[:from].try(:to_sym) || :title
|
11
11
|
@separator = @config[:underscore] ? "_" : "-"
|
12
|
+
@scope = @config[:scope].presence
|
12
13
|
|
13
14
|
@max_length = @config[:max_length].to_i rescue 0
|
14
15
|
@max_length = 60 unless @max_length > 0
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: acts_as_permalink
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kevin McPhillips
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-11-
|
11
|
+
date: 2015-11-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|