app/models/decidim/proposals/proposal.rb in decidim-proposals-0.7.4 vs app/models/decidim/proposals/proposal.rb in decidim-proposals-0.8.0

- old
+ new

@@ -130,8 +130,31 @@ def maximum_votes_reached? return false unless maximum_votes votes.count >= maximum_votes end + + # Checks whether the user is author of the given proposal, either directly + # authoring it or via a user group. + # + # user - the user to check for authorship + def authored_by?(user) + author == user || user.user_groups.include?(user_group) + end + + # Checks whether the user can edit the given proposal. + # + # user - the user to check for authorship + def editable_by?(user) + authored_by?(user) && !answered? && within_edit_time_limit? + end + + private + + # Checks whether the proposal is inside the time window to be editable or not. + def within_edit_time_limit? + limit = created_at + feature.settings.proposal_edit_before_minutes.minutes + Time.current < limit + end end end end