Permalink
Browse files

Merge branch 'i18n' of https://github.com/MichalZ/friendly_id into i18n

Conflicts:
	lib/friendly_id/active_record_adapter/finders.rb
	lib/friendly_id/active_record_adapter/relation.rb
  • Loading branch information...
2 parents 491398a + a604364 commit 8f070041e813347982118f106fe467672e109c3b @davatis committed May 10, 2011
@@ -6,6 +6,7 @@ def self.up
t.integer :sequence, :null => false, :default => 1
t.string :sluggable_type, :limit => 40
t.string :scope
+ t.string :locale
t.datetime :created_at
end
add_index :slugs, :sluggable_id
@@ -26,6 +26,10 @@ def initialize(klass, id, options)
end
end
+ def locale
+ I18n.locale
+ end
+
def find_one
return find_one_with_cached_slug if !fc.scope? && cache_column?
return find_one_with_slug if use_slugs?
@@ -55,6 +59,8 @@ def find_one_with_cached_slug
def find_one_with_slug
name, seq = id.to_s.parse_friendly_id
scope = scoped(:joins => :slugs, :conditions => {:slugs => {:name => name, :sequence => seq}})
+ scope = scope.scoped(:conditions => {:slugs => {:scope => scope_val}}) if fc.scope?
+ scope = scope.scoped(:conditions => { :slugs => { :locale => locale }})
options[:readonly] = false unless options[:readonly]
@result = scope.first(options)
assign_status
@@ -104,6 +104,17 @@ def sluggable_ids_for(ids)
string = fragment % [connection.quote(klass.base_class.name), connection.quote(name), seq]
clause ? clause + " OR #{string}" : string
end
+ if fc.scope?
+ conditions = if friendly_id_scope
+ scope = connection.quote(friendly_id_scope)
+ "slugs.scope = %s AND (%s)" % [scope, conditions]
+ else
+ "slugs.scope IS NULL AND (%s)" % [conditions]
+ end
+ end
+ # TODO mal refactorn in arel
+ locale = I18n.locale
+ conditions = "slugs.locale = %s AND (%s)" % [connection.quote(locale), conditions]
sql = "SELECT sluggable_id FROM slugs WHERE (%s)" % conditions
connection.select_values sql
end
@@ -1,19 +1,21 @@
# A Slug is a unique, human-friendly identifier for an ActiveRecord.
class Slug < ::ActiveRecord::Base
attr_writer :sluggable
- attr_accessible :name, :scope, :sluggable, :sequence
+ attr_accessible :name, :scope, :sluggable, :sequence, :locale
def self.named_scope(*args, &block) scope(*args, &block) end if FriendlyId.on_ar3?
table_name = "slugs"
before_save :enable_name_reversion, :set_sequence
validate :validate_name
named_scope :similar_to, lambda {|slug| {:conditions => {
:name => slug.name,
:scope => slug.scope,
- :sluggable_type => slug.sluggable_type
+ :sluggable_type => slug.sluggable_type,
+ :locale => slug.locale
},
:order => "sequence ASC"
}
}
+ named_scope :with_locale, lambda {|loc| { :conditions => { :locale => loc } }}
def sluggable
sluggable_id && !@sluggable and begin
@@ -50,7 +52,8 @@ def validate_name
# If we're renaming back to a previously used friendly_id, delete the
# slug so that we can recycle the name without having to use a sequence.
def enable_name_reversion
- sluggable.slugs.find_all_by_name_and_scope(name, scope).each { |slug| slug.destroy }
+ sluggable.slugs.find_all_by_name_and_scope_and_locale(name, scope, locale).each { |slug| slug.destroy unless slug == self }
+ true
end
def friendly_id_with_sequence
@@ -5,20 +5,57 @@ module SluggedModel
def self.included(base)
base.class_eval do
has_many :slugs, :order => 'id DESC', :as => :sluggable, :dependent => :destroy
- has_one :slug, :order => 'id DESC', :as => :sluggable, :dependent => :destroy
- before_save :build_a_slug
+ # deaktiviert wegen i18n
+ #has_one :slug, :order => 'id DESC', :as => :sluggable, :dependent => :destroy
+ before_save :save_slugs
after_save :set_slug_cache
after_update :update_scope
after_update :update_dependent_scopes
protect_friendly_id_attributes
extend FriendlyId::ActiveRecordAdapter::Finders unless FriendlyId.on_ar3?
+
+ if friendly_id_config.slug_method == true
+ define_method("#{friendly_id_config.method}=") do |*args|
+ super
+ build_a_slug # if args[0] == friendly_id_config.method.to_s
+ end
+ end
+
end
end
include FriendlyId::Slugged::Model
+ def locale
+ I18n.locale
+ end
+
+
+ def write_attribute *args
+ #p 'write attribute'
+ #p args
+ #p friendly_id_config.method
+ super *args
+ if args[0].to_s == friendly_id_config.method.to_s
+ @value = args[1]
+ build_a_slug
+ end
+ end
+
+ def slug
+ #p 'method :slug in slugged_model'
+ @slug ||= {}
+ #p @slug
+ #p slugs
+ #p slugs.with_locale(locale)
+ (@slug && @slug[locale]) || @slug[locale] = slugs.with_locale(locale).first
+ #p @slug
+ #p "returning #{@slug[locale]} as slug"
+ @slug[locale]
+ end
+
def find_slug(name, sequence)
- slugs.find_by_name_and_sequence(name, sequence)
+ slugs.find_by_name_and_sequence_and_locale(name, sequence, locale)
end
# Returns the friendly id, or if none is available, the numeric id. Note that this
@@ -45,10 +82,41 @@ def to_param_from_slug
# Build the new slug using the generated friendly id.
def build_a_slug
+ #p' build a slug'
return unless new_slug_needed?
- @slug = slugs.build :name => slug_text.to_s, :scope => friendly_id_config.scope_for(self),
- :sluggable => self
- @new_friendly_id = @slug.to_friendly_id
+ #p 'new slug needed'
+ raise "steht schon was anderes drin #{@slug.inspect} - #{locale}" if @slug != nil && !@slug.kind_of?(Hash)
+ @slug ||= {} #if @slug == nil
+ # p slug_text
+ #p self
+ # @slug[locale] = slugs.build :name => slug_text.to_s, :scope => friendly_id_config.scope_for(self),
+ # :sluggable => self, :locale => locale
+
+ @slug[locale] = slugs.build :name => slug_text.to_s, :scope => friendly_id_config.scope_for(self),
+ :sluggable => self, :locale => locale
+ #raise @slug.inspect
+ #p @slug
+ #raise locale.inspect
+ #raise @slug[locale].inspect
+ @new_friendly_id = @slug[locale].to_friendly_id
+ end
+
+ def save_slugs
+ # p 'save slugs 33'
+ build_a_slug if friendly_id_config.slug_method
+
+ if @slug && @slug.kind_of?( Hash )
+ #p @slug
+ @slug.each do |k, v|
+ #p "nil #{k} => #{v}" unless v
+ if v && v.new_record?
+ v.sluggable = self
+ #p "saving slug #{v.name}"
+ v.save!
+ end
+ end
+ end
+ true
end
# Reset the cached friendly_id?
@@ -108,4 +176,4 @@ def update_without_callbacks
end
end
end
-end
+end
@@ -27,7 +27,8 @@ class Configuration
:max_length => 255,
:reserved_words => ["index", "new"],
:reserved_message => 'can not be "%s"',
- :sequence_separator => "--"
+ :sequence_separator => "--",
+ :slug_method => nil
}
# Whether to allow friendly_id and/or slugs to be nil. This is not
@@ -75,6 +76,10 @@ class Configuration
# Use slugs for storing the friendly_id string.
attr_accessor :use_slug
alias :use_slugs= :use_slug
+
+ #
+ attr_accessor :slug_method
+ alias :slug_methods= :slug_method
def initialize(configured_class, method, options = nil, &block)
@configured_class = configured_class
View
@@ -30,9 +30,17 @@ def outdated?
!current?
end
+ def locale
+ I18n.locale
+ end
+
# The slug that was used to find the model.
def slug
- @slug ||= record.find_slug(name, sequence)
+ #p 'method :slug in slugged'
+ #raise @slug.inspect if @slug
+ @slug ||= {}
+ @slug[locale] ||= record.find_slug(name, sequence)
+ @slug[locale]
end
end
@@ -72,14 +80,21 @@ def normalize_friendly_id(string)
# Does the instance have a slug?
def slug?
+ #p 'no slug'
+ #p slug
+ #p "has slug #{!!slug}"
!! slug
end
private
# Get the processed string used as the basis of the friendly id.
def slug_text
- base = send(friendly_id_config.method)
+ base = @value || send(friendly_id_config.method)
+ base = I18n.with_locale(I18n.default_locale) { send(friendly_id_config.method) } if base.blank?
+ #p 'slug_text'
+ #p I18n.with_locale(I18n.default_locale) { send(friendly_id_config.method) }
+ #p base
unless base.nil? && friendly_id_config.allow_nil?
text = normalize_friendly_id(SlugString.new(base))
SlugString.new(text.to_s).validate_for!(friendly_id_config).to_s
@@ -88,13 +103,16 @@ def slug_text
# Has the slug text changed?
def slug_text_changed?
+ #p "slug_text_changed"
+ #p slug_text != slug.name
slug_text != slug.name
end
# Has the basis of our friendly id changed, requiring the generation of a
# new slug?
def new_slug_needed?
if friendly_id_config.allow_nil?
+ #p 'nil allowed'
(!slug? && !slug_text.blank?) || (slug? && slug_text_changed?)
else
!slug? || slug_text_changed?
View
@@ -1,4 +1,6 @@
# encoding: utf-8
+require 'ruby-debug'
+Debugger.start
Module.send :include, Module.new {
def test(name, &block)
define_method("test_#{name.gsub(/[^a-z0-9]/i, "_")}".to_sym, &block)
@@ -161,11 +163,18 @@ module Slugged
test "should have a slug" do
assert_not_nil instance.slug
+ assert_equal 1, instance.slugs.size
end
test "should not make a new slug unless the friendly_id method value has changed" do
+ debugger
+ #p 'hier'
+ #p instance.slugs
+ #p instance.slugs.size
instance.note = instance.note.to_s << " updated"
+ debugger
instance.send save_method
+ #p instance.slugs
assert_equal 1, instance.slugs.size
end
@@ -11,7 +11,7 @@ module Slugged
end
assert_nothing_raised do
- klass.find(instance.friendly_id, :include => :slug)
+ klass.find(instance.friendly_id) # , :include => :slug)
end
end
View
@@ -11,3 +11,11 @@
require "active_support"
require "friendly_id"
require "friendly_id/test"
+
+module I18n
+ extend self
+
+ def locale
+ :de
+ end
+end

0 comments on commit 8f07004

Please sign in to comment.