Sha256: 956c0b7c36b47301e58d2ea7b4e351f8ec30b7f4292db9d839bf4ad73fab3c09

Contents?: true

Size: 1.03 KB

Versions: 28

Compression:

Stored size: 1.03 KB

Contents

class Pulitzer::PostTypeVersion::Publish
  include StateMachine::Transition

  self.action_name = :publish
  self.target_state = 'published'
  self.valid_from_states = [:preview,:retired]

  def initialize(post_type_version)
    @post_type_version = post_type_version
    self.object = @post_type_version
    self.errors = ActiveModel::Errors.new(self)
    @old_published = get_old_published_version
  end

  def publish
    self.validate_transition!
    validate_published_post or return false
    update_status
    archive_old_version
    return true
  end

  def archive_old_version
    @old_published.update(status: 'retired') if @old_published.present?
  end

  def get_old_published_version
    @post_type_version.post_type.published_type_version
  end

  def validate_published_post
    if @post_type_version.template? && @post_type_version.posts.none?{|post| post.active_version.present?}
      @post_type_version.errors.add(:base, 'You must publish a post before publishing the post type')
      return false
    end
    return true
  end
end

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
pulitzer-0.15.31 app/services/pulitzer/post_type_version/publish.rb
pulitzer-0.15.30 app/services/pulitzer/post_type_version/publish.rb
pulitzer-0.15.29 app/services/pulitzer/post_type_version/publish.rb
pulitzer-0.15.28 app/services/pulitzer/post_type_version/publish.rb
pulitzer-0.15.27 app/services/pulitzer/post_type_version/publish.rb
pulitzer-0.15.26 app/services/pulitzer/post_type_version/publish.rb
pulitzer-0.15.25 app/services/pulitzer/post_type_version/publish.rb
pulitzer-0.15.24 app/services/pulitzer/post_type_version/publish.rb
pulitzer-0.15.23 app/services/pulitzer/post_type_version/publish.rb
pulitzer-0.15.22 app/services/pulitzer/post_type_version/publish.rb
pulitzer-0.15.21 app/services/pulitzer/post_type_version/publish.rb
pulitzer-0.15.20 app/services/pulitzer/post_type_version/publish.rb
pulitzer-0.15.19 app/services/pulitzer/post_type_version/publish.rb
pulitzer-0.15.18 app/services/pulitzer/post_type_version/publish.rb
pulitzer-0.15.17 app/services/pulitzer/post_type_version/publish.rb
pulitzer-0.15.16 app/services/pulitzer/post_type_version/publish.rb
pulitzer-0.15.15 app/services/pulitzer/post_type_version/publish.rb
pulitzer-0.15.14 app/services/pulitzer/post_type_version/publish.rb
pulitzer-0.15.13 app/services/pulitzer/post_type_version/publish.rb
pulitzer-0.15.12 app/services/pulitzer/post_type_version/publish.rb