Class: Decidim::Proposals::Proposal

Inherits:
ApplicationRecord show all
Includes:
Authorable, Comments::Commentable, Followable, HasAttachments, HasCategory, HasFeature, HasReference, HasScope, Reportable, Resourceable
Defined in:
app/models/decidim/proposals/proposal.rb

Overview

The data store for a Proposal in the Decidim::Proposals component.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.order_randomly(seed) ⇒ Object



30
31
32
33
34
35
# File 'app/models/decidim/proposals/proposal.rb', line 30

def self.order_randomly(seed)
  transaction do
    connection.execute("SELECT setseed(#{connection.quote(seed)})")
    order("RANDOM()").load
  end
end

Instance Method Details

#accepted?Boolean

Public: Checks if the organization has accepted a proposal.

Returns Boolean.

Returns:

  • (Boolean)


63
64
65
# File 'app/models/decidim/proposals/proposal.rb', line 63

def accepted?
  answered? && state == "accepted"
end

#accepts_new_comments?Boolean

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

Returns:

  • (Boolean)


87
88
89
# File 'app/models/decidim/proposals/proposal.rb', line 87

def accepts_new_comments?
  commentable? && !feature.current_settings.comments_blocked
end

#answered?Boolean

Public: Checks if the organization has given an answer for the proposal.

Returns Boolean.

Returns:

  • (Boolean)


56
57
58
# File 'app/models/decidim/proposals/proposal.rb', line 56

def answered?
  answered_at.present?
end

#author_avatar_urlObject



42
43
44
# File 'app/models/decidim/proposals/proposal.rb', line 42

def author_avatar_url
  author&.avatar&.url || ActionController::Base.helpers.asset_path("decidim/default-avatar.svg")
end

#author_nameObject



37
38
39
40
# File 'app/models/decidim/proposals/proposal.rb', line 37

def author_name
  return I18n.t("decidim.proposals.models.proposal.fields.official_proposal") if official?
  user_group&.name || author.name
end

#authored_by?(user) ⇒ Boolean

Checks whether the user is author of the given proposal, either directly authoring it or via a user group.

user - the user to check for authorship

Returns:

  • (Boolean)


140
141
142
# File 'app/models/decidim/proposals/proposal.rb', line 140

def authored_by?(user)
  author == user || user.user_groups.include?(user_group)
end

#commentable?Boolean

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

Returns:

  • (Boolean)


82
83
84
# File 'app/models/decidim/proposals/proposal.rb', line 82

def commentable?
  feature.settings.comments_enabled?
end

#comments_have_alignment?Boolean

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

Returns:

  • (Boolean)


92
93
94
# File 'app/models/decidim/proposals/proposal.rb', line 92

def comments_have_alignment?
  true
end

#comments_have_votes?Boolean

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

Returns:

  • (Boolean)


97
98
99
# File 'app/models/decidim/proposals/proposal.rb', line 97

def comments_have_votes?
  true
end

#editable_by?(user) ⇒ Boolean

Checks whether the user can edit the given proposal.

user - the user to check for authorship

Returns:

  • (Boolean)


147
148
149
# File 'app/models/decidim/proposals/proposal.rb', line 147

def editable_by?(user)
  authored_by?(user) && !answered? && within_edit_time_limit?
end

#evaluating?Boolean

Public: Checks if the organization has marked the proposal as evaluating it.

Returns Boolean.

Returns:

  • (Boolean)


77
78
79
# File 'app/models/decidim/proposals/proposal.rb', line 77

def evaluating?
  answered? && state == "evaluating"
end

#maximum_votesObject

Public: The maximum amount of votes allowed for this proposal.

Returns an Integer with the maximum amount of votes, nil otherwise.



120
121
122
123
124
125
# File 'app/models/decidim/proposals/proposal.rb', line 120

def maximum_votes
  maximum_votes = feature.settings.maximum_votes_per_proposal
  return nil if maximum_votes.zero?

  maximum_votes
end

#maximum_votes_reached?Boolean

Public: The maximum amount of votes allowed for this proposal. 0 means infinite.

Returns true if reached, false otherwise.

Returns:

  • (Boolean)


130
131
132
133
134
# File 'app/models/decidim/proposals/proposal.rb', line 130

def maximum_votes_reached?
  return false unless maximum_votes

  votes.count >= maximum_votes
end

#official?Boolean

Public: Whether the proposal is official or not.

Returns:

  • (Boolean)


113
114
115
# File 'app/models/decidim/proposals/proposal.rb', line 113

def official?
  author.nil?
end

#rejected?Boolean

Public: Checks if the organization has rejected a proposal.

Returns Boolean.

Returns:

  • (Boolean)


70
71
72
# File 'app/models/decidim/proposals/proposal.rb', line 70

def rejected?
  answered? && state == "rejected"
end

#reported_content_urlObject

Public: Overrides the ‘reported_content_url` Reportable concern method.



108
109
110
# File 'app/models/decidim/proposals/proposal.rb', line 108

def reported_content_url
  ResourceLocatorPresenter.new(self).url
end

#users_to_notify_on_comment_createdObject

Public: Override Commentable concern method ‘users_to_notify_on_comment_created`



102
103
104
105
# File 'app/models/decidim/proposals/proposal.rb', line 102

def users_to_notify_on_comment_created
  return (followers | feature.participatory_space.admins).uniq if official?
  followers
end

#voted_by?(user) ⇒ Boolean

Public: Check if the user has voted the proposal.

Returns Boolean.

Returns:

  • (Boolean)


49
50
51
# File 'app/models/decidim/proposals/proposal.rb', line 49

def voted_by?(user)
  votes.where(author: user).any?
end