Class: Decidim::Posts::Post

Inherits:
Feeds::ApplicationRecord
  • Object
show all
Includes:
Authorable, Comments::Commentable, Endorsable, FilterableResource, Fingerprintable, HasAttachments, HasComponent, Reactionable, Reportable, Resourceable, Searchable, TranslatableAttributes, TranslatableResource
Defined in:
app/models/decidim/posts/post.rb

Instance Method Summary collapse

Instance Method Details

#comments_have_alignment?Boolean

Public: Overrides the ‘comments_have_alignment?` Commentable concern method. Whether the object’s comments can have alignment or not. It enables the alignment selector in the add new comment form.

Returns:

  • (Boolean)


102
103
104
# File 'app/models/decidim/posts/post.rb', line 102

def comments_have_alignment?
  false
end

#comments_have_votes?Boolean

Public: Overrides the ‘comments_have_votes?` Commentable concern method.

Returns:

  • (Boolean)


107
108
109
# File 'app/models/decidim/posts/post.rb', line 107

def comments_have_votes?
  true
end

#deleteable_by?(user) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
# File 'app/models/decidim/posts/post.rb', line 74

def deleteable_by?(user)
  user == author || user.admin?
end

#editable_by?(user) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
# File 'app/models/decidim/posts/post.rb', line 70

def editable_by?(user)
  user == author
end

#reported_attributesObject

Public: Overrides the ‘reported_attributes` Reportable concern method.



84
85
86
# File 'app/models/decidim/posts/post.rb', line 84

def reported_attributes
  [:body]
end

#reported_content_urlObject

Public: Overrides the ‘reported_content_url` Reportable concern method.



79
80
81
# File 'app/models/decidim/posts/post.rb', line 79

def reported_content_url
  ResourceLocatorPresenter.new(self).url
end

#reported_searchable_content_extrasObject

Public: Overrides the ‘reported_searchable_content_extras` Reportable concern method.



89
90
91
# File 'app/models/decidim/posts/post.rb', line 89

def reported_searchable_content_extras
  [author.name]
end

#survey_responses_countObject



93
94
95
96
97
# File 'app/models/decidim/posts/post.rb', line 93

def survey_responses_count
  # questions.includes(answers: :user_answers).map(&:answers).flatten.map(&:user_answers).flatten.pluck(:decidim_user_id).uniq.count
  answer_ids = questions.includes(:answers).map(&:answers).flatten.pluck(:id)
  @survey_responses_count ||= UserAnswer.where(decidim_posts_answer_id: answer_ids).distinct.count(:decidim_user_id)
end

#titleObject

Posts don’t have a title, but Commentable requires it, so let’s extract the first words of each translation of the body



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/models/decidim/posts/post.rb', line 53

def title
  truncated_body = {}
  body.each do |locale, translations|
    if locale == "machine_translations"
      truncated_body[locale] = {}
      translations.each do |key, value|
        next if value.class != String
        truncated_body[locale][key] = value.truncate_words(4)
      end
    else
      next if translations.class != String
      truncated_body[locale] = translations.truncate_words(4)
    end
  end
  truncated_body
end

#users_to_notify_on_comment_createdObject



111
112
113
# File 'app/models/decidim/posts/post.rb', line 111

def users_to_notify_on_comment_created
  [author]
end