Class: ArchivePage

Inherits:
Page
  • Object
show all
Defined in:
app/models/archive_page.rb

Constant Summary collapse

@@single_use_children =
[ArchiveDayIndexPage, ArchiveMonthIndexPage, ArchiveYearIndexPage, FileNotFoundPage]

Instance Method Summary collapse

Instance Method Details

#child_path(child) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/models/archive_page.rb', line 27

def child_path(child)
  year, month, day = $1, ($2 || 1).to_i, ($3 || 1).to_i if child.request and child.request.request_uri =~ %r{/(\d{4})(?:/(\d{2})(?:/(\d{2}))?)?/?$}
  
  if year && %w{ ArchiveYearIndexPage ArchiveMonthIndexPage ArchiveDayIndexPage }.include?(child.class_name)
    date = Date.new(year.to_i, month, day)
    if ArchiveYearIndexPage === child
      clean_path "#{ path }/#{ date.strftime '%Y' }/"
    elsif ArchiveMonthIndexPage === child
      clean_path "#{ path }/#{ date.strftime '%Y/%m' }/"
    else ArchiveDayIndexPage === child
      clean_path "#{ path }/#{ date.strftime '%Y/%m/%d/' }/"
    end
  else
    if child.published_at?
      clean_path "#{ path }/#{ child.published_at.strftime '%Y/%m/%d' }/#{ child.slug }"
    else
      clean_path "#{ path }/#{ Time.zone.now.strftime '%Y/%m/%d' }/#{ child.slug }"
    end
  end
end

#child_url(child) ⇒ Object



47
48
49
50
# File 'app/models/archive_page.rb', line 47

def child_url(child)
  ActiveSupport::Deprecation.warn("`child_url' has been deprecated; use `child_path' instead.", caller)
  child_path(child)
end

#existing_child_typesObject



13
14
15
# File 'app/models/archive_page.rb', line 13

def existing_child_types
  children(:select => 'DISTINCT class_name, title, virtual', :order => nil).map(&:class_name).compact.map(&:constantize).uniq
end

#find_by_path(path, live = true, clean = false) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'app/models/archive_page.rb', line 52

def find_by_path(path, live = true, clean = false)
  path = clean_path(path) if clean
  if path =~ %r{^#{ self.path }(\d{4})(?:/(\d{2})(?:/(\d{2})(?:/([-_.A-Za-z0-9]*))?)?)?/?$}
    year, month, day, slug = $1, $2, $3, $4
    children.find_by_class_name(
      case
      when slug.present?
        found = children.find_by_slug(slug)
        return found if found
      when day.present?
        'ArchiveDayIndexPage'
      when month.present?
        'ArchiveMonthIndexPage'
      else
        'ArchiveYearIndexPage'
      end
    )
  else
    super
  end
end

#find_by_url(*args) ⇒ Object



73
74
75
76
# File 'app/models/archive_page.rb', line 73

def find_by_url(*args)
  ActiveSupport::Deprecation.warn("`find_by_url' has been deprecated; use `find_by_path' instead.", caller)
  find_by_path(*args)
end

#set_allowed_children_cacheObject



5
6
7
8
9
10
11
# File 'app/models/archive_page.rb', line 5

def set_allowed_children_cache
  singles = self.class.single_use_children.map(&:name)
  existing = existing_child_types.map(&:name)
  all = allowed_children_lookup.map(&:name)
  
  self.allowed_children_cache = (all - (singles & existing)).join(',')
end