Skip to content

Instantly share code, notes, and snippets.

@benben
Created October 17, 2012 10:53
Show Gist options
  • Save benben/3904942 to your computer and use it in GitHub Desktop.
Save benben/3904942 to your computer and use it in GitHub Desktop.
Extracting to_param, find_one functionality out of friendly_id gem
# *****************************************
# * *
# * extracted from *
# * https://github.com/norman/friendly_id *
# * Version: 4.0.8 *
# * *
# * modified by *
# * Benjamin Knofe (github.com/benben) *
# * *
# *****************************************
#
# Example Usage:
#
# class Post < ActiveRecord::Base
# extend MyApp::Slug
# end
#
# License:
#
# Copyright (c) 2008-2012 Norman Clarke and contributors, released under the MIT license.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
module MyApp
module Slug
def self.extended(model_class)
class << model_class
alias relation_without_slug relation
end
model_class.instance_eval do
extend Base
include Model
end
@relation_class = model_class.send(:relation_class)
end
module Base
private
def relation
@relation = nil unless @relation.class <= relation_class
@relation ||= relation_class.new(self, arel_table)
super
end
def relation_class
@relation_class or begin
@relation_class = Class.new(relation_without_slug.class) do
alias_method :find_one_without_slug, :find_one
alias_method :exists_without_slug?, :exists?
protected
def is_sluggy? id
unsluggy_classes = [ActiveRecord::Base, Array, Hash, NilClass, Numeric,
Symbol, TrueClass, FalseClass]
if unsluggy_classes.detect {|klass| id.class <= klass}
false
elsif id.respond_to?(:to_i) && id.to_i.to_s != id.to_s
true
end
end
def find_one id
return super unless is_sluggy? id
where(slug: id).first or super
end
def exists? id = false
return super unless is_sluggy? id
super 'slug' => id
end
end
end
end
end
module Model
def to_param
if diff = changes[:slug]
diff.first || diff.second
else
slug.presence || super
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment