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
@ -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.
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)
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
@ -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.

View File

@ -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)

View File

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

View File

@ -1,6 +1,5 @@
module Slugtastic
module ModelAdditions
# 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.
# By default the slug will be generated from the <tt>title</tt> 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

View File

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

View File

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

View File

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

View File

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