Module: FriendlyId::SluggableClassMethods

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

Instance Method Summary collapse

Methods included from Helpers

#expected_size

Instance Method Details

#find_one(id_or_name, options) ⇒ Object

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



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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/friendly_id/sluggable_class_methods.rb', line 6

def find_one(id_or_name, options) #:nodoc:#
  scope = options.delete(:scope)
  scope = scope.to_param if scope && scope.respond_to?(:to_param)

  if id_or_name.is_a?(Integer) || id_or_name.kind_of?(ActiveRecord::Base)
    return super(id_or_name, options)
  else
    if self.cache_column
      find_options = { :conditions => { self.cache_column => id_or_name } }
    else
      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
      }
    end

    result = with_scope(:find => find_options) { find_initial(options) }
    if result
      result.finder_slug_name = id_or_name
    elsif id_or_name.to_i.to_s != id_or_name
      raise ActiveRecord::RecordNotFound
    else
      result = super id_or_name, options
    end

    result
  end

rescue ActiveRecord::RecordNotFound => e

  if friendly_id_options[:scope]
    if !scope
      raise ActiveRecord::RecordNotFound.new("%s; expected scope but got none" % e.message)
    else
      raise ActiveRecord::RecordNotFound.new("%s and scope=#{scope}" % e.message)
    end
  end

  raise e

end

#find_some(ids_and_names, options) ⇒ Object

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



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/friendly_id/sluggable_class_methods.rb', line 55

def find_some(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) }.uniq

  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(options) ⇒ Object

:nodoc:#



77
78
79
80
# File 'lib/friendly_id/sluggable_class_methods.rb', line 77

def validate_find_options(options) #:nodoc:#
  options.assert_valid_keys([:conditions, :include, :joins, :limit, :offset,
    :order, :select, :readonly, :group, :from, :lock, :having, :scope])
end