1
0
mirror of https://github.com/danbee/slugtastic synced 2025-03-04 08:49:05 +00:00

Significant refactor and cleanup

Thanks Rubocop!
This commit is contained in:
Daniel Barber 2016-03-25 23:55:29 +00:00
parent a6637f463a
commit 1536e51534
Signed by: danbarber
GPG Key ID: 931D8112E0103DD8
10 changed files with 85 additions and 48 deletions

16
.rubocop.yml Normal file
View File

@ -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$/

View File

@ -1,4 +1,4 @@
Copyright (c) 2012 Dan Barber Copyright (c) 2015 Dan Barber
MIT License MIT License
@ -19,4 +19,4 @@ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View File

@ -2,7 +2,10 @@
[![Codeship Status for danbee/slugtastic](https://codeship.com/projects/7a08ef30-0518-0132-d4b6-223503fac7d3/status?branch=master)](https://codeship.com/projects/30797) [![Codeship Status for danbee/slugtastic](https://codeship.com/projects/7a08ef30-0518-0132-d4b6-223503fac7d3/status?branch=master)](https://codeship.com/projects/30797)
Simple gem for autogenerating permalink style slugs for your ActiveRecord models. [![Gem Version](https://badge.fury.io/rb/slugtastic.svg)](https://badge.fury.io/rb/slugtastic)
Simple gem for autogenerating permalink style slugs for your ActiveRecord
models.
## Requirements ## Requirements
@ -29,9 +32,11 @@ Or install it yourself as:
Usage is very simple. Just add the following to your model: 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. There are no extra options at present.

View File

@ -1,6 +1,6 @@
#!/usr/bin/env rake #!/usr/bin/env rake
require "bundler/gem_tasks" require 'bundler/gem_tasks'
require "rspec/core/rake_task" require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:spec) RSpec::Core::RakeTask.new(:spec)

View File

@ -1,12 +1,12 @@
require "slugtastic/version" require 'slugtastic/version'
require "slugtastic/model_additions" require 'slugtastic/model_additions'
require "slugtastic/railtie" if defined? Rails require 'slugtastic/railtie' if defined? Rails
module Slugtastic module Slugtastic
def self.generate_slug(string, delimiter = nil) def self.generate_slug(string, delimiter = nil)
return if string.nil? return if string.nil?
slug = string.parameterize slug = string.parameterize
slug.gsub!("-", delimiter) if delimiter slug.gsub!('-', delimiter) if delimiter
slug slug
end end
end end

View File

@ -1,6 +1,5 @@
module Slugtastic module Slugtastic
module ModelAdditions module ModelAdditions
# To generate a slug from another value, call <tt>has_slug</tt> in any # To generate a slug from another value, call <tt>has_slug</tt> in any
# ActiveRecord model and pass in the name of the slug attribute. # ActiveRecord model and pass in the name of the slug attribute.
# By default the slug will be generated from the <tt>title</tt> attribute, but # By default the slug will be generated from the <tt>title</tt> attribute, but
@ -9,12 +8,20 @@ module Slugtastic
# class Article < ActiveRecord::Base # class Article < ActiveRecord::Base
# has_slug :slug, :from => :title # has_slug :slug, :from => :title
# end # end
def has_slug name, options = {} #
options.reverse_merge!({ :from => :title }) def slug(name, options = {})
before_validation do |record| options.reverse_merge!(from: :title)
send("#{name}=", Slugtastic.generate_slug(send(options[:from]), options[:delimiter])) if send(name).nil? or send(name).blank? before_validation do
if send(name).nil? || send(name).blank?
send("#{name}=", Slugtastic.generate_slug(send(options[:from]), options[:delimiter]))
end
end 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
end end

View File

@ -1,6 +1,6 @@
module Slugtastic module Slugtastic
class Railtie < Rails::Railtie class Railtie < Rails::Railtie
initializer "slugtastic.model_additions" do initializer 'slugtastic.model_additions' do
ActiveSupport.on_load :active_record do ActiveSupport.on_load :active_record do
extend ModelAdditions extend ModelAdditions
end end

View File

@ -8,38 +8,47 @@ end
class Model < BaseModel class Model < BaseModel
attr_accessor :slug, :name attr_accessor :slug, :name
has_slug :slug, from: :name slug :slug, from: :name
end end
class ModelDefault < BaseModel class ModelDefault < BaseModel
attr_accessor :slug, :title attr_accessor :slug, :title
has_slug :slug slug :slug
end end
class ModelDelimiter < BaseModel class ModelDelimiter < BaseModel
attr_accessor :slug, :title attr_accessor :slug, :title
has_slug :slug, delimiter: "_" slug :slug, delimiter: '_'
end
class ModelAlias < BaseModel
attr_accessor :slug, :title
has_slug :slug
end end
describe Slugtastic::ModelAdditions do describe Slugtastic::ModelAdditions do
it "generates a slug from the name" do it 'generates a slug from the name' do
expect(Model.create!(:name => "A Simple Name").slug).to eq "a-simple-name" expect(Model.create!(name: 'A Simple Name').slug).to eq 'a-simple-name'
end end
it "defaults to generating the slug from title" do it 'defaults to generating the slug from title' do
expect(ModelDefault.create!(:title => "A Simple Title").slug).to eq "a-simple-title" expect(ModelDefault.create!(title: 'A Simple Title').slug).to eq 'a-simple-title'
end end
it "generates a slug from the title with delimiter substitutions" do it 'generates a slug from the title with delimiter substitutions' do
expect(ModelDelimiter.create!(:title => "A Simple Title").slug).to eq "a_simple_title" expect(ModelDelimiter.create!(title: 'A Simple Title').slug).to eq 'a_simple_title'
end end
it "doesn't regenerate the slug if it already exists" do it "doesn't regenerate the slug if it already exists" do
model = Model.create!(:name => "A Simple Name") model = Model.create!(name: 'A Simple Name')
expect(model.slug).to eq "a-simple-name" expect(model.slug).to eq 'a-simple-name'
model.title = "A new title" model.title = 'A new title'
model.save 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
end end

View File

@ -2,34 +2,34 @@
require 'spec_helper' require 'spec_helper'
describe Slugtastic do describe Slugtastic do
describe ".generate_slug" do describe '.generate_slug' do
it "returns empty if the input string is empty" do it 'returns empty if the input string is empty' do
expect(Slugtastic.generate_slug("")).to eq "" expect(Slugtastic.generate_slug('')).to eq ''
end end
it "generates a slug from a simple string" do it 'generates a slug from a simple string' do
expect(Slugtastic.generate_slug("A simple string.")).to eq "a-simple-string" expect(Slugtastic.generate_slug('A simple string.')).to eq 'a-simple-string'
end end
it "substitutes hyphens for delimiter if specified" do it 'substitutes hyphens for delimiter if specified' do
expect(Slugtastic.generate_slug("A simple string.", "_")).to eq "a_simple_string" expect(Slugtastic.generate_slug('A simple string.', '_')).to eq 'a_simple_string'
end end
it "generates a slug from a string with numbers" do 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" expect(Slugtastic.generate_slug('Slugtastic was built in 2012.')).to eq 'slugtastic-was-built-in-2012'
end end
it "handles strings with hypens in them" do it 'handles strings with hypens in them' do
expect(Slugtastic.generate_slug("A string - with Hyphens")).to eq "a-string-with-hyphens" expect(Slugtastic.generate_slug('A string - with Hyphens')).to eq 'a-string-with-hyphens'
end end
it "handles strings with other characters in them" do 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" expect(Slugtastic.generate_slug('A string, with /All sorts!')).to eq 'a-string-with-all-sorts'
end end
it "handles basic transliteration" do it 'handles basic transliteration' do
expect(Slugtastic.generate_slug("Un été À la maison.")).to eq "un-ete-a-la-maison" 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" expect(Slugtastic.generate_slug('Ātri brūna lapsa')).to eq 'atri-bruna-lapsa'
end end
end end
end end

View File

@ -1,2 +1,2 @@
require "slugtastic" require 'slugtastic'
require "supermodel" require 'supermodel'