Skip to content

Commit

Permalink
Added update_attributes_without_clone method. 3000 tests exceeded..
Browse files Browse the repository at this point in the history
  • Loading branch information
gaspard committed Aug 21, 2013
1 parent 25c0c03 commit 021b585
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 12 deletions.
4 changes: 4 additions & 0 deletions History.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
== 1.2.8

* Major changes
* Added "update_attributes_without_clone" to update attributes without versioning (used to sync
attributes with external app). This does not change timestamps nor version or node author.

* Minor changes
* Fixed gemspec to not include TextMate helper and selenium plugin.

Expand Down
55 changes: 43 additions & 12 deletions lib/zena/use/workflow.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,18 @@ def should_clone?

# Return true if the version should be cloned if it was changed.
def clone_on_change?
# not same user
user_id != visitor.id ||
# changed lang
lang_changed? ||
# new version on top of publication
status_changed? ||
# not in redit time
Time.now > created_at + current_site[:redit_time].to_i
if node && node.no_clone_on_change?
false
else
# not same user
user_id != visitor.id ||
# changed lang
lang_changed? ||
# new version on top of publication
status_changed? ||
# not in redit time
Time.now > created_at + current_site[:redit_time].to_i
end
end

# Returns true if the version has been edited (not just a status change)
Expand Down Expand Up @@ -487,11 +491,35 @@ def get_publish_from(ignore_id = nil)
end

# Update an node's attributes or the node's version/content attributes. If the attributes contains only
# :v_... or :c_... keys, then only the version will be saved. If the attributes does not contain any :v_... or :c_...
# attributes, only the node is saved, without creating a new version.
# properties, then only the version will be saved. If the attributes does not contain any properties
# only the node is saved, without creating a new version.
def update_attributes(new_attributes)
apply(:update_attributes, new_attributes)
end

# Used when we want to update properties *without* changing author and/or creating new versions. This
# is needed when we want to synchronise some properties with an external application.
def update_attributes_without_clone(new_attributes)
@no_clone_on_change = true
Node.record_timestamps = false
Version.record_timestamps = false
# We set v_status
if v_status == Zena::Status::Pub
# This forces index rebuild by selecting the :publish transition instead of :edit.
attrs = new_attributes.merge(:v_status => Zena::Status::Pub)
else
attrs = new_attributes
end
apply(:update_attributes, attrs)
ensure
@no_clone_on_change = nil
Node.record_timestamps = true
Version.record_timestamps = true
end

def no_clone_on_change?
@no_clone_on_change == true
end

private
def set_workflow_defaults
Expand Down Expand Up @@ -611,8 +639,11 @@ def set_current_version_before_update
self.publish_from = get_publish_from(version.id)
end
end

self.updated_at = Time.now unless changed? # force 'updated_at' sync

unless @no_clone_on_change
# Do not force updated_at sync when using "update_attributes_without_clone"
self.updated_at = Time.now unless changed? # force 'updated_at' sync
end
true
end

Expand Down
66 changes: 66 additions & 0 deletions test/unit/workflow_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,72 @@ def defaults
assert_equal 'Brazil', node.prop['country']
end
end

context 'with another user saving without clone' do
setup do
login(:lion)
end

should 'not create new version' do
node = secure!(Node) { nodes(:lake) }
assert_difference('Version.count', 0) do
assert node.update_attributes_without_clone(:first_name => 'Mea Lua', :country => 'Brazil')
node = secure!(Node) { nodes(:lake) } # reload
assert_equal 'Mea Lua we love', node.title
assert_equal 'Brazil', node.prop['country']
end
end

should 'not change version author' do
node = secure!(Node) { nodes(:lake) }
orig_user = node.version.user_id
assert_not_equal visitor.id, orig_user
assert_difference('Version.count', 0) do
assert node.update_attributes_without_clone(:first_name => 'Mea Lua', :country => 'Brazil')
node = secure!(Node) { nodes(:lake) } # reload
assert_equal orig_user, node.version.user_id
end
end

should 'not change node timestamp' do
node = secure!(Node) { nodes(:lake) }
updated_at = node.updated_at
assert_difference('Version.count', 0) do
assert node.update_attributes_without_clone(:first_name => 'Mea Lua', :country => 'Brazil')
node = secure!(Node) { nodes(:lake) } # reload
assert_equal updated_at, node.updated_at
end
end

should 'not change version timestamp' do
node = secure!(Node) { nodes(:lake) }
updated_at = node.version.updated_at
assert_difference('Version.count', 0) do
assert node.update_attributes_without_clone(:first_name => 'Mea Lua', :country => 'Brazil')
node = secure!(Node) { nodes(:lake) } # reload
assert_equal updated_at, node.version.updated_at
end
end

should 'evaluate prop_eval' do
node = secure!(Node) { nodes(:lake) }
assert_difference('Version.count', 0) do
assert node.update_attributes_without_clone(:first_name => 'Mea Lua', :country => 'Brazil')
node = secure!(Node) { nodes(:lake) } # reload
assert_equal 'Mea Lua we love', node.title
end
end

should 'evaluate indices' do
node = secure!(Node) { nodes(:opening) }
assert_difference('Version.count', 0) do
t = Time.utc(2013, 8, 21, 10, 59)
assert node.update_attributes_without_clone(:date => t)
node = secure!(Node) { nodes(:opening) } # reload
assert_equal t, node.idx_datetime1
end
end
end

should 'be able to create nodes using properties' do
node = secure!(Node) { Node.create(defaults.merge(:title => 'Pandeiro')) }
Expand Down

1 comment on commit 021b585

@testbird
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are already syncing to an external app? Wow.
From my inital search for this area, I had http://docs.rhomobile.com/rhoconnect/rails-plugin on the top of the list to try/use later.

Please sign in to comment.