Skip to content

Allow Attachment to answer to can_participate? based on it's attached model#15950

Merged
alecslupu merged 2 commits intodecidim:developfrom
openpoke:fix/attachment_can_participate
Feb 5, 2026
Merged

Allow Attachment to answer to can_participate? based on it's attached model#15950
alecslupu merged 2 commits intodecidim:developfrom
openpoke:fix/attachment_can_participate

Conversation

@davidbeig
Copy link
Copy Markdown
Contributor

@davidbeig davidbeig commented Jan 27, 2026

🎩 What? Why?

Please describe your pull request.

The model attachment is missing a can_participate? function. With this PR we are delegating that to the model at the attached_to field.

📌 Related Issues

Link your PR to an issue

Testing

Describe the best way to test or validate your PR.
You can reproduce the same steps as in the related issue.

Also, you can test in the console if an attachment now answers to this function.

♥️ Thank you!

Summary by CodeRabbit

  • Improvements
    • Attachments now inherit participation status from the content they’re attached to, so uploaded files reflect the same participation permissions and visibility as their parent items. This streamlines permission handling and improves consistency when viewing or interacting with attachments across the platform.

✏️ Tip: You can customize this high-level summary in your review settings.

@davidbeig davidbeig self-assigned this Jan 27, 2026
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Jan 27, 2026

📝 Walkthrough

Walkthrough

Adds a delegation so Decidim::Attachment forwards can_participate? to its attached_to association, enabling can_participate? calls on attachment instances.

Changes

Cohort / File(s) Summary
Attachment model delegation
decidim-core/app/models/decidim/attachment.rb
Added delegation: can_participate? -> attached_to (+1 line). This prevents NoMethodError when callers invoke can_participate? on attachments.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 I hopped along a code-lined trail,
Found an attachment that tripped on a tale.
One tiny delegate — a hop, not a flight,
Now questions of joining return to the right light. 🥕

🚥 Pre-merge checks | ✅ 4 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: adding a can_participate? delegation to the Attachment model based on its attached resource.
Linked Issues check ✅ Passed The PR implements the primary objective from issue #15949 by adding a can_participate? delegation to Attachment, enabling the notification landing to handle attachment notifications without a NoMethodError.
Out of Scope Changes check ✅ Passed The change is minimal and focused solely on adding the can_participate? delegation to the Attachment model, directly addressing the linked issue without introducing unrelated modifications.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions github-actions bot added module: core type: fix PRs that implement a fix for a bug labels Jan 27, 2026
github-actions[bot]
github-actions bot previously approved these changes Jan 27, 2026
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Fix all issues with AI agents
In `@decidim-core/app/models/decidim/attachment.rb`:
- Line 23: The current delegate line "delegate :can_participate?, to:
:attached_to" can raise NoMethodError for polymorphic targets that don't
implement can_participate?; replace this with an explicit instance method (e.g.,
def can_participate?) that mirrors the file's defensive pattern used by
organization and context: check attached_to.present? and use
attached_to.respond_to?(:can_participate?) ? attached_to.can_participate? : true
so attachments default to allowing participation when the target lacks the
method.

@microstudi
Copy link
Copy Markdown
Contributor

Wouldn't be best to use respond_to? wherever this method is used?

@alecslupu
Copy link
Copy Markdown
Contributor

Wouldn't be best to use respond_to? wherever this method is used?

Or to implement what coderabbit suggests ?
image

@davidbeig
Copy link
Copy Markdown
Contributor Author

davidbeig commented Jan 27, 2026

Wouldn't be best to use respond_to? wherever this method is used?

I think that, in the notifications (where we are having this problem), the models that it can be attached to are either components or participatory spaces (both have this method as done in https://github.com/decidim/decidim/pull/15830/files)

An example in one of our instances:

irb(main):005> Decidim::Attachment.pluck(:attached_to_type).uniq
=> ["Decidim::Assembly", "Decidim::Accountability::Result", "Decidim::Meetings::Meeting", "Decidim::ParticipatoryProcess", "Decidim::Blogs::Post"]

Edit:

The models shown do this with the HasComponent concert, that includes this:

    included do
      belongs_to :component, foreign_key: "decidim_component_id", class_name: "Decidim::Component", touch: true
      delegate :organization, to: :component, allow_nil: true
      delegate :participatory_space, :can_participate_in_space?, to: :component, allow_nil: true

      alias_method :can_participate?, :can_participate_in_space?
    end

@microstudi
Copy link
Copy Markdown
Contributor

Wouldn't be best to use respond_to? wherever this method is used?

Or to implement what coderabbit suggests ? image

Sure but preventing the notifications to fail would be nice, these are no critical entries. In this case we should ignore notifications with model attached not responding to this method no?

@davidbeig
Copy link
Copy Markdown
Contributor Author

Well, I've seen that the reported bug has been delivered already in the 0.30.5 and 0.31.1 :_)

@alecslupu
Copy link
Copy Markdown
Contributor

@davidbeig is this PR ready for review?

@davidbeig davidbeig removed their assignment Feb 5, 2026
@davidbeig davidbeig requested review from alecslupu and removed request for alecslupu February 5, 2026 15:29
@alecslupu alecslupu added release: v0.30 Issues or PRs that need to be tackled for v0.30 release: v0.31 Issues or PRs that need to be tackled for v0.31 labels Feb 5, 2026
Copy link
Copy Markdown
Contributor

@alecslupu alecslupu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 - Merging without associated spec

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

module: core release: v0.30 Issues or PRs that need to be tackled for v0.30 release: v0.31 Issues or PRs that need to be tackled for v0.31 type: fix PRs that implement a fix for a bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Notification landing fails when the notification is an attachment

3 participants