Class: CmsPage
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- CmsPage
- Includes:
- ActiveModel::Dirty, ActsAsTree
- Defined in:
- app/models/cms_page.rb
Class Method Summary collapse
Instance Method Summary collapse
- #article_date_day ⇒ Object
- #article_date_mon ⇒ Object
- #article_date_month ⇒ Object
- #article_date_year ⇒ Object
- #article_date_yr ⇒ Object
- #compute_and_store_path ⇒ Object
- #html_head=(value) ⇒ Object
- #name=(value) ⇒ Object
- #resave_children ⇒ Object
-
#set_page_attributes(attrs) ⇒ Object
pass a hash to set page attributes in bulk.
- #set_parent_id!(new_id) ⇒ Object
- #set_versions ⇒ Object
- #tags_as_css_classes ⇒ Object
- #update_index ⇒ Object
- #update_index! ⇒ Object
Class Method Details
.index_all ⇒ Object
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_all ⇒ Object
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_day ⇒ Object
137 |
# File 'app/models/cms_page.rb', line 137 def article_date_day ; article_date.strftime("%d").to_i ; end |
#article_date_mon ⇒ Object
136 |
# File 'app/models/cms_page.rb', line 136 def article_date_mon ; article_date.strftime("%b") ; end |
#article_date_month ⇒ Object
135 |
# File 'app/models/cms_page.rb', line 135 def article_date_month ; article_date.strftime("%B") ; end |
#article_date_year ⇒ Object
138 |
# File 'app/models/cms_page.rb', line 138 def article_date_year ; article_date.strftime("%Y").to_i ; end |
#article_date_yr ⇒ Object
139 |
# File 'app/models/cms_page.rb', line 139 def article_date_yr ; article_date.strftime("%y").to_i ; end |
#compute_and_store_path ⇒ Object
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*%)>/, '<\1>') value.gsub!(/<(%.*?\%x\s?\[.*?\s*%)>/, '<\1>') value.gsub!(/<(%.*?`.*?\s*%)>/, '<\1>') 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*%)>/, '<\1>') value.gsub!(/<(%.*?\%x\s?\[.*?\s*%)>/, '<\1>') value.gsub!(/<(%.*?`.*?\s*%)>/, '<\1>') end super(value) end |
#resave_children ⇒ Object
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_versions ⇒ Object
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_classes ⇒ Object
62 63 64 |
# File 'app/models/cms_page.rb', line 62 def self..map { |t| t.name.downcase.gsub(/[^\w]+/, '-') }.join(' ') end |
#update_index ⇒ Object
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..each do |obj| content << obj.content << "\n" if obj.content.to_s =~ /[^\d\.]/ end end self.search_index = ActionController::Base.helpers.(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 |