Class: Page

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.find_live_by_path(path) ⇒ Object



246
247
248
# File 'app/models/page.rb', line 246

def self.find_live_by_path(path)
  published.not_archived.first(:conditions => {:path => path})
end

Instance Method Details

#after_build_new_version(new_version) ⇒ Object



56
57
58
59
60
61
62
63
# File 'app/models/page.rb', line 56

def after_build_new_version(new_version)
  copy_connectors(
    :from_version_number => @copy_connectors_from_version || (new_version.version - 1),
    :to_version_number => new_version.version
  )
  @copy_connectors_from_version = nil
  true
end

#after_publishObject

Publish all



66
67
68
69
70
71
72
73
# File 'app/models/page.rb', line 66

def after_publish
  self.reload # Get's the correct version number loaded
  self.connectors.for_page_version(self.version).all(:order => "position").each do |c| 
    if c.connectable_type.constantize.publishable? && con = c.connectable
      con.publish
    end
  end
end

#ancestorsObject



222
223
224
# File 'app/models/page.rb', line 222

def ancestors
  section_node.ancestors
end

#append_leading_slash_to_pathObject



195
196
197
198
199
200
201
# File 'app/models/page.rb', line 195

def append_leading_slash_to_path
  if path.blank?
    self.path = "/"
  elsif path[0,1] != "/"
    self.path = "/#{path}"
  end
end

#assigned_toObject



267
268
269
# File 'app/models/page.rb', line 267

def assigned_to
  current_task ? current_task.assigned_to : nil
end

#assigned_to?(user) ⇒ Boolean

Returns:

  • (Boolean)


271
272
273
# File 'app/models/page.rb', line 271

def assigned_to?(user)
  assigned_to == user
end

#connectable_count_for_container(container) ⇒ Object

Returns the number of connectables in the given container for this version of this page



242
243
244
# File 'app/models/page.rb', line 242

def connectable_count_for_container(container)
  connectors.for_page_version(version).in_container(container.to_s).count
end

#container_published?(container) ⇒ Boolean

Returns true if the block attached to each connector in the given container are published

Returns:

  • (Boolean)


235
236
237
238
239
# File 'app/models/page.rb', line 235

def container_published?(container)
  connectors.for_page_version(draft.version).in_container(container.to_s).all? do |c| 
    c.connectable_type.constantize.publishable? ? c.connectable.live? : true
  end
end

#copy_connectors(options = {}) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'app/models/page.rb', line 75

def copy_connectors(options={})
  connectors.for_page_version(options[:from_version_number]).all(:order => "connectors.container, connectors.position").each do |c|
    # The connector won't have a connectable if it has been deleted
    # Also need to see if the draft has been deleted,
    # in which case we are in the process of deleting it
    if c.should_be_copied?
      connectable = c.connectable_type.constantize.versioned? ? c.connectable.as_of_version(c.connectable_version) : c.connectable
    
      #If we are copying connectors from a previous version, that means we are reverting this page,
      #in which case we should create a new version of the block, and connect this page to that block
      if @copy_connectors_from_version && connectable.class.versioned? && (connectable.version != connectable.draft.version)
        connectable = connectable.class.find(connectable.id)
        connectable.updated_by_page = self
        connectable.revert_to(c.connectable_version)
      end      
    
      new_connector = connectors.build(
        :page_version => options[:to_version_number], 
        :connectable => connectable, 
        :connectable_version => connectable.class.versioned? ? connectable.version : nil,         
        :container => c.container, 
        :position => c.position
      )
    end
  end
  true
end

#create_connector(connectable, container) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'app/models/page.rb', line 103

def create_connector(connectable, container)
  transaction do
    raise "Connectable is nil" unless connectable
    raise "Container is required" if container.blank?
    update_attributes(
      :version_comment => I18n.t("models.page.create_version_comment", :connectable => connectable, :container => container),
      :publish_on_save => (
        published? && 
        connectable.connected_page && 
        (connectable.class.publishable? ? connectable.published? : true)))
    connectors.create(
      :page_version => draft.version,
      :connectable => connectable,
      :connectable_version => connectable.class.versioned? ? connectable.version : nil, 
      :container => container)      
  end
end

#current_taskObject



263
264
265
# File 'app/models/page.rb', line 263

def current_task
  tasks.incomplete.first
end

#delete_connectorsObject



152
153
154
# File 'app/models/page.rb', line 152

