How to update a different table in an issues hook

Added by Jason Messmer over 8 years ago

I am trying to write a plugin that when a user updates an issue they have an additional text field called "Foo" that they will enter in some integer. I have been using Eric's budget,rate, and timesheet plugins as examples for writing this one (thanks Eric). I have the view_issues_show_details_bottom hook working properly but I can't figure out how to get the view_issues_form_details_bottom hook to work. I have a foos table which has a field called "foo_number" and issues belongs_to foo. How do I in the view_issues_show_details_bottom hook display the correct "foo_number" and if the user updates the number it will updated the foo_number field in the foos tables. I am fairly new to ruby & rails and been looking @ the redmine code for that past couple of weeks...What I have is the following which is wrong because it will update the issues table foo_id with the actual foo number and not update foos table properly:

input = context.[form].text_field :foo_id, :size => 10, :label => "#{l(:foo_foo_label)}" 

I can't use a custom field because it is very limited of what you can do. This plugin allows the user to link issues to another issue tracker software (Jira, etc...)

Replies (4)

RE: How to update a different table in an issues hook - Added by Jason Messmer over 8 years ago

Please note that I could have easily added a field to the issues table called 'foo_number' and be done with my issue. In this plugin, an issue can be associated with multiple issues from the other issue tracker...

RE: How to update a different table in an issues hook - Added by Eric Davis over 8 years ago

Jason:

There's two steps you'll need:

Form field for the user input

This would use the view_issues_form_details_bottom. You are really close with your snippet above but by adding a text_field to the form, it's getting associated with the issue when posted (as params[:issue][:foo_id]). What you would probably want to do instead is to send the Foo data separately. Example:

# This will send data in params as:
#
#   params[:foo][:id]
#
def view_issues_form_details_bottom(context = {})
  return text_field :foo, :id, :size => 10, :label => "#{l(:foo_foo_label)}" 
end

Backend to save the value

Now that the data is sent, you need to associate it with the issue. The :controller_issues_edit_before_save is perfect for that. Just hook into there and setup your association by hand. Example:

def controller_issues_edit_before_save(context = {})
  # Create a new foo
  foo = Foo.new(context[:params][:foo]

  # Add the foo to the issue
  context[:issue].foos << foo
  return ''
end

You might need some additional code if you Foos don't save automatically with the issue. Another option is to use the :controller_issues_edit_after_save hook which is the same hook but after an issue has successfully saved.

If you take a look at the Budget plugin's issue hooks, you should see something similar to this for the "bulk_edit" actions. Hope this helps.

Eric

P.S. Let me know if you release this plugin, I might have a use for it.

RE: How to update a different table in an issues hook - Added by Jason Messmer over 8 years ago

Thanks for the input...that worked perfectly...

I do have an issue with the History and the emailer in that when I do change the foo value from 45024 to 46123
it identifies the change as id's and not by value, basically the History would report "Changed from 4 to 5" vs
"Changed from 45024 to 46123". Can I control this by one of the journal hooks?

RE: How to update a different table in an issues hook - Added by Eric Davis over 8 years ago

I did something like this for my Budget plugin, so a Deliverable's name is used instead of it's id. Check out the :helper_issues_show_detail_after_setting hook and my Budget plugin's use of that hook in lib/budget_issue_hook.rb.

Eric

(1-4/4)