Module: FriendlyId::SluggableClassMethods
- Includes:
- Helpers
- Defined in:
- lib/friendly_id/sluggable_class_methods.rb
Instance Method Summary collapse
-
#find_one(id_or_name, options) ⇒ Object
Finds a single record using the friendly id, or the record’s id.
-
#find_some(ids_and_names, options) ⇒ Object
Finds multiple records using the friendly ids, or the records’ ids.
-
#validate_find_options(options) ⇒ Object
:nodoc:#.
Methods included from Helpers
Instance Method Details
#find_one(id_or_name, options) ⇒ Object
Finds a single record using the friendly id, or the record’s id.
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 |
# File 'lib/friendly_id/sluggable_class_methods.rb', line 8 def find_one(id_or_name, ) #:nodoc:# scope = .delete(:scope) return super(id_or_name, ) if id_or_name.is_a?(Integer) = {: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 elsif id_or_name.to_i.to_s != id_or_name raise ActiveRecord::RecordNotFound else result = super id_or_name, end result rescue ActiveRecord::RecordNotFound => e if [:scope] if !scope raise ActiveRecord::RecordNotFound.new("%s; expected scope but got none" % e.) else raise ActiveRecord::RecordNotFound.new("%s and scope=#{scope}" % e.) end end raise e end |
#find_some(ids_and_names, options) ⇒ Object
Finds multiple records using the friendly ids, or the records’ ids.
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/friendly_id/sluggable_class_methods.rb', line 51 def find_some(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() }.uniq 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(options) ⇒ Object
:nodoc:#
73 74 75 76 |
# File 'lib/friendly_id/sluggable_class_methods.rb', line 73 def () #:nodoc:# .assert_valid_keys([:conditions, :include, :joins, :limit, :offset, :order, :select, :readonly, :group, :from, :lock, :having, :scope]) end |