Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing :if condition on instance creation #12

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions lib/acts_as_versioned.rb
Original file line number Diff line number Diff line change
Expand Up @@ -387,11 +387,10 @@ def empty_callback()
end

#:nodoc:

protected
# sets the new version before saving, unless you're using optimistic locking. In that case, let it take care of the version.
def set_new_version
@saving_version = new_record? || save_version?
@saving_version = save_version?
self.send("#{self.class.version_column}=", next_version) if new_record? || (!locking_enabled? && save_version?)
end

Expand Down
41 changes: 29 additions & 12 deletions test/versioned_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,23 @@ def test_version_if_condition
assert_equal 1, p.version
Page.feeling_good = true
end

def test_version_if_condition_false_on_first_save
Page.feeling_good = false
p = Page.create! :title => "title"
assert p.version.nil?
assert p.versions.empty?
Page.feeling_good = true
end

def test_version_if_condition_false_on_first_save2
Page.feeling_good = false
p = Page.create! :title => "title"

Page.feeling_good = true
p.update_attributes(:title => 'new title')
assert_equal 1, p.version
end

def test_version_if_condition2
# set new if condition
Expand All @@ -146,16 +163,16 @@ def new_feeling_good() title[0..0] == 'a'; end
end

p = Page.create! :title => "title"
assert_equal 1, p.version # version does not increment
assert_equal 1, p.versions.count
assert p.version.nil? # no version gets saved
assert p.versions.empty?

p.update_attributes(:title => 'new title')
assert_equal 1, p.version # version does not increment
assert_equal 1, p.versions.count
assert p.version.nil? # no version gets saved
assert p.versions.empty?

p.update_attributes(:title => 'a title')
assert_equal 2, p.version
assert_equal 2, p.versions.count
assert_equal 1, p.version
assert_equal 1, p.versions.count

# reset original if condition
Page.class_eval { alias_method :feeling_good?, :old_feeling_good }
Expand All @@ -167,16 +184,16 @@ def test_version_if_condition_with_block
Page.version_condition = Proc.new { |page| page.title[0..0] == 'b' }

p = Page.create! :title => "title"
assert_equal 1, p.version # version does not increment
assert_equal 1, p.versions.count
assert p.version.nil? # no version gets saved
assert p.versions.empty?

p.update_attributes(:title => 'a title')
assert_equal 1, p.version # version does not increment
assert_equal 1, p.versions.count
assert p.version.nil? # no version gets saved
assert p.versions.empty?

p.update_attributes(:title => 'b title')
assert_equal 2, p.version
assert_equal 2, p.versions.count
assert_equal 1, p.version
assert_equal 1, p.versions.count

# reset original if condition
Page.version_condition = old_condition
Expand Down