Skip to content

Commit

Permalink
Switch cloneable specs to objects and nullify all instance vars on cl…
Browse files Browse the repository at this point in the history
…one.
  • Loading branch information
jnunemaker committed Feb 13, 2012
1 parent 8477051 commit 99e74ec
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
10 changes: 5 additions & 5 deletions lib/toy/cloneable.rb
Expand Up @@ -3,13 +3,13 @@ module Cloneable
extend ActiveSupport::Concern

def initialize_copy(other)
instance_variables.each do |name|
instance_variable_set(name, nil)
end

@_new_record = true
@_destroyed = false
@attributes = {}

self.class.lists.each do |name, list|
instance_variable_set(list.instance_variable, nil)
end
@attributes = {}

other.attributes.except('id').each do |key, value|
value = value.duplicable? ? value.clone : value
Expand Down
13 changes: 8 additions & 5 deletions spec/toy/cloneable_spec.rb
@@ -1,21 +1,17 @@
require 'helper'

describe Toy::Cloneable do
uses_constants('User', 'Game')
uses_constants('User')

before do
User.attribute(:name, String)
User.attribute(:skills, Array)
User.list(:games)

@game = Game.create
@user = User.create({
:name => 'John',
:skills => ['looking awesome', 'growing beards'],
:games => [@game],
})
end
let(:game) { @game }
let(:user) { @user }

describe "#clone" do
Expand Down Expand Up @@ -53,5 +49,12 @@
clone.id.should_not == user.id
end
end

it "nullifies defined instance variables" do
user.instance_variable_set("@foo", true)
user.clone.tap do |clone|
clone.instance_variable_get("@foo").should be_nil
end
end
end
end

0 comments on commit 99e74ec

Please sign in to comment.