Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Commentable Interface #882

Merged
merged 13 commits into from
Feb 14, 2017
Merged

Add Commentable Interface #882

merged 13 commits into from
Feb 14, 2017

Conversation

beagleknight
Copy link
Contributor

@beagleknight beagleknight commented Feb 10, 2017

🎩 What? Why?

I added two flags for managing comments in a specific feature:

  • comments_enabled: A global setting to enable or disable comments for a feature.
  • comments_blocked: A step setting to prevent new comments to be added.

Then I created the Commentable interface so any model can implement it. Since it include all the logic needed to render the comments component, the comment helper has been simplified:

<%= comments_for @some_commentable%>

This changed pushed a few changes into the API:

Get commentable's comments:

commentable(id: $id, type: $type) {
  comments {
    ...
  }
}

Add a new comment:

commentable(id: $id, type: $type) {
  addComment(body: $body) {
    ...
  }
}

These changes will be more effective when we start implementing other types for our current models like Proposal, Meeting and so on.

📌 Related Issues

📋 Subtasks

  • Remove comments_always_enabled general setting
  • Add Commentable interface
  • Add Commentable type and interface for graphql
  • Refactor components
  • Fix AddCommentFormComponent
  • Add comments_enabled and comments_blocked logic for all features using comments
  • Add blocked comments callout
  • Add sign-in to comment message

📷 Screenshots (optional)

image
image

👻 GIF

@beagleknight beagleknight self-assigned this Feb 10, 2017
@beagleknight beagleknight force-pushed the feature/comments/enabled branch from 8e6a98b to 7406e0e Compare February 13, 2017 09:04
@beagleknight beagleknight changed the title Add comments_enabled step setting logic Add Commentable Interface Feb 13, 2017
@@ -22,11 +22,11 @@
feature.settings(:global) do |settings|
settings.attribute :total_budget, type: :integer, default: 100_000_000
settings.attribute :vote_threshold_percent, type: :integer, default: 70
settings.attribute :comments_always_enabled, type: :boolean, default: true
settings.attribute :comments_enabled, type: :boolean, default: true
Copy link
Contributor

Choose a reason for hiding this comment

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

We need a migration for these settings and current features

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@oriolgual If the setting is not found it uses the default value so the migration is not needed.

if comment.replies.size.positive?
comment.replies.size + comment.replies.map { |reply| count_replies(reply) }.sum
if comment.comments.size.positive?
comment.comments.size + comment.comments.map { |reply| count_replies(reply) }.sum
Copy link
Contributor

Choose a reason for hiding this comment

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

No need to map & sum, you can sum directly

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, I didn't know that! Great

@@ -21,6 +21,7 @@ ca:
subject: Tens una nova resposta del teu comentari
components:
add_comment_form:
account_message: "<a href=\"%{sign_in_url}\">Entra amb el teu compte</a> o <a href=\"%{sign_up_url}\">regístra't</a> per a deixar un comentari."
Copy link
Contributor

Choose a reason for hiding this comment

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

registra't

@@ -47,6 +48,7 @@ ca:
comment_thread:
title: Conversa amb %{authorName}
comments:
blocked_comments_warning: Els comentaris estan desactivats a la fase actual pero pots llegir els comentaris de les fases anteriors.
Copy link
Contributor

Choose a reason for hiding this comment

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

però

@@ -22,6 +22,8 @@ en:
subject: You have a new reply of your comment
components:
add_comment_form:
account_message: "<a href=\"%{sign_in_url}\">Sign in with your account</a> or <a href=\"%{sign_up_url}\">sign up</a> to leave your comment.
Copy link
Contributor

Choose a reason for hiding this comment

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

add your comment

@@ -21,6 +21,7 @@ es:
subject: Uno de tus comentarios ha recibido respuesta
components:
add_comment_form:
account_message: "<a href=\"%{sign_in_url}\">Entra con tu cuenta</a> o <a href=\"%{sign_up_url}\">regístratet</a> para dejar tu comentario."
Copy link
Contributor

Choose a reason for hiding this comment

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

regístrate


has_many :comments, as: :commentable, foreign_key: "decidim_commentable_id", foreign_type: "decidim_commentable_type", class_name: "Decidim::Comments::Comment"

def commentable?
Copy link
Contributor

