Skip to content

Commit

Permalink
Simplified Unicode slug string handling. Restored compatibility with …
Browse files Browse the repository at this point in the history
…2.0.
  • Loading branch information
Norman Clarke committed Dec 16, 2008
1 parent e0a8dce commit 219d312
Show file tree
Hide file tree
Showing 13 changed files with 114 additions and 237 deletions.
4 changes: 1 addition & 3 deletions Manifest.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@ generators/friendly_id_migration/templates/create_slugs.rb
lib/friendly_id.rb
lib/friendly_id/non_sluggable_class_methods.rb
lib/friendly_id/non_sluggable_instance_methods.rb
lib/friendly_id/shoulda_macros.rb
lib/friendly_id/sluggable_class_methods.rb
lib/friendly_id/sluggable_instance_methods.rb
lib/friendly_id/string_helpers.rb
lib/friendly_id/version.rb
lib/shoulda_macros.rb
lib/slug.rb
lib/tasks/friendly_id.rake
lib/tasks/friendly_id.rb
Expand All @@ -47,5 +46,4 @@ test/schema.rb
test/scoped_model_test.rb
test/slug_test.rb
test/sluggable_test.rb
test/string_helpers_test.rb
test/test_helper.rb
47 changes: 4 additions & 43 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ rather than

http://example.com/states/4323454

