Project

General

Profile

Plugin converison from 4.0.3 to 4.2.1

Added by Federico Vergelli about 3 years ago

Hello!

I've been trying to make a plugin work on a Redmine 4.2.1.stable. I already have this plugin running in a 4.0.3.stable version, and works well.

( the plugin is project_accordion: https://github.com/reubenmallaby/redmine_projects_accordion )

The thing is, that I need this plugin on my 4.2 Redmine and I can't go back to an older version sadly. I thought that maybe I can convert this plugin into something that can work with version 4.2 of Redmine. Here is the thing, I've never done anything on Ruby and I just started a Rails course, so my knowledge of it is very limited. But I figured some things out.

Maybe you guys could guide me and tell me if it is viable or not, if it's gonna take me a few days to achieve this, or maybe it's just a few lines of code and that's it, Idk.

Plattaform:
  • Redmine 4.2.1 on Docker container
  • Redmine 4.0.3 on CentOS8 Installed locally

Behavior on 4.2.1: With the plugin installed, an Internal Error (500) is triggered whenever I click on Projects.

The log:

.
.
redmine_1      | I, [2021-09-01T21:04:07.346250 #1]  INFO -- :   Current user: admin (id=1)
redmine_1      | I, [2021-09-01T21:04:07.350804 #1]  INFO -- :   Rendering welcome/index.html.erb within layouts/base
redmine_1      | I, [2021-09-01T21:04:07.354260 #1]  INFO -- :   Rendered welcome/index.html.erb within layouts/base (3.3ms)
redmine_1      | I, [2021-09-01T21:04:07.408304 #1]  INFO -- : Completed 200 OK in 83ms (Views: 54.7ms | ActiveRecord: 18.3ms)
redmine_1      | I, [2021-09-01T21:04:14.210157 #1]  INFO -- : Started GET "/projects" for <ip> at 2021-09-01 21:04:14 +0000
redmine_1      | I, [2021-09-01T21:04:14.212257 #1]  INFO -- : Processing by ProjectsController#index as HTML
redmine_1      | I, [2021-09-01T21:04:14.221146 #1]  INFO -- :   Current user: admin (id=1)
redmine_1      | I, [2021-09-01T21:04:14.301931 #1]  INFO -- :   Rendering plugins/redmine_projects_accordion/app/views/projects/index.html.erb within layouts/base
redmine_1      | I, [2021-09-01T21:04:14.309838 #1]  INFO -- :   Rendered plugins/redmine_projects_accordion/app/views/projects/index.html.erb within layouts/base (7.8ms)
redmine_1      | I, [2021-09-01T21:04:14.310107 #1]  INFO -- : Completed 500 Internal Server Error in 98ms (ActiveRecord: 16.0ms)
redmine_1      | F, [2021-09-01T21:04:14.311105 #1] FATAL -- :   
redmine_1      | F, [2021-09-01T21:04:14.311150 #1] FATAL -- : ActionView::Template::Error (undefined method `delete_if' for nil:NilClass):
redmine_1      | F, [2021-09-01T21:04:14.311317 #1] FATAL -- :     18:   </form>
redmine_1      |     19: </h2>
redmine_1      |     20: 
redmine_1      |     21: <% @projects.delete_if { |p| !p.active? } -%>
redmine_1      |     22: 
redmine_1      |     23: <% ids = @projects.map { |p| p.id } %> 
redmine_1      |     24: <%- #unless p.children.empty? } %>
redmine_1      | F, [2021-09-01T21:04:14.311363 #1] FATAL -- :   
redmine_1      | F, [2021-09-01T21:04:14.311394 #1] FATAL -- : plugins/redmine_projects_accordion/app/views/projects/index.html.erb:21:in `_plugins_redmine_projects_accordion_app_views_projects_index_html_erb__4397864275038323496_111340'
redmine_1      | lib/redmine/sudo_mode.rb:61:in `sudo_mode'
redmine_1      | plugins/redmine_dmsf/lib/redmine_dmsf/webdav/custom_middleware.rb:56:in `call'
redmine_1      | I, [2021-09-01T21:04:21.026728 #1]  INFO -- : Started GET "/projects" for <ip> at 2021-09-01 21:04:21 +0000
redmine_1      | I, [2021-09-01T21:04:21.028229 #1]  INFO -- : Processing by ProjectsController#index as HTML
redmine_1      | I, [2021-09-01T21:04:21.035672 #1]  INFO -- :   Current user: admin (id=1)
.
.

As you can see here, the problem arises from line

21: <% @projects.delete_if { |p| !p.active? } -%>

with an error:

ActionView::Template::Error (undefined method `delete_if' for nil:NilClass):

I came up with the idea that maybe the variable "@projects" is returning a nill state instead of a list because is not defined or something, so I went to the projects_controller inside /app/controllers folder of my 4.0.3.stable Redmine just to compare it with the 4.2 one, and found this:

4.2.1

  # Lists visible projects
  def index
    # try to redirect to the requested menu item
    if params[:jump] && redirect_to_menu_item(params[:jump])
      return
    end

    retrieve_project_query
    scope = project_scope

    respond_to do |format|
      format.html do
        # TODO: see what to do with the board view and pagination
        if @query.display_type == 'board'
          @entries = scope.to_a
        else
          @entry_count = scope.count
          @entry_pages = Paginator.new @entry_count, per_page_option, params['page']
          @entries = scope.offset(@entry_pages.offset).limit(@entry_pages.per_page).to_a
        end
      end
      format.api do
        @offset, @limit = api_offset_and_limit
        @project_count = scope.count
        @projects = scope.offset(@offset).limit(@limit).to_a
      end
      format.atom do
        projects = scope.reorder(:created_on => :desc).limit(Setting.feeds_limit.to_i).to_a
        render_feed(projects, :title => "#{Setting.app_title}: #{l(:label_project_latest)}")
      end

4.0.3

  # Lists visible projects
  def index
    # try to redirect to the requested menu item
    if params[:jump] && redirect_to_menu_item(params[:jump])
      return
    end

    scope = Project.visible.sorted

    respond_to do |format|
      format.html {
        unless params[:closed]
          scope = scope.active
        end
        @projects = scope.to_a
      }
      format.api  {
        @offset, @limit = api_offset_and_limit
        @project_count = scope.count
        @projects = scope.offset(@offset).limit(@limit).to_a
      }
      format.atom {
        projects = scope.reorder(:created_on => :desc).limit(Setting.feeds_limit.to_i).to_a
        render_feed(projects, :title => "#{Setting.app_title}: #{l(:label_project_latest)}")
      }
    end
  end

Seems like Projects Projects is an array defined based on the scope variable, but its occurrence is only on the 4.0.3 version. On 4.2.1 on the other hand, I only can see the @entries variable on its place.

What are your thoughts? I know I may be asking for too much, but any guidance would be very appreciated.

Regards.