slugtastic 0.1.2 → 0.2.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.
- data/.gitignore +18 -0
- data/.rspec +2 -0
- data/.travis.yml +3 -0
- data/CHANGELOG.md +12 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +36 -0
- data/LICENSE +22 -0
- data/README.md +20 -7
- data/Rakefile +5 -12
- data/lib/slugtastic/model_additions.rb +11 -0
- data/lib/slugtastic/railtie.rb +9 -0
- data/lib/slugtastic/version.rb +3 -0
- data/lib/slugtastic.rb +5 -19
- data/slugtastic.gemspec +16 -24
- data/spec/slugtastic/model_additions_spec.rb +24 -0
- data/spec/slugtastic_spec.rb +18 -0
- data/spec/spec_helper.rb +2 -0
- metadata +77 -22
- data/Manifest +0 -5
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
slugtastic (0.2.0.alpha)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
activemodel (3.0.17)
|
10
|
+
activesupport (= 3.0.17)
|
11
|
+
builder (~> 2.1.2)
|
12
|
+
i18n (~> 0.5.0)
|
13
|
+
activesupport (3.0.17)
|
14
|
+
builder (2.1.2)
|
15
|
+
diff-lcs (1.1.3)
|
16
|
+
i18n (0.5.0)
|
17
|
+
rake (0.9.2.2)
|
18
|
+
rspec (2.11.0)
|
19
|
+
rspec-core (~> 2.11.0)
|
20
|
+
rspec-expectations (~> 2.11.0)
|
21
|
+
rspec-mocks (~> 2.11.0)
|
22
|
+
rspec-core (2.11.1)
|
23
|
+
rspec-expectations (2.11.2)
|
24
|
+
diff-lcs (~> 1.1.3)
|
25
|
+
rspec-mocks (2.11.2)
|
26
|
+
supermodel (0.1.6)
|
27
|
+
activemodel (~> 3.0.0)
|
28
|
+
|
29
|
+
PLATFORMS
|
30
|
+
ruby
|
31
|
+
|
32
|
+
DEPENDENCIES
|
33
|
+
rake
|
34
|
+
rspec
|
35
|
+
slugtastic!
|
36
|
+
supermodel
|
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Dan Barber
|
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
CHANGED
@@ -1,15 +1,20 @@
|
|
1
|
-
Slugtastic
|
2
|
-
==========
|
1
|
+
# Slugtastic [](http://travis-ci.org/danbee/slugtastic)
|
3
2
|
|
4
3
|
Simple gem for autogenerating permalink style slugs for your ActiveRecord models.
|
5
4
|
|
6
|
-
##
|
5
|
+
## Installation
|
7
6
|
|
8
|
-
Add this to your Gemfile:
|
7
|
+
Add this line to your application's Gemfile:
|
9
8
|
|
10
|
-
gem
|
9
|
+
gem 'slugtastic'
|
11
10
|
|
12
|
-
And
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install slugtastic
|
13
18
|
|
14
19
|
## Usage
|
15
20
|
|
@@ -19,4 +24,12 @@ Usage is very simple. Just add the following to your model:
|
|
19
24
|
|
20
25
|
This will generate a slug string from the title atrribute and store it in the slug attribute unless the slug already contains a string. The slug is generated pre-validation so you can still use `validates_presence_of :slug`.
|
21
26
|
|
22
|
-
There are no extra options at present.
|
27
|
+
There are no extra options at present.
|
28
|
+
|
29
|
+
## Contributing
|
30
|
+
|
31
|
+
1. Fork it
|
32
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
33
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
34
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
35
|
+
5. Create new Pull Request
|
data/Rakefile
CHANGED
@@ -1,14 +1,7 @@
|
|
1
|
-
|
2
|
-
require
|
3
|
-
require
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
require "bundler/gem_tasks"
|
3
|
+
require "rspec/core/rake_task"
|
4
4
|
|
5
|
-
|
6
|
-
p.description = "A simple slug string generator for ActiveRecord. Will populate a slug attribute from another attribute."
|
7
|
-
p.url = "http://github.com/danbee/slugtastic"
|
8
|
-
p.author = "Dan Barber"
|
9
|
-
p.email = "danbee@gmail.com"
|
10
|
-
p.ignore_pattern = ["tmp/*", "script/*"]
|
11
|
-
p.development_dependencies = []
|
12
|
-
end
|
5
|
+
RSpec::Core::RakeTask.new(:spec)
|
13
6
|
|
14
|
-
|
7
|
+
task default: :spec
|
@@ -0,0 +1,11 @@
|
|
1
|
+
module Slugtastic
|
2
|
+
module ModelAdditions
|
3
|
+
|
4
|
+
def has_slug name, options = { :from => :title }
|
5
|
+
before_validation do |record|
|
6
|
+
send("#{name}=", Slugtastic.generate_slug(send(options[:from]))) if send(name).nil? or send(name).blank?
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
end
|
11
|
+
end
|
data/lib/slugtastic.rb
CHANGED
@@ -1,23 +1,9 @@
|
|
1
|
+
require "slugtastic/version"
|
2
|
+
require "slugtastic/model_additions"
|
3
|
+
require "slugtastic/railtie" if defined? Rails
|
4
|
+
|
1
5
|
module Slugtastic
|
2
|
-
def self.
|
3
|
-
base.extend ClassMethods
|
4
|
-
end
|
5
|
-
|
6
|
-
def generate_slug(string)
|
6
|
+
def self.generate_slug(string)
|
7
7
|
string.downcase.gsub(/ /, '_').gsub(/[^a-z0-9\-_]/, '') unless string.nil?
|
8
8
|
end
|
9
|
-
|
10
|
-
module ClassMethods
|
11
|
-
|
12
|
-
def has_slug(name, options = { :from => :title })
|
13
|
-
before_validation do |record|
|
14
|
-
self[name] = generate_slug(self[options[:from]]) if self[name].nil? or self[name].empty?
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
end
|
19
9
|
end
|
20
|
-
|
21
|
-
class ActiveRecord::Base
|
22
|
-
include Slugtastic
|
23
|
-
end
|
data/slugtastic.gemspec
CHANGED
@@ -1,29 +1,21 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/slugtastic/version', __FILE__)
|
2
3
|
|
3
|
-
Gem::Specification.new do |
|
4
|
-
|
5
|
-
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Dan Barber"]
|
6
|
+
gem.email = ["danbee@gmail.com"]
|
7
|
+
gem.description = %q{A simple slug string generator for ActiveRecord. Will populate a slug attribute from another attribute.}
|
8
|
+
gem.summary = %q{A simple slug string generator for ActiveRecord.}
|
9
|
+
gem.homepage = ""
|
6
10
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
s.files = [%q{Manifest}, %q{README.md}, %q{Rakefile}, %q{lib/slugtastic.rb}, %q{slugtastic.gemspec}]
|
14
|
-
s.homepage = %q{http://github.com/danbee/slugtastic}
|
15
|
-
s.rdoc_options = [%q{--line-numbers}, %q{--inline-source}, %q{--title}, %q{Slugtastic}, %q{--main}, %q{README.md}]
|
16
|
-
s.require_paths = [%q{lib}]
|
17
|
-
s.rubyforge_project = %q{slugtastic}
|
18
|
-
s.rubygems_version = %q{1.8.6}
|
19
|
-
s.summary = %q{A simple slug string generator for ActiveRecord. Will populate a slug attribute from another attribute.}
|
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 = "slugtastic"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = Slugtastic::VERSION
|
20
17
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
25
|
-
else
|
26
|
-
end
|
27
|
-
else
|
28
|
-
end
|
18
|
+
gem.add_development_dependency "rake"
|
19
|
+
gem.add_development_dependency "rspec"
|
20
|
+
gem.add_development_dependency "supermodel"
|
29
21
|
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
class Model < SuperModel::Base
|
5
|
+
include ActiveModel::Validations::Callbacks
|
6
|
+
extend Slugtastic::ModelAdditions
|
7
|
+
attr_accessor :slug, :title
|
8
|
+
has_slug :slug, from: :title
|
9
|
+
end
|
10
|
+
|
11
|
+
describe Slugtastic::ModelAdditions do
|
12
|
+
it "generates a slug from the title" do
|
13
|
+
Model.create!(:title => "A Simple Title").slug.should eq "a_simple_title"
|
14
|
+
end
|
15
|
+
|
16
|
+
it "doesn't regenerate the slug if it already exists" do
|
17
|
+
model = Model.create!(:title => "A Simple Title")
|
18
|
+
model.slug.should eq "a_simple_title"
|
19
|
+
|
20
|
+
model.title = "A new title"
|
21
|
+
model.save
|
22
|
+
model.slug.should eq "a_simple_title"
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe Slugtastic do
|
5
|
+
describe ".generate_slug" do
|
6
|
+
it "generates a slug from a simple string" do
|
7
|
+
Slugtastic.generate_slug("A simple string.").should eq "a_simple_string"
|
8
|
+
end
|
9
|
+
|
10
|
+
it "handles strings with hypens in them" do
|
11
|
+
Slugtastic.generate_slug("A string - with Hyphens").should eq "a_string_-_with_hyphens"
|
12
|
+
end
|
13
|
+
|
14
|
+
it "handles strings with other characters in them" do
|
15
|
+
Slugtastic.generate_slug("A string, with /All sorts!").should eq "a_string_with_all_sorts"
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: slugtastic
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,32 +9,85 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
13
|
-
dependencies:
|
12
|
+
date: 2012-08-18 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rake
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rspec
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: supermodel
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
14
62
|
description: A simple slug string generator for ActiveRecord. Will populate a slug
|
15
63
|
attribute from another attribute.
|
16
|
-
email:
|
64
|
+
email:
|
65
|
+
- danbee@gmail.com
|
17
66
|
executables: []
|
18
67
|
extensions: []
|
19
|
-
extra_rdoc_files:
|
20
|
-
- README.md
|
21
|
-
- lib/slugtastic.rb
|
68
|
+
extra_rdoc_files: []
|
22
69
|
files:
|
23
|
-
-
|
70
|
+
- .gitignore
|
71
|
+
- .rspec
|
72
|
+
- .travis.yml
|
73
|
+
- CHANGELOG.md
|
74
|
+
- Gemfile
|
75
|
+
- Gemfile.lock
|
76
|
+
- LICENSE
|
24
77
|
- README.md
|
25
78
|
- Rakefile
|
26
79
|
- lib/slugtastic.rb
|
80
|
+
- lib/slugtastic/model_additions.rb
|
81
|
+
- lib/slugtastic/railtie.rb
|
82
|
+
- lib/slugtastic/version.rb
|
27
83
|
- slugtastic.gemspec
|
28
|
-
|
84
|
+
- spec/slugtastic/model_additions_spec.rb
|
85
|
+
- spec/slugtastic_spec.rb
|
86
|
+
- spec/spec_helper.rb
|
87
|
+
homepage: ''
|
29
88
|
licenses: []
|
30
89
|
post_install_message:
|
31
|
-
rdoc_options:
|
32
|
-
- --line-numbers
|
33
|
-
- --inline-source
|
34
|
-
- --title
|
35
|
-
- Slugtastic
|
36
|
-
- --main
|
37
|
-
- README.md
|
90
|
+
rdoc_options: []
|
38
91
|
require_paths:
|
39
92
|
- lib
|
40
93
|
required_ruby_version: !ruby/object:Gem::Requirement
|
@@ -48,12 +101,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
48
101
|
requirements:
|
49
102
|
- - ! '>='
|
50
103
|
- !ruby/object:Gem::Version
|
51
|
-
version: '
|
104
|
+
version: '0'
|
52
105
|
requirements: []
|
53
|
-
rubyforge_project:
|
54
|
-
rubygems_version: 1.8.
|
106
|
+
rubyforge_project:
|
107
|
+
rubygems_version: 1.8.23
|
55
108
|
signing_key:
|
56
109
|
specification_version: 3
|
57
|
-
summary: A simple slug string generator for ActiveRecord.
|
58
|
-
|
59
|
-
|
110
|
+
summary: A simple slug string generator for ActiveRecord.
|
111
|
+
test_files:
|
112
|
+
- spec/slugtastic/model_additions_spec.rb
|
113
|
+
- spec/slugtastic_spec.rb
|
114
|
+
- spec/spec_helper.rb
|