montera34 / pageonex

PageOneX. Analyzing front pages
http://pageonex.com
GNU Affero General Public License v3.0
54 stars 13 forks source link

Problems removing topics: areas change from topic without notice #233

Open numeroteca opened 4 years ago

numeroteca commented 4 years ago

I had forked a thread (http://pageonex.com/numeroteca/partidos-y-elecciones-20d/ to http://pageonex.com/numeroteca/partidos-y-elecciones-20d/) to rework one of the topics. I needed to erase all but one of the topics and I discover these bugs (I'll update this when I find more problems or can give specifics):

Given a thread with 4 topics (A, B, C, D) when the first topic (A) while editing-updating a thread:

I made a test thread to see this behavior. To make it easier the thread I have just one day coded, where the full front page is assigned to a topic that has the name of that very same newspaper.

Original thread: Screenshot from 2019-09-16 13-51-30

Thread after removing first topic: Screenshot from 2019-09-16 13-52-55

I repeated the process with 5 topics with the same results:

What is causing this problem?

From the topic template (/app/views/threads/_topic_form.html.erb) the delete topic button is formed with the class .delete_topic:

<div class="controls">
    <%= hidden_field_tag "topic_deleted_#{index}" %>
    <a id="delete_topic_<%= index %>" class="btn delete_topic">Delete Code/Topic</a>
</div>

It is used with this javascript /app/assets/javascripts/topic_form.js script to detect where the click happens and change the id of the topic with a particular index:

 var initDelete = function () {
    $('.delete_topic').unbind('click');
    $('.delete_topic').click(function () {
      var index = $(this).attr('id').substr('delete_topic_'.length);
      $("#topic_deleted_" + index).val('1');
      $("#topic_" + index).hide();
    });
  }

When you click in a delete topic button value="1" is added: <input id="topic_deleted_0" name="topic_deleted_0" type="hidden" value="1">

so the ruby script /app/controllers/threads_controller.rb is able to detect this and destroy the Topic (called code in the project):

# 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

It seems there is a problem with the selection of these topics ids.

numeroteca commented 4 years ago

Either removing this part of the code: https://github.com/montera34/pageonex/blob/master/app/controllers/threads_controller.rb#L200-L212

                # 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
                        unless code_name.empty?
                            code.update_attributes({code_text: code_name,
                                color: params["topic_color_#{index}"],
                                code_description: params["topic_description_#{index}"]})
                        end
                    end

or this https://github.com/montera34/pageonex/blob/master/app/controllers/threads_controller.rb#L215-L226

            @thread.codes.to_enum.with_index.each do |code,index|
                code_name = params["topic_name_#{index}"]
                if params["topic_deleted_#{index}"] == '1'
                    code.destroy()
                else #To Do: it should save the new codes created
                    unless code_name.empty?
                        code.update_attributes({code_text: code_name,
                            color: params["topic_color_#{index}"],
                            code_description: params["topic_description_#{index}"]})
                    end
                end
            end

fixes it. It seems there are two codes making a similar/same thing and causes the removal of two codes (not clear to me exactly why). Check #61, maybe relevant related info.