Sha256: 7c2052ae0de2f6050e09e028a37974c031aeb0c88ac8df33e746b907ae012653

Contents?: true

Size: 1.96 KB

Versions: 36

Compression:

Stored size: 1.96 KB

Contents

module FriendlyId
  # Utility methods for determining whether any object is a friendly id.
  #
  # Monkey-patching Object is a somewhat extreme measure not to be taken lightly
  # by libraries, but in this case I decided to do it because to me, it feels
  # cleaner than adding a module method to {FriendlyId}. I've given the methods
  # names that unambigously refer to the library of their origin, which should
  # be sufficient to avoid conflicts with other libraries.
  module ObjectUtils

    # True is the id is definitely friendly, false if definitely unfriendly,
    # else nil.
    #
    # An object is considired "definitely unfriendly" if its class is or
    # inherits from ActiveRecord::Base, Array, Hash, NilClass, Numeric, or
    # Symbol.
    #
    # An object is considered "definitely friendly" if it responds to +to_i+,
    # and its value when cast to an integer and then back to a string is
    # different from its value when merely cast to a string:
    #
    #   123.friendly_id?                  #=> false
    #   :id.friendly_id?                  #=> false
    #   {:name => 'joe'}.friendly_id?     #=> false
    #   ['name = ?', 'joe'].friendly_id?  #=> false
    #   nil.friendly_id?                  #=> false
    #   "123".friendly_id?                #=> nil
    #   "abc123".friendly_id?             #=> true
    def friendly_id?
      # Considered unfriendly if this is an instance of an unfriendly class or
      # one of its descendants.
      unfriendly_classes = [ActiveRecord::Base, Array, Hash, NilClass, Numeric,
                            Symbol, TrueClass, FalseClass]

      if unfriendly_classes.detect {|klass| self.class <= klass}
        false
      elsif respond_to?(:to_i) && to_i.to_s != to_s
        true
      end
    end

    # True if the id is definitely unfriendly, false if definitely friendly,
    # else nil.
    def unfriendly_id?
      val = friendly_id? ; !val unless val.nil?
    end
  end
end

Object.send :include, FriendlyId::ObjectUtils

Version data entries

36 entries across 31 versions & 4 rubygems

Version Path
mil_friendly_id-4.0.9.9 lib/friendly_id/object_utils.rb
friendly_id-4.0.10.1 lib/friendly_id/object_utils.rb
friendly_id-4.0.10 lib/friendly_id/object_utils.rb
mil_friendly_id-4.0.9.8 lib/friendly_id/object_utils.rb
geothird_friendly_id-4.0.9.8 lib/friendly_id/object_utils.rb
geothird_friendly_id-4.0.9.7 lib/friendly_id/object_utils.rb
geothird_friendly_id-4.0.9.6 lib/friendly_id/object_utils.rb
geothird_friendly_id-4.0.9.5 lib/friendly_id/object_utils.rb
geothird_friendly_id-4.0.9.4 lib/friendly_id/object_utils.rb
geothird_friendly_id-4.0.9.3 lib/friendly_id/object_utils.rb
geothird_friendly_id-4.0.9.2 lib/friendly_id/object_utils.rb
geothird_friendly_id-4.0.9.1 lib/friendly_id/object_utils.rb
friendly_id-4.1.0.beta.1 lib/friendly_id/object_utils.rb
friendly_id-4.0.9 lib/friendly_id/object_utils.rb
friendly_id-4.0.8 lib/friendly_id/object_utils.rb
sunrise-cms-0.3.3 vendor/bundle/ruby/1.9.1/gems/friendly_id-4.0.6/lib/friendly_id/object_utils.rb
sunrise-cms-0.3.3 vendor/bundle/ruby/1.9.1/gems/friendly_id-4.0.5/lib/friendly_id/object_utils.rb
sunrise-cms-0.3.2 vendor/bundle/ruby/1.9.1/gems/friendly_id-4.0.6/lib/friendly_id/object_utils.rb
sunrise-cms-0.3.2 vendor/bundle/ruby/1.9.1/gems/friendly_id-4.0.5/lib/friendly_id/object_utils.rb
sunrise-cms-0.3.1 vendor/bundle/ruby/1.9.1/gems/friendly_id-4.0.6/lib/friendly_id/object_utils.rb