module Decidim::Amendable
This concern contains the logic related to amendable resources.
Public Instance Methods
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
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
Returns the fields that can be amended.
# File lib/decidim/amendable.rb, line 86 def amendable_fields self.class.amendable_options[:fields] end
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
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
Checks if the resource HAS amended another resource. Returns true or false.
# File lib/decidim/amendable.rb, line 104 def emendation? amendable.present? end
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
Returns an Array of Decidim::User.
# 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
Callback called when amendment state is updated
# File lib/decidim/amendable.rb, line 165 def process_amendment_state_change!; end
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
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
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