Skip to content

Commit

Permalink
Allow valuation internal comments to be created
Browse files Browse the repository at this point in the history
How:

Using a local variable at partials to set a hidden true/false value for
`valuation` parameter on the comment creation form.

Allowing that new param at the comment controller and using it when
building a new Comment.
  • Loading branch information
bertocq committed Jan 30, 2018
1 parent 3ca134c commit 5dd0ef0
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 13 deletions.
5 changes: 3 additions & 2 deletions app/controllers/comments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,13 @@ def unflag

def comment_params
params.require(:comment).permit(:commentable_type, :commentable_id, :parent_id,
:body, :as_moderator, :as_administrator)
:body, :as_moderator, :as_administrator, :valuation)
end

def build_comment
@comment = Comment.build(@commentable, current_user, comment_params[:body],
comment_params[:parent_id].presence)
comment_params[:parent_id].presence,
comment_params[:valuation])
check_for_special_comments
end

Expand Down
7 changes: 4 additions & 3 deletions app/models/comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,12 @@ class Comment < ActiveRecord::Base

after_create :call_after_commented

def self.build(commentable, user, body, p_id = nil)
new commentable: commentable,
def self.build(commentable, user, body, p_id = nil, valuation = false)
new(commentable: commentable,
user_id: user.id,
body: body,
parent_id: p_id
parent_id: p_id,
valuation: valuation)
end

def self.find_commentable(c_type, c_id)
Expand Down
3 changes: 2 additions & 1 deletion app/views/comments/_comment_tree.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
<% else %>
<%= render 'comments/form', { commentable: commentable,
parent_id: nil,
toggeable: false } %>
toggeable: false,
valuation: local_assigns.fetch(:valuation, false) } %>
<% end %>
<% else %>
<br>
Expand Down
1 change: 1 addition & 0 deletions app/views/comments/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<%= f.hidden_field :commentable_type, value: commentable.class.name %>
<%= f.hidden_field :commentable_id, value: commentable.id %>
<%= f.hidden_field :parent_id, value: parent_id %>
<%= f.hidden_field :valuation, value: local_assigns.fetch(:valuation, false) %>
<%= f.submit comment_button_text(parent_id, commentable), class: "button", id: "publish_comment" %>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
<%= render partial: '/comments/comment_tree', locals: { comment_tree: @comment_tree,
comment_flags: @comment_flags,
concealed: @concealed,
display_comments_count: false } %>
display_comments_count: false,
valuation: true } %>
<% end %>
</div>
3 changes: 2 additions & 1 deletion app/views/valuation/budget_investments/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@
<%= render partial: '/comments/comment_tree', locals: { comment_tree: @comment_tree,
comment_flags: @comment_flags,
concealed: @concealed,
display_comments_count: false } %>
display_comments_count: false,
valuation: true } %>
<% end %>
</div>

Expand Down
15 changes: 10 additions & 5 deletions spec/features/comments/budget_investments_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,35 @@

scenario 'Index' do
3.times { create(:comment, commentable: investment) }
valuation_comment = create(:comment, :valuation, commentable: investment, subject: 'Not viable')

visit budget_investment_path(investment.budget, investment)

expect(page).to have_css('.comment', count: 3)
expect(page).not_to have_content('Not viable')

comment = Comment.last
within first('.comment') do
expect(page).to have_content comment.user.name
expect(page).to have_content I18n.l(comment.created_at, format: :datetime)
expect(page).to have_content comment.body
within("#comments") do
Comment.not_valuations.last(3).each do |comment|
expect(page).to have_content comment.user.name
expect(page).to have_content I18n.l(comment.created_at, format: :datetime)
expect(page).to have_content comment.body
end
end
end

scenario 'Show' do
parent_comment = create(:comment, commentable: investment)
first_child = create(:comment, commentable: investment, parent: parent_comment)
second_child = create(:comment, commentable: investment, parent: parent_comment)
valuation_comment = create(:comment, :valuation, commentable: investment, subject: 'Not viable')

visit comment_path(parent_comment)

expect(page).to have_css(".comment", count: 3)
expect(page).to have_content parent_comment.body
expect(page).to have_content first_child.body
expect(page).to have_content second_child.body
expect(page).not_to have_content('Not viable')

expect(page).to have_link "Go back to #{investment.title}", href: budget_investment_path(investment.budget, investment)

Expand Down

0 comments on commit 5dd0ef0

Please sign in to comment.