Skip to content

Commit

Permalink
Added support for Rails 3.2
Browse files Browse the repository at this point in the history
  • Loading branch information
parndt committed Jan 24, 2012
1 parent 152d19a commit 63417be
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 9 deletions.
4 changes: 2 additions & 2 deletions friendly_id_globalize3.gemspec
@@ -1,13 +1,13 @@
require File.expand_path("../lib/friendly_id/version", __FILE__)

Gem::Specification.new do |s|
s.authors = ["Norman Clarke", "Adrian Mugnolo", "Emilio Tagua"]
s.authors = ["Norman Clarke", "Adrian Mugnolo", "Emilio Tagua", "Philip Arndt"]
s.description = <<-EOM
FriendlyId is the "Swiss Army bulldozer" of slugging and permalink plugins
for Ruby on Rails. It allows you to create pretty URL's and work with
human-friendly strings as if they were numeric ids for ActiveRecord models.
EOM
s.email = ["norman@njclarke.com", "adrian@mugnolo.com", "miloops@gmail.com"]
s.email = ["norman@njclarke.com", "adrian@mugnolo.com", "miloops@gmail.com", "parndt@gmail.com"]
s.files = Dir["lib/**/*.rb", "lib/**/*.rake", "*.md", "MIT-LICENSE",
"Rakefile", "rails/init.rb", "generators/**/*.*", "test/**/*.*",
"extras/**/*.*", ".gemtest"]
Expand Down
21 changes: 17 additions & 4 deletions lib/friendly_id/active_record_adapter/relation.rb
Expand Up @@ -61,8 +61,12 @@ def find_one_with_cached_slug
def find_one_with_slug
sluggable_ids = sluggable_ids_for([id])

if sluggable_ids.size > 1 && fc.scope?
return relation.where(relation.primary_key.in(sluggable_ids)).first
if sluggable_ids.many? && fc.scope?
if relation.primary_key.respond_to?(:in)
return relation.where(relation.primary_key.in(sluggable_ids)).first
else
return relation.where(relation.primary_key => sluggable_ids).first
end
end

sluggable_id = sluggable_ids.first
Expand All @@ -83,7 +87,12 @@ def friendly_records(friendly_ids, unfriendly_ids)
return find_some_using_slug(friendly_ids, unfriendly_ids) if use_slugs_table
column = fc.cache_column || fc.column
friendly = arel_table[column].in(friendly_ids)
unfriendly = arel_table[relation.primary_key.name].in unfriendly_ids
unfriendly = if relation.primary_key.respond_to?(:name)
arel_table[relation.primary_key.name].in unfriendly_ids
else
arel_table[relation.primary_key].in unfriendly_ids
end

if friendly_ids.present? && unfriendly_ids.present?
where(friendly.or(unfriendly))
else
Expand All @@ -93,7 +102,11 @@ def friendly_records(friendly_ids, unfriendly_ids)

def find_some_using_slug(friendly_ids, unfriendly_ids)
ids = [unfriendly_ids + sluggable_ids_for(friendly_ids)].flatten.uniq
where(arel_table[relation.primary_key.name].in(ids))
if relation.primary_key.respond_to?(:name)
where(arel_table[relation.primary_key.name].in(ids))
else
where(arel_table[relation.primary_key].in(ids))
end
end

def sluggable_ids_for(ids)
Expand Down
3 changes: 1 addition & 2 deletions lib/friendly_id/active_record_adapter/slug.rb
Expand Up @@ -9,9 +9,8 @@ def self.named_scope(*args, &block) scope(*args, &block) end if FriendlyId.on_ar
named_scope :similar_to, lambda {|slug| {:conditions => {
:name => slug.name,
:scope => slug.scope,
:locale => slug.locale,
:sluggable_type => slug.sluggable_type
},
}.merge(({:locale => slug.locale} if slug.respond_to?(:locale)) || {}),
:order => "sequence ASC"
}
}
Expand Down
12 changes: 12 additions & 0 deletions lib/friendly_id/active_record_adapter/slugged_model.rb
Expand Up @@ -15,6 +15,18 @@ def self.included(base)
after_update :update_dependent_scopes
protect_friendly_id_attributes
extend FriendlyId::ActiveRecordAdapter::Finders unless FriendlyId.on_ar3?
def slug_with_rails_3_2_patch
unless (_slug = slug_without_rails_3_2_patch)
_slug = if friendly_id_config.class.locales_used?
slugs.where(:locale => (Thread.current[:globalize_locale] || ::I18n.locale)).first
else
slugs.first
end
end
_slug
end
alias_method_chain :slug, :rails_3_2_patch

end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/friendly_id/version.rb
Expand Up @@ -3,7 +3,7 @@ module Version
MAJOR = 3
MINOR = 2
TINY = 1
BUILD = 5
BUILD = 6
STRING = [MAJOR, MINOR, TINY, BUILD].compact.join('.')
end
end

0 comments on commit 63417be

Please sign in to comment.