Module: FriendlyId::ActiveRecordAdapter::SluggedModel

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

Defined Under Namespace

Modules: SluggedFinder Classes: CachedMultipleFinder, CachedSingleFinder, MultipleFinder, SingleFinder

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



136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/friendly_id/active_record_adapter/slugged_model.rb', line 136

def self.included(base)
  base.class_eval do
    has_many :slugs, :order => 'id DESC', :as => :sluggable, :dependent => :destroy
    before_create :build_slug
    after_create :set_slug_cache
    after_update :update_scope
    after_update :update_dependent_scopes
					after_update :update_slug_from_cache
    protect_friendly_id_attributes
    extend FriendlyId::ActiveRecordAdapter::FinderMethods
  end
end

Instance Method Details

#find_slug(name, sequence) ⇒ Object



151
152
153
# File 'lib/friendly_id/active_record_adapter/slugged_model.rb', line 151

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

#slugObject

The model instance’s current slug.



156
157
158
159
# File 'lib/friendly_id/active_record_adapter/slugged_model.rb', line 156

def slug
  return @slug if new_record?
  @slug ||= slugs.first(:order => "id DESC")
end

#slug=(slug) ⇒ Object

Set the slug.



162
163
164
165
# File 'lib/friendly_id/active_record_adapter/slugged_model.rb', line 162

def slug=(slug)
  @new_friendly_id = slug.to_friendly_id unless slug.nil?
  super
end

#to_paramObject

Returns the friendly id, or if none is available, the numeric id.



168
169
170
# File 'lib/friendly_id/active_record_adapter/slugged_model.rb', line 168

def to_param
  friendly_id_config.cache_column ? to_param_from_cache : to_param_from_slug
end