Project

General

Profile

Actions

Defect #4533

closed

ARCondition not handling dup/clone

Added by Felix Schäfer over 14 years ago. Updated about 11 years ago.

Status:
Closed
Priority:
High
Assignee:
-
Category:
-
Target version:
-
Start date:
2010-01-07
Due date:
% Done:

0%

Estimated time:
Resolution:
Wont fix
Affected version:

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
Actions #1

Updated by Jean-Baptiste Barth almost 14 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 ?

Actions #2

Updated by Filou Centrinov about 11 years ago

"ARCondition" class ist not anymore part of Redmine. Close this issue?

Actions #3

Updated by Etienne Massip about 11 years ago

  • Status changed from New to Closed
  • Resolution set to Wont fix

Indeed, the class has been removed with r8089.

Actions

Also available in: Atom PDF