Class: PostPresenter

Inherits:
BasePresenter show all
Includes:
MetaDataHelper, PostsHelper
Defined in:
app/presenters/post_presenter.rb

Instance Attribute Summary collapse

Attributes inherited from BasePresenter

#current_user

Instance Method Summary collapse

Methods included from MetaDataHelper

#default_author_name, #default_description, #default_image_url, #default_title, #general_metas, #meta_tag, #metas_tags, #og_prefix, #site_url

Methods included from PostsHelper

#post_iframe_url, #post_page_title

Methods inherited from BasePresenter

as_collection, #method_missing, new

Constructor Details

#initialize(presentable, current_user = nil) ⇒ PostPresenter

Returns a new instance of PostPresenter.


9
10
11
12
# File 'app/presenters/post_presenter.rb', line 9

def initialize(presentable, current_user=nil)
  @post = presentable
  super
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class BasePresenter

Instance Attribute Details

#postObject

Returns the value of attribute post


7
8
9
# File 'app/presenters/post_presenter.rb', line 7

def post
  @post
end

Instance Method Details

#as_api_responseObject

rubocop:disable Metrics/AbcSize


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/presenters/post_presenter.rb', line 19

def as_api_response # rubocop:disable Metrics/AbcSize
  {
    guid:                  @post.guid,
    body:                  build_text,
    title:                 title,
    post_type:             @post.post_type,
    public:                @post.public,
    created_at:            @post.created_at,
    nsfw:                  @post.nsfw,
    author:                PersonPresenter.new(@post.author).as_api_json,
    provider_display_name: @post.provider_display_name,
    interaction_counters:  PostInteractionPresenter.new(@post, current_user).as_counters,
    location:              location_as_api_json,
    poll:                  PollPresenter.new(@post.poll, current_user).as_api_json,
    mentioned_people:      build_mentioned_people_json,
    photos:                build_photos_json,
    root:                  root_api_response,
    own_interaction_state: build_own_interaction_state,
    open_graph_object:     open_graph_object_api_response,
    oembed:                @post.o_embed_cache.try(:data)
  }.compact
end

#as_json(_options = {}) ⇒ Object


14
15
16
17
# File 'app/presenters/post_presenter.rb', line 14

def as_json(_options={})
  @post.as_json(only: directly_retrieved_attributes)
       .merge(non_directly_retrieved_attributes)
end

#metas_attributesObject


56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/presenters/post_presenter.rb', line 56

def metas_attributes
  {
    keywords:             {name:     "keywords",       content: comma_separated_tags},
    description:          {name:     "description",    content: description},
    og_url:               {property: "og:url",         content: url},
    og_title:             {property: "og:title",       content: title},
    og_image:             {property: "og:image",       content: images},
    og_description:       {property: "og:description", content: description},
    og_article_tag:       {property: "og:article:tag", content: tags},
    og_article_author:    {property: "og:article:author",         content: author_name},
    og_article_modified:  {property: "og:article:modified_time",  content: modified_time_iso8601},
    og_article_published: {property: "og:article:published_time", content: published_time_iso8601}
  }
end

#page_titleObject


71
72
73
# File 'app/presenters/post_presenter.rb', line 71

def page_title
  post_page_title @post
end

#with_initial_interactionsObject


47
48
49
50
51
52
53
54
# File 'app/presenters/post_presenter.rb', line 47

def with_initial_interactions
  as_json.tap do |post|
    post[:interactions].merge!(
      likes:    LikeService.new(current_user).find_for_post(@post.id).limit(30).as_api_response(:backbone),
      reshares: ReshareService.new(current_user).find_for_post(@post.id).limit(30).as_api_response(:backbone)
    )
  end
end

#with_interactionsObject


42
43
44
45
# File 'app/presenters/post_presenter.rb', line 42

def with_interactions
  interactions = PostInteractionPresenter.new(@post, current_user)
  as_json.merge!(interactions: interactions.as_json)
end