Choose a reason for hiding this comment

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

Should all these methods be implemented in the models were we include this? If so I'd raise NotImplementedError

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think there is no need to implement those methods. They are just returning boolean values and not doing anything else. The default ones are just standard: enable comments without extra options. What do you think?

commentable_id = resource.id.to_s
node_id = "comments-for-#{commentable_type.demodulize}-#{commentable_id}"
def comments_for(resource)
if resource.commentable?
Copy link
Contributor

Choose a reason for hiding this comment

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

return unless resource.commentable?

"{ addComment(body: \"#{body}\", alignment: #{alignment}) { body } }"
}

it "should call CreateComment command" do
Copy link
Contributor

Choose a reason for hiding this comment

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

it "calls

@codecov-io
Copy link

codecov-io commented Feb 13, 2017

Codecov Report

Merging #882 into master will decrease coverage by -0.02%.
The diff coverage is 99.29%.

@@            Coverage Diff             @@
##           master     #882      +/-   ##
==========================================
- Coverage   97.25%   97.23%   -0.02%     
==========================================
  Files         370      374       +4     
  Lines        6075     6248     +173     
==========================================
+ Hits         5908     6075     +167     
- Misses        167      173       +6
Impacted Files Coverage Δ
...m-comments/lib/decidim/comments/comments_helper.rb 100% <100%> (ø)
...-comments/lib/decidim/comments/api/comment_type.rb 100% <100%> (ø)
...im-comments/app/models/decidim/comments/comment.rb 100% <100%> (ø)
...idim-budgets/app/models/decidim/budgets/project.rb 100% <100%> (ø)
...nts/app/types/decidim/comments/commentable_type.rb 100% <100%> (ø)
decidim-pages/app/models/decidim/pages/page.rb 100% <100%> (ø)
decidim-results/lib/decidim/results/feature.rb 95.45% <100%> (ø)
...ypes/decidim/comments/commentable_mutation_type.rb 100% <100%> (ø)
decidim-proposals/lib/decidim/proposals/feature.rb 100% <100%> (ø)
...p/controllers/concerns/decidim/feature_settings.rb 100% <100%> (ø)
... and 17 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d16d4ad...bd15299. Read the comment docs.

@mention-bot
Copy link

@beagleknight, thanks for your PR! By analyzing the history of the files in this pull request, we identified @josepjaume and @oriolgual to be potential reviewers.

@@ -1,12 +1,12 @@
fragment Comment on Comment {
...CommentData
replies {
comments {
Copy link
Contributor

Choose a reason for hiding this comment

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

As a future opportunity, maybe we could flatten this query and build the tree it afterwards, and/or use query batching

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree. I think we can use another approach in order to allow infinite nesting (as long as the ui supports it).

@@ -46,7 +52,8 @@ describe('<Comments />', () => {

stubComponent(AddCommentForm, {
fragments: {
user: addCommentFragment
session: addCommentFragmentSession,
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't this be addCommentSessionFragment?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No! It should be addCommentFormSessionFragment 😂

@@ -46,7 +52,8 @@ describe('<Comments />', () => {

stubComponent(AddCommentForm, {
fragments: {
user: addCommentFragment
session: addCommentFragmentSession,
commentable: addCommentFragmentCommentable
Copy link
Contributor

Choose a reason for hiding this comment

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

Same as up above.

description "A commentable object"

interfaces [
Decidim::Comments::CommentableInterface
Copy link
Contributor

Choose a reason for hiding this comment

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

ULTRA NICE 🐇 🌈 🎈


has_many :comments, as: :commentable, foreign_key: "decidim_commentable_id", foreign_type: "decidim_commentable_type", class_name: "Decidim::Comments::Comment"

def commentable?
Copy link
Contributor

Choose a reason for hiding this comment

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

We should add docs indicating what all these defaults mean and what effect they have on comments.

@beagleknight beagleknight force-pushed the feature/comments/enabled branch from 1e6e76b to bd15299 Compare February 14, 2017 10:10
@oriolgual oriolgual requested a review from josepjaume February 14, 2017 10:11
@josepjaume josepjaume merged commit 18a1e26 into master Feb 14, 2017
@josepjaume josepjaume deleted the feature/comments/enabled branch February 14, 2017 14:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants