Forums » Development »
IssuePatch, belongs_to works once
Added by Sandro Munda over 14 years ago
Hello,
I have a issue_patch.rb file with the following code :
require_dependency 'issue'
module IssuePatch
  def self.included(base) # :nodoc:
    base.extend(ClassMethods)
    base.send(:include, InstanceMethods)
    base.class_eval do
      unloadable # Send unloadable so it will not be unloaded in development
      belongs_to :analytic_code, :class_name => 'AnalyticCode', :foreign_key => 'analytic_code_id'
    end
  end
  module ClassMethods
  end
  module InstanceMethods
  end
end
Issue.send(:include, IssuePatch)
	When I go the issue page, I can access to the 'issue.analytic_code'. The problem is that I can access it only once. Next time, I have the following error :
undefined method `analytic_code' for #<Issue:0x1076d16d8> Extracted source (around line #45): 42: <% end %> 43: </tr> 44: <%= render_custom_fields_rows(@issue) %> 45: <%= call_hook(:view_issues_show_details_bottom, :issue => @issue) %> 46: </table> 47: 48: <% if @issue.description? || @issue.attachments.any? -%>
On my Hook, I have the following code :
def view_issues_show_details_bottom(context={})
    issue = context[:issue]
    ac = issue.analytic_code[:code]
    return "<tr><td><b>Analytic Code:</b></td><td>#{ac}</td></tr>" 
  end
Replies (7)
    
    RE: IssuePatch, belongs_to works once
    -
    Added by Sandro Munda over 14 years ago
  
  It works when I run my server with the production environment. In development environment, it only works once :-( How is it possible ?
    
    RE: IssuePatch, belongs_to works once
    -
    Added by Kirill Bezrukov (RedmineUP) over 14 years ago
  
  You should do like this:
Dispatcher.to_prepare do  
  unless Attachment.included_modules.include?(RedmineContacts::Patches::AttachmentPatch)
    Attachment.send(:include, RedmineContacts::Patches::AttachmentPatch)
  end
  unless AttachmentsController.included_modules.include?(RedmineContacts::Patches::AttachmentsControllerPatch)
    AttachmentsController.send(:include, RedmineContacts::Patches::AttachmentsControllerPatch)
  end
end     
    
    RE: IssuePatch, belongs_to works once
    -
    Added by Sandro Munda over 14 years ago
  
  Hello,
Thanks for your answer. I'm not sure to understand. What's "Attachment" and RedmineContacts ?
Thanks.
    
    RE: IssuePatch, belongs_to works once
    -
    Added by Kirill Bezrukov (RedmineUP) over 14 years ago
  
  It's just an example how to include patches
You need this:
Dispatcher.to_prepare do  
    unless  Issue.included_modules.include?(IssuePatch)
       Issue.send(:include, IssuePatch)
    end
end
	instead
Issue.send(:include, IssuePatch)
    
    RE: IssuePatch, belongs_to works once
    -
    Added by Sandro Munda over 14 years ago
  
  Allright, it works ! Thanks very much.
However, can you explain me the reason that my code worked only in production mode and not in development mode ?
What is the Dispatcher.to_prepare ?
Thanks.
    
    RE: IssuePatch, belongs_to works once
    -
    Added by Kirill Bezrukov (RedmineUP) over 14 years ago
  
  I don't know :)) I've founded it in other plugin and copied
    
    RE: IssuePatch, belongs_to works once
    -
    Added by Sandro Munda over 14 years ago
  
  Ok thanks for your answer.
If anybody knows exactly the explanation, say me :-)