Module: HasSlug::SluggableClassMethods

Defined in:
lib/has_slug/sluggable_class_methods.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object

:nodoc:#



3
4
5
6
7
8
# File 'lib/has_slug/sluggable_class_methods.rb', line 3

def self.extended(base) #:nodoc:#
  class << base
    alias_method_chain :find_one, :slug
    alias_method_chain :find_some, :slug
  end
end

Instance Method Details

#find_one_with_same_slug(object) ⇒ Object

Find a single record that has the same slug as the given record’s slug



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/has_slug/sluggable_class_methods.rb', line 27

def find_one_with_same_slug(object)
  slug_column = has_slug_options[:slug_column]
  
  options  = if object.new_record? then {}
                                   else { :conditions => ["#{quoted_table_name}.#{primary_key} != ?", object] }
                                   end

  if scope = has_slug_options[:scope]
    result = send("find_by_#{slug_column}_and_#{slug_scope_attribute}",
                  object.slug, object.send(scope), options)
  else
    result = send("find_by_#{slug_column}", object.slug, options)
  end
    
  result.found_by_slug! if result

  result
end

#find_one_with_slug(id_or_slug, options = {}) ⇒ Object

Find a single record using the record’s slug or the record’s id



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/has_slug/sluggable_class_methods.rb', line 47

def find_one_with_slug(id_or_slug, options = {})
  return find_one_without_slug(id_or_slug, options) if id_or_slug.is_a?(Fixnum)

  slug_column = has_slug_options[:slug_column]

  if result = send("find_by_#{slug_column}", id_or_slug, options)
    result.found_by_slug!
  else
    result = find_one_without_slug(id_or_slug, options)
  end
  
  result
end

#find_some_with_slug(ids_or_slugs, options = {}) ⇒ Object

Find multiple records using the records slugs or the records id’s



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/has_slug/sluggable_class_methods.rb', line 62

def find_some_with_slug(ids_or_slugs, options = {})
  return find_some_without_slug(ids_or_slugs, options) if ids_or_slugs.all? { |x| x.is_a?(Fixnum) }
  
  find_options = options.dup
  
  find_options[:conditions] ||= [""]
  find_options[:conditions]   = [find_options[:conditions] + " AND "] if find_options[:conditions].is_a?(String)
  find_options[:conditions][0] << "(#{quoted_table_name}.#{primary_key} IN (?)" <<
                                  " OR #{quoted_table_name}.#{has_slug_options[:slug_column]} IN (?))"
  
  find_options[:conditions] << ids_or_slugs
  find_options[:conditions] << ids_or_slugs.map(&:to_s)
  
  found    = find_every(find_options)
  expected = ids_or_slugs.map(&:to_s).uniq
  
  unless found.size == expected.size
    raise ActiveRecord::RecordNotFound, "Couldn't find all #{name.pluralize} with IDs (#{ids_or_slugs * ', '}) AND #{sanitize_sql(options[:conditions])} (found #{found.size} results, but was looking for #{expected.size})"
  end
  
  ids_or_slugs.each do |slug|
    slug_record = found.detect { |record| record.send(has_slug_options[:slug_column]) == slug }
    slug_record.found_by_slug! if slug_record
  end
  
  found
end

#slug_scope_attributeObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/has_slug/sluggable_class_methods.rb', line 10

def slug_scope_attribute
  return @@slug_scope_attribute if defined?(@@slug_scope_attribute)

  if scope = has_slug_options[:scope]
    if columns.any? { |c| c.name == scope }
      @@slug_scope_attribute = scope.to_sym
    elsif columns.any? { |c| c.name == "#{scope}_id" }
      @@slug_scope_attribute = "#{scope}_id".to_sym
    else
      raise Exception, "has_slug's scope '#{scope}' does not exist in the model '#{self.class.to_s}'"
    end
  end
  
  @@slug_scope_attribute
end