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

[WIP]Comment and subscribe #4296

Closed
wants to merge 13 commits into from
2 changes: 2 additions & 0 deletions app/controllers/comment_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ def create
@node = Node.find params[:id]
@body = params[:body]
@user = current_user
@tag_list = params[:choice]
TagSelection.subscribe_multiple_tags(@node,@user,@tag_list)
begin
@comment = create_comment(@node, @user, @body)
respond_with do |format|
Expand Down
15 changes: 15 additions & 0 deletions app/models/tag_selection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,19 @@ def ruser
def tagname
tag.name
end

#Given a tagList subscribe to the tags which are checked in tagList
# and unsubscribe to tags unchecked in the tag_list
def subscribe_multiple_tags(node, user, tag_list)
for node.tags do |t|
value = tag_list[t.tid]
subscription = TagSelection.where(:user_id => user.uid,
:tid => t.tid).first_or_create
subscription.following = value
if subscription.following_changed?
subscription.save!
end
end
end

end
11 changes: 10 additions & 1 deletion app/views/comments/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,19 @@

</span>
</p>

</div>
</div>

Subscribe to popular topics:
<% if params[:controller] == "notes" %>
<%= form_for Comment.new do |f| %>
<% tag_list = @node.tags %>
<% tag_list.each do |t| %>
<%= check_box_tag 'choice[tid][]', t.name %>
<%= t.name %>
<% end %>
<% end %>
<% end %>
<div class="well col-md-11" id="preview" style="background: white; display: none;">
</div>
<script>
Expand Down
9 changes: 9 additions & 0 deletions app/views/subscriptions/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<div class="field">
<%= f.label "Subscribe to popular tags related to this post" %><br />
<%= f.collection_check_boxes :tids, Tag.all, :id, :name do |b| %>
<div class="collection-check-box">
<%= b.check_box %>
<%= b.label %>
</div>
<% end %>
</div>
12 changes: 11 additions & 1 deletion test/fixtures/tag_selections.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,14 @@ selection_ten:
selection_eleven:
user_id: 10
tid: 20
following: true
following: true

spectacular_one:
user_id: 1
tid: 14
following: false

spectacular_two:
user_id: 1
tid: 12
following: false
17 changes: 17 additions & 0 deletions test/unit/node_tag_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,21 @@ class NodeTagTest < ActiveSupport::TestCase
assert_not nodes(:blog).can_tag(tagname, user)
assert_equal I18n.t('node.page_does_not_exist'), nodes(:blog).can_tag(tagname, user, true)
end

test 'subscribe to the tags' do
user = users(:bob)
node = nodes(:one)
# present in the db tag_list = { {1 => true}, {2 => true}, {14 => false}, {12 => false} }
# supplied with the radio buttons
tag_list = { {1 => true}, {2 => false}, {14 => true}, {12 => false}}
TagSelection.subscribe_multiple_tags(node, user, tag_list)
selection1 = tag_selections(:selection_one)
awesome = tag_selections(:awesome)
spectacular1 = tag_selections(:spectacular_one)
spectacular2 = tag_selections(:spectacular_two)
assert_equal selection.following, true
assert_equal awesome.following, false
assert_equal spectacular1.following, true
assert_equal spectacular2.following, false
end
end