Skip to content

Instantly share code, notes, and snippets.

@nbulaj
Last active November 14, 2019 12:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nbulaj/ea434c69d0faf910319efc31a3f85865 to your computer and use it in GitHub Desktop.
Save nbulaj/ea434c69d0faf910319efc31a3f85865 to your computer and use it in GitHub Desktop.
# frozen_string_literal: true
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
# Activate the gem you are reporting the issue against.
gem "activerecord", "6.0.0"
gem "sqlite3"
gem "paranoia", "~> 2.4", ">= 2.4.1"
end
require "active_record"
require "minitest/autorun"
require "logger"
# 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 :name
t.string :slug, null: false
t.time :deleted_at
end
end
module Sluggable
extend ActiveSupport::Concern
included do
before_create :set_slug
def generate_slug(payload)
slug = payload.underscore.tr("-", "_")
if self.class.where(slug: slug).exists?
"#{slug}_#{SecureRandom.hex(6)}"
else
slug
end
end
def sluggable_value
name
end
private
def set_slug
self.slug = generate_slug(sluggable_value) if slug.blank?
end
end
end
class Post < ActiveRecord::Base
include Sluggable
acts_as_paranoid
end
class BugTest < Minitest::Test
def test_association_stuff
Post.find_or_create_by!(name: "test")
# here we see a deprecation message
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment