Hi, this is my first post, so nice to meet you all and thanks in
advance for your help.
I have a “task” model that has it’s own CRUD interface, and can get
“promoted” to another model, that is, it gets associated with a new
record of another model. In the new form for the new model I want to
prepopulate the fields that both models share so the user doesn’t have
to copy them. I did this with the following code:
class Task < ActiveRecord::Base
belongs_to :entity, :polymorphic => true
end
…and…
class Issue < ActiveRecord::Base
has_one :task, :as => "entity"
alias_method :set_task=, :task=
def task=(t)
task = Task.find(t) if t.class != Task
self.set_task = t
copy_task_attributes
end
def copy_task_attributes
shared_attributes = self.task.attributes.reject do |attribute,
value|
!self.attributes.has_key? attribute
end
self.attributes = shared_attributes
end
end
This works, if I create a new Issue:
@issue = Issue.new :task => Task.find(params[:task])
It inherits the common attributes.
The problem arises when I want to put all that logic into a plugin:
module ActiveRecord
module Acts
module Taskable # need to change the name
def self.included(base)
base.extend(ClassMethods)
end
module ClassMethods
def acts_as_taskable
has_one :task, :as => "entity"
alias_method :set_task=, :task=
include InstanceMethods
end
end
module InstanceMethods
def task=(t)
task = Task.find(t) if t.class != Task
self.set_task = t
copy_task_attributes
end
def copy_task_attributes
shared_attributes = self.task.attributes.reject do |
attribute, value|
!self.attributes.has_key? attribute
end
self.attributes = shared_attributes
end
end
end
end
end
Then if I do:
class Issue < ActiveRecord::Base
acts_as_taskable
end
It doesn’t pass the tests (the new record does not inherit the
attributes).
Does anyone knows why this happens? Or better yet, what’s the correct
way of doing this? (I can’t stop thinking that rails already has this
functionality and I can’t find it)
Thanks. Lucas.