Defect #4533
closedARCondition not handling dup/clone
0%
Description
Long story short: ARCondition#clone
and #dup
create new objects, but all objects created this way somehow share the string in @conditions[0]
.
s = ARCondition.new "#{Issue.table_name}.assigned_to_id IS NOT NULL"
p = s.clone
s << ["#{Issue.table_name}.start_date <= ?", Date.today]
p
Results in:
$ script/console Loading development environment (Rails 2.3.5) >> s = ARCondition.new "#{Issue.table_name}.assigned_to_id IS NOT NULL" => #<ARCondition:0x102bbb040 @conditions=["1=1 AND (issues.assigned_to_id IS NOT NULL)"]> >> p = s.clone => #<ARCondition:0x102baac68 @conditions=["1=1 AND (issues.assigned_to_id IS NOT NULL)"]> >> s << ["#{Issue.table_name}.start_date <= ?", Date.today] => #<ARCondition:0x102bbb040 @conditions=["1=1 AND (issues.assigned_to_id IS NOT NULL) AND (issues.start_date <= ?)", Thu, 07 Jan 2010]> >> p => #<ARCondition:0x102baac68 @conditions=["1=1 AND (issues.assigned_to_id IS NOT NULL) AND (issues.start_date <= ?)"]>
Notice p
got the condition added to s
but not the parameter.
I was able to get around this by being a little more brutal, but I don't think that's how it should work:
p = ARCondition.new s.conditions
Updated by Jean-Baptiste Barth almost 15 years ago
It seems that by default ruby just gives you a shallow copy of your objects when calling #dup
or #clone
. Underlying objects are just a reference to original ones. If you want deep copies, many people advice to marshal your object and then restore it, which is not cleaner than your solution in my opinion.
A solution would be to rely on ruby hooks called when #dup/clone
are called:
def initialize_copy(source) super @conditions = source.conditions.clone @conditions[0] = source.conditions[0].clone end
With this added in ARCondition class, ARCondition objects are really independant...
But maybe it's simpler to keep the first solution you exposed, no ?
Updated by Filou Centrinov about 12 years ago
"ARCondition" class ist not anymore part of Redmine. Close this issue?
Updated by Etienne Massip about 12 years ago
- Status changed from New to Closed
- Resolution set to Wont fix
Indeed, the class has been removed with r8089.