Class: PublishedWebsite

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/published_website.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.activate(website, version, current_user) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/models/published_website.rb', line 12

def self.activate(website, version, current_user)
  #deactivate
  published_websites = self.where(:website_id => website.id).where(:active => true).all
  published_websites.each do |published_website|
    published_website.active = false
    published_website.save
  end

  #activate
  published_website = self.where(:website_id => website.id).where(:version => version).first
  published_website.active = true
  published_website.published_by = current_user
  published_website.save
end

Instance Method Details

#publish(comment, current_user) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'app/models/published_website.rb', line 27

def publish(comment, current_user)
  # if the site is currently being published we need to wait
  # we want to sleep for 2 seconds and only try 3 times then give up
  try_count = 0
  while website.reload.publishing? and try_count < 2
    sleep(2.seconds)
    try_count += 1
  end

  # lock website for publishing
  website.publishing = true
  website.save

  # wrap in transaction so if something fails it rolls back
  ActiveRecord::Base.transaction do
    new_publication = clone_publication(comment, current_user)
    elements = []

    #get a publish sections
    website_sections = new_publication.website.website_sections
    website_sections = website_sections | website_sections.collect { |section| section.descendants }.flatten
    website_sections.each do |website_section|
      #get nested elements too
      website_section.self_and_descendants.each do |website_section|
        if new_publication.published_elements.where('published_element_record_id = ? and (published_element_record_type = ? or published_element_record_type = ?)', website_section.id, website_section.class.to_s, website_section.class.superclass.to_s).first.nil?
          published_element = PublishedElement.new
          published_element.published_website = new_publication
          published_element.published_element_record = website_section
          published_element.version = website_section.version
          published_element.published_by = current_user
          published_element.save
        end
      end

      if website_section.is_a?(OnlineDocumentSection)
        elements = elements | [website_section.documented_item.content]
      else
        elements = elements | website_section.contents
      end
    end

    #make sure all elements have published_element objects
    elements.each do |element|
      if new_publication.published_elements.where('published_element_record_id = ? and (published_element_record_type = ? or published_element_record_type = ?)', element.id, element.class.to_s, element.class.superclass.to_s).first.nil?
        published_element = PublishedElement.new
        published_element.published_website = new_publication
        published_element.published_element_record = element
        published_element.version = element.version
        published_element.published_by = current_user
        published_element.save
      end
    end

    #get latest version for all elements
    new_publication.published_elements.each do |published_element|
      published_element.version = published_element.published_element_record.version
      published_element.save
    end

    #check if we want to auto active this publication
    if new_publication.website.auto_activate_publication?
      PublishedWebsite.activate(new_publication.website, new_publication.version, current_user)
    end

  end

  # unlock website for publishing
  website.publishing = false
  website.save
end

#publish_element(comment, element, version, current_user) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
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
# File 'app/models/published_website.rb', line 98

def publish_element(comment, element, version, current_user)
  # if the site is currently being published we need to wait
  # we want to sleep for 2 seconds and only try 3 times then give up
  try_count = 0
  while website.reload.publishing? and try_count < 2
    sleep(2.seconds)
    try_count += 1
  end

  # lock website for publishing
  website.publishing = true
  website.save

  # wrap in transaction so if something fails it rolls back
  ActiveRecord::Base.transaction do
    new_publication = clone_publication(comment, current_user)

    published_element = new_publication.published_elements.where('published_element_record_id = ? and (published_element_record_type = ? or published_element_record_type = ?)', element.id, element.class.to_s, element.class.superclass.to_s).first

    unless published_element.nil?
      published_element.version = version
      published_element.published_by = current_user
      published_element.save
    else
      new_published_element = PublishedElement.new
      new_published_element.published_website = new_publication
      new_published_element.published_element_record = element
      new_published_element.version = version
      new_published_element.published_by = current_user
      new_published_element.save
    end

    #check if we want to auto active this publication
    if new_publication.website.auto_activate_publication?
      PublishedWebsite.activate(new_publication.website, new_publication.version, current_user)
    end
  end

  # unlock website for publishing
  website.publishing = false
  website.save
end

#published_by_usernameObject



8
9
10
# File 'app/models/published_website.rb', line 8

def published_by_username
  self.published_by.username rescue ''
end