Module: Decidim::Amendable

Extended by:
ActiveSupport::Concern
Defined in:
lib/decidim/amendable.rb,
app/forms/decidim/amendable/form.rb,
app/commands/decidim/amendable/accept.rb,
app/commands/decidim/amendable/reject.rb,
app/forms/decidim/amendable/edit_form.rb,
app/commands/decidim/amendable/promote.rb,
app/commands/decidim/amendable/withdraw.rb,
app/forms/decidim/amendable/create_form.rb,
app/forms/decidim/amendable/reject_form.rb,
app/forms/decidim/amendable/review_form.rb,
app/forms/decidim/amendable/promote_form.rb,
app/forms/decidim/amendable/publish_form.rb,
app/commands/decidim/amendable/create_draft.rb,
app/commands/decidim/amendable/update_draft.rb,
app/commands/decidim/amendable/destroy_draft.rb,
app/commands/decidim/amendable/publish_draft.rb,
app/cells/decidim/amendable/announcement_cell.rb,
app/cells/decidim/amendable/amenders_list_cell.rb,
app/cells/decidim/amendable/wizard_step_form_cell.rb,
app/events/decidim/amendable/amendment_base_event.rb,
app/cells/decidim/amendable/amend_button_card_cell.rb,
app/cells/decidim/amendable/emendation_actions_cell.rb,
app/cells/decidim/amendable/promote_button_card_cell.rb,
app/events/decidim/amendable/amendment_created_event.rb,
app/events/decidim/amendable/amendment_accepted_event.rb,
app/events/decidim/amendable/amendment_rejected_event.rb,
app/events/decidim/amendable/emendation_promoted_event.rb

Overview

This concern contains the logic related to amendable resources.

Defined Under Namespace

Classes: Accept, AmendButtonCardCell, AmendersListCell, AmendmentAcceptedEvent, AmendmentBaseEvent, AmendmentCreatedEvent, AmendmentRejectedEvent, AnnouncementCell, CreateDraft, CreateForm, DestroyDraft, EditForm, EmendationActionsCell, EmendationPromotedEvent, Form, Promote, PromoteButtonCardCell, PromoteForm, PublishDraft, PublishForm, Reject, RejectForm, ReviewForm, UpdateDraft, Withdraw, WizardStepFormCell

Instance Method Summary collapse

Instance Method Details

#add_author(author, user_group = nil) ⇒ Object

Handles the logic to assign an author to the resource, be it Authorable or Coauthorable.



150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/decidim/amendable.rb', line 150

def add_author(author, user_group = nil)
  if is_a?(Decidim::Authorable)
    if persisted?
      update(author: user_group || author)
    else
      self.author = user_group || author
    end
  else # Assume is_a?(Decidim::Coauthorable)
    coauthorships.clear
    add_coauthor(author, user_group: user_group)
  end
end

#amendable?Boolean

Checks if the resource CAN be amended by other resources. Returns true or false.

Returns:

  • (Boolean)


109
110
111
# File 'lib/decidim/amendable.rb', line 109

def amendable?
  amendable.blank?
end

#amendable_fieldsObject

Returns the fields that can be amended.



85
86
87
# File 'lib/decidim/amendable.rb', line 85

def amendable_fields
  self.class.amendable_options[:fields]
end

#amendable_formObject

Returns the form used for the validation and creation of the emendation.



90
91
92
# File 'lib/decidim/amendable.rb', line 90

def amendable_form
  self.class.amendable_options[:form].constantize
end

#amendmentObject

Returns the polymorphic association.



95
96
97
98
99
# File 'lib/decidim/amendable.rb', line 95

def amendment
  associated_resource = emendation? ? :emendation : :amendable

  Decidim::Amendment.find_by(associated_resource => id)
end

#emendation?Boolean

Checks if the resource HAS amended another resource. Returns true or false.

Returns:

  • (Boolean)


103
104
105
# File 'lib/decidim/amendable.rb', line 103

def emendation?
  amendable.present?
end

#linked_promoted_resourceObject

Returns the linked resource to or from this model for the given resource name and link name. See Decidim::Resourceable#link_resources



123
124
125
# File 'lib/decidim/amendable.rb', line 123

def linked_promoted_resource
  linked_resources(self.class, "created_from_rejected_emendation").first
end

#notifiable_identitiesObject

Returns an Array of Decidim::User.



164
165
166
167
168
169
170
# File 'lib/decidim/amendable.rb', line 164

def notifiable_identities
  if is_a?(Decidim::Authorable)
    [author]
  else # Assume is_a?(Decidim::Coauthorable)
    super
  end
end

#stateObject

Returns the state of the amendment or the state of the resource.



114
115
116
117
118
# File 'lib/decidim/amendable.rb', line 114

def state
  return amendment.state if emendation?

  attributes["state"]
end

#visible_amendments_for(user) ⇒ Object

Returns the amendments (polymorphic association) of the emendations that are visible to the user based on the component’s amendments settings.



145
146
147
# File 'lib/decidim/amendable.rb', line 145

def visible_amendments_for(user)
  amendments.where(emendation: visible_emendations_for(user))
end

#visible_emendations_for(user) ⇒ Object

Returns the emendations of an amendable that are visible to the user based on the component’s amendments settings and filtering out the “drafts”.



129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/decidim/amendable.rb', line 129

def visible_emendations_for(user)
  pubslished_emendations = emendations.published
  return pubslished_emendations unless component.settings.amendments_enabled

  case component.current_settings.amendments_visibility
  when "participants"
    return self.class.none unless user

    pubslished_emendations.where("decidim_amendments.decidim_user_id = ?", user.id)
  else # Assume 'all'
    pubslished_emendations
  end
end