states.rb
上传用户:netsea168
上传日期:2022-07-22
资源大小:4652k
文件大小:4k
源码类别:

Ajax

开发平台:

Others

  1. module Article::States
  2.   class Base < Stateful::State
  3.     alias_method :content, :model
  4.     def to_s
  5.       self.class.to_s.demodulize
  6.     end
  7.     def exit_hook(target)
  8.       RAILS_DEFAULT_LOGGER.debug("#{content} leaving state #{self.class}")
  9.     end
  10.     def enter_hook
  11.       RAILS_DEFAULT_LOGGER.debug("#{content} entering state #{self.class}")
  12.     end
  13.     def before_save; true; end
  14.     def after_save; true; end
  15.     def post_trigger; true; end
  16.     def send_notifications; true; end
  17.     def send_pings; true; end
  18.     def withdraw
  19.     end
  20.   end
  21.   class New < Base
  22.     def enter_hook
  23.       super
  24.       content[:published] = false
  25.       content[:published_at] = nil
  26.     end
  27.     def before_save
  28.       content.state = :draft
  29.     end
  30.     def published=(boolean)
  31.       returning(boolean) do
  32.         if boolean
  33.           content.state = :just_published
  34.         end
  35.       end
  36.     end
  37.     def published_at=(new_time)
  38.       new_time = (new_time.to_time rescue nil)
  39.       returning(content[:published_at] = new_time) do
  40.         break if new_time.nil?
  41.         content.state = (new_time <= Time.new) ? :just_published : :publication_pending
  42.       end
  43.     end
  44.     def draft?
  45.       true
  46.     end
  47.   end
  48.   class JustPublished < Base
  49.     def enter_hook
  50.       super
  51.       content.just_changed_published_status = true
  52.       content.state = :published
  53.     end
  54.   end
  55.   class Published < Base
  56.     def enter_hook
  57.       super
  58.       content[:published] = true
  59.       content[:published_at] ||= Time.now
  60.     end
  61.     def published=(boolean)
  62.       if !boolean
  63.         content.state = :just_withdrawn
  64.       end
  65.     end
  66.     def withdraw
  67.       content.state = :just_withdrawn
  68.     end
  69.     def published_at=(new_time)
  70.       new_time = (new_time.to_time rescue nil)
  71.       return if new_time.nil?
  72.       content[:published_at] = new_time
  73.       if new_time > Time.now
  74.         content.state = :publication_pending
  75.       end
  76.     end
  77.     def send_notifications
  78.       content.really_send_notifications if just_published?
  79.       true
  80.     end
  81.     def send_pings
  82.       content.really_send_pings if just_published?
  83.       true
  84.     end
  85.     def just_published?
  86.       content.just_changed_published_status?
  87.     end
  88.   end
  89.   class JustWithdrawn < Base
  90.     def enter_hook
  91.       super
  92.       content.just_changed_published_status = true
  93.       content.state = :withdrawn
  94.     end
  95.   end
  96.   class Withdrawn < Base
  97.     def enter_hook
  98.       content[:published] = false
  99.     end
  100.     def published=(boolean)
  101.       return unless boolean
  102.       content.state = :published
  103.     end
  104.     def published_at=(new_time)
  105.       new_time = (new_time.to_time rescue nil)
  106.       content[:published_at] = new_time
  107.       Trigger.remove(content, :trigger_method => 'publish!')
  108.       return if new_time.nil? || new_time <= Time.now
  109.       content.state = :publication_pending
  110.     end
  111.   end
  112.   class PublicationPending < Base
  113.     def enter_hook
  114.       content[:published] = false if content.new_record?
  115.     end
  116.     def published=(published)
  117.       content[:published] = published
  118.       if published && content.published_at <= Time.now
  119.         content.state = :just_published
  120.       end
  121.     end
  122.     def published_at=(new_time)
  123.       new_time = (new_time.to_time rescue nil)
  124.       content[:published_at] = new_time
  125.       Trigger.remove(content, :trigger_method => 'publish!')
  126.       if new_time.nil?
  127.         content.state = :draft
  128.       elsif new_time <= Time.now
  129.         content.state = :just_published
  130.       end
  131.     end
  132.     def post_trigger
  133.       Trigger.post_action(content.published_at, content, 'publish!')
  134.     end
  135.     def withdraw(content)
  136.       content.state = :draft
  137.     end
  138.   end
  139.   class Draft < Base
  140.     def enter_hook
  141.       super
  142.       content[:published] = false
  143.       content[:published_at] = nil
  144.     end
  145.     def published=(boolean)
  146.       if boolean
  147.         content.state = :just_published
  148.       end
  149.     end
  150.     def published_at=(new_time)
  151.       # Because of the workings of the controller, we should ignore
  152.       # publication times before the current time.
  153.       new_time = (new_time.to_time rescue nil)
  154.       return if new_time.nil? || new_time <= Time.now
  155.       content[:published_at] = new_time
  156.       content.state = :publication_pending
  157.     end
  158.   end
  159. end