Sha256: 1751c13b6f67085788d9fef1be92a50ef2e6ceda76321c0e41a4b834276c0764

Contents?: true

Size: 1.02 KB

Versions: 54

Compression:

Stored size: 1.02 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 < ActiveRecord::Base
      
    # 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

54 entries across 54 versions & 1 rubygems

Version Path
lines-engine-1.2 app/models/lines/author.rb
lines-engine-1.1.5.3 app/models/lines/author.rb
lines-engine-1.1.5.2 app/models/lines/author.rb
lines-engine-1.1.5.1 app/models/lines/author.rb
lines-engine-1.1.5 app/models/lines/author.rb
lines-engine-1.1.4 app/models/lines/author.rb
lines-engine-1.1.3 app/models/lines/author.rb
lines-engine-1.1.2 app/models/lines/author.rb
lines-engine-1.1.1 app/models/lines/author.rb
lines-engine-1.1.0 app/models/lines/author.rb
lines-engine-1.0.0 app/models/lines/author.rb
lines-engine-0.9.0 app/models/lines/author.rb
lines-engine-0.6.2 app/models/lines/author.rb
lines-engine-0.6.1 app/models/lines/author.rb
lines-engine-0.6 app/models/lines/author.rb
lines-engine-0.5 app/models/lines/author.rb
lines-engine-0.4.6.4 app/models/lines/author.rb
lines-engine-0.4.6.3 app/models/lines/author.rb
lines-engine-0.4.6.1 app/models/lines/author.rb
lines-engine-0.4.6 app/models/lines/author.rb