Module: HasSlug::SluggableClassMethods
- Defined in:
- lib/has_slug/sluggable_class_methods.rb
Class Method Summary collapse
-
.extended(base) ⇒ Object
:nodoc:#.
Instance Method Summary collapse
-
#find_one_with_same_slug(object) ⇒ Object
Find a single record that has the same slug as the given record’s slug.
-
#find_one_with_slug(id_or_slug, options = {}) ⇒ Object
Find a single record using the record’s slug or the record’s id.
-
#find_some_with_slug(ids_or_slugs, options = {}) ⇒ Object
Find multiple records using the records slugs or the records id’s.
- #slug_scope_attribute ⇒ Object
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 = [:slug_column] = if object.new_record? then {} else { :conditions => ["#{quoted_table_name}.#{primary_key} != ?", object] } end if scope = [:scope] result = send("find_by_#{slug_column}_and_#{slug_scope_attribute}", object.slug, object.send(scope), ) else result = send("find_by_#{slug_column}", object.slug, ) 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, = {}) return find_one_without_slug(id_or_slug, ) if id_or_slug.is_a?(Fixnum) slug_column = [:slug_column] if result = send("find_by_#{slug_column}", id_or_slug, ) result.found_by_slug! else result = find_one_without_slug(id_or_slug, ) 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, = {}) return find_some_without_slug(ids_or_slugs, ) if ids_or_slugs.all? { |x| x.is_a?(Fixnum) } = .dup [:conditions] ||= [""] [:conditions] = [[:conditions] + " AND "] if [:conditions].is_a?(String) [:conditions][0] << "(#{quoted_table_name}.#{primary_key} IN (?)" << " OR #{quoted_table_name}.#{[:slug_column]} IN (?))" [:conditions] << ids_or_slugs [:conditions] << ids_or_slugs.map(&:to_s) found = find_every() 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([: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([:slug_column]) == slug } slug_record.found_by_slug! if slug_record end found end |
#slug_scope_attribute ⇒ Object
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 = [: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 |