Class: CmsPage

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
ActiveModel::Dirty, ActsAsTree
Defined in:
app/models/cms_page.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.index_allObject



66
67
68
69
# File 'app/models/cms_page.rb', line 66

def self.index_all
  where('search_index is null').each(&:update_index!)
  true
end

.reindex_allObject



71
72
73
74
# File 'app/models/cms_page.rb', line 71

def self.reindex_all
  find_each(&:update_index!)
  true
end

Instance Method Details

#article_date_dayObject



137
# File 'app/models/cms_page.rb', line 137

def    ; .strftime("%d").to_i ; end

#article_date_monObject



136
# File 'app/models/cms_page.rb', line 136

def    ; .strftime("%b")      ; end

#article_date_monthObject



135
# File 'app/models/cms_page.rb', line 135

def  ; .strftime("%B")      ; end

#article_date_yearObject



138
# File 'app/models/cms_page.rb', line 138

def   ; .strftime("%Y").to_i ; end

#article_date_yrObject



139
# File 'app/models/cms_page.rb', line 139

def     ; .strftime("%y").to_i ; end

#compute_and_store_pathObject



26
27
28
29
30
31
32
33
34
35
36
# File 'app/models/cms_page.rb', line 26

def compute_and_store_path
  if self.parent
    if self.parent.path != ''
      self.path = "#{self.parent.path}/#{self.name}"
    else
      self.path = self.name
    end
  else
    self.path = ''
  end
end

#html_head=(value) ⇒ Object



86
87
88
89
90
91
92
93
94
# File 'app/models/cms_page.rb', line 86

def html_head=(value)
  if value && value.is_a?(String)
    # filter suspicious content... go overboard for now, fine-tune later perhaps
    value.gsub!(/<(%.*?(exec|system)\s?\(.*?\s*%)>/, '&lt;\1&gt;')
    value.gsub!(/<(%.*?\%x\s?\[.*?\s*%)>/, '&lt;\1&gt;')
    value.gsub!(/<(%.*?`.*?\s*%)>/, '&lt;\1&gt;')
  end
  super(value)
end

#name=(value) ⇒ Object



76
77
78
79
80
81
82
83
84
# File 'app/models/cms_page.rb', line 76

def name=(value)
  if value && value.is_a?(String)
    # filter suspicious content... go overboard for now, fine-tune later perhaps
    value.gsub!(/<(%.*?(exec|system)\s?\(.*?\s*%)>/, '&lt;\1&gt;')
    value.gsub!(/<(%.*?\%x\s?\[.*?\s*%)>/, '&lt;\1&gt;')
    value.gsub!(/<(%.*?`.*?\s*%)>/, '&lt;\1&gt;')
  end
  super(value)
end

#resave_childrenObject



53
54
55
56
57
58
59
60
# File 'app/models/cms_page.rb', line 53

def resave_children
  if path_changed?
    # get all pages under this one (even the offline ones)
    CmsPage.where(parent_id: id).each do |subpg|
      subpg.save_without_revision if subpg.valid?
    end
  end
end

#set_page_attributes(attrs) ⇒ Object

pass a hash to set page attributes in bulk



128
129
130
131
132
# File 'app/models/cms_page.rb', line 128

def set_page_attributes(attrs)
  attrs.each do |key, value|
    objects.find_or_initialize_by(name: key, obj_type: 'attribute').update_attributes(content: value)
  end
end

#set_parent_id!(new_id) ⇒ Object



121
122
123
124
125
# File 'app/models/cms_page.rb', line 121

def set_parent_id!(new_id)
  self.parent_id = new_id
  self.save_without_revision
  self.valid?
end

#set_versionsObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/models/cms_page.rb', line 38

def set_versions
  if self.template
    self.cms_template_version ||= self.template.version
  end
  
  self.published_version ||= -1
  self.published_date ||= self.created_on || Time.now
  
  if path == '' && published_version == -1
    self.published_version = 0  # home page should never be set to offline
  end
  
  true
end

#tags_as_css_classesObject



62
63
64
# File 'app/models/cms_page.rb', line 62

def tags_as_css_classes
  self.tags.map { |t| t.name.downcase.gsub(/[^\w]+/, '-') }.join(' ')
end

#update_indexObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'app/models/cms_page.rb', line 96

def update_index
  content = ''
  
  if self.published_version.to_i >= 0
    idx_version = self.published_version.to_i == 0 ? self.version : self.published_version
    self.objects.where(cms_page_version: idx_version, obj_type: [ 'text', 'string' ]).each do |obj|
      content << obj.content << "\n" if obj.content.to_s =~ /[^\d\.]/
    end
    self.custom_attributes_and_options.each do |obj|
      content << obj.content << "\n" if obj.content.to_s =~ /[^\d\.]/
    end
  end
  
  self.search_index = ActionController::Base.helpers.strip_tags(content.gsub('><', '> <'))
end

#update_index!Object



112
113
114
115
116
117
118
119
# File 'app/models/cms_page.rb', line 112

def update_index!
  update_index
  begin
    self.class.without_revision { save }
  rescue NoMethodError => e
    # sometime the cache sweeper fails, but that's okay here
  end
end