def delete_connectors
  connectors.for_page_version(version).all.each{|c| c.destroy }
end

#file_sizeObject



163
164
165
# File 'app/models/page.rb', line 163

def file_size
  "?"
end

#in_section?(section_or_section_name) ⇒ Boolean

Returns:

  • (Boolean)


226
227
228
229
230
231
232
# File 'app/models/page.rb', line 226

def in_section?(section_or_section_name)
  sec = section_or_section_name.is_a?(String) ? 
    Section.first(:conditions => {:name => section_or_section_name}) : 
    section_or_section_name
  fn = lambda{|s| s ? (s == sec || fn.call(s.parent)) : false}
  fn.call(section)
end

#layoutObject



209
210
211
# File 'app/models/page.rb', line 209

def layout
  template_file_name && "templates/#{template_file_name.split('.').first}"
end

#move_connector(connector, direction) ⇒ Object

TODO (Translate)



122
123
124
125
126
127
128
129
130
# File 'app/models/page.rb', line 122

def move_connector(connector, direction)
  transaction do
    raise "Connector is nil" unless connector
    raise "Direction is nil" unless direction
    orientation = direction[/_/] ? "#{direction.sub('_', ' the ')} of" : "#{direction} within"
    update_attributes(:version_comment => "#{connector.connectable} was moved #{orientation} the '#{connector.container}' container")
    connectors.for_page_version(draft.version).like(connector).first.send("move_#{direction}")
  end    
end

#name_with_section_pathObject



250
251
252
253
# File 'app/models/page.rb', line 250

def name_with_section_path
  a = ancestors
  (a[1..a.size].map{|a| a.name} + [name]).join(" / ")
end

#page_titleObject



191
192
193
# File 'app/models/page.rb', line 191

def page_title
  title.blank? ? name : title
end

#path_not_reservedObject



203
204
205
206
207
# File 'app/models/page.rb', line 203

def path_not_reserved
  if Cms.reserved_paths.include?(path)
    errors.add(:path, I18n.t("models.page.path_reserved", :path => path))
  end
end

#public?Boolean

Returns:

  • (Boolean)


187
188
189
# File 'app/models/page.rb', line 187

def public?
  section ? section.public? : false
end

#remove_connector(connector) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'app/models/page.rb', line 138

def remove_connector(connector)
  transaction do
    raise "Connector is nil" unless connector
    update_attributes(:version_comment => I18n.t("models.page.remove_version_comment", :connectable => connector.connectable, :container => connector.container))
    
    #The logic of this is to go ahead and let the container get copied forward, then delete the new connector
    if new_connector = connectors.for_page_version(draft.version).like(connector).first
      new_connector.destroy
    else
      raise "Error occurred while trying to remove connector #{connector.id}"
    end
  end
end

#revert_to(version) ⇒ Object

This is done to let copy_connectors know which version to pull from copy_connectors will get called later as an after_update callback



158
159
160
161
# File 'app/models/page.rb', line 158

def revert_to(version)
  @copy_connectors_from_version = version
  super(version)
end

#sectionObject



171
172
173
# File 'app/models/page.rb', line 171

def section
  section_node ? section_node.section : nil
end

#section=(sec) ⇒ Object



179
180
181
182
183
184
185
# File 'app/models/page.rb', line 179

def section=(sec)
  if section_node
    section_node.move_to_end(sec)
  else
    build_section_node(:node => self, :section => sec)
  end      
end

#section_idObject



167
168
169
# File 'app/models/page.rb', line 167

def section_id
  section ? section.id : nil
end

#section_id=(sec_id) ⇒ Object



175
176
177
# File 'app/models/page.rb', line 175

def section_id=(sec_id)
  self.section = Section.find(sec_id)
end

#templateObject

This will be nil if it is a file system based template



214
215
216
# File 'app/models/page.rb', line 214

def template
  PageTemplate.find_by_file_name(template_file_name)
end

#template_nameObject



218
219
220
# File 'app/models/page.rb', line 218

def template_name
  template_file_name && PageTemplate.display_name(template_file_name)
end

#top_level_sectionObject

This will return the “top level section” for a page, which is the section directly below the root (a.k.a My Site) that this page is in. If this page is in root, then this will return root.



258
259
260
261
# File 'app/models/page.rb', line 258

def top_level_section
  a = ancestors
  (a.size > 0 && ancestors[1]) ? ancestors[1] : Section.root.first
end