mirror of
https://github.com/danbee/slugtastic
synced 2025-03-04 08:49:05 +00:00
Fix specs
This commit is contained in:
parent
1c4478223f
commit
4bedac5aed
4
Rakefile
4
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)
|
||||
|
||||
|
||||
@ -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
|
||||
|
||||
@ -2,8 +2,8 @@ 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
|
||||
# you can specify by adding <tt>:from => {attribute}</tt>.
|
||||
# By default the slug will be generated from the <tt>title</tt> attribute,
|
||||
# but you can specify by adding <tt>:from => {attribute}</tt>.
|
||||
#
|
||||
# class Article < ActiveRecord::Base
|
||||
# has_slug :slug, :from => :title
|
||||
@ -11,17 +11,15 @@ module Slugtastic
|
||||
#
|
||||
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]))
|
||||
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
|
||||
|
||||
@ -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
|
||||
|
||||
@ -1,3 +1,3 @@
|
||||
module Slugtastic
|
||||
VERSION = '1.3.0'.freeze
|
||||
VERSION = "1.3.0".freeze
|
||||
end
|
||||
|
||||
@ -1,22 +1,24 @@
|
||||
# -*- 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.authors = ["Dan Barber"]
|
||||
gem.email = ["danbee@gmail.com"]
|
||||
gem.licenses = ['MIT']
|
||||
gem.description = %q{A simple slug string generator for ActiveRecord. Will populate a slug attribute from another attribute.}
|
||||
gem.summary = %q{A simple slug string generator for ActiveRecord.}
|
||||
gem.licenses = ["MIT"]
|
||||
gem.description = "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.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.name = "slugtastic"
|
||||
gem.require_paths = ["lib"]
|
||||
gem.version = Slugtastic::VERSION
|
||||
|
||||
gem.add_development_dependency "rake", "~> 11.1"
|
||||
gem.add_development_dependency "rspec", "~> 3.4"
|
||||
gem.add_development_dependency "supermodel", "~> 0"
|
||||
gem.add_development_dependency "activemodel", "~> 5"
|
||||
gem.add_development_dependency "rake", "~> 12.3"
|
||||
gem.add_development_dependency "rspec", "~> 3.7"
|
||||
end
|
||||
|
||||
@ -1,9 +1,27 @@
|
||||
# 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
|
||||
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
|
||||
|
||||
class Model < BaseModel
|
||||
@ -18,37 +36,30 @@ end
|
||||
|
||||
class ModelDelimiter < BaseModel
|
||||
attr_accessor :slug, :title
|
||||
slug :slug, delimiter: '_'
|
||||
end
|
||||
|
||||
class ModelAlias < BaseModel
|
||||
attr_accessor :slug, :title
|
||||
has_slug :slug
|
||||
slug :slug, delimiter: "_"
|
||||
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.name = "A new title"
|
||||
model.save
|
||||
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'
|
||||
expect(model.slug).to eq "a-simple-name"
|
||||
end
|
||||
end
|
||||
|
||||
@ -1,35 +1,43 @@
|
||||
# encoding: utf-8
|
||||
require 'spec_helper'
|
||||
|
||||
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
|
||||
|
||||
@ -1,2 +1 @@
|
||||
require 'slugtastic'
|
||||
require 'supermodel'
|
||||
require "slugtastic"
|
||||
|
||||
Loading…
Reference in New Issue
Block a user