From e63525513f5a232757d3017cc305574655bcc2d8 Mon Sep 17 00:00:00 2001 From: Mike Nelson Date: Tue, 7 Apr 2015 13:00:35 -0400 Subject: [PATCH 1/7] travis and readme updates --- .travis.yml | 6 ++++++ README.md | 8 +------- gemfiles/ar42.gemfile | 12 ++++++++++++ louisville.gemspec | 8 ++++---- 4 files changed, 23 insertions(+), 11 deletions(-) create mode 100644 gemfiles/ar42.gemfile diff --git a/.travis.yml b/.travis.yml index b6e0f03..21efff5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -15,6 +15,7 @@ rvm: - 1.9.2 - 1.9.3 - 2.0.0 + - 2.1.5 - jruby gemfile: @@ -23,6 +24,7 @@ gemfile: - gemfiles/ar32.gemfile - gemfiles/ar40.gemfile - gemfiles/ar41.gemfile + - gemfiles/ar42.gemfile matrix: exclude: @@ -34,3 +36,7 @@ matrix: gemfile: gemfiles/ar41.gemfile - rvm: 1.9.2 gemfile: gemfiles/ar41.gemfile + - rvm: 1.8.7 + gemfile: gemfiles/ar42.gemfile + - rvm: 1.9.2 + gemfile: gemfiles/ar42.gemfile diff --git a/README.md b/README.md index cb5b332..49e744b 100644 --- a/README.md +++ b/README.md @@ -2,12 +2,6 @@ This is not a Swiss Army Bulldozer. This is not a Pseudocephalopod. Contrary to popular belief, this was not written in Kentucky. This is a moderately simple, moderately extensible, moderately opinionated slugging library. -## Installation - -```ruby -gem 'louisville', github: 'mnelson/louisville', tag: 'vmajor.minor.path' -``` - ## Usage Just need the most basic of slugging? @@ -34,7 +28,7 @@ Need a litte more? The `slug` class method accepts an options hash. | :finder | true | true | Adds the finder extension. The finder extension allows `class.find('slug')` to work. | | :finder | false | true | Removes the finder option, disabling the `class.find` override. | | :collision | :string_sequence | :none | Handles collisions by appending a sequence to the slug. A generated slug which collides with an existing slug will gain a "--number". So if there was a record with "foobar" as it's slug and another record generated the slug "foobar", the second record would save as "foobar--2". | -| :collision | :numeric\_sequence | :none | Handles collisions my incrementing a numeric column named `"#{slug\_column}\_sequence"`. With this configuration, the slug column may not be unique but the `[slug, slug\_sequence]` combination would be. | +| :collision | :numeric_sequence | :none | Handles collisions my incrementing a numeric column named `"#{slug_column}_sequence"`. With this configuration, the slug column may not be unique but the `[slug, slug_sequence]` combination would be. | | :setter | Any Valid Ruby Method String | false | Allows the slug generation to be short circuited by providing a setter. Think about a user choosing their username or a page having an seo title. Collisions with the provided value will not be resolved, meaning a validation error will occur if an existing slug is provided. | | :history | true | false | When a record's slug changes this will create a record in the slugs table. The finder and collision resolver extensions respect the existence of the history table if this option is enabled. diff --git a/gemfiles/ar42.gemfile b/gemfiles/ar42.gemfile new file mode 100644 index 0000000..36b4b5f --- /dev/null +++ b/gemfiles/ar42.gemfile @@ -0,0 +1,12 @@ +source 'https://rubygems.org' + +# Specify your gem's dependencies in makara.gemspec +gemspec :path => '../' + + +gem 'rake' +gem 'activerecord', '~> 4.2.0' +gem 'rspec' +gem 'rack' +gem 'mysql2', :platform => :ruby +gem 'activerecord-jdbcmysql-adapter', :platform => :jruby diff --git a/louisville.gemspec b/louisville.gemspec index 4763c72..74a63ad 100644 --- a/louisville.gemspec +++ b/louisville.gemspec @@ -7,10 +7,10 @@ Gem::Specification.new do |gem| gem.name = "louisville" gem.version = Louisville::VERSION gem.authors = ["Mike Nelson"] - gem.email = ["mike@mikeonrails.com"] - gem.description = %q{A simple slugging library} - gem.summary = %q{A simple slugging library} - gem.homepage = "" + gem.email = ["mike@mnelson.io"] + gem.description = %q{A simple and extensible slugging library} + gem.summary = %q{A simple and extensible slugging library} + gem.homepage = "http://github.com/mnelson/louisville" gem.files = `git ls-files`.split($/) gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) } From 961a89a56e1a0458f2c90cf79277d755f3f439b9 Mon Sep 17 00:00:00 2001 From: Mike Nelson Date: Tue, 7 Apr 2015 13:05:45 -0400 Subject: [PATCH 2/7] tighten up i18n restrictions for older bundles --- gemfiles/ar30.gemfile | 1 + gemfiles/ar31.gemfile | 1 + gemfiles/ar32.gemfile | 1 + 3 files changed, 3 insertions(+) diff --git a/gemfiles/ar30.gemfile b/gemfiles/ar30.gemfile index 5424044..1ac8bb8 100644 --- a/gemfiles/ar30.gemfile +++ b/gemfiles/ar30.gemfile @@ -7,6 +7,7 @@ gemspec :path => '../' gem 'rake' gem 'activerecord', '3.0.20' gem 'rspec' +gem 'i18n', '0.6.11' gem 'mysql2', '0.2.11', :platform => :ruby gem 'activerecord-jdbcmysql-adapter', :platform => :jruby diff --git a/gemfiles/ar31.gemfile b/gemfiles/ar31.gemfile index 0183a07..86637df 100644 --- a/gemfiles/ar31.gemfile +++ b/gemfiles/ar31.gemfile @@ -7,5 +7,6 @@ gemspec :path => '../' gem 'rake' gem 'activerecord', '3.1.12' gem 'rspec' +gem 'i18n', '0.6.11' gem 'mysql2', :platform => :ruby gem 'activerecord-jdbcmysql-adapter', :platform => :jruby diff --git a/gemfiles/ar32.gemfile b/gemfiles/ar32.gemfile index 582fbbf..ce05851 100644 --- a/gemfiles/ar32.gemfile +++ b/gemfiles/ar32.gemfile @@ -7,5 +7,6 @@ gemspec :path => '../' gem 'rake' gem 'activerecord', '3.2.19' gem 'rspec' +gem 'i18n', '0.6.11' gem 'mysql2', :platform => :ruby gem 'activerecord-jdbcmysql-adapter', :platform => :jruby From 427ef4b24e8bd272fab2368da811270e0a9a21ad Mon Sep 17 00:00:00 2001 From: Mike Nelson Date: Tue, 7 Apr 2015 13:48:26 -0400 Subject: [PATCH 3/7] rails 4.2 support --- .ruby-version | 2 +- Gemfile | 3 ++- lib/louisville/extensions/finder.rb | 16 ++++++++++++++++ spec/spec_helper.rb | 2 +- 4 files changed, 20 insertions(+), 3 deletions(-) diff --git a/.ruby-version b/.ruby-version index f3a9c9a..cd57a8b 100644 --- a/.ruby-version +++ b/.ruby-version @@ -1 +1 @@ -1.9.3-p194 +2.1.5 diff --git a/Gemfile b/Gemfile index ecc8283..001591e 100644 --- a/Gemfile +++ b/Gemfile @@ -7,4 +7,5 @@ gemspec gem 'rake' gem 'mysql2' gem 'rspec' -gem 'debugger' +gem 'byebug' +gem 'activerecord', '~> 4.2.0' diff --git a/lib/louisville/extensions/finder.rb b/lib/louisville/extensions/finder.rb index 030c2c8..18c8a6b 100644 --- a/lib/louisville/extensions/finder.rb +++ b/lib/louisville/extensions/finder.rb @@ -17,6 +17,11 @@ def self.included(base) base.class_eval do class << self alias_method_chain :relation, :louisville_finder + + if ActiveRecord::VERSION::MAJOR >= 4 && ActiveRecord::VERSION::MINOR >= 2 + alias_method_chain :find, :louisville_finder + end + end end end @@ -24,6 +29,17 @@ class << self module ClassMethods + + def find_with_louisville_finder(*args) + return find_without_lousville_finder(*args) if args.length != 1 + + id = args[0] + id = id.id if ActiveRecord::Base === id + return find_without_louisville_finder(*args) if Louisville::Util.numeric?(id) + + relation_with_louisville_finder.find_one(id) + end + private def relation_with_louisville_finder diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 4dc84e8..be63482 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -9,7 +9,7 @@ require 'louisville' begin - require 'debugger' + require 'byebug' rescue LoadError => e puts 'running without debugger' end From 934f87d0f899f52f5fc3d89cf398f4a2aa6f2eca Mon Sep 17 00:00:00 2001 From: Mike Nelson Date: Tue, 7 Apr 2015 14:01:55 -0400 Subject: [PATCH 4/7] compatible i18n versions for travis --- gemfiles/ar30.gemfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gemfiles/ar30.gemfile b/gemfiles/ar30.gemfile index 1ac8bb8..4a10fed 100644 --- a/gemfiles/ar30.gemfile +++ b/gemfiles/ar30.gemfile @@ -7,7 +7,7 @@ gemspec :path => '../' gem 'rake' gem 'activerecord', '3.0.20' gem 'rspec' -gem 'i18n', '0.6.11' +gem 'i18n', '~> 0.5.0' gem 'mysql2', '0.2.11', :platform => :ruby gem 'activerecord-jdbcmysql-adapter', :platform => :jruby From 2e1f21c85692ba8a67f6c11181380d8cf1c998bb Mon Sep 17 00:00:00 2001 From: Mike Nelson Date: Tue, 7 Apr 2015 14:30:41 -0400 Subject: [PATCH 5/7] version bump --- lib/louisville/version.rb | 2 +- louisville.gemspec | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/louisville/version.rb b/lib/louisville/version.rb index acfffed..6b4e9c4 100644 --- a/lib/louisville/version.rb +++ b/lib/louisville/version.rb @@ -3,7 +3,7 @@ module VERSION MAJOR = 0 MINOR = 0 - PATCH = 2 + PATCH = 3 PRE = nil def self.to_s diff --git a/louisville.gemspec b/louisville.gemspec index 74a63ad..38bdf30 100644 --- a/louisville.gemspec +++ b/louisville.gemspec @@ -8,8 +8,8 @@ Gem::Specification.new do |gem| gem.version = Louisville::VERSION gem.authors = ["Mike Nelson"] gem.email = ["mike@mnelson.io"] - gem.description = %q{A simple and extensible slugging library} - gem.summary = %q{A simple and extensible slugging library} + gem.description = %q{A simple and extensible slugging library for ActiveRecord.} + gem.summary = %q{Simple and Extensible Slugging} gem.homepage = "http://github.com/mnelson/louisville" gem.files = `git ls-files`.split($/) From c2cef6bc590408bfe75243614afe9d87d1611d33 Mon Sep 17 00:00:00 2001 From: Mike Nelson Date: Thu, 9 Apr 2015 16:34:16 -0400 Subject: [PATCH 6/7] allow sluggable_types to be defined rather than assumed to be the sti_name. enables engine based architecture --- lib/louisville/extensions/history.rb | 21 +++++++++++++++++++-- lib/louisville/util.rb | 1 + lib/louisville/version.rb | 2 +- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/lib/louisville/extensions/history.rb b/lib/louisville/extensions/history.rb index 30ae863..76aca11 100644 --- a/lib/louisville/extensions/history.rb +++ b/lib/louisville/extensions/history.rb @@ -12,24 +12,41 @@ module Louisville module Extensions module History + module ClassMethods + + def historical_slug_sluggable_type + self.sti_name + end + + end + def self.included(base) base.class_eval do - # provide an association for easy lookup, joining, etc. - has_many :historical_slugs, :class_name => 'Louisville::Slug', :dependent => :destroy, :as => :sluggable + extend ::Louisville::Extensions::History::ClassMethods # If our slug has changed we should manage the history. after_save :delete_matching_historical_slug, :if => :louisville_slug_changed? after_save :generate_historical_slug, :if => :louisville_slug_changed? + + before_destroy :destroy_historical_slugs! end end + def historical_slugs + return ::Louisville::Slug.where('1=0') unless persisted? + ::Louisville::Slug.where(sluggable_id: id, sluggable_type: ::Louisville::Util.polymorphic_name(self.class)) + end protected + def destroy_historical_slugs! + historical_slugs.destroy_all + end + # First, we delete any previous slugs that this record owned that match the current slug. # This allows a record to return to a previous slug without duplication in the history table. diff --git a/lib/louisville/util.rb b/lib/louisville/util.rb index cf24675..b407c3d 100644 --- a/lib/louisville/util.rb +++ b/lib/louisville/util.rb @@ -30,6 +30,7 @@ def slug_parts(compare) def polymorphic_name(klass) + return klass.historical_slug_sluggable_type if klass.respond_to?(:historical_slug_sluggable_type) klass.base_class.sti_name end diff --git a/lib/louisville/version.rb b/lib/louisville/version.rb index 6b4e9c4..569ac77 100644 --- a/lib/louisville/version.rb +++ b/lib/louisville/version.rb @@ -3,7 +3,7 @@ module VERSION MAJOR = 0 MINOR = 0 - PATCH = 3 + PATCH = 4 PRE = nil def self.to_s From d68189059531741005949e5456f62fce7727172f Mon Sep 17 00:00:00 2001 From: Mike Nelson Date: Thu, 9 Apr 2015 16:47:55 -0400 Subject: [PATCH 7/7] compatible with ruby 1.8 --- lib/louisville/extensions/history.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/louisville/extensions/history.rb b/lib/louisville/extensions/history.rb index 76aca11..6afa098 100644 --- a/lib/louisville/extensions/history.rb +++ b/lib/louisville/extensions/history.rb @@ -36,7 +36,7 @@ def self.included(base) def historical_slugs return ::Louisville::Slug.where('1=0') unless persisted? - ::Louisville::Slug.where(sluggable_id: id, sluggable_type: ::Louisville::Util.polymorphic_name(self.class)) + ::Louisville::Slug.where(:sluggable_id => id, :sluggable_type => ::Louisville::Util.polymorphic_name(self.class)) end