superslug 1.0.0 → 1.1.0
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/.gitignore +2 -0
- data/Gemfile +14 -0
- data/Gemfile.lock +80 -0
- data/LICENSE.md +20 -0
- data/README.md +80 -0
- data/Rakefile +1 -0
- data/lib/superslug.rb +2 -1
- data/lib/superslug/superslug.rb +47 -42
- data/lib/superslug/version.rb +1 -1
- data/superslug.gemspec +18 -9
- metadata +40 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2f0ee11116381b3e24a955f962973bda7198984b
|
4
|
+
data.tar.gz: a10c2275d5ebc3475f2c95633e13595d1ecd34b3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5b25d0056ce67fa436c15a7813873a57767429f6329a2ce99564fa7625f4fe417084db1138a2753b6de42e74ffae9755302ec4957393f07edf24402f7023a25c
|
7
|
+
data.tar.gz: dad171b682434a8d8ec046c27a474570ade5dbb8d449b901774c7575779111731b08373d83b89483c40ee85f753cb3e4dec550dcd25746f8e0d2161fe7db7636
|
data/.gitignore
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
source "https://rubygems.org"
|
2
|
+
|
3
|
+
# Declare your gem's dependencies in mark_it_zero.gemspec.
|
4
|
+
# Bundler will treat runtime dependencies like base dependencies, and
|
5
|
+
# development dependencies will be added by default to the :development group.
|
6
|
+
gemspec
|
7
|
+
|
8
|
+
# Declare any dependencies that are still in development here instead of in
|
9
|
+
# your gemspec. These might include edge Rails or gems from your path or
|
10
|
+
# Git. Remember to move these dependencies to your gemspec before releasing
|
11
|
+
# your gem to rubygems.org.
|
12
|
+
|
13
|
+
# To use debugger
|
14
|
+
# gem 'debugger'
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
superslug (1.0.0)
|
5
|
+
rails (~> 4.1.0)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
actionmailer (4.1.10)
|
11
|
+
actionpack (= 4.1.10)
|
12
|
+
actionview (= 4.1.10)
|
13
|
+
mail (~> 2.5, >= 2.5.4)
|
14
|
+
actionpack (4.1.10)
|
15
|
+
actionview (= 4.1.10)
|
16
|
+
activesupport (= 4.1.10)
|
17
|
+
rack (~> 1.5.2)
|
18
|
+
rack-test (~> 0.6.2)
|
19
|
+
actionview (4.1.10)
|
20
|
+
activesupport (= 4.1.10)
|
21
|
+
builder (~> 3.1)
|
22
|
+
erubis (~> 2.7.0)
|
23
|
+
activemodel (4.1.10)
|
24
|
+
activesupport (= 4.1.10)
|
25
|
+
builder (~> 3.1)
|
26
|
+
activerecord (4.1.10)
|
27
|
+
activemodel (= 4.1.10)
|
28
|
+
activesupport (= 4.1.10)
|
29
|
+
arel (~> 5.0.0)
|
30
|
+
activesupport (4.1.10)
|
31
|
+
i18n (~> 0.6, >= 0.6.9)
|
32
|
+
json (~> 1.7, >= 1.7.7)
|
33
|
+
minitest (~> 5.1)
|
34
|
+
thread_safe (~> 0.1)
|
35
|
+
tzinfo (~> 1.1)
|
36
|
+
arel (5.0.1.20140414130214)
|
37
|
+
builder (3.2.2)
|
38
|
+
erubis (2.7.0)
|
39
|
+
i18n (0.7.0)
|
40
|
+
json (1.8.2)
|
41
|
+
mail (2.6.3)
|
42
|
+
mime-types (>= 1.16, < 3)
|
43
|
+
mime-types (2.5)
|
44
|
+
minitest (5.6.0)
|
45
|
+
rack (1.5.2)
|
46
|
+
rack-test (0.6.3)
|
47
|
+
rack (>= 1.0)
|
48
|
+
rails (4.1.10)
|
49
|
+
actionmailer (= 4.1.10)
|
50
|
+
actionpack (= 4.1.10)
|
51
|
+
actionview (= 4.1.10)
|
52
|
+
activemodel (= 4.1.10)
|
53
|
+
activerecord (= 4.1.10)
|
54
|
+
activesupport (= 4.1.10)
|
55
|
+
bundler (>= 1.3.0, < 2.0)
|
56
|
+
railties (= 4.1.10)
|
57
|
+
sprockets-rails (~> 2.0)
|
58
|
+
railties (4.1.10)
|
59
|
+
actionpack (= 4.1.10)
|
60
|
+
activesupport (= 4.1.10)
|
61
|
+
rake (>= 0.8.7)
|
62
|
+
thor (>= 0.18.1, < 2.0)
|
63
|
+
rake (10.4.2)
|
64
|
+
sprockets (3.0.2)
|
65
|
+
rack (~> 1.0)
|
66
|
+
sprockets-rails (2.2.4)
|
67
|
+
actionpack (>= 3.0)
|
68
|
+
activesupport (>= 3.0)
|
69
|
+
sprockets (>= 2.8, < 4.0)
|
70
|
+
thor (0.19.1)
|
71
|
+
thread_safe (0.3.5)
|
72
|
+
tzinfo (1.2.2)
|
73
|
+
thread_safe (~> 0.1)
|
74
|
+
|
75
|
+
PLATFORMS
|
76
|
+
ruby
|
77
|
+
|
78
|
+
DEPENDENCIES
|
79
|
+
bundler (~> 1.6)
|
80
|
+
superslug!
|
data/LICENSE.md
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2015 Warren Harrison, Sean C Davis
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
SuperSlug
|
2
|
+
==========
|
3
|
+
|
4
|
+
SuperSlug handles automatic generation of slugs (permalinks) for your rails
|
5
|
+
models
|
6
|
+
|
7
|
+
Installation
|
8
|
+
----------
|
9
|
+
|
10
|
+
This gem is meant to work with Ruby on Rails projects. I recommend you add it
|
11
|
+
to your project's Gemfile:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'superslug'
|
15
|
+
```
|
16
|
+
|
17
|
+
Then install your bundle:
|
18
|
+
|
19
|
+
```text
|
20
|
+
$ bundle
|
21
|
+
```
|
22
|
+
|
23
|
+
Usage
|
24
|
+
----------
|
25
|
+
|
26
|
+
All you need are two columns on an Active Record model. I use `title` and
|
27
|
+
`slug` by default, but you can use whatever you want.
|
28
|
+
|
29
|
+
> Note: It's always faster to use string columns unless you absolutely have to
|
30
|
+
> use text.
|
31
|
+
|
32
|
+
When you have your columns, just add the `has_superslug` method into your
|
33
|
+
model.
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
has_superslug(source = :title, dest = :slug, options = {})
|
37
|
+
```
|
38
|
+
|
39
|
+
The `source` column is the column *from which you want to generate the slug*,
|
40
|
+
while the destination (dest) column is *where the sluggified string is stored*.
|
41
|
+
|
42
|
+
### Options
|
43
|
+
|
44
|
+
The only option is `separator`, which defines the character with which to fill
|
45
|
+
blank spaces in the string. For example, if it is a hyphen, then `Hello World`
|
46
|
+
becomes `hello-world`. See below for an example.
|
47
|
+
|
48
|
+
### Example
|
49
|
+
|
50
|
+
If you have a `Page` model that has a `name` and a `permalink` column, and you
|
51
|
+
want to use underscore separators, then your model would look like this:
|
52
|
+
|
53
|
+
```ruby
|
54
|
+
class Page < ActiveRecord::Base
|
55
|
+
has_superslug :name, :permalink, :separator => '_'
|
56
|
+
end
|
57
|
+
```
|
58
|
+
|
59
|
+
### Query Helpers
|
60
|
+
|
61
|
+
When you add the `has_superslug` method to your model, you also get a couple helpers.
|
62
|
+
|
63
|
+
First, SuperSlug assumes you are doing this to make friendly URLs, so it adds a
|
64
|
+
`to_param` method and uses your destination column for the parameter. Rails
|
65
|
+
uses the `id` attribute by default.
|
66
|
+
|
67
|
+
And second, the `find` method on an Active Record class using SuperSlug checks
|
68
|
+
for the slug first. Using the example above, if you ran `Page.find('hello-
|
69
|
+
world')`, it would check for pages with `permalink` matching `hello-world`, and
|
70
|
+
not the ID (which is the default).
|
71
|
+
|
72
|
+
|
73
|
+
Contributing
|
74
|
+
----------
|
75
|
+
|
76
|
+
1. Fork it ( https://github.com/[my-github-username]/cambium/fork )
|
77
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
78
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
79
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
80
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/lib/superslug.rb
CHANGED
data/lib/superslug/superslug.rb
CHANGED
@@ -1,59 +1,64 @@
|
|
1
|
-
|
2
|
-
extend ActiveSupport::Concern
|
1
|
+
class << ActiveRecord::Base
|
3
2
|
|
4
|
-
|
5
|
-
after_save :sluggify_slug
|
3
|
+
def has_superslug(source = :title, dest = :slug, options = {})
|
6
4
|
|
7
|
-
|
5
|
+
source = source.to_s
|
6
|
+
dest = dest.to_s
|
7
|
+
|
8
|
+
# ---------------------------------------- Query Helpers
|
9
|
+
|
10
|
+
def find(input)
|
8
11
|
if input.class == Array
|
9
12
|
super
|
10
13
|
else
|
11
14
|
input.to_i.to_s == input.to_s ? super : find_by_slug(input)
|
12
15
|
end
|
13
16
|
end
|
14
|
-
end
|
15
17
|
|
16
|
-
|
17
|
-
|
18
|
-
update_column(:slug, create_slug)
|
19
|
-
else
|
20
|
-
new_slug = slug.gsub(/[^a-zA-Z0-9 \-]/, "") # remove all bad characters
|
21
|
-
new_slug.gsub!(/\ /, "-") # replace spaces with underscores
|
22
|
-
new_slug.gsub!(/\-+/, "-") # replace repeating underscores
|
23
|
-
update_column(:slug, new_slug) unless slug == new_slug
|
18
|
+
define_method "to_param" do
|
19
|
+
send(dest)
|
24
20
|
end
|
25
|
-
end
|
26
21
|
|
27
|
-
|
28
|
-
slug = self.title.downcase.gsub(/\&/, ' and ') # & -> and
|
29
|
-
slug.gsub!(/[^a-zA-Z0-9 \-]/, "") # remove all bad characters
|
30
|
-
slug.gsub!(/\ /, "-") # replace spaces with underscores
|
31
|
-
slug.gsub!(/\-+/, "-") # replace repeating underscores
|
22
|
+
# ---------------------------------------- Validations
|
32
23
|
|
33
|
-
|
34
|
-
if dups.count == 1 and dups.first != self
|
35
|
-
if self.try(:idx)
|
36
|
-
slug = "#{slug}-#{self.idx}"
|
37
|
-
else
|
38
|
-
slug = "#{slug}-#{self.id}"
|
39
|
-
end
|
40
|
-
end
|
41
|
-
slug
|
42
|
-
end
|
24
|
+
validates :"#{source}", :presence => true
|
43
25
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
26
|
+
# ---------------------------------------- Callbacks
|
27
|
+
|
28
|
+
after_save :"convert_#{source}_to_#{dest}"
|
29
|
+
|
30
|
+
define_method "convert_#{source}_to_#{dest}" do
|
31
|
+
# reference the separator option
|
32
|
+
separator = options[:separator].present? ? options[:separator] : '-'
|
33
|
+
# Set the slug column to the source column if it's
|
34
|
+
# empty
|
35
|
+
superslug = send(dest).blank? ? self.send(source) : self.send(dest)
|
36
|
+
# make lower case
|
37
|
+
superslug = superslug.downcase
|
38
|
+
# replace ampersands with 'and'
|
39
|
+
superslug.gsub!(/\&/, ' and ')
|
40
|
+
# remove all bad characters
|
41
|
+
# (we allow hyphens, underscores, and plus marks)
|
42
|
+
superslug.gsub!(/[^a-zA-Z0-9 \-\_\+]/, "")
|
43
|
+
# replace spaces with underscores
|
44
|
+
superslug.gsub!(/\ /, separator)
|
45
|
+
# replace repeating underscores
|
46
|
+
superslug.gsub!(/#{separator}+/, separator)
|
47
|
+
# Find all records with the same slug value for the
|
48
|
+
# slug column
|
49
|
+
duplicates = self.class.name.constantize.where(dest.to_sym => superslug)
|
50
|
+
# Append the ID to the end of the slug if the slug
|
51
|
+
# column is already taken
|
52
|
+
if (duplicates - [self]).size > 0
|
53
|
+
superslug = "#{superslug}#{separator}#{self.id}"
|
54
|
+
end
|
55
|
+
# Update the column in the database unless it matches
|
56
|
+
# what's already there
|
57
|
+
unless superslug == self.send(dest)
|
58
|
+
update_columns(dest.to_sym => superslug)
|
51
59
|
end
|
52
60
|
end
|
53
|
-
slug
|
54
|
-
end
|
55
61
|
|
56
|
-
def to_param
|
57
|
-
slug
|
58
62
|
end
|
59
|
-
|
63
|
+
|
64
|
+
end
|
data/lib/superslug/version.rb
CHANGED
data/superslug.gemspec
CHANGED
@@ -1,13 +1,22 @@
|
|
1
1
|
lib = File.expand_path('../lib', __FILE__)
|
2
2
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
3
|
require 'superslug/version'
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'superslug'
|
7
|
+
spec.version = SuperSlug::VERSION
|
8
|
+
spec.authors = ['Warren Harrison','Sean Davis']
|
9
|
+
spec.email = ['warren@hungry-media.com','scdavis41@gmail.com']
|
10
|
+
spec.homepage = "https://github.com/seancdavis/mark_it_zero"
|
11
|
+
spec.summary = "Automate slug creation in your rails app"
|
12
|
+
spec.description = ""
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0")
|
16
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
17
|
+
spec.require_paths = ["lib"]
|
18
|
+
|
19
|
+
spec.add_dependency "rails", "~> 4.1.0"
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.6"
|
13
22
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: superslug
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Warren Harrison
|
@@ -9,9 +9,37 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2015-04-
|
13
|
-
dependencies:
|
14
|
-
|
12
|
+
date: 2015-04-27 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rails
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - "~>"
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: 4.1.0
|
21
|
+
type: :runtime
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - "~>"
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: 4.1.0
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: bundler
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - "~>"
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '1.6'
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - "~>"
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '1.6'
|
42
|
+
description: ''
|
15
43
|
email:
|
16
44
|
- warren@hungry-media.com
|
17
45
|
- scdavis41@gmail.com
|
@@ -19,11 +47,17 @@ executables: []
|
|
19
47
|
extensions: []
|
20
48
|
extra_rdoc_files: []
|
21
49
|
files:
|
50
|
+
- ".gitignore"
|
51
|
+
- Gemfile
|
52
|
+
- Gemfile.lock
|
53
|
+
- LICENSE.md
|
54
|
+
- README.md
|
55
|
+
- Rakefile
|
22
56
|
- lib/superslug.rb
|
23
57
|
- lib/superslug/superslug.rb
|
24
58
|
- lib/superslug/version.rb
|
25
59
|
- superslug.gemspec
|
26
|
-
homepage:
|
60
|
+
homepage: https://github.com/seancdavis/mark_it_zero
|
27
61
|
licenses:
|
28
62
|
- MIT
|
29
63
|
metadata: {}
|
@@ -46,5 +80,5 @@ rubyforge_project:
|
|
46
80
|
rubygems_version: 2.2.0
|
47
81
|
signing_key:
|
48
82
|
specification_version: 4
|
49
|
-
summary:
|
83
|
+
summary: Automate slug creation in your rails app
|
50
84
|
test_files: []
|