Vote #65521
未完了Unable to set "value" and "old_value" through :helper_issues_show_detail_after_setting hook
0%
説明
In http://www.redmine.org/wiki/1/Hooks, the description for :helper_issues_show_detail_after_setting states that it "Passes data to the hook to allow it to set the label and value", but it seems this is only partially true.
Setting context variables in a hook using the "=" operand just doesn't work. For "context[:label]", which is already initialized (as a String) when the hook is called, this may be worked around calling "context[:label].replace". However, as "context[:value]" and "context[:old_value]" may be uninitialized when the hook is called, I don't see a way to change their value at all. And setting "context[:detail].value" and "context[:detail].old_value" is obviously not as harmless as setting "context[:value]" and "context[:old_value]".
Using Redmine 0.8.4, Rails 2.1.2 and Ruby 1.8.6.
journals
Can you attach a test case showing this? I've been able to set @context[:value]@ and @context[:old_value]@ in my Budget plugin:
http://github.com/edavis10/redmine-budget-plugin/blob/8413b26a15b74817b28509c3664da76f072ab36d/lib/budget_issue_hook.rb#L82
--------------------------------------------------------------------------------
Your plugin sets <code>context[:detail].value</code> and <code>context[:detail].old_value</code>, which are different from <code>context[:value]</code> and <code>context[:old_value]</code>.
Unfortunately, the code in question is not very "testable". Indeed, I have no idea how to test it.
--------------------------------------------------------------------------------
I just ran into this myself. Setting @context[:object]@ to a new object isn't sent back to the calling code (new object reference). Setting @context[:object].value@ works because @:object@ is still the same object.
I've worked around this in some private code but it wasn't an easy implementation. I think we might need to split how the View Hooks return data from the Model/Controller hooks (View should merge the response strings, Model/Controller should check for reassignments).
--------------------------------------------------------------------------------
I worked around it by adding another hook for plugin-defined detail properties (see @issues_helper.rb.patch@ for Redmine 0.9-stable).
Then the hook code would look like this:
<pre>
def helper_issues_show_detail_format(context)
detail = context[:detail]
if detail.property == "user_notified"
user = User.find_by_id(detail.prop_key) or return
l(:label_user_notified, context[:html] ? link_to_user(user) : user.name)
end
end
</pre>
--------------------------------------------------------------------------------
Couldn't this issue be fixed quite easily by assigning the value, old_value, and label defaults before calling the hook in app/helpers/issues_helper.rb?
h3. Current
<pre>
call_hook(:helper_issues_show_detail_after_setting, {:detail => detail, :label => label, :value => value, :old_value => old_value })
label ||= detail.prop_key
value ||= detail.value
old_value ||= detail.old_value
</pre>
h3. Fixed
<pre>
label ||= detail.prop_key
value ||= detail.value
old_value ||= detail.old_value
call_hook(:helper_issues_show_detail_after_setting, {:detail => detail, :label => label, :value => value, :old_value => old_value })
</pre>
Now the objects are initialized, and the if the hook modifies them it will get passed back.
--------------------------------------------------------------------------------
I am stepping down from working on Redmine. If someone else is interesting in working on this issue, feel free to reassign it to them.
Eric Davis
--------------------------------------------------------------------------------
Assigned issue with no assignee back to New status.
--------------------------------------------------------------------------------