You can find a {FriendlyId tutorial here}[http://randomba.org/posts/friendly_id-tutorial].
The {most recent version of the FriendlyId
RDocs}[http://friendly-id.rubyforge.org] can always be found on
Rubyforge[http://www.rubyforge.org].

== Why?

Expand Down Expand Up @@ -195,9 +197,7 @@ discouraged:

== Setting it up

FriendlyId currently works with Rails 2.1.0 and higher. Older versions of
FriendlyId that work with Rails 1.2.x and Rails 2.0.x are available as tags on
Github.
FriendlyId currently works with Rails 2.0.0 and higher.

Here's how to set it up:

Expand Down Expand Up @@ -245,45 +245,6 @@ as of December 2008.
FriendlyId is {hosted on Github}[git://github.com/norman/friendly_id.git], and
we love pull requests. :-)

== Using an older version of FriendlyId

=== 1.0 (for Rails 2.1.0 or higher)

If you're on Rails 2.1.0, you may wish to use a previous version of
FriendlyId. The current FriendlyId works with 2.1. but if for some reason you
don't want to upgrade, you don't have to.

To install the older version, you can {download the 1.0
tarball}[http://github.com/norman/friendly_id/tree/1.0] from Github and unpack
it into your vendor/plugins directory.

Alternatively, you can perform the following steps:

cd vendor/plugins
git clone git://github.com/norman/friendly_id.git
cd friendly_id
git branch 1.0
git pull origin 1.0
rm -rf .git

=== Even older versions (Rails 2.0.x or lower)

If you're on Rails 2.0.x or lower, there's an older version of FriendlyId
tagged on Github for you. You should use this version because the last several
months of FriendlyId have introduced changes that are incompatible with 2.0.

To install it, please {download the
tarball}[http://github.com/norman/friendly_id/tree/Rails-2.0.x] and unpack it
in your vendor/gems directory.

Alternatively, you can perform the following steps:

cd vendor/plugins
git clone git://github.com/norman/friendly_id.git
cd friendly_id
git checkout Rails-2.0.x
rm -rf .git

== Bugs:

Please report them on Lighthouse[http://randomba.lighthouseapp.com/projects/14675-friendly_id].
Expand Down
4 changes: 1 addition & 3 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
require 'hoe'
require 'newgem'
require 'hanna'
require 'hanna/rdoctask'

require File.join(File.dirname(__FILE__), 'lib', 'friendly_id', 'version')

Hoe.new("friendly_id", FriendlyId::Version::STRING) do |p|
Expand Down
2 changes: 0 additions & 2 deletions lib/friendly_id.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require 'unicode'
require 'friendly_id/string_helpers'
require 'friendly_id/shoulda_macros'

# FriendlyId is a comprehensize Rails plugin/gem for slugging and permalinks.
Expand All @@ -9,7 +8,6 @@ module FriendlyId
def self.enable
return if ActiveRecord::Base.methods.include? 'has_friendly_id'
ActiveRecord::Base.class_eval { extend FriendlyId::ClassMethods }
String.class_eval { include FriendlyId::StringHelpers }
Test::Unit::TestCase.class_eval { include FriendlyId::ShouldaMacros }
end

Expand Down
12 changes: 6 additions & 6 deletions lib/friendly_id/non_sluggable_class_methods.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module FriendlyId::NonSluggableClassMethods

def self.extended(base)
def self.extended(base) #:nodoc:#
class << base
alias_method_chain :find_one, :friendly
alias_method_chain :find_some, :friendly
Expand All @@ -9,16 +9,16 @@ class << base

protected

def find_one_with_friendly(id, options)
def find_one_with_friendly(id, options) #:nodoc:#
if id.is_a?(String) && result = send("find_by_#{ friendly_id_options[:column] }", id, options)
result.found_using_friendly_id = true
result.send(:found_using_friendly_id=, true)
else
result = find_one_without_friendly id, options
end
result
end

def find_some_with_friendly(ids_and_names, options)
def find_some_with_friendly(ids_and_names, options) #:nodoc:#
results_by_name = with_scope :find => options do
find :all, :conditions => ["#{ quoted_table_name }.#{ friendly_id_options[:column] } IN (?)", ids_and_names]
end
Expand All @@ -35,7 +35,7 @@ def find_some_with_friendly(ids_and_names, options)

raise ActiveRecord::RecordNotFound, "Couldn't find all #{ name.pluralize } with IDs (#{ ids_and_names * ', ' }) AND #{ sanitize_sql options[:conditions] } (found #{ results.size } results, but was looking for #{ expected_size })" if results.size != expected_size

results_by_name.each { |r| r.found_using_friendly_id = true }
results_by_name.each { |r| r.send(:found_using_friendly_id=, true) }
results
end
end
end
4 changes: 3 additions & 1 deletion lib/friendly_id/non_sluggable_instance_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ def to_param
friendly_id.to_s || id.to_s
end

def found_using_friendly_id=(value)
private

def found_using_friendly_id=(value) #:nodoc#
@found_using_friendly_id = value
end

Expand Down
20 changes: 10 additions & 10 deletions lib/friendly_id/sluggable_class_methods.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module FriendlyId::SluggableClassMethods

def self.extended(base)
def self.extended(base) #:nodoc:#

class << base
alias_method_chain :find_one, :friendly
Expand All @@ -10,8 +10,8 @@ class << base

end

# Finds a single record using the friendly_id, or the record's id.
def find_one_with_friendly(id_or_name, options)
# Finds a single record using the friendly id, or the record's id.
def find_one_with_friendly(id_or_name, options) #:nodoc:#

scope = options.delete(:scope)
return find_one_without_friendly(id_or_name, options) if id_or_name.is_a?(Fixnum)
Expand Down Expand Up @@ -39,8 +39,8 @@ def find_one_with_friendly(id_or_name, options)

end

# Finds multiple records using the friendly_ids, or the records' ids.
def find_some_with_friendly(ids_and_names, options)
# Finds multiple records using the friendly ids, or the records' ids.
def find_some_with_friendly(ids_and_names, options) #:nodoc:#

slugs, ids = get_slugs_and_ids(ids_and_names, options)
results = []
Expand All @@ -63,15 +63,15 @@ def find_some_with_friendly(ids_and_names, options)
results
end

def validate_find_options_with_friendly(options) #:nodoc:
def validate_find_options_with_friendly(options) #:nodoc:#
options.assert_valid_keys([:conditions, :include, :joins, :limit, :offset,
:order, :select, :readonly, :group, :from, :lock, :having, :scope])
end

private

# Assign finder slugs for the results found in find_some_with_friendly
def assign_finder_slugs(slugs, results)
def assign_finder_slugs(slugs, results) #:nodoc:#
slugs.each do |slug|
results.select { |r| r.id == slug.sluggable_id }.each do |result|
result.send(:finder_slug=, slug)
Expand All @@ -80,14 +80,14 @@ def assign_finder_slugs(slugs, results)
end

# Calculate expected result size for find_some_with_friendly
def expected_size(ids_and_names, options)
def expected_size(ids_and_names, options) #:nodoc:#
size = options[:offset] ? ids_and_names.size - options[:offset] : ids_and_names.size
size = options[:limit] if options[:limit] && size > options[:limit]
size
end

# Build arrays of slugs and ids, for the find_some_with_friendly method.
def get_slugs_and_ids(ids_and_names, options)
def get_slugs_and_ids(ids_and_names, options) #:nodoc:#
scope = options.delete(:scope)
slugs = []
ids = []
Expand All @@ -106,4 +106,4 @@ def get_slugs_and_ids(ids_and_names, options)
return slugs, ids
end

end
end
2 changes: 1 addition & 1 deletion lib/friendly_id/sluggable_instance_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def friendly_id
end
alias best_id friendly_id

# Has the basis of our friendly_id changed, requiring the generation of a
# Has the basis of our friendly id changed, requiring the generation of a
# new slug?
def new_slug_needed?
!slug || slug_text != slug.name
Expand Down
80 changes: 0 additions & 80 deletions lib/friendly_id/string_helpers.rb

This file was deleted.

Loading

0 comments on commit 219d312

Please sign in to comment.