Skip to content
This repository has been archived by the owner on Mar 15, 2021. It is now read-only.

Commit

Permalink
Initial implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
ereslibre committed May 16, 2012
1 parent 8729507 commit 7fbfe59
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 26 deletions.
20 changes: 20 additions & 0 deletions LICENSE
@@ -0,0 +1,20 @@
Copyright (c) 2012 Rafael Fernández López <ereslibre@ereslibre.es>

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.
26 changes: 0 additions & 26 deletions inferred_slug.gemspec
@@ -1,29 +1,3 @@
# encoding: UTF-8

# Copyright (c) 2012 Rafael Fernández López <ereslibre@ereslibre.es>
#
# 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.

$LOAD_PATH.unshift File.expand_path('../lib', __FILE__)
require 'inferred_slug/version'

Expand Down
17 changes: 17 additions & 0 deletions lib/generators/inferred_slug/install/install_generator.rb
@@ -0,0 +1,17 @@
require 'rails/generators'

module InferredSlug
module Generators
class InstallGenerator < ::Rails::Generators::Base

desc "This generator installs inferred slugs on all your models"
source_root File.expand_path('templates', __FILE__)

def copy_slug_hook
say_status "copying", "slug global hook", :green
copy_file "slug.rb", "config/initializers/slug.rb"
end

end
end
end
12 changes: 12 additions & 0 deletions lib/generators/inferred_slug/install/templates/slug.rb
@@ -0,0 +1,12 @@
#
# This generator automatically adds slugs to all your models. If you want to use
# inferred slugs on individual models, delete this file and make your desired
# models include the 'Slug' module. This is:
#
# class MyModel < ActiveRecord::Base
# include Slug
# end
#
class ActiveRecord::Base
include InferredSlug::Slug
end
2 changes: 2 additions & 0 deletions lib/inferred_slug.rb
@@ -0,0 +1,2 @@
require 'inferred_slug/slug'
require 'inferred_slug/finders'
21 changes: 21 additions & 0 deletions lib/inferred_slug/finders.rb
@@ -0,0 +1,21 @@
module InferredSlug
module Finders

def find_by_slug(*args)
record = find(*args)
if record && record.respond_to? :slug
return nil unless record.slug == args.first
end
record
end

def find_by_slug!(*args)
find_by_slug(*args) || raise ActiveRecord::RecordNotFound
end

end
end

class ActiveRecord::Base
extend InferredSlug::Finders
end
17 changes: 17 additions & 0 deletions lib/inferred_slug/slug.rb
@@ -0,0 +1,17 @@
module InferredSlug
module Slug

def slug
if !respond_to?(:name) || name.empty?
id
else
"#{id}-#{name}".to_url
end
end

def to_param
slug
end

end
end

0 comments on commit 7fbfe59

Please sign in to comment.