From 7fbfe59b8427c0781f528d7ac97ac53288a09b90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Fern=C3=A1ndez=20L=C3=B3pez?= Date: Wed, 16 May 2012 23:35:28 +0200 Subject: [PATCH] Initial implementation --- LICENSE | 20 ++++++++++++++ inferred_slug.gemspec | 26 ------------------- .../install/install_generator.rb | 17 ++++++++++++ .../inferred_slug/install/templates/slug.rb | 12 +++++++++ lib/inferred_slug.rb | 2 ++ lib/inferred_slug/finders.rb | 21 +++++++++++++++ lib/inferred_slug/slug.rb | 17 ++++++++++++ 7 files changed, 89 insertions(+), 26 deletions(-) create mode 100644 LICENSE create mode 100644 lib/generators/inferred_slug/install/install_generator.rb create mode 100644 lib/generators/inferred_slug/install/templates/slug.rb create mode 100644 lib/inferred_slug.rb create mode 100644 lib/inferred_slug/finders.rb create mode 100644 lib/inferred_slug/slug.rb diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..b343ece --- /dev/null +++ b/LICENSE @@ -0,0 +1,20 @@ +Copyright (c) 2012 Rafael Fernández López + +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. \ No newline at end of file diff --git a/inferred_slug.gemspec b/inferred_slug.gemspec index ca500ef..26d6e7b 100644 --- a/inferred_slug.gemspec +++ b/inferred_slug.gemspec @@ -1,29 +1,3 @@ -# encoding: UTF-8 - -# Copyright (c) 2012 Rafael Fernández López -# -# 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' diff --git a/lib/generators/inferred_slug/install/install_generator.rb b/lib/generators/inferred_slug/install/install_generator.rb new file mode 100644 index 0000000..f259509 --- /dev/null +++ b/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 \ No newline at end of file diff --git a/lib/generators/inferred_slug/install/templates/slug.rb b/lib/generators/inferred_slug/install/templates/slug.rb new file mode 100644 index 0000000..0e29a4a --- /dev/null +++ b/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 \ No newline at end of file diff --git a/lib/inferred_slug.rb b/lib/inferred_slug.rb new file mode 100644 index 0000000..435a067 --- /dev/null +++ b/lib/inferred_slug.rb @@ -0,0 +1,2 @@ +require 'inferred_slug/slug' +require 'inferred_slug/finders' \ No newline at end of file diff --git a/lib/inferred_slug/finders.rb b/lib/inferred_slug/finders.rb new file mode 100644 index 0000000..5c2e065 --- /dev/null +++ b/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 \ No newline at end of file diff --git a/lib/inferred_slug/slug.rb b/lib/inferred_slug/slug.rb new file mode 100644 index 0000000..3d59b50 --- /dev/null +++ b/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 \ No newline at end of file