Sha256: fd0010c1b260e683ecc771ec078d838cd8af99dd86648ce7430110b5407eaa54

Contents?: true

Size: 1.85 KB

Versions: 27

Compression:

Stored size: 1.85 KB

Contents

module FriendlyId
  module ActiveRecordAdapter

    module SimpleModel

      def self.included(base)
        base.class_eval do
          column = friendly_id_config.column
          validate :validate_friendly_id, :unless => :skip_friendly_id_validations
          validates_presence_of column, :unless => :skip_friendly_id_validations
          validates_length_of column, :maximum => friendly_id_config.max_length, :unless => :skip_friendly_id_validations
          after_update :update_scopes
          extend FriendlyId::ActiveRecordAdapter::Finders unless FriendlyId.on_ar3?
        end
      end

      # Get the {FriendlyId::Status} after the find has been performed.
      def friendly_id_status
        @friendly_id_status ||= Status.new :record => self
      end

      # Returns the friendly_id.
      def friendly_id
        send friendly_id_config.column
      end
      alias best_id friendly_id

      # Returns the friendly id, or if none is available, the numeric id.
      def to_param
        (friendly_id || id).to_s
      end

      private

      # The old and new values for the friendly_id column.
      def friendly_id_changes
        changes[friendly_id_config.column.to_s]
      end

      # Update the slugs for any model that is using this model as its
      # FriendlyId scope.
      def update_scopes
        if changes = friendly_id_changes
          friendly_id_config.child_scopes.each do |klass|
            Slug.update_all "scope = '#{changes[1]}'", ["sluggable_type = ? AND scope = ?", klass.to_s, changes[0]]
          end
        end
      end

      def skip_friendly_id_validations
        friendly_id.nil? && friendly_id_config.allow_nil?
      end

      def validate_friendly_id
        if result = friendly_id_config.reserved_error_message(friendly_id)
          self.errors.add(*result)
          return false
        end
      end

    end
  end
end

Version data entries

27 entries across 27 versions & 2 rubygems

Version Path
friendly_id_globalize3-3.2.1.6 lib/friendly_id/active_record_adapter/simple_model.rb
friendly_id_globalize3-3.2.1.5 lib/friendly_id/active_record_adapter/simple_model.rb
friendly_id-3.3.0.rc1 lib/friendly_id/active_record_adapter/simple_model.rb
friendly_id_globalize3-3.2.1.4 lib/friendly_id/active_record_adapter/simple_model.rb
friendly_id-3.3.0.alpha2 lib/friendly_id/active_record_adapter/simple_model.rb
friendly_id-3.3.0.alpha1 lib/friendly_id/active_record_adapter/simple_model.rb
friendly_id_globalize3-3.2.1.3 lib/friendly_id/active_record_adapter/simple_model.rb
friendly_id_globalize3-3.2.1.2 lib/friendly_id/active_record_adapter/simple_model.rb
friendly_id_globalize3-3.2.1.1 lib/friendly_id/active_record_adapter/simple_model.rb
friendly_id-3.2.1.1 lib/friendly_id/active_record_adapter/simple_model.rb
friendly_id_globalize3-3.2.1 lib/friendly_id/active_record_adapter/simple_model.rb
friendly_id-3.2.1 lib/friendly_id/active_record_adapter/simple_model.rb
friendly_id_globalize3-3.2.0 lib/friendly_id/active_record_adapter/simple_model.rb
friendly_id-3.2.0 lib/friendly_id/active_record_adapter/simple_model.rb
friendly_id-3.2.0.rc1 lib/friendly_id/active_record_adapter/simple_model.rb
friendly_id-3.2.0.beta1 lib/friendly_id/active_record_adapter/simple_model.rb
friendly_id-3.1.8 lib/friendly_id/active_record_adapter/simple_model.rb
friendly_id-3.1.7 lib/friendly_id/active_record_adapter/simple_model.rb
friendly_id-3.1.7.pre lib/friendly_id/active_record_adapter/simple_model.rb
friendly_id-3.1.6 lib/friendly_id/active_record_adapter/simple_model.rb