diff --git a/.rubocop.yml b/.rubocop.yml
new file mode 100644
index 0000000..7d174e5
--- /dev/null
+++ b/.rubocop.yml
@@ -0,0 +1,16 @@
+Metrics/LineLength:
+ Max: 120
+
+Documentation:
+ Enabled: false
+
+AllCops:
+ Include:
+ - '**/Rakefile'
+ - '**/config.ru'
+ Exclude:
+ - 'db/**/*'
+ - 'config/**/*'
+ - 'script/**/*'
+ - 'bin/**/*'
+ - !ruby/regexp /old_and_unused\.rb$/
diff --git a/LICENSE b/LICENSE
index 4e37348..47ddd21 100644
--- a/LICENSE
+++ b/LICENSE
@@ -1,4 +1,4 @@
-Copyright (c) 2012 Dan Barber
+Copyright (c) 2015 Dan Barber
MIT License
@@ -19,4 +19,4 @@ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
-WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\ No newline at end of file
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
diff --git a/README.md b/README.md
index b975359..048421c 100644
--- a/README.md
+++ b/README.md
@@ -2,7 +2,10 @@
[](https://codeship.com/projects/30797)
-Simple gem for autogenerating permalink style slugs for your ActiveRecord models.
+[](https://badge.fury.io/rb/slugtastic)
+
+Simple gem for autogenerating permalink style slugs for your ActiveRecord
+models.
## Requirements
@@ -29,9 +32,11 @@ Or install it yourself as:
Usage is very simple. Just add the following to your model:
- has_slug :slug, :from => :title
+ slug :slug, from: :title
-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`.
+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`.
There are no extra options at present.
diff --git a/Rakefile b/Rakefile
index 93b3e16..7d03146 100644
--- a/Rakefile
+++ b/Rakefile
@@ -1,6 +1,6 @@
#!/usr/bin/env rake
-require "bundler/gem_tasks"
-require "rspec/core/rake_task"
+require 'bundler/gem_tasks'
+require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:spec)
diff --git a/lib/slugtastic.rb b/lib/slugtastic.rb
index 6e36a79..e108ebf 100644
--- a/lib/slugtastic.rb
+++ b/lib/slugtastic.rb
@@ -1,12 +1,12 @@
-require "slugtastic/version"
-require "slugtastic/model_additions"
-require "slugtastic/railtie" if defined? Rails
+require 'slugtastic/version'
+require 'slugtastic/model_additions'
+require 'slugtastic/railtie' if defined? Rails
module Slugtastic
def self.generate_slug(string, delimiter = nil)
return if string.nil?
slug = string.parameterize
- slug.gsub!("-", delimiter) if delimiter
+ slug.gsub!('-', delimiter) if delimiter
slug
end
end
diff --git a/lib/slugtastic/model_additions.rb b/lib/slugtastic/model_additions.rb
index b72e7d0..e7bf34d 100644
--- a/lib/slugtastic/model_additions.rb
+++ b/lib/slugtastic/model_additions.rb
@@ -1,6 +1,5 @@
module Slugtastic
module ModelAdditions
-
# To generate a slug from another value, call has_slug in any
# ActiveRecord model and pass in the name of the slug attribute.
# By default the slug will be generated from the title attribute, but
@@ -9,12 +8,20 @@ module Slugtastic
# class Article < ActiveRecord::Base
# has_slug :slug, :from => :title
# end
- def has_slug name, options = {}
- options.reverse_merge!({ :from => :title })
- before_validation do |record|
- send("#{name}=", Slugtastic.generate_slug(send(options[:from]), options[:delimiter])) if send(name).nil? or send(name).blank?
+ #
+ def slug(name, options = {})
+ options.reverse_merge!(from: :title)
+ before_validation do
+ if send(name).nil? || send(name).blank?
+ send("#{name}=", Slugtastic.generate_slug(send(options[:from]), options[:delimiter]))
+ end
end
end
+ def has_slug(name, options = {})
+ $stderr.puts '[deprecated] Slugtastic: `has_slug` has been renamed `slug` and will likely '\
+ 'be removed in a future version.'
+ slug(name, options)
+ end
end
end
diff --git a/lib/slugtastic/railtie.rb b/lib/slugtastic/railtie.rb
index f597cc0..97c1b22 100644
--- a/lib/slugtastic/railtie.rb
+++ b/lib/slugtastic/railtie.rb
@@ -1,6 +1,6 @@
module Slugtastic
class Railtie < Rails::Railtie
- initializer "slugtastic.model_additions" do
+ initializer 'slugtastic.model_additions' do
ActiveSupport.on_load :active_record do
extend ModelAdditions
end
diff --git a/spec/slugtastic/model_additions_spec.rb b/spec/slugtastic/model_additions_spec.rb
index e1fc801..5c66243 100644
--- a/spec/slugtastic/model_additions_spec.rb
+++ b/spec/slugtastic/model_additions_spec.rb
@@ -8,38 +8,47 @@ end
class Model < BaseModel
attr_accessor :slug, :name
- has_slug :slug, from: :name
+ slug :slug, from: :name
end
class ModelDefault < BaseModel
attr_accessor :slug, :title
- has_slug :slug
+ slug :slug
end
class ModelDelimiter < BaseModel
attr_accessor :slug, :title
- has_slug :slug, delimiter: "_"
+ slug :slug, delimiter: '_'
+end
+
+class ModelAlias < BaseModel
+ attr_accessor :slug, :title
+ has_slug :slug
end
describe Slugtastic::ModelAdditions do
- it "generates a slug from the name" do
- expect(Model.create!(:name => "A Simple Name").slug).to eq "a-simple-name"
+ it 'generates a slug from the name' do
+ expect(Model.create!(name: 'A Simple Name').slug).to eq 'a-simple-name'
end
- it "defaults to generating the slug from title" do
- expect(ModelDefault.create!(:title => "A Simple Title").slug).to eq "a-simple-title"
+ it 'defaults to generating the slug from title' do
+ expect(ModelDefault.create!(title: 'A Simple Title').slug).to eq 'a-simple-title'
end
- it "generates a slug from the title with delimiter substitutions" do
- expect(ModelDelimiter.create!(:title => "A Simple Title").slug).to eq "a_simple_title"
+ it 'generates a slug from the title with delimiter substitutions' do
+ expect(ModelDelimiter.create!(title: 'A Simple Title').slug).to eq 'a_simple_title'
end
it "doesn't regenerate the slug if it already exists" do
- model = Model.create!(:name => "A Simple Name")
- expect(model.slug).to eq "a-simple-name"
+ model = Model.create!(name: 'A Simple Name')
+ expect(model.slug).to eq 'a-simple-name'
- model.title = "A new title"
+ model.title = 'A new title'
model.save
- expect(model.slug).to eq "a-simple-name"
+ expect(model.slug).to eq 'a-simple-name'
+ end
+
+ it 'aliases slug to has_slug for backwards compatibility' do
+ expect(ModelAlias.create!(title: 'A Simple Title').slug).to eq 'a-simple-title'
end
end
diff --git a/spec/slugtastic_spec.rb b/spec/slugtastic_spec.rb
index 0a23084..f2a3e53 100644
--- a/spec/slugtastic_spec.rb
+++ b/spec/slugtastic_spec.rb
@@ -2,34 +2,34 @@
require 'spec_helper'
describe Slugtastic do
- describe ".generate_slug" do
- it "returns empty if the input string is empty" do
- expect(Slugtastic.generate_slug("")).to eq ""
+ describe '.generate_slug' do
+ it 'returns empty if the input string is empty' do
+ expect(Slugtastic.generate_slug('')).to eq ''
end
- it "generates a slug from a simple string" do
- expect(Slugtastic.generate_slug("A simple string.")).to eq "a-simple-string"
+ it 'generates a slug from a simple string' do
+ expect(Slugtastic.generate_slug('A simple string.')).to eq 'a-simple-string'
end
- it "substitutes hyphens for delimiter if specified" do
- expect(Slugtastic.generate_slug("A simple string.", "_")).to eq "a_simple_string"
+ it 'substitutes hyphens for delimiter if specified' do
+ expect(Slugtastic.generate_slug('A simple string.', '_')).to eq 'a_simple_string'
end
- it "generates a slug from a string with numbers" do
- expect(Slugtastic.generate_slug("Slugtastic was built in 2012.")).to eq "slugtastic-was-built-in-2012"
+ it 'generates a slug from a string with numbers' do
+ expect(Slugtastic.generate_slug('Slugtastic was built in 2012.')).to eq 'slugtastic-was-built-in-2012'
end
- it "handles strings with hypens in them" do
- expect(Slugtastic.generate_slug("A string - with Hyphens")).to eq "a-string-with-hyphens"
+ it 'handles strings with hypens in them' do
+ expect(Slugtastic.generate_slug('A string - with Hyphens')).to eq 'a-string-with-hyphens'
end
- it "handles strings with other characters in them" do
- expect(Slugtastic.generate_slug("A string, with /All sorts!")).to eq "a-string-with-all-sorts"
+ it 'handles strings with other characters in them' do
+ expect(Slugtastic.generate_slug('A string, with /All sorts!')).to eq 'a-string-with-all-sorts'
end
- it "handles basic transliteration" do
- expect(Slugtastic.generate_slug("Un été À la maison.")).to eq "un-ete-a-la-maison"
- expect(Slugtastic.generate_slug("Ātri brūna lapsa")).to eq "atri-bruna-lapsa"
+ it 'handles basic transliteration' do
+ expect(Slugtastic.generate_slug('Un été À la maison.')).to eq 'un-ete-a-la-maison'
+ expect(Slugtastic.generate_slug('Ātri brūna lapsa')).to eq 'atri-bruna-lapsa'
end
end
end
diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb
index 4618b2a..25053aa 100644
--- a/spec/spec_helper.rb
+++ b/spec/spec_helper.rb
@@ -1,2 +1,2 @@
-require "slugtastic"
-require "supermodel"
+require 'slugtastic'
+require 'supermodel'