Class: Thredded::PostView

Inherits:
Object
  • Object
show all
Defined in:
app/view_models/thredded/post_view.rb

Overview

A view model for PostCommon.

Constant Summary collapse

POST_IS_READ =
:read
POST_IS_UNREAD =
:unread

Instance Method Summary collapse

Constructor Details

#initialize(post, policy, topic_view: nil, first_in_page: false, first_unread_in_page: false) ⇒ PostView

Returns a new instance of PostView.

Parameters:

  • post (Thredded::PostCommon)
  • policy (#create? #update? #destroy? #moderate?)
  • topic_view (Thredded::TopicView) (defaults to: nil)
  • first_in_page (Boolean) (defaults to: false)
  • first_unread_in_page (Boolean) (defaults to: false)

24
25
26
27
28
29
30
# File 'app/view_models/thredded/post_view.rb', line 24

def initialize(post, policy, topic_view: nil, first_in_page: false, first_unread_in_page: false)
  @post   = post
  @policy = policy
  @topic_view = topic_view
  @first_unread_in_page = first_unread_in_page
  @first_in_page = first_in_page
end

Instance Method Details

#can_destroy?Boolean

Returns:

  • (Boolean)

40
41
42
# File 'app/view_models/thredded/post_view.rb', line 40

def can_destroy?
  @can_destroy ||= @policy.destroy?
end

#can_moderate?Boolean

Returns:

  • (Boolean)

44
45
46
# File 'app/view_models/thredded/post_view.rb', line 44

def can_moderate?
  @can_moderate ||= @policy.moderate?
end

#can_reply?Boolean

Returns:

  • (Boolean)

32
33
34
# File 'app/view_models/thredded/post_view.rb', line 32

def can_reply?
  @can_reply ||= @policy.create?
end

#can_update?Boolean

Returns:

  • (Boolean)

36
37
38
# File 'app/view_models/thredded/post_view.rb', line 36

def can_update?
  @can_update ||= @policy.update?
end

#destroy_pathObject


68
69
70
# File 'app/view_models/thredded/post_view.rb', line 68

def destroy_path
  Thredded::UrlsHelper.delete_post_path(@post)
end

#edit_pathObject


60
61
62
# File 'app/view_models/thredded/post_view.rb', line 60

def edit_path
  Thredded::UrlsHelper.edit_post_path(@post)
end

#first_in_page?Boolean

Returns:

  • (Boolean)

94
95
96
# File 'app/view_models/thredded/post_view.rb', line 94

def first_in_page?
  @first_in_page
end

#first_unread_in_page?Boolean

Returns:

  • (Boolean)

90
91
92
# File 'app/view_models/thredded/post_view.rb', line 90

def first_unread_in_page?
  @first_unread_in_page
end

#mark_unread_pathObject


64
65
66
# File 'app/view_models/thredded/post_view.rb', line 64

def mark_unread_path
  Thredded::UrlsHelper.mark_unread_path(@post)
end

72
73
74
# File 'app/view_models/thredded/post_view.rb', line 72

def permalink_path
  Thredded::UrlsHelper.permalink_path(@post)
end

#quote_pathObject


56
57
58
# File 'app/view_models/thredded/post_view.rb', line 56

def quote_path
  Thredded::UrlsHelper.quote_post_path(@post)
end

#quote_url_paramsObject


48
49
50
51
52
53
54
# File 'app/view_models/thredded/post_view.rb', line 48

def quote_url_params
  if @post.private_topic_post?
    { post: { quote_private_post_id: @post.id } }
  else
    { post: { quote_post_id: @post.id } }
  end.update(anchor: 'post_content')
end

#read_stateObject

returns nil if read state is not appropriate to the view (i.e. viewing posts outside a topic)


80
81
82
83
84
85
86
87
88
# File 'app/view_models/thredded/post_view.rb', line 80

def read_state
  if @topic_view.nil? || @policy.anonymous?
    nil
  elsif @topic_view.post_read?(@post)
    POST_IS_READ
  else
    POST_IS_UNREAD
  end
end