Sha256: 559e5424baed52cc9c542d38201682c05aa88c5e6ccc78340483211007eb1aa4

Contents?: true

Size: 1.07 KB

Versions: 3

Compression:

Stored size: 1.07 KB

Contents

module Theblog
  class ContentNode < ActiveRecord::Base
    include AASM

    belongs_to :parent_node, class_name: 'Theblog::ContentNode'
    belongs_to :author, class_name: 'Theblog::Account'

    has_many :child_nodes, class_name: 'Theblog::ContentNode', foreign_key: :parent_node_id

    validates_presence_of :content_status

    scope :by_parent, ->(parent_slug) do
      if parent_slug.present?
        joins('JOIN theblog_content_nodes AS parents ON theblog_content_nodes.parent_node_id = parents.id').
          where(parents: {slug: parent_slug})
      else
        where(parent_node_id: nil)
      end
    end

    aasm :content_status do
      state :drafted, initial: true
      state :published
      state :blocked

      event :publish do
        transitions from: :drafted, to: :published
      end

      event :draft do
        transitions from: :published, to: :drafted
      end

      event :block do
        transitions from: [:published, :drafted], to: :blocked
      end

      event :unblock do
        transitions from: :blocked, to: :drafted
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
theblog-0.0.2.3 app/models/theblog/content_node.rb
theblog-0.0.2.2 app/models/theblog/content_node.rb
theblog-0.0.2.1 app/models/theblog/content_node.rb