Project

General

Profile

Custom Plugin -> IssueHelper Extension -> Methods not found

Added by Christoph Kitzler over 4 years ago

Hi!
I have writte a plugin to hide custom fields in Issues with a collapse checkbox. Since upgrade to Redmine 4.1.1 i get a method not found error :(

plugins init.rb:

require 'redmine'
require 'redmine_mp2'
Redmine::Plugin.register :redmine_mp2 do
  name 'Redmine MP2 Plugin'
  author 'MP2'
  description 'Changes for MP2 Dev Team'
  version '1.0.0'

  # required redmine version
  requires_redmine version_or_higher: '3.2.0'

  # add setting
  default_settings = {
    darkmode: 0
  }

  settings(default: default_settings, partial: 'settings/mp2settings')
end

class RedmineMP2Helper

    class << self

        def plugin
          Redmine::Plugin.find(:redmine_mp2)
        end

        # Get plugin setting value or it's default value in a safe way.
        # If the setting key is not found, returns nil.
        # If the plugin has not been registered yet, returns nil.
        def get_setting(name)
          begin
            if plugin
              if Setting["plugin_#{plugin.id}"]
                Setting["plugin_#{plugin.id}"][name]
              else
                if plugin.settings[:default].has_key?(name)
                  plugin.settings[:default][name]
                end
              end
            end
          rescue

            # We don't care about exceptions which can actually occur ie. when running
            # migrations and settings table has not yet been created.
            nil
          end
        end
    end
end

file "plugins/redmine_mp2/lib/redmine_mp2/redmine_mp2.rb"

if ActiveRecord::Base.connection.table_exists?(:settings)
    Rails.configuration.to_prepare do
        # Patches
        require_dependency 'redmine_mp2/patches/issue_patch'
        require_dependency 'redmine_mp2/patches/projects_controller_patch'
                require 'redmine_mp2/patches/issue_helper_patch'
                require 'redmine_mp2/patches/mail_handler_patch'
    end
end

file "plugins/redmine_mp2/lib/redmine_mp2/patches/issue_helper_patch.rb"

module RedmineMP2
    module Patches
        module IssuesHelperPatch
            def self.included(base)
                base.send(:include, InstanceMethods)
            end

            # Instance methods
            module InstanceMethods
                class IssueFieldsRowsMP2
                    include ActionView::Helpers::TagHelper

                    def initialize
                      @left = []
                      @right = []
                    end

                    def left(*args)
                      args.any? ? @left << cells(*args) : @left
                    end

                    def right(*args)
                      args.any? ? @right << cells(*args) : @right
                    end

                    def size
                      @left.size > @right.size ? @left.size : @right.size
                    end

                    def to_html
                      content =
                        content_tag('div', @left.reduce(&:+), :class => 'splitcontentleft') +
                        content_tag('div',
                            content_tag('a', 'Zusätzliche Informationen', :id => 'hideAttributes', :href => '#hideAttributes', :class => 'hideExpanded') +
                            content_tag('a', 'Zusätzliche Informationen', :id => 'showAttributes', :href => '#showAttributes', :class => 'showCollapsed') +
                            content_tag('div', @right.reduce(&:+), :class => 'detailsCollapsed'),
                        :class => 'splitcontentright')

                      content_tag('div', content, :class => 'splitcontent')
                    end

                    def cells(label, text, options={})
                      options[:class] = [options[:class] || "", 'attribute'].join(' ')
                      content_tag 'div',
                        content_tag('div', label + ":", :class => 'label') + content_tag('div', text, :class => 'value'),
                        options
                    end
                end

                def issue_fields_rows_mp2
                r = IssueFieldsRowsMP2.new
                yield r
                r.to_html
                end

                def render_half_width_custom_fields_rows_mp2(issue, rows)
                    values = issue.visible_custom_field_values.reject {|value| value.custom_field.full_width_layout?}
                    return if values.empty?
                    values.each_with_index do |value, i|
                        css = "cf_#{value.custom_field.id} collapsed" 
                        rows.right custom_field_name_tag(value.custom_field), show_value(value), :class => css
                    end
                end
            end        
        end
    end
end

unless IssuesHelper.included_modules.include? RedmineMP2::Patches::IssuesHelperPatch
  IssuesHelper.send(:prepend, RedmineMP2::Patches::IssuesHelperPatch)
end

I cloned the issues view (show.html.erb) and changed the method call from


...
<div class="attributes">
<%= issue_fields_rows do |rows|
  rows.left l(:field_status), @issue.status.name, :class => 'status'
  rows.left l(:field_priority), @issue.priority.name, :class => 'priority'
...

to


...
<div class="attributes">
<%= issue_fields_rows_mp2 do |rows|
  rows.left l(:field_status), @issue.status.name, :class => 'status'
  rows.left l(:field_priority), @issue.priority.name, :class => 'priority'
...

ActionView::Template::Error (undefined method `issue_fields_rows_mp2' for #<#<Class:0x0000000011074390>:0x000000000fd03c98>
Did you mean? issue_fields_rows):

and got a method not found error in redmine log. Can anybody tell me what i am doing wrong here?