Search code examples
ruby-on-rails-4quickbooks-onlineredmine-plugins

Redmine Adding Custom Selection Box


I have started working on a plugin for redmine that will allow me to assign a Quickbooks Online (QBO) contact to an issue.

I have created a table for the contacts that only stores a name for each qbo contact.

I have also added a migration to add a reference for qbo_contact to Issues

class UpdateIssues < ActiveRecord::Migration
  def change
    add_reference :issues, :qbo_customer, index: true
  end
end

The issue I am having is that when the issue is being edited, the user can select a QBO contact. When the user saves the issue, Issues.qbo_contact_id is not updated.

I feel it might have something to do with the form selection box

Please Advise

class QboHookListener < Redmine::Hook::ViewListener

  # Edit Issue Form
  # Show a dropdown for quickbooks contacts
  def view_issues_form_details_bottom(context={})
    selected = ""

    # Check to see if there is a quickbooks user attached to the issue
    if not context[:issue].qbo_customer_id.nil? then
      selected = QboCustomers.find_by_id(context[:issue].qbo_customer_id).name
    end

    # Generate the drop down list of quickbooks contacts
    select = context[:form].select :qbo_customer_id, QboCustomers.all.pluck(:name, :id), include_blank: true, selected: selected
    return "<p>#{select}</p>"

    #TODO save selection to Issues.qbp_customer_id
  end
end

If you need more, I have shared my work on github


Solution

  • The problem was that there is a white/black list of attributes allowed for an Issue.

    Turns out this was an issue documented here

    Fixed in r4491. You can now extend safe attributes for a given model using:

    Issue.safe_attributes 'foo', 'bar'

    or makes safe attributes conditional:

    Issue.safe_attributes 'foo', 'bar', :if => lambda {|issue, user| issue.author == user}

    You can have a look at redmine/safe_attributes.rb.

    I simply added the following to init.rb to allow my plugin to add qbo_customer_id to the safe attributes list.

    # Add qbo_customer to the safe Issue Attributes list
    Issue.safe_attributes 'qbo_customer_id'