Class: PostManager

Inherits:
Object
  • Object
show all
Defined in:
app/services/post_manager.rb

Overview

Manager for handling post-related cases

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(entity, locale = I18n.locale) ⇒ PostManager

Returns a new instance of PostManager.

Parameters:

  • entity (Post|PostCategory)
  • locale (Symbol|String) (defaults to: I18n.locale)


9
10
11
12
# File 'app/services/post_manager.rb', line 9

def initialize(entity, locale = I18n.locale)
  self.entity = entity
  self.locale = locale
end

Instance Attribute Details

#localeObject

Returns the value of attribute locale.



5
6
7
# File 'app/services/post_manager.rb', line 5

def locale
  @locale
end

Class Method Details

.editor?(user, type) ⇒ Boolean

Parameters:

  • user (User)
  • type (String|PostType)

Returns:

  • (Boolean)


16
17
18
19
20
21
22
# File 'app/services/post_manager.rb', line 16

def self.editor?(user, type)
  return true if UserPrivilege.user_has_privilege?(user, :chief_editor)

  type = PostType.find_by(slug: type) unless type.is_a?(PostType)

  EditorialMember.find_by(user: user)&.post_type?(type)
end

.enclosures(post) ⇒ Object

Parameters:

  • post (Post)


25
26
27
28
29
# File 'app/services/post_manager.rb', line 25

def self.enclosures(post)
  post.parsed_body.scan(/<img[^>]+>/).map do |image|
    image.scan(/src="([^"]+)"/)[0][0]
  end
end

Instance Method Details

#category_nameObject



64
65
66
67
68
69
70
71
72
73
74
# File 'app/services/post_manager.rb', line 64

def category_name
  if @entity.is_a?(Post)
    if @entity..nil?
      @entity.post_type.default_category_name
    else
      @entity.post_categories.map(&:text_for_link)
    end
  else
    @entity.text_for_link
  end
end

#category_pathObject



56
57
58
59
60
61
62
# File 'app/services/post_manager.rb', line 56

def category_path
  postfix = ''
  suffix = @entity.is_a?(Post) ? @entity. : @entity
  postfix += "/#{suffix.long_slug}" unless suffix.nil?

  empty_category_path + postfix
end

#edit_pathObject



47
48
49
# File 'app/services/post_manager.rb', line 47

def edit_path
  "#{@prefix}/posts/#{@entity.id}/edit" if @entity.is_a?(Post)
end

#empty_category_pathObject



76
77
78
# File 'app/services/post_manager.rb', line 76

def empty_category_path
  "#{@prefix}/#{@url_part}"
end

#entity=(entity) ⇒ Object

Parameters:

  • entity (Post|PostCategory)


32
33
34
35
36
37
# File 'app/services/post_manager.rb', line 32

def entity=(entity)
  @entity = entity
  @body = entity.body.to_s if @entity.is_a?(Post)
  @prefix = locale.nil? || locale == I18n.default_locale ? '' : "/#{locale}"
  @url_part = entity.post_type.url_part
end

#parsed_bodyObject



39
40
41
# File 'app/services/post_manager.rb', line 39

def parsed_body
  PostParser.new(@entity).parsed_body if @entity.is_a?(Post)
end

#post_pathObject



43
44
45
# File 'app/services/post_manager.rb', line 43

def post_path
  "#{@prefix}/#{@url_part}/#{@entity.id}-#{@entity.slug}" if @entity.is_a?(Post)
end

#tagged_path(tag_name) ⇒ Object

Parameters:

  • tag_name (String)


52
53
54
# File 'app/services/post_manager.rb', line 52

def tagged_path(tag_name)
  "#{@prefix}/#{@url_part}/tagged/#{CGI.escape(tag_name)}"
end