module Decidim::Coauthorable
This concern contains the logic related to collective or shared authorship.
Coauthorable
shares nearly the same object interface as Authorable
but with some differences.
-
`authored_by?(user)` is exactly the same
-
`normalized_author` is now `identities`.
All coauthors, including the initial author, will share the same permissions.
Public Instance Methods
Checks whether the user is the creator of the given proposal
author - the author to check for authorship
# File lib/decidim/coauthorable.rb, line 119 def created_by?(author) author == creator_author end
Syntactic sugar to access first coauthor as a Coauthorship.
# File lib/decidim/coauthorable.rb, line 112 def creator coauthorships.order(:created_at).first end
Syntactic sugar to access first identity whether it is a User or a UserGroup. @return The User od UserGroup that created this Coauthorable
.
# File lib/decidim/coauthorable.rb, line 130 def creator_identity creator.try(:identity) end
Returns the identities for the authors, whether they are user groups, users or others.
Returns an Array of User, UserGroups or any other entity capable object (such as Organization).
# File lib/decidim/coauthorable.rb, line 100 def identities coauthorships.includes(:author, :user_group).order(:created_at).collect(&:identity) end
Returns the identities that should be notified for a coauthorable.
# File lib/decidim/coauthorable.rb, line 77 def notifiable_identities @notifiable_identities ||= identities.flat_map do |identity| if identity.is_a?(Decidim::User) identity elsif identity.is_a?(Decidim::UserGroup) identity.managers elsif respond_to?(:component) component.participatory_space.admins end end.compact.uniq end
Overwrite default reload method to unset the instance variables so reloading works as expected.
# File lib/decidim/coauthorable.rb, line 52 def reload(options = nil) remove_instance_variable(:@authors) if defined?(@authors) remove_instance_variable(:@notifiable_identities) if defined?(@notifiable_identities) super(options) end
Returns the identities for the authors, only if they are user groups or users.
Returns an Array of User and/or UserGroups.
# File lib/decidim/coauthorable.rb, line 107 def user_identities coauthorships.where(decidim_author_type: "Decidim::UserBaseEntity").order(:created_at).collect(&:identity) end