Class: Pulitzer::PostTypeVersion::Publish

Inherits:
Object
  • Object
show all
Includes:
StateMachine::Transition
Defined in:
app/services/pulitzer/post_type_version/publish.rb

Instance Method Summary collapse

Constructor Details

#initialize(post_type_version) ⇒ Publish

Returns a new instance of Publish.



8
9
10
11
12
13
# File 'app/services/pulitzer/post_type_version/publish.rb', line 8

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

Instance Method Details

#archive_old_versionObject



23
24
25
# File 'app/services/pulitzer/post_type_version/publish.rb', line 23

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

#get_old_published_versionObject



27
28
29
# File 'app/services/pulitzer/post_type_version/publish.rb', line 27

def get_old_published_version
  @post_type_version.post_type.published_type_version
end

#publishObject



15
16
17
18
19
20
21
# File 'app/services/pulitzer/post_type_version/publish.rb', line 15

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

#validate_published_postObject



31
32
33
34
35
36
37
# File 'app/services/pulitzer/post_type_version/publish.rb', line 31

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