Sha256: 1e44c5404b674f5daea2417b07b24a5257550dfb9f87a537ca5a063eb8d87c9a

Contents?: true

Size: 1.03 KB

Versions: 9

Compression:

Stored size: 1.03 KB

Contents

# Each {Article} must have one or more {Author}s. 
# 
# An {Author} is unrelated to the currently logged in user. This allows to publish
# {Article}s by guest authors that don't need an own user account

module Lines
  class Author < Lines::ApplicationRecord
      
    # Relations
    has_many :authorables
    has_many :articles, through: :authorables
    
    # Validations
    validates :name, :email, :description, presence: true

    # Callbacks
    before_destroy :check_for_articles

    # Returns the first name 
    def first_name
    	self.name.split(" ").first
    end

    # Returns the last name
    def last_name
    	self.name.split(" ").last
    end

    private

      # Return false if an author has associated articles. 
      # This check is called before destroying an author to prevent leaving articles
      # without an associated author.
      def check_for_articles
        if articles.count > 0
          errors[:base] << "cannot delete author with existing blog articles"
          return false
        end
      end
  end
end

Version data entries

9 entries across 9 versions & 2 rubygems

Version Path
jeygeethanmedia-inkpen-1.2.6.2 app/models/lines/author.rb
jeygeethanmedia-inkpen-1.2.6.1 app/models/lines/author.rb
lines-engine-1.2.6.1 app/models/lines/author.rb
lines-engine-1.2.6 app/models/lines/author.rb
lines-engine-1.2.5 app/models/lines/author.rb
lines-engine-1.2.3 app/models/lines/author.rb
lines-engine-1.2.2 app/models/lines/author.rb
lines-engine-1.2.1.1 app/models/lines/author.rb
lines-engine-1.2.1 app/models/lines/author.rb