pretty_slugs 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +8 -0
- data/LICENSE +22 -0
- data/README.md +47 -0
- data/Rakefile +3 -0
- data/lib/generators/pretty_slugs_generator.rb +13 -0
- data/lib/pretty_slugs/migration.rb +18 -0
- data/lib/pretty_slugs/version.rb +3 -0
- data/lib/pretty_slugs.rb +119 -0
- data/pretty_slugs.gemspec +17 -0
- metadata +77 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Kevin Hopkins
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
# PrettySlugs
|
2
|
+
|
3
|
+
[Slug Monster](http://www.horrordvds.com/reviews/n-z/slugs/slugs_shot3l.jpg)
|
4
|
+
|
5
|
+
TODO: Write a gem description
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'pretty_slugs'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install pretty_slugs
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
to add the migration(s) to your app, run
|
24
|
+
|
25
|
+
$ rails g pretty_slugs
|
26
|
+
|
27
|
+
then run
|
28
|
+
|
29
|
+
$ rake db:migrate
|
30
|
+
|
31
|
+
To your models that need to implement pretty slugs, add this line:
|
32
|
+
|
33
|
+
include PrettySlugs
|
34
|
+
|
35
|
+
And that is all you need if your model has a :name column,
|
36
|
+
if you want to override the slug via a form field, then
|
37
|
+
you can create a field named :slug in the model's form
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
## Contributing
|
42
|
+
|
43
|
+
1. Fork it
|
44
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
45
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
46
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
47
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'rails/generators'
|
2
|
+
require 'rails/generators/active_record'
|
3
|
+
|
4
|
+
class PrettySlugsGenerator < Rails::Generators::Base
|
5
|
+
include Rails::Generators::Migration
|
6
|
+
extend ActiveRecord::Generators::Migration
|
7
|
+
|
8
|
+
source_root File.expand_path("../../pretty_slugs", __FILE__)
|
9
|
+
|
10
|
+
def copy_files(*args)
|
11
|
+
migration_template "migration.rb", "db/migrate/create_pretty_slugs.rb"
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
class CreatePrettySlugs < ActiveRecord::Migration
|
2
|
+
def self.up
|
3
|
+
create_table :pretty_slugs do |t|
|
4
|
+
t.string :slug, null: false
|
5
|
+
t.integer :sluggable_id, null: false
|
6
|
+
t.string :sluggable_class, null: false
|
7
|
+
t.timestamps
|
8
|
+
end
|
9
|
+
|
10
|
+
add_index :pretty_slugs, :sluggable_id
|
11
|
+
add_index :pretty_slugs, [:slug, :sluggable_class]
|
12
|
+
add_index :pretty_slugs, :sluggable_class
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.down
|
16
|
+
drop_table :pretty_slugs
|
17
|
+
end
|
18
|
+
end
|
data/lib/pretty_slugs.rb
ADDED
@@ -0,0 +1,119 @@
|
|
1
|
+
require "pretty_slugs/version"
|
2
|
+
|
3
|
+
module PrettySlugs
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
included do
|
6
|
+
attr_accessor :slug
|
7
|
+
|
8
|
+
after_commit lambda {
|
9
|
+
action = transaction_include_action?(:create) ? "create" : transaction_include_action?(:destroy) ? "destroy" : "save"
|
10
|
+
case action
|
11
|
+
when "destroy"
|
12
|
+
remove_slug
|
13
|
+
end
|
14
|
+
}
|
15
|
+
|
16
|
+
# Class method overload for ActiveRecord's find. Covers id's passed in as string or number, and slugs passed.
|
17
|
+
# This is wrapped in an eval block because of Ruby 1.9.2 and issues with super in a singleton included by multiple classes.
|
18
|
+
# This is the error otherwise:
|
19
|
+
# super from singleton method that is defined to multiple classes is not supported; this will be fixed in 1.9.3 or later
|
20
|
+
# http://stackoverflow.com/questions/4261615/workaround-for-ruby-1-9-2-super-from-singleton-method-that-is-defined-to-multip
|
21
|
+
# error occurs when same lexical method (which calls super) is defined on the singleton class of two or more objects of different classes, and then that method is called on any of the objects other than the last one the method was defined on...
|
22
|
+
eval %(
|
23
|
+
def self.find(input, *args)
|
24
|
+
if input.is_a?(Integer) || input.is_a?(Fixnum)
|
25
|
+
super
|
26
|
+
elsif input.is_a?(Symbol)
|
27
|
+
super
|
28
|
+
elsif input.is_a?(String) && input.to_i.to_s == input
|
29
|
+
super
|
30
|
+
else
|
31
|
+
obj = Slug.find_by_slug(input)
|
32
|
+
return obj.sluggable_class.constantize.find(obj.sluggable_id, args) rescue nil
|
33
|
+
end
|
34
|
+
end
|
35
|
+
)
|
36
|
+
class Slug < ActiveRecord::Base
|
37
|
+
self.table_name = 'pretty_slugs'
|
38
|
+
attr_accessible :slug, :sluggable_id, :sluggable_class
|
39
|
+
end
|
40
|
+
|
41
|
+
def slug
|
42
|
+
slug = Slug.find_by_sluggable_id_and_sluggable_class(self.id, self.class.to_s).slug rescue nil
|
43
|
+
return slug if slug
|
44
|
+
generate_slug
|
45
|
+
end
|
46
|
+
|
47
|
+
def slug=(val)
|
48
|
+
check_slug_existence
|
49
|
+
if val != nil && val != ""
|
50
|
+
slug = val
|
51
|
+
Slug.find_by_sluggable_id_and_sluggable_class(self.id, self.class.to_s).update_attribute(:slug, val)
|
52
|
+
update_menu_elements(val)
|
53
|
+
else
|
54
|
+
# do nothing
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def check_slug_existence
|
59
|
+
if self.slug.nil?
|
60
|
+
generate_slug
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def generate_slug
|
65
|
+
inc = 1
|
66
|
+
slug = self.to_slug
|
67
|
+
while !Slug.find_by_sluggable_id_and_sluggable_class(self.id, self.class.to_s).nil?
|
68
|
+
slug += "-#{inc}"
|
69
|
+
inc += 1
|
70
|
+
end
|
71
|
+
|
72
|
+
obj = Slug.create({
|
73
|
+
slug: slug,
|
74
|
+
sluggable_id: self.id,
|
75
|
+
sluggable_class: self.class.to_s
|
76
|
+
})
|
77
|
+
|
78
|
+
update_menu_elements(slug)
|
79
|
+
slug
|
80
|
+
end
|
81
|
+
|
82
|
+
def remove_slug
|
83
|
+
Slug.find_by_sluggable_id_and_sluggable_class(self.id, self.class.to_s).destroy rescue true
|
84
|
+
end
|
85
|
+
|
86
|
+
def update_slug
|
87
|
+
Slug.find_by_sluggable_id_and_sluggable_class(self.id, self.class.to_s).update_attribute(slug: self.slug)
|
88
|
+
end
|
89
|
+
|
90
|
+
def update_menu_elements(url=nil)
|
91
|
+
if self.respond_to?("menu_elements") && self.class.to_s == "Page"
|
92
|
+
self.menu_elements.each{|me| me.update_attribute(:url, ("/pages/" + (url.nil? || url.empty? ? self.slug : url)))}
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
def to_slug(val=nil)
|
97
|
+
#strip the string
|
98
|
+
ret = val.nil? ? self[self.respond_to?("name") ? "name" : "title"].strip : val
|
99
|
+
ret = ret.downcase
|
100
|
+
#blow away apostrophes
|
101
|
+
ret.gsub! /['`]/,""
|
102
|
+
|
103
|
+
# @ --> at, and & --> and
|
104
|
+
ret.gsub! /\s*@\s*/, " at "
|
105
|
+
ret.gsub! /\s*&\s*/, " and "
|
106
|
+
|
107
|
+
#replace all non alphanumeric, underscore or periods with underscore
|
108
|
+
ret.gsub! /\s*[^A-Za-z0-9\.\-]\s*/, '_'
|
109
|
+
|
110
|
+
#convert double underscores to single
|
111
|
+
ret.gsub! /_+/,"_"
|
112
|
+
|
113
|
+
#strip off leading/trailing underscore
|
114
|
+
ret.gsub! /\A[_\.]+|[_\.]+\z/,""
|
115
|
+
|
116
|
+
ret
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/pretty_slugs/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Kevin Hopkins", "Timothy Johnson"]
|
6
|
+
gem.email = ["kevin@h-pk-ns.com", "timothy@wearefound.com"]
|
7
|
+
gem.description = %q{Pretty Slugs for Rails models}
|
8
|
+
gem.summary = %q{Pretty Slugs stores sluggified strings for rails models in an extensible way}
|
9
|
+
gem.homepage = ""
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "pretty_slugs"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = PrettySlugs::VERSION
|
17
|
+
end
|
metadata
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pretty_slugs
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 2
|
10
|
+
version: 0.0.2
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Kevin Hopkins
|
14
|
+
- Timothy Johnson
|
15
|
+
autorequire:
|
16
|
+
bindir: bin
|
17
|
+
cert_chain: []
|
18
|
+
|
19
|
+
date: 2013-02-28 00:00:00 Z
|
20
|
+
dependencies: []
|
21
|
+
|
22
|
+
description: Pretty Slugs for Rails models
|
23
|
+
email:
|
24
|
+
- kevin@h-pk-ns.com
|
25
|
+
- timothy@wearefound.com
|
26
|
+
executables: []
|
27
|
+
|
28
|
+
extensions: []
|
29
|
+
|
30
|
+
extra_rdoc_files: []
|
31
|
+
|
32
|
+
files:
|
33
|
+
- .gitignore
|
34
|
+
- Gemfile
|
35
|
+
- LICENSE
|
36
|
+
- README.md
|
37
|
+
- Rakefile
|
38
|
+
- lib/generators/pretty_slugs_generator.rb
|
39
|
+
- lib/pretty_slugs.rb
|
40
|
+
- lib/pretty_slugs/migration.rb
|
41
|
+
- lib/pretty_slugs/version.rb
|
42
|
+
- pretty_slugs.gemspec
|
43
|
+
homepage: ""
|
44
|
+
licenses: []
|
45
|
+
|
46
|
+
post_install_message:
|
47
|
+
rdoc_options: []
|
48
|
+
|
49
|
+
require_paths:
|
50
|
+
- lib
|
51
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
hash: 3
|
57
|
+
segments:
|
58
|
+
- 0
|
59
|
+
version: "0"
|
60
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
hash: 3
|
66
|
+
segments:
|
67
|
+
- 0
|
68
|
+
version: "0"
|
69
|
+
requirements: []
|
70
|
+
|
71
|
+
rubyforge_project:
|
72
|
+
rubygems_version: 1.8.24
|
73
|
+
signing_key:
|
74
|
+
specification_version: 3
|
75
|
+
summary: Pretty Slugs stores sluggified strings for rails models in an extensible way
|
76
|
+
test_files: []
|
77
|
+
|