diff --git a/init.rb b/init.rb index a4db0ba..2f8a8a9 100644 --- a/init.rb +++ b/init.rb @@ -6,6 +6,7 @@ require_dependency 'query_patch' # Hooks require_dependency 'budget_issue_hook' +require_dependency 'budget_project_hook' RAILS_DEFAULT_LOGGER.info 'Starting Budget plugin for RedMine' diff --git a/lib/budget_project_hook.rb b/lib/budget_project_hook.rb new file mode 100644 index 0000000..c714cb0 --- /dev/null +++ b/lib/budget_project_hook.rb @@ -0,0 +1,26 @@ +class BudgetProjectHook < Redmine::Hook::ViewListener + def model_project_copy_before_save(context = {}) + source = context[:source_project] + destination = context[:destination_project] + + if source.module_enabled?(:budget_module) + Deliverable.find(:all, :conditions => {:project_id => source.id}).each do |source_deliverable| + destination_deliverable = source_deliverable.class.new # STI classes + + # Copy attribute except for the ones that have wrapped + # accessors, use read/write attribute for them + destination_deliverable.attributes = source_deliverable.attributes.except("project_id", "profit", "materials", "overhead") + + destination_deliverable.write_attribute(:profit, source_deliverable.read_attribute(:profit)) + destination_deliverable.write_attribute(:profit_percent, source_deliverable.read_attribute(:profit_percent)) + destination_deliverable.write_attribute(:materials, source_deliverable.read_attribute(:materials)) + destination_deliverable.write_attribute(:materials_percent, source_deliverable.read_attribute(:materials_percent)) + destination_deliverable.write_attribute(:overhead, source_deliverable.read_attribute(:overhead)) + destination_deliverable.write_attribute(:overhead_percent, source_deliverable.read_attribute(:overhead_percent)) + + destination_deliverable.project = destination + destination_deliverable.save # Need to save here because there is no relation on project to deliverable + end + end + end +end