Skip to content

Commit

Permalink
Fixes thread edit to save newly created codes for #61.
Browse files Browse the repository at this point in the history
  • Loading branch information
elplatt committed Mar 20, 2013
1 parent f166044 commit 89100a5
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 16 deletions.
19 changes: 16 additions & 3 deletions app/controllers/threads_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,22 @@ def update
image = Image.create!({ image_name: image_info["image_name"],publication_date: image_info[:publication_date], local_path: image_info[:local_path], media_id: media.id, size: image_size})
end
end
number_of_topics = params[:topic_count]

#it should iterate through the recently created codes
#it should iterate through the recently created codes
params["code_id"].each_with_index do |id, index|
if id.empty?
@thread.codes.create({code_text: params["topic_name_#{index}"], color: params["topic_color_#{index}"], code_description: params["topic_description_#{index}"]})
# New code
else
# Fetch existing code from database by id and update
code = @thread.codes.find_by_id(id)
if params["topic_deleted_#{index}"] == '1'
code.destroy()
else
code.update_attributes({code_text: params["topic_name_#{index}"], color: params["topic_color_#{index}"], code_description: params["topic_description_#{index}"]})
end
end
end
@thread.codes.to_enum.with_index.each do |code,index|
if params["topic_deleted_#{index}"] == '1'
code.destroy()
Expand Down Expand Up @@ -253,7 +266,7 @@ def destroy

def new_topic #we should be consitent and use code or topic
render :partial => 'topic_form', :locals => {
:index => params[:index], :name => nil, :color => nil, :description => nil}
:index => params[:index], :id => nil, :name => nil, :color => nil, :description => nil}
end

end
3 changes: 3 additions & 0 deletions app/views/threads/_topic_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
=begin
Locals:
index - integer index of the topic
id - database id of the code
name - name of the topic
color - hex color for the topic
description - text describing the topic
Expand All @@ -12,6 +13,8 @@ human_topic_number = (Integer(index) + 1).to_s

<div id=<%= "topic_#{index}" %> class="thumbnail">

<%= hidden_field_tag "code_id[]", id %>

<div id="topic_name_box" class="control-group">
<%= label_tag "topic_name_#{index}", "Topic #{human_topic_number}", :id => "topic_name_label_#{index}", :class => "control-label" %>
<div class="controls">
Expand Down
13 changes: 1 addition & 12 deletions app/views/threads/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
<%
@thread.codes.to_enum.with_index.each do |code,index| %>
<%= render :partial => "topic_form", :locals => {
:index => index, :name => code.code_text , :color => code.color, :description => code.code_description
:index => index, :id => code.id, :name => code.code_text , :color => code.color, :description => code.code_description
} %><%
end
%>
Expand Down Expand Up @@ -146,16 +146,5 @@ $(function () {
$("#threadx_end_date").show();
$("#status").val("closed");
});

// lets you add new topics via Ajax request
$('#add_topic').click(function() {
var existingTopicCount = parseInt($("#topic_count").val());
$.get('/threads/new_topic/'+existingTopicCount, function(data) {
$('#topic-list').append(data);
$("#topic_color_"+$("#topic_count").val()).minicolors({ defaultValue: '#FF0000' });
$("#topic_count").val(existingTopicCount+1);
});
});

})
</script>
2 changes: 1 addition & 1 deletion app/views/threads/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@
if params["topic_count"].to_i > 0
1.upto(params["topic_count"].to_i) do |c|
%><%= render :partial => "topic_form", :locals => {
:index => c, :name => params["topic_name_#{c}"] , :color => params["topic_color_#{c}"],
:index => c, :id => 1, :name => params["topic_name_#{c}"] , :color => params["topic_color_#{c}"],
:description => params["topic_description_#{c}"]
} %><%
end
Expand Down

0 comments on commit 89100a5

Please sign in to comment.