Class: Decidim::Debates::Debate

Inherits:
ApplicationRecord show all
Includes:
Authorable, Comments::Commentable, Decidim::DataPortability, Followable, HasCategory, HasComponent, HasReference, Loggable, NewsletterParticipant, Reportable, Resourceable, ScopableComponent, Traceable
Defined in:
app/models/decidim/debates/debate.rb

Overview

The data store for a Debate in the Decidim::Debates component. It stores a title, description and any other useful information to render a custom debate.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.export_serializerObject



101
102
103
# File 'app/models/decidim/debates/debate.rb', line 101

def self.export_serializer
  Decidim::Debates::DataPortabilityDebateSerializer
end

.log_presenter_class_for(_log) ⇒ Object



27
28
29
# File 'app/models/decidim/debates/debate.rb', line 27

def self.log_presenter_class_for(_log)
  Decidim::Debates::AdminLog::DebatePresenter
end

.newsletter_participant_ids(component) ⇒ Object



110
111
112
113
114
115
# File 'app/models/decidim/debates/debate.rb', line 110

def self.newsletter_participant_ids(component)
  Decidim::Debates::Debate.where(component: component).joins(:component)
                          .where(decidim_author_type: Decidim::UserBaseEntity.name)
                          .where.not(author: nil)
                          .pluck(:decidim_author_id).flatten.compact.uniq
end

Instance Method Details

#accepts_new_comments?Boolean

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

Returns:

  • (Boolean)


73
74
75
76
77
# File 'app/models/decidim/debates/debate.rb', line 73

def accepts_new_comments?
  return false unless open?

  commentable? && !comments_blocked?
end

#ama?Boolean

Public: Calculates whether the current debate is an AMA-styled one or not. AMA-styled debates are those that have a start and end time set, and comments are only open during that timelapse. AMA stands for Ask Me Anything, a type of debate inspired by Reddit.

Returns a Boolean.

Returns:

  • (Boolean)


49
50
51
# File 'app/models/decidim/debates/debate.rb', line 49

def ama?
  start_time.present? && end_time.present?
end

#commentable?Boolean

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

Returns:

  • (Boolean)


68
69
70
# File 'app/models/decidim/debates/debate.rb', line 68

def commentable?
  component.settings.comments_enabled?
end

#commentable_typeObject

Public: Identifies the commentable type in the API.



90
91
92
# File 'app/models/decidim/debates/debate.rb', line 90

def commentable_type
  self.class.name
end

#comments_have_alignment?Boolean

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

Returns:

  • (Boolean)


80
81
82
# File 'app/models/decidim/debates/debate.rb', line 80

def comments_have_alignment?
  true
end

#comments_have_votes?Boolean

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

Returns:

  • (Boolean)


85
86
87
# File 'app/models/decidim/debates/debate.rb', line 85

def comments_have_votes?
  true
end

#official?Boolean

Public: Checks whether the debate is official or not.

Returns a boolean.

Returns:

  • (Boolean)


34
35
36
# File 'app/models/decidim/debates/debate.rb', line 34

def official?
  author.is_a?(Decidim::Organization)
end

#open?Boolean

Public: Checks if the debate is open or not.

Returns a boolean.

Returns:

  • (Boolean)


63
64
65
# File 'app/models/decidim/debates/debate.rb', line 63

def open?
  (ama? && open_ama?) || !ama?
end

#open_ama?Boolean

Public: Checks whether the debate is an AMA-styled one and is open.

Returns a boolean.

Returns:

  • (Boolean)


56
57
58
# File 'app/models/decidim/debates/debate.rb', line 56

def open_ama?
  ama? && Time.current.between?(start_time, end_time)
end

#reported_content_urlObject

Public: Overrides the ‘reported_content_url` Reportable concern method.



39
40
41
# File 'app/models/decidim/debates/debate.rb', line 39

def reported_content_url
  ResourceLocatorPresenter.new(self).url
end

#user_allowed_to_comment?(user) ⇒ Boolean

Public: Whether the object can have new comments or not.

Returns:

  • (Boolean)


106
107
108
# File 'app/models/decidim/debates/debate.rb', line 106

def user_allowed_to_comment?(user)
  can_participate_in_space?(user)
end

#users_to_notify_on_comment_createdObject

Public: Override Commentable concern method ‘users_to_notify_on_comment_created`



95
96
97
98
99
# File 'app/models/decidim/debates/debate.rb', line 95

def users_to_notify_on_comment_created
  return Decidim::User.where(id: followers).or(Decidim::User.where(id: component.participatory_space.admins)).distinct if official?

  followers
end