begin
require 'bundler/inline'
rescue LoadError => e
$stderr.puts 'Bundler version 1.10 or later is required. Please update your Bundler'
raise e
end
gemfile(true) do
gem 'activerecord', '4.2.5'
gem 'sqlite3'
gem 'friendly_id', '~> 5.1.0', :require => false
end
require 'active_record'
require 'minitest/autorun'
require 'logger'
require 'friendly_id'
# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
create_table :posts, force: true do |t|
t.string :title
t.string :slug
end
create_table :friendly_id_slugs do |t|
t.string :slug, :null => false
t.integer :sluggable_id, :null => false
t.string :sluggable_type, :limit => 50
t.string :scope
t.datetime :created_at
end
add_index :friendly_id_slugs, :sluggable_id
add_index :friendly_id_slugs, [:slug, :sluggable_type], length: { slug: 140, sluggable_type: 50 }
add_index :friendly_id_slugs, [:slug, :sluggable_type, :scope], length: { slug: 70, sluggable_type: 50, scope: 70 }, unique: true
add_index :friendly_id_slugs, :sluggable_type
end
FriendlyId.defaults do |config|
config.use :slugged
config.use :finders
config.use :history
end
class Post < ActiveRecord::Base
extend FriendlyId
friendly_id :title
def should_generate_new_friendly_id?
slug.blank? || title_changed?
end
end
class BugTest < Minitest::Test
def test_association_stuff
post = Post.create!(:title => "Post")
assert_equal post.slug, "post"
end
end