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

Fix specs

This commit is contained in:
Daniel Barber 2018-04-18 15:20:18 -04:00
parent 1c4478223f
commit 4bedac5aed
Signed by: danbarber
GPG Key ID: 931D8112E0103DD8
9 changed files with 84 additions and 66 deletions

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

@ -2,8 +2,8 @@ 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,
# you can specify by adding <tt>:from => {attribute}</tt>. # but you can specify by adding <tt>:from => {attribute}</tt>.
# #
# class Article < ActiveRecord::Base # class Article < ActiveRecord::Base
# has_slug :slug, :from => :title # has_slug :slug, :from => :title
@ -11,17 +11,15 @@ module Slugtastic
# #
def slug(name, options = {}) def slug(name, options = {})
options.reverse_merge!(from: :title) options.reverse_merge!(from: :title)
before_validation do before_validation do
if send(name).nil? || send(name).blank? if send(name).nil? || send(name).blank?
send("#{name}=", Slugtastic.generate_slug(send(options[:from]), options[:delimiter])) send(
"#{name}=",
Slugtastic.generate_slug(send(options[:from]), options[:delimiter]),
)
end 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

@ -1,3 +1,3 @@
module Slugtastic module Slugtastic
VERSION = '1.3.0'.freeze VERSION = "1.3.0".freeze
end end

View File

@ -1,22 +1,24 @@
# -*- encoding: utf-8 -*- # -*- encoding: utf-8 -*-
require File.expand_path('../lib/slugtastic/version', __FILE__)
require File.expand_path("lib/slugtastic/version", __dir__)
Gem::Specification.new do |gem| Gem::Specification.new do |gem|
gem.authors = ["Dan Barber"] gem.authors = ["Dan Barber"]
gem.email = ["danbee@gmail.com"] gem.email = ["danbee@gmail.com"]
gem.licenses = ['MIT'] gem.licenses = ["MIT"]
gem.description = %q{A simple slug string generator for ActiveRecord. Will populate a slug attribute from another attribute.} gem.description = "A simple slug string generator for ActiveRecord. " \
gem.summary = %q{A simple slug string generator for ActiveRecord.} "Will populate a slug attribute from another attribute."
gem.summary = "A simple slug string generator for ActiveRecord."
gem.homepage = "http://danbarber.me/slugtastic" gem.homepage = "http://danbarber.me/slugtastic"
gem.files = `git ls-files`.split($\) gem.files = `git ls-files`.split($\)
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) } gem.executables = gem.files.grep(%r{^bin/}).map { |f| File.basename(f) }
gem.test_files = gem.files.grep(%r{^(test|spec|features)/}) gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
gem.name = "slugtastic" gem.name = "slugtastic"
gem.require_paths = ["lib"] gem.require_paths = ["lib"]
gem.version = Slugtastic::VERSION gem.version = Slugtastic::VERSION
gem.add_development_dependency "rake", "~> 11.1" gem.add_development_dependency "activemodel", "~> 5"
gem.add_development_dependency "rspec", "~> 3.4" gem.add_development_dependency "rake", "~> 12.3"
gem.add_development_dependency "supermodel", "~> 0" gem.add_development_dependency "rspec", "~> 3.7"
end end

View File

@ -1,9 +1,27 @@
# encoding: utf-8 # encoding: utf-8
require 'spec_helper'
class BaseModel < SuperModel::Base require "spec_helper"
require "active_support/core_ext/hash/reverse_merge"
require "active_model"
class BaseModel
include ActiveModel::Validations::Callbacks include ActiveModel::Validations::Callbacks
extend Slugtastic::ModelAdditions extend Slugtastic::ModelAdditions
def initialize(attrs = {})
attrs.each do |key, value|
instance_variable_set(:"@#{key}", value)
end
end
def self.create!(attrs)
new(attrs).save
end
def save
_run_validation_callbacks
self
end
end end
class Model < BaseModel class Model < BaseModel
@ -18,37 +36,30 @@ end
class ModelDelimiter < BaseModel class ModelDelimiter < BaseModel
attr_accessor :slug, :title attr_accessor :slug, :title
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.name = "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

@ -1,35 +1,43 @@
# encoding: utf-8 # encoding: utf-8
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."))
expect(Slugtastic.generate_slug('Ātri brūna lapsa')).to eq 'atri-bruna-lapsa' .to eq "un-ete-a-la-maison"
expect(Slugtastic.generate_slug("Ātri brūna lapsa"))
.to eq "atri-bruna-lapsa"
end end
end end
end end

View File

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