Module: FriendlyId::SluggableClassMethods

Includes:
Helpers
Defined in:
lib/friendly_id/sluggable_class_methods.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#expected_size

Class Method Details

.extended(base) ⇒ Object

:nodoc:#



5
6
7
8
9
10
11
12
13
# File 'lib/friendly_id/sluggable_class_methods.rb', line 5

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

  class << base
    alias_method_chain :find_one, :friendly
    alias_method_chain :find_some, :friendly
    alias_method_chain :validate_find_options, :friendly
  end

end

Instance Method Details

#find_one_with_friendly(id_or_name, options) ⇒ Object

Finds a single record using the friendly id, or the record’s id.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/friendly_id/sluggable_class_methods.rb', line 16

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)

  find_options = {:select => "#{self.table_name}.*"}
  find_options[:joins] = :slugs unless options[:include] && [*options[:include]].flatten.include?(:slugs)

  name, sequence = Slug.parse(id_or_name)

  find_options[:conditions] = {
    "#{Slug.table_name}.name"     => name,
    "#{Slug.table_name}.scope"    => scope,
    "#{Slug.table_name}.sequence" => sequence
  }

  result = with_scope(:find => find_options) { find_initial(options) }

  if result
    result.finder_slug_name = id_or_name
  else
    result = find_one_without_friendly id_or_name, options
  end

  result

end

#find_some_with_friendly(ids_and_names, options) ⇒ Object

Finds multiple records using the friendly ids, or the records’ ids.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/friendly_id/sluggable_class_methods.rb', line 45

def find_some_with_friendly(ids_and_names, options) #:nodoc:#

  slugs, ids = get_slugs_and_ids(ids_and_names, options)
  results = []

  find_options = {:select => "#{self.table_name}.*"}
  find_options[:joins] = :slugs unless options[:include] && [*options[:include]].flatten.include?(:slugs)
  find_options[:conditions] = "#{quoted_table_name}.#{primary_key} IN (#{ids.empty? ? 'NULL' : ids.join(',')}) "
  find_options[:conditions] << "OR slugs.id IN (#{slugs.to_s(:db)})"

  results = with_scope(:find => find_options) { find_every(options) }

  expected = expected_size(ids_and_names, options)
  if results.size != expected
    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 })"
  end

  assign_finder_slugs(slugs, results)

  results
end

#validate_find_options_with_friendly(options) ⇒ Object

:nodoc:#



67
68
69
70
# File 'lib/friendly_id/sluggable_class_methods.rb', line 67

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