Module: ConcurrentDraft::ModelExtensions

Defined in:
lib/concurrent_draft/model_extensions.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/concurrent_draft/model_extensions.rb', line 3

def self.included(base)
  base.extend ClassMethods
  base.class_eval do
    validate :promotion_date_in_future
    if instance_methods.include?('after_initialize')
      alias_method_chain :after_initialize, :drafts
    else
      def after_initialize
        promote_on_load
      end
    end
  end
end

Instance Method Details

#after_initialize_with_draftsObject



43
44
45
46
# File 'lib/concurrent_draft/model_extensions.rb', line 43

def after_initialize_with_drafts
  promote_on_load
  after_initialize_without_drafts
end

#cancel_promotion!Object



56
57
58
# File 'lib/concurrent_draft/model_extensions.rb', line 56

def cancel_promotion!
  update_attribute("draft_promotion_scheduled_at", nil)
end

#display_nameObject



23
24
25
26
27
28
29
30
31
# File 'lib/concurrent_draft/model_extensions.rb', line 23

def display_name
  if self.respond_to?(:title)
    self.title
  elsif self.respond_to?(:name)
    self.name
  else
    'resource'
  end
end

#draft_should_be_promoted?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/concurrent_draft/model_extensions.rb', line 60

def draft_should_be_promoted?
  has_draft_promotion_scheduled? && draft_promotion_scheduled_at <= Time.now
end

#has_draft_promoted?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/concurrent_draft/model_extensions.rb', line 52

def has_draft_promoted?
  !draft_promoted_at.blank? && draft_promoted_at <= Time.now
end

#has_draft_promotion_scheduled?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/concurrent_draft/model_extensions.rb', line 48

def has_draft_promotion_scheduled?
  !draft_promotion_scheduled_at.blank?
end

#promote_draft!Object



64
65
66
67
68
# File 'lib/concurrent_draft/model_extensions.rb', line 64

def promote_draft!
  update_attributes("content" => draft_content) if respond_to?(:content) && respond_to?(:draft_content)
  update_attributes("draft_promotion_scheduled_at" => nil, "draft_promoted_at" => Time.now) if respond_to?(:draft_promoted_at)
  Radiant::Cache.clear if defined?(Radiant::Cache)
end

#promote_on_loadObject



39
40
41
# File 'lib/concurrent_draft/model_extensions.rb', line 39

def promote_on_load
  promote_draft! if respond_to?(:draft_promotion_scheduled_at) && draft_should_be_promoted?
end

#promotion_date_in_futureObject



33
34
35
36
37
# File 'lib/concurrent_draft/model_extensions.rb', line 33

def promotion_date_in_future
  if respond_to?(:draft_promotion_scheduled_at) && !draft_promotion_scheduled_at.blank? && draft_promotion_scheduled_at < Time.now
    errors.add :draft_promotion_scheduled_at, "may not be in the past"
  end
end

#publishable?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/concurrent_draft/model_extensions.rb', line 75

def publishable?
  has_attribute?("published_at") && has_attribute?("status_id")
end

#unpublish!Object



70
71
72
73
# File 'lib/concurrent_draft/model_extensions.rb', line 70

def unpublish!
  update_attributes("content" => nil) if respond_to?(:content) && respond_to?(:draft_content)
  update_attributes("draft_promotion_scheduled_at" => nil, "draft_promoted_at" => nil) if respond_to?(:draft_promoted_at)
end