Class: Decidim::Initiative

Inherits:
ApplicationRecord
  • Object
show all
Includes:
ActiveModel::Dirty, Authorable, Comments::Commentable, Followable, HasAttachmentCollections, HasAttachments, HasReference, Decidim::Initiatives::InitiativeSlug, Loggable, Participable, Publicable, Resourceable, Scopable, Traceable
Defined in:
app/models/decidim/initiative.rb

Overview

The data store for a Initiative in the Decidim::Initiatives component.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Decidim::Initiatives::InitiativeSlug

#id_from_slug, #slug_from_id

Class Method Details

.future_spacesObject



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

def self.future_spaces
  none
end

.log_presenter_class_for(_log) ⇒ Object



109
110
111
# File 'app/models/decidim/initiative.rb', line 109

def self.log_presenter_class_for(_log)
  Decidim::Initiatives::AdminLog::InitiativePresenter
end

.order_randomly(seed) ⇒ Object



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

def self.order_randomly(seed)
  transaction do
    connection.execute("SELECT setseed(#{connection.quote(seed)})")
    select('"decidim_initiatives".*, RANDOM()').order(Arel.sql("RANDOM()")).load
  end
end

.past_spacesObject



105
106
107
# File 'app/models/decidim/initiative.rb', line 105

def self.past_spaces
  closed
end

Instance Method Details

#accepts_offline_votes?Boolean

Returns:

  • (Boolean)


278
279
280
281
282
# File 'app/models/decidim/initiative.rb', line 278

def accepts_offline_votes?
  Decidim::Initiatives.face_to_face_voting_allowed &&
    (offline? || any?) &&
    published?
end

#answered?Boolean

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

Returns Boolean.

Returns:

  • (Boolean)


184
185
186
# File 'app/models/decidim/initiative.rb', line 184

def answered?
  answered_at.present?
end

#author_avatar_urlObject

PUBLIC author_avatar_url

Returns the author’s avatar URL. In case it is not defined the method falls back to decidim/default-avatar.svg

RETURNS STRING



162
163
164
165
# File 'app/models/decidim/initiative.rb', line 162

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

#author_nameObject

PUBLIC

Returns the author name. If it has been created by an organization it will return the organization’s name. Otherwise it will return author’s name.

RETURN string



152
153
154
# File 'app/models/decidim/initiative.rb', line 152

def author_name
  user_group&.name || author.name
end

#closed?Boolean

PUBLIC

Returns when an initiative is closed. An initiative is closed when at least one of the following conditions is true:

  • It has been discarded.

  • It has been rejected.

  • It has been accepted.

  • Signature collection period has finished.

RETURNS BOOLEAN

Returns:

  • (Boolean)


142
143
144
# File 'app/models/decidim/initiative.rb', line 142

def closed?
  discarded? || rejected? || accepted? || !votes_enabled?
end

#comments_have_alignment?Boolean

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

Returns:

  • (Boolean)


258
259
260
# File 'app/models/decidim/initiative.rb', line 258

def comments_have_alignment?
  true
end

#comments_have_votes?Boolean

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

Returns:

  • (Boolean)


263
264
265
# File 'app/models/decidim/initiative.rb', line 263

def comments_have_votes?
  true
end

#created_by_individual?Boolean

PUBLIC

Returns true when an initiative has been created by an individual person. False in case it has been created by an authorized organization.

RETURN boolean

Returns:

  • (Boolean)


119
120
121
# File 'app/models/decidim/initiative.rb', line 119

def created_by_individual?
  decidim_user_group_id.nil?
end

#has_authorship?(user) ⇒ Boolean

PUBLIC

Checks if user is the author or is part of the promotal committee of the initiative.

RETURNS boolean

Returns:

  • (Boolean)


273
274
275
276
# File 'app/models/decidim/initiative.rb', line 273

def has_authorship?(user)
  return true if author.id == user.id
  committee_members.approved.where(decidim_users_id: user.id).any?
end

#has_signature_interval_defined?Boolean

Public: Returns wether the signature interval is already defined or not.

Returns:

  • (Boolean)


225
226
227
# File 'app/models/decidim/initiative.rb', line 225

def has_signature_interval_defined?
  signature_end_date.present? && signature_start_date.present?
end

#hashtagObject

Public: Returns the hashtag for the initiative.



230
231
232
# File 'app/models/decidim/initiative.rb', line 230

def hashtag
  attributes["hashtag"].to_s.delete("#")
end

#open?Boolean

PUBLIC

RETURN boolean TRUE when the initiative is open, false in case its not closed.

Returns:

  • (Boolean)


127
128
129
# File 'app/models/decidim/initiative.rb', line 127

def open?
  !closed?
end

#percentageObject

Public: Returns the percentage of required supports reached



241
242
243
244
245
# File 'app/models/decidim/initiative.rb', line 241

def percentage
  percentage = supports_count * 100 / scoped_type.supports_required
  percentage = 100 if percentage > 100
  percentage
end

#publish!Object

Public: Publishes this initiative

Returns true if the record was properly saved, false otherwise.



205
206
207
208
209
210
211
212
213
# File 'app/models/decidim/initiative.rb', line 205

def publish!
  return false if published?
  update(
    published_at: Time.current,
    state: "published",
    signature_start_date: Date.current,
    signature_end_date: Date.current + Decidim::Initiatives.default_signature_time_period_length
  )
end

#scopes_enabledObject

Public: Overrides scopes enabled attribute value. For initiatives it won’t be directly managed by the user and it will be enabled by default.



198
199
200
# File 'app/models/decidim/initiative.rb', line 198

def scopes_enabled
  true
end

#scopes_enabled?Boolean

Public: Overrides scopes enabled flag available in other models like participatory space or assemblies. For initiatives it won’t be directly managed by the user and it will be enabled by default.

Returns:

  • (Boolean)


191
192
193
# File 'app/models/decidim/initiative.rb', line 191

def scopes_enabled?
  true
end

#slugObject

Public: Overrides slug attribute from participatory processes.



248
249
250
# File 'app/models/decidim/initiative.rb', line 248

def slug
  slug_from_id(id)
end

#supports_countObject



234
235
236
237
238
# File 'app/models/decidim/initiative.rb', line 234

def supports_count
  face_to_face_votes = offline_votes.nil? || online? ? 0 : offline_votes
  digital_votes = offline? ? 0 : (initiative_votes_count + initiative_supports_count)
  digital_votes + face_to_face_votes
end

#to_paramObject



252
253
254
# File 'app/models/decidim/initiative.rb', line 252

def to_param
  slug
end

#unpublish!Object

Public: Unpublishes this initiative

Returns true if the record was properly saved, false otherwise.



219
220
221
222
# File 'app/models/decidim/initiative.rb', line 219

def unpublish!
  return false unless published?
  update(published_at: nil, state: "discarded")
end

#votes_enabled?Boolean

Returns:

  • (Boolean)


175
176
177
178
179
# File 'app/models/decidim/initiative.rb', line 175

def votes_enabled?
  published? &&
    signature_start_date <= Date.current &&
    signature_end_date >= Date.current
end