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
- #auto_publish? ⇒ Boolean
-
#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 229 |
# 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
425 426 427 |
# File 'lib/zena/use/workflow.rb', line 425 def after_all true end |
#apply(method, *args) ⇒ Object
Gateway to all modifications of the node or it’s versions.
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 |
# File 'lib/zena/use/workflow.rb', line 383 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
411 412 413 414 415 416 417 418 419 420 421 |
# File 'lib/zena/use/workflow.rb', line 411 def apply_with_callbacks(method, *args) if apply_without_callbacks(method, *args) transition_name = @current_transition ? @current_transition[:name] : method callback = :"after_#{transition_name}" if respond_to?(callback, true) send(callback) else true end end && after_all # after_all can trigger even if no save operation occured end |
#auto_publish? ⇒ Boolean
281 282 283 |
# File 'lib/zena/use/workflow.rb', line 281 def auto_publish? current_site.auto_publish? || dgroup.auto_publish? end |
#can_apply?(method) ⇒ Boolean
Returns false is the current visitor does not have enough rights to perform the action.
370 371 372 373 374 375 376 377 378 379 380 |
# File 'lib/zena/use/workflow.rb', line 370 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)
327 328 329 |
# File 'lib/zena/use/workflow.rb', line 327 def can_destroy_version? can_apply? :destroy_version end |
#can_edit?(lang = nil) ⇒ Boolean
285 286 287 288 |
# File 'lib/zena/use/workflow.rb', line 285 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
300 301 302 |
# File 'lib/zena/use/workflow.rb', line 300 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
307 308 309 |
# File 'lib/zena/use/workflow.rb', line 307 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.
312 313 314 |
# File 'lib/zena/use/workflow.rb', line 312 def can_refuse? can_apply? :refuse end |
#can_remove? ⇒ Boolean
Can remove any other version
322 323 324 |
# File 'lib/zena/use/workflow.rb', line 322 def can_remove? can_apply? :remove end |
#can_unpublish? ⇒ Boolean
Can remove publication
317 318 319 |
# File 'lib/zena/use/workflow.rb', line 317 def can_unpublish? can_apply? :unpublish end |
#can_update? ⇒ Boolean
290 291 292 |
# File 'lib/zena/use/workflow.rb', line 290 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)
479 480 481 |
# File 'lib/zena/use/workflow.rb', line 479 def destroy_version apply(:destroy_version) end |
#edit_content! ⇒ Object
FIXME: remove !
295 296 297 |
# File 'lib/zena/use/workflow.rb', line 295 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
484 485 486 487 |
# File 'lib/zena/use/workflow.rb', line 484 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
430 431 432 433 434 435 436 |
# File 'lib/zena/use/workflow.rb', line 430 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’
446 447 448 449 450 451 452 |
# File 'lib/zena/use/workflow.rb', line 446 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.
473 474 475 |
# File 'lib/zena/use/workflow.rb', line 473 def redit apply(:redit) end |
#refuse ⇒ Object
Refuse publication
439 440 441 |
# File 'lib/zena/use/workflow.rb', line 439 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
464 465 466 |
# File 'lib/zena/use/workflow.rb', line 464 def remove apply(:remove) end |
#set_current_transition ⇒ Object
331 332 333 334 335 336 |
# File 'lib/zena/use/workflow.rb', line 331 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
253 254 255 256 257 258 259 260 261 262 263 264 265 266 |
# File 'lib/zena/use/workflow.rb', line 253 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
348 349 350 351 352 353 354 |
# File 'lib/zena/use/workflow.rb', line 348 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
338 339 340 341 342 343 344 345 346 |
# File 'lib/zena/use/workflow.rb', line 338 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
454 455 456 |
# File 'lib/zena/use/workflow.rb', line 454 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.
492 493 494 |
# File 'lib/zena/use/workflow.rb', line 492 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)
269 270 271 272 273 274 275 276 277 278 279 |
# File 'lib/zena/use/workflow.rb', line 269 def would_change_original? # on original version.number == 1 && # redaction or pub with autopublish ( version.status == Zena::Status::Red || (version.status == Zena::Status::Pub && auto_publish?) ) && # same owner, in redit time, ... !version.clone_on_change? end |