Search code examples
ruby-on-railsredmineredmine-plugins

Adding attributes to Project module through plugin patch


I'm trying to create a patch in redmine to add a project specific email header and email footer for outgoing emails. I looked at a few examples and managed to get something up and running, but whenever I click 'Save' it flashes a 'success' notice and never updates the database. No errors are reported in the logfile and everything looks normal (as far as I can tell) in firebug.

The getters work just fine and changes are reflected when I modify the database records manually, so I know my includes/requires are all working.

Can anybody suggest a likely culprit for why it's not saving my new attributes? Did I miss something?

lib/project_email_patch/project_patch.rb

require_dependency 'project'

module ProjectEmailPatch
 module ProjectPatch

  def self.included(base)
    base.send(:include, InstanceMethods)
    base.class_eval do
      unloadable

      attr_accessible :email_header, :email_footer
    end
  end

  module InstanceMethods
    def email_header
      read_attribute(:email_header) ? read_attribute(:email_header) : Setting.emails_header
    end

    #def email_header=(header)
    #  write_attribute(:email_header, header)
    #end

    def email_footer
      read_attribute(:email_footer) ? read_attribute(:email_footer) : Setting.emails_footer
    end

    #def email_footer=(footer)
    #  write_attribute(:email_footer, footer)
    #end

  end
 end
end

Rails.configuration.to_prepare do
  #unless Project.included_modules.include?(ProjectEmailPatch::ProjectPatch)
    Project.send(:include, ProjectEmailPatch::ProjectPatch)
  #end
end

app/views/project_emails/_edit.html.erb

<table border="1">
  <tr><th>Test</th><td>Hello World</td></tr>
  <tr><th>Header</th><td><%= @project.email_header %></td></tr>
  <tr><th>Footer</th><td><%= @project.email_footer %></td></tr>
</table>
<%= form_for(@project, :url => {:action => 'update', :id => @project}) do |f| %>
<div>
  <table>
    <tr>
      <th><%= f.label :email_header %></th>
      <td><%= f.text_area :email_header, :size => "50x10" %></td>
    </tr>
    <tr>
      <th><%= f.label :email_footer %></th>
      <td><%= f.text_area :email_footer, :size => "50x10" %></td>
    </tr>

  </table>
</div>
<%= f.submit "Save", :confirm => "Are you sure you are ready to submit?" %>
<% end %>

Any help would be greatly appreciated! Thanks in advance!


Solution

  • Look at the model project You will not see attr_accessible method in it.

    Redmine uses own solution safe_attributes (example). So I think you should write instead of attr_accessible ...

    safe_attributes 'email_header', 'email_footer'
    

    (pay attention that Redmine passes strings in own code)

    If it doesn't solve the problem check logs carefully. I think you will find that some attributes are forbidden or something like this.