1

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

Rick Barrette
  • 272
  • 4
  • 19
  • 1
    The reason might be in white/black list of attributes. Redmine has own logic (`safe_attributes`) to define which attributes are allowed to be changed (like `attr_accessible` in Rails core). Look at [this code](https://github.com/redmine/redmine/blob/3.2-stable/app/models/issue.rb#L401-L437) (Redmine 3-2 stable), Redmine core defines allowed attributes here. I suppose you should add your attribute (`qbo_customer_id`) to this list. Of course you should do it in your pluging not in origin model `issue`. – gotva Jan 01 '16 at 20:18
  • That was exactly the issue. Thank you! Now I just have to figure out how to add a safe_attribute to a model via my plugin... – Rick Barrette Jan 02 '16 at 03:21

1 Answers1

2

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'
Rick Barrette
  • 272
  • 4
  • 19