Skip to content

Commit

Permalink
Merge pull request #1 from guideline-tech/master
Browse files Browse the repository at this point in the history
bring over stuff from guideline-tech master
  • Loading branch information
davidjairala authored Oct 7, 2017
2 parents 830a7e4 + d681890 commit 7ff2bda
Show file tree
Hide file tree
Showing 14 changed files with 67 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.9.3-p194
2.1.5
6 changes: 6 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ rvm:
- 1.9.2
- 1.9.3
- 2.0.0
- 2.1.5
- jruby

gemfile:
Expand All @@ -23,6 +24,7 @@ gemfile:
- gemfiles/ar32.gemfile
- gemfiles/ar40.gemfile
- gemfiles/ar41.gemfile
- gemfiles/ar42.gemfile

matrix:
exclude:
Expand All @@ -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
3 changes: 2 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ gemspec
gem 'rake'
gem 'mysql2'
gem 'rspec'
gem 'debugger'
gem 'byebug'
gem 'activerecord', '~> 4.2.0'
8 changes: 1 addition & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand All @@ -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.

Expand Down
1 change: 1 addition & 0 deletions gemfiles/ar30.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ gemspec :path => '../'
gem 'rake'
gem 'activerecord', '3.0.20'
gem 'rspec'
gem 'i18n', '~> 0.5.0'

gem 'mysql2', '0.2.11', :platform => :ruby
gem 'activerecord-jdbcmysql-adapter', :platform => :jruby
1 change: 1 addition & 0 deletions gemfiles/ar31.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions gemfiles/ar32.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 12 additions & 0 deletions gemfiles/ar42.gemfile
Original file line number Diff line number Diff line change
@@ -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
16 changes: 16 additions & 0 deletions lib/louisville/extensions/finder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,29 @@ 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



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
Expand Down
21 changes: 19 additions & 2 deletions lib/louisville/extensions/history.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions lib/louisville/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/louisville/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module VERSION

MAJOR = 0
MINOR = 0
PATCH = 2
PATCH = 4
PRE = nil

def self.to_s
Expand Down
8 changes: 4 additions & 4 deletions louisville.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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 for ActiveRecord.}
gem.summary = %q{Simple and Extensible Slugging}
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) }
Expand Down
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
require 'louisville'

begin
require 'debugger'
require 'byebug'
rescue LoadError => e
puts 'running without debugger'
end
Expand Down

0 comments on commit 7ff2bda

Please sign in to comment.