Module: Zena::Use::Workflow
Overview
The workflow module manages the different versions’ status and transitions. This module depends on MultiVersion and VersionHash and it should be included before these two modules.
Defined Under Namespace
Modules: ClassMethods, VersionMethods
Constant Summary collapse
- WORKFLOW_ATTRIBUTES =
%w{status publish_from}
Class Method Summary collapse
-
.included(base) ⇒ Object
ClassMethods.
Instance Method Summary collapse
- #after_all ⇒ Object
-
#apply(method, *args) ⇒ Object
Gateway to all modifications of the node or it’s versions.
- #apply_with_callbacks(method, *args) ⇒ Object
-
#can_apply?(method) ⇒ Boolean
Returns false is the current visitor does not have enough rights to perform the action.
-
#can_destroy_version? ⇒ Boolean
Can destroy current version ? (only logged in user can destroy).
- #can_edit?(lang = nil) ⇒ Boolean
-
#can_propose? ⇒ Boolean
can propose for validation.
-
#can_publish? ⇒ Boolean
people who can publish: * people who #can_drive? if
status
>= prop or owner * people who #can_drive? if node is private. -
#can_refuse? ⇒ Boolean
Can refuse a publication.
-
#can_remove? ⇒ Boolean
Can remove any other version.
-
#can_unpublish? ⇒ Boolean
Can remove publication.
- #can_update? ⇒ Boolean
-
#destroy_version ⇒ Object
Versions can be destroyed if they are in ‘deleted’ status.
-
#edit_content! ⇒ Object
FIXME: remove !.
-
#get_publish_from(ignore_id = nil) ⇒ Object
Set
publish_from
to the minimum publication time of all editions. -
#propose(prop_status = Zena::Status::Prop) ⇒ Object
Propose for publication.
-
#publish(pub_time = nil) ⇒ Object
publish if version status is : redaction, proposition, replaced or removed if version to publish is ‘rem’ or ‘red’ or ‘prop’ : old publication => ‘replaced’ if version to publish is ‘rep’ : old publication => ‘removed’.
-
#redit ⇒ Object
Edit again a previously published/removed version.
-
#refuse ⇒ Object
Refuse publication.
-
#remove ⇒ Object
A published version can be removed by the members of the publish group A redaction can be removed by it’s owner.
- #set_current_transition ⇒ Object
-
#traductions(opts = {}) ⇒ Object
return an array of published versions.
- #transition_allowed?(transition) ⇒ Boolean
- #transition_for(prev, curr) ⇒ Object
- #unpublish ⇒ Object
-
#update_attributes(new_attributes) ⇒ Object
Update an node’s attributes or the node’s version/content attributes.
-
#would_change_original? ⇒ Boolean
Return true if the version is the original version and we are in redit time (will not clone).
Class Method Details
.included(base) ⇒ Object
ClassMethods
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/zena/use/workflow.rb', line 110 def self.included(base) base.has_many :editions, :class_name => 'Version', :conditions=>"publish_from <= #{Zena::Db::NOW} AND status = #{Zena::Status::Pub}", :order=>'lang' #, :inverse_of => :node base.before_validation :set_workflow_defaults base.validate :workflow_validation base.before_create :workflow_before_create base.after_save :update_status_after_save base.class_eval do extend Zena::Use::Workflow::ClassMethods # List of allowed *version* transitions with their validation rules. This list # concerns the life and death of *a single version*, not the corresponding Node. # FIXME: we should not use the same name for 'edit' # We could use 'edit', 'create', 'update' add_transition(:edit, :from => Zena::Status::Red, :to => Zena::Status::Red) do |node, version| node.can_write? end add_transition(:edit, :from => -1, :to => Zena::Status::Red) do |node, version| # create a new node node.can_write? end add_transition(:edit, :from => Zena::Status::Pub, :to => Zena::Status::Red) do |node, version| node.can_write? && version.edited? end add_transition(:auto_publish, :from => Zena::Status::Pub, :to => Zena::Status::Pub) do |node, version| node.full_drive? end add_transition(:publish, :from => [-1, Zena::Status::Red], :to => Zena::Status::Pub) do |node, version| node.full_drive? end add_transition(:publish, :from => [Zena::Status::Prop, Zena::Status::PropWith], :to => Zena::Status::Pub) do |node, version| # editing content when publishing a proposition is not allowed if node.full_drive? && !version.edited? true elsif node.full_drive? [false, "You do not have the rights to change a proposition's attributes."] else false end end add_transition(:publish, :from => (Zena::Status::Del..(Zena::Status::Pub-1)), :to => Zena::Status::Pub) do |node, version| node.full_drive? end add_transition(:propose, :from => Zena::Status::Red, :to => Zena::Status::Prop) do |node, version| node.can_write? end add_transition(:propose, :from => Zena::Status::Red, :to => Zena::Status::PropWith) do |node, version| node.can_write? end add_transition(:refuse, :from => [Zena::Status::Prop, Zena::Status::PropWith], :to => Zena::Status::Red) do |node, version| # refuse and change attributes not allowed if node.full_drive? && !version.edited? true elsif version.edited? [false, 'You cannot edit while a proposition is beeing reviewed.'] else false end end add_transition(:unpublish, :from => Zena::Status::Pub, :to => Zena::Status::Rem) do |node, version| if node.can_drive? && !version.edited? true elsif version.edited? [false, "You cannot unpublish and edit at the same time."] else false end end add_transition(:remove, :from => (((Zena::Status::Rep+1)..(Zena::Status::Pub-1)).to_a + [Zena::Status::Red]), :to => Zena::Status::Rem) do |node, version| node.can_drive? && !version.edited? end add_transition(:destroy_version, :from => (-1..Zena::Status::Rep), :to => -1) do |node, version| if node.can_drive? && !visitor.is_anon? && (node.versions.count > 1 || node.empty?) true elsif visitor.is_anon? [false, "Anonymous users are not allowed to destroy versions."] elsif node.versions.count > 1 || node.empty? [false, "Cannot destroy last version: node is not empty."] else false end end add_transition(:redit, :from => (Zena::Status::Rem..(Zena::Status::Pub-1)), :to => Zena::Status::Red) do |node, version| node.can_drive? && !version.edited? end end end |
Instance Method Details
#after_all ⇒ Object
417 418 419 |
# File 'lib/zena/use/workflow.rb', line 417 def after_all true end |
#apply(method, *args) ⇒ Object
Gateway to all modifications of the node or it’s versions.
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 |
# File 'lib/zena/use/workflow.rb', line 375 def apply(method, *args) res = case method when :update_attributes self.attributes = args.first save when :propose # TODO: replace with version.status = ... self.version_attributes = {'status' => args[0] || Zena::Status::Prop} save when :publish self.version_attributes = {'status' => Zena::Status::Pub} save when :refuse, :redit self.version_attributes = {'status' => Zena::Status::Red} save when :unpublish, :remove self.version_attributes = {'status' => Zena::Status::Rem} save when :destroy_version if versions.count == 1 && empty? self.destroy # will destroy last version else self.version_attributes = {:__destroy => true} save end end end |
#apply_with_callbacks(method, *args) ⇒ Object
403 404 405 406 407 408 409 410 411 412 413 |
# File 'lib/zena/use/workflow.rb', line 403 def apply_with_callbacks(method, *args) if apply_without_callbacks(method, *args) # TODO: we should build callback from @current_transition.name callback = :"after_#{method}" if respond_to?(callback, true) send(callback) else true end end && after_all # after_all can trigger even if no save operation occured end |
#can_apply?(method) ⇒ Boolean
Returns false is the current visitor does not have enough rights to perform the action.
362 363 364 365 366 367 368 369 370 371 372 |
# File 'lib/zena/use/workflow.rb', line 362 def can_apply?(method) case method when :edit can_edit? when :drive can_drive? else # All the other actions are version transition changes transition_allowed?(method) end end |
#can_destroy_version? ⇒ Boolean
Can destroy current version ? (only logged in user can destroy)
319 320 321 |
# File 'lib/zena/use/workflow.rb', line 319 def can_destroy_version? can_apply? :destroy_version end |
#can_edit?(lang = nil) ⇒ Boolean
277 278 279 280 |
# File 'lib/zena/use/workflow.rb', line 277 def can_edit?(lang=nil) # Has the visitor write access to the node & node is not a proposition ? can_write? && !(Zena::Status::Prop..Zena::Status::PropWith).include?(version.status) end |
#can_propose? ⇒ Boolean
can propose for validation
292 293 294 |
# File 'lib/zena/use/workflow.rb', line 292 def can_propose? can_apply? :propose end |
#can_publish? ⇒ Boolean
people who can publish:
-
people who #can_drive? if
status
>= prop or owner -
people who #can_drive? if node is private
299 300 301 |
# File 'lib/zena/use/workflow.rb', line 299 def can_publish? can_apply? :publish end |
#can_refuse? ⇒ Boolean
Can refuse a publication. Same rights as can_publish? if the current version is a redaction.
304 305 306 |
# File 'lib/zena/use/workflow.rb', line 304 def can_refuse? can_apply? :refuse end |
#can_remove? ⇒ Boolean
Can remove any other version
314 315 316 |
# File 'lib/zena/use/workflow.rb', line 314 def can_remove? can_apply? :remove end |
#can_unpublish? ⇒ Boolean
Can remove publication
309 310 311 |
# File 'lib/zena/use/workflow.rb', line 309 def can_unpublish? can_apply? :unpublish end |
#can_update? ⇒ Boolean
282 283 284 |
# File 'lib/zena/use/workflow.rb', line 282 def can_update? can_write? && version.status == Zena::Status::Red end |
#destroy_version ⇒ Object
Versions can be destroyed if they are in ‘deleted’ status. Destroying the last version completely removes the node (it must thus be empty)
471 472 473 |
# File 'lib/zena/use/workflow.rb', line 471 def destroy_version apply(:destroy_version) end |
#edit_content! ⇒ Object
FIXME: remove !
287 288 289 |
# File 'lib/zena/use/workflow.rb', line 287 def edit_content! redaction && redaction.redaction_content end |
#get_publish_from(ignore_id = nil) ⇒ Object
Set publish_from
to the minimum publication time of all editions
476 477 478 479 |
# File 'lib/zena/use/workflow.rb', line 476 def get_publish_from(ignore_id = nil) pub_string = (self.class.connection.select_one("SELECT publish_from FROM #{version.class.table_name} WHERE node_id = '#{self[:id]}' AND status = #{Zena::Status::Pub} #{ignore_id ? "AND id != '#{ignore_id}'" : ''} order by publish_from ASC LIMIT 1") || {})['publish_from'] ActiveRecord::ConnectionAdapters::Column.string_to_time(pub_string) end |
#propose(prop_status = Zena::Status::Prop) ⇒ Object
Propose for publication
422 423 424 425 426 427 428 |
# File 'lib/zena/use/workflow.rb', line 422 def propose(prop_status=Zena::Status::Prop) if version.status == Zena::Status::Prop errors.add(:base, 'Already proposed.') return false end apply(:propose, prop_status) end |
#publish(pub_time = nil) ⇒ Object
publish if version status is : redaction, proposition, replaced or removed if version to publish is ‘rem’ or ‘red’ or ‘prop’ : old publication => ‘replaced’ if version to publish is ‘rep’ : old publication => ‘removed’
438 439 440 441 442 443 444 |
# File 'lib/zena/use/workflow.rb', line 438 def publish(pub_time=nil) if version.status == Zena::Status::Pub errors.add(:base, 'Already published.') return false end apply(:publish, pub_time) end |
#redit ⇒ Object
Edit again a previously published/removed version.
465 466 467 |
# File 'lib/zena/use/workflow.rb', line 465 def redit apply(:redit) end |
#refuse ⇒ Object
Refuse publication
431 432 433 |
# File 'lib/zena/use/workflow.rb', line 431 def refuse apply(:refuse) end |
#remove ⇒ Object
A published version can be removed by the members of the publish group A redaction can be removed by it’s owner
456 457 458 |
# File 'lib/zena/use/workflow.rb', line 456 def remove apply(:remove) end |
#set_current_transition ⇒ Object
323 324 325 326 327 328 |
# File 'lib/zena/use/workflow.rb', line 323 def set_current_transition version = self.version prev = new_record? ? -1 : version.status_was curr = version.status @current_transition = transition_for(prev, curr) end |
#traductions(opts = {}) ⇒ Object
return an array of published versions
252 253 254 255 256 257 258 259 260 261 262 263 264 265 |
# File 'lib/zena/use/workflow.rb', line 252 def traductions(opts={}) if opts == {} trad = editions else trad = editions.find(:all, opts) end if trad == [] nil else trad.each {|t| t.node = self} trad end end |
#transition_allowed?(transition) ⇒ Boolean
340 341 342 343 344 345 346 |
# File 'lib/zena/use/workflow.rb', line 340 def transition_allowed?(transition) from_status = self.version.status if transition.kind_of?(Symbol) transition = self.class.transitions.detect { |t| t[:name] == transition && t[:from].include?(from_status) } end transition && (transition[:validate].nil? || transition[:validate].call(self, version)) end |
#transition_for(prev, curr) ⇒ Object
330 331 332 333 334 335 336 337 338 |
# File 'lib/zena/use/workflow.rb', line 330 def transition_for(prev, curr) self.class.transitions.each do |t| from, to = t[:from], t[:to] if curr == to && from.include?(prev) return t end end return nil end |
#unpublish ⇒ Object
446 447 448 |
# File 'lib/zena/use/workflow.rb', line 446 def unpublish apply(:unpublish) end |
#update_attributes(new_attributes) ⇒ Object
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.
484 485 486 |
# File 'lib/zena/use/workflow.rb', line 484 def update_attributes(new_attributes) apply(:update_attributes, new_attributes) end |
#would_change_original? ⇒ Boolean
Return true if the version is the original version and we are in redit time (will not clone)
268 269 270 271 272 273 274 275 |
# File 'lib/zena/use/workflow.rb', line 268 def would_change_original? # on original version.number == 1 && # redaction or pub with autopublish (version.status == Zena::Status::Red || (version.status == Zena::Status::Pub && current_site.auto_publish?)) && # same owner, in redit time, ... !version.clone_on_change? end |