Module: FriendlyId::SluggableClassMethods
- Includes:
- Helpers
- Defined in:
- lib/friendly_id/sluggable_class_methods.rb
Class Method Summary collapse
-
.extended(base) ⇒ Object
:nodoc:#.
Instance Method Summary collapse
-
#find_one_with_friendly(id_or_name, options) ⇒ Object
Finds a single record using the friendly id, or the record’s id.
-
#find_some_with_friendly(ids_and_names, options) ⇒ Object
Finds multiple records using the friendly ids, or the records’ ids.
-
#validate_find_options_with_friendly(options) ⇒ Object
:nodoc:#.
Methods included from Helpers
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, ) #:nodoc:# scope = .delete(:scope) return find_one_without_friendly(id_or_name, ) if id_or_name.is_a?(Fixnum) = {:select => "#{self.table_name}.*"} [:joins] = :slugs unless [:include] && [*[:include]].flatten.include?(:slugs) name, sequence = Slug.parse(id_or_name) [:conditions] = { "#{Slug.table_name}.name" => name, "#{Slug.table_name}.scope" => scope, "#{Slug.table_name}.sequence" => sequence } result = with_scope(:find => ) { find_initial() } if result result.finder_slug_name = id_or_name else result = find_one_without_friendly id_or_name, 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, ) #:nodoc:# slugs, ids = get_slugs_and_ids(ids_and_names, ) results = [] = {:select => "#{self.table_name}.*"} [:joins] = :slugs unless [:include] && [*[:include]].flatten.include?(:slugs) [:conditions] = "#{quoted_table_name}.#{primary_key} IN (#{ids.empty? ? 'NULL' : ids.join(',')}) " [:conditions] << "OR slugs.id IN (#{slugs.to_s(:db)})" results = with_scope(:find => ) { find_every() } expected = expected_size(ids_and_names, ) if results.size != expected raise ActiveRecord::RecordNotFound, "Couldn't find all #{ name.pluralize } with IDs (#{ ids_and_names * ', ' }) AND #{ sanitize_sql [: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 () #:nodoc:# .assert_valid_keys([:conditions, :include, :joins, :limit, :offset, :order, :select, :readonly, :group, :from, :lock, :having, :scope]) end |