Sha256: 9c10348128dc4493eb92132202aae441c1f550d49a7f802a40554d4682ac3037

Contents?: true

Size: 657 Bytes

Versions: 5

Compression:

Stored size: 657 Bytes

Contents

module Popular

  # Friendship class. Provides self-referential join for popular_models and their friends
  class Friendship < ::ActiveRecord::Base

    belongs_to :popular_model, polymorphic: true
    belongs_to :friend, polymorphic: true

    validates_presence_of :friend, :popular_model

    # Ensures that popular_models cannot add themselves as a friend
    validate :ensure_popular_model_different_from_friend

    private

    # Checks friend against popular_model.
    #
    # TODO add more descriptive error message/i18n
    def ensure_popular_model_different_from_friend
      errors.add( :friend ) if friend == popular_model
    end

  end

end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
popular-0.6.0 lib/popular/friendship.rb
popular-0.5.1 lib/popular/friendship.rb
popular-0.5.0 lib/popular/friendship.rb
popular-0.4.0 lib/popular/friendship.rb
popular-0.3.2 lib/popular/friendship.rb