module Decidim::Amendable

This concern contains the logic related to amendable resources.

Public Instance Methods

add_author(author, user_group = nil) click to toggle source

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

# File lib/decidim/amendable.rb, line 151
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(fields: nil, form: nil) click to toggle source

Public: Configures amendable for this model.

fields - An `Array` of `symbols` specifying the fields that can be amended form - The form used for the validation and creation of the emendation

Returns nothing.

# File lib/decidim/amendable.rb, line 77
def amendable(fields: nil, form: nil)
  raise "You must provide a set of fields to amend" unless fields
  raise "You must provide a form class of the amendable" unless form

  @amendable_options = { fields: fields, form: form }
end
amendable?() click to toggle source

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

# File lib/decidim/amendable.rb, line 110
def amendable?
  amendable.blank?
end
amendable_fields() click to toggle source

Returns the fields that can be amended.

# File lib/decidim/amendable.rb, line 86
def amendable_fields
  self.class.amendable_options[:fields]
end
amendable_form() click to toggle source

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

# File lib/decidim/amendable.rb, line 91
def amendable_form
  self.class.amendable_options[:form].constantize
end
amendment() click to toggle source

Returns the polymorphic association.

# File lib/decidim/amendable.rb, line 96
def amendment
  associated_resource = emendation? ? :emendation : :amendable

  Decidim::Amendment.find_by(associated_resource => id)
end
emendation?() click to toggle source

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

# File lib/decidim/amendable.rb, line 104
def emendation?
  amendable.present?
end
linked_promoted_resource() click to toggle source

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

# File lib/decidim/amendable.rb, line 124
def linked_promoted_resource
  linked_resources(self.class, "created_from_rejected_emendation").first
end
notifiable_identities() click to toggle source

Returns an Array of Decidim::User.

Calls superclass method
# File lib/decidim/amendable.rb, line 168
def notifiable_identities
  if is_a?(Decidim::Authorable)
    [author]
  else # Assume is_a?(Decidim::Coauthorable)
    super
  end
end
process_amendment_state_change!() click to toggle source

Callback called when amendment state is updated

# File lib/decidim/amendable.rb, line 165
def process_amendment_state_change!; end
state() click to toggle source

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

# File lib/decidim/amendable.rb, line 115
def state
  return amendment.state if emendation?

  attributes["state"]
end
visible_amendments_for(user) click to toggle source

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

# File lib/decidim/amendable.rb, line 146
def visible_amendments_for(user)
  amendments.where(emendation: visible_emendations_for(user))
end
visible_emendations_for(user) click to toggle source

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

# File lib/decidim/amendable.rb, line 130
def visible_emendations_for(user)
  published_emendations = emendations.published
  return published_emendations unless component.settings.amendments_enabled

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

    published_emendations.where(decidim_amendments: { decidim_user_id: user.id })
  else # Assume 'all'
    published_emendations
  end
end