Reputation: 3762
Good evening everyone! I am stuck with something about a plugin of mine, and I would really appreciate some advice.
I wrote a simple plugin, that modifies the form for creating a new project by adding new fieldset with some checkboxes. I wrote all of the login in my files in my plugin directory. So far, so good. Everything works fine, except that when it came to sending the files to my colleagues, there was something they didn't like. It appears that they are not too fond of the idea of modifying the core files a bit in order to get things working, because things may go messed up when they update the version of Redmine. I've managed to reduce the code injected in the core files to 3 lines only (3 lines, ikr!!), but that's as far as I could go.
projects_controller.rb
selected_tasks = params[:checkListItems].collect {|id| id.to_i}
call_hook(:add_tasks_after_project_save, { :selected_ids => selected_tasks, :project => @project})
And in project's form /views/projects/_form.html.erb
<%= call_hook(:append_check_list_tasks, { :project => @project }) %>
I wont be surprised if there is a way to remove the code I use in projects_controller.rb and use only the one in my files, but I'm not so sure about the hook I placed inside the form.html.
IS there a way to get my plugin working the way it is like now, but without having to modify the core files of Redmine?
Upvotes: 0
Views: 801
Reputation: 5998
1 How to patch form.
You can use a hook from the core. Here is hook list
Or if you need to patch a view where no hooks you can use gem deface
. Please see this commit
2 How to patch controller.
It is a problem, because you can't use before_filter
or alias_method_chain
in this case - Project is initialized inside method.
I suppose that you would like to do some action after (or before) project is created and you need to know selected_tasks
. My idea is to follow standart workflow and pass these values to model through new virtual attr_writer
.
What I think you should do:
attr_writer
to model projectsafe_attribute
smth like this (I think Redmine won't allow assign unsafed attributes, see this)before_create
or after_create
callback to process accepted params through attr_writer
.PS of course it is just a plan and I can't check if it works :)
Plan B
There is a new (simpler) idea how to patch. Code for patching can be placed to after_filter
where you can check if @project
has been created successfully and process selected_tasks
.
You can patch core from pluging adding after_filter
this way. Do not forget to include your patch to controller see this
Upvotes: 1