Module: FriendlyId::ActiveRecordAdapter::SluggedModel

Includes:
Slugged::Model
Defined in:
lib/friendly_id/active_record_adapter/slugged_model.rb

Instance Attribute Summary

Attributes included from Slugged::Model

#slug

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Slugged::Model

#friendly_id, #friendly_id_config, #friendly_id_status, #normalize_friendly_id, #slug?

Class Method Details

.included(base) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/friendly_id/active_record_adapter/slugged_model.rb', line 5

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,
            :conditions => (proc{ {
              :locale => (Thread.current[:globalize_locale] || ::I18n.locale)
            } } if friendly_id_config.class.locales_used?)
    before_save :build_a_slug
    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?
    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

Instance Method Details

#find_slug(name, sequence) ⇒ Object



35
36
37
# File 'lib/friendly_id/active_record_adapter/slugged_model.rb', line 35

def find_slug(name, sequence)
  slugs.find_by_name_and_sequence(name, sequence)
end

#to_paramObject

Returns the friendly id, or if none is available, the numeric id. Note that this method will use the cached_slug value if present, unlike Slugged::Model#friendly_id.



41
42
43
# File 'lib/friendly_id/active_record_adapter/slugged_model.rb', line 41

def to_param
  friendly_id_config.cache_column ? to_param_from_cache : to_param_from_slug
end