Class: Concen::Page
- Inherits:
-
Object
- Object
- Concen::Page
- Includes:
- Mongoid::Document, Mongoid::Timestamps
- Defined in:
- app/models/concen/page.rb
Constant Summary collapse
- PREDEFINED_FIELDS =
Get the list of dynamic fields by checking againts this array. Values should mirror the listed fields above.
[:_id, :parent_id, :level, :created_at, :updated_at, :slug, :content, :raw_text, :position, :grid_files, :title, :description, :publish_time, :labels, :authors, :status]
- PROTECTED_FIELDS =
These fields can’t be overwritten by user’s meta data when parsing raw_text.
[:_id, :parent_id, :level, :created_at, :updated_at, :content, :raw_text, :position, :grid_files]
Instance Method Summary collapse
- #authors_as_user ⇒ Object
- #content_in_html(key = "main", data = {}) ⇒ Object
- #first?(*args) ⇒ Boolean
- #images(filename = nil) ⇒ Object
- #javascripts(filename = nil) ⇒ Object
- #last?(*args) ⇒ Boolean
- #next(*args) ⇒ Object
- #others(filename = nil) ⇒ Object
- #parse_publish_time(publish_time_string) ⇒ Object
- #previous(*args) ⇒ Object
- #published? ⇒ Boolean
- #search_grid_files(extensions, filename = nil) ⇒ Object
- #stylesheets(filename = nil) ⇒ Object
- #underscore_hash_keys(hash) ⇒ Object
Instance Method Details
#authors_as_user ⇒ Object
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'app/models/concen/page.rb', line 205 def users = [] for in self. if .is_a?(String) if user = User.where(:username => ).first users << user elsif user = User.where(:email => ).first users << user elsif user = User.where(:full_name => ).first users << user end else users << user if User.where(:_id => ).first end end return users end |
#content_in_html(key = "main", data = {}) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'app/models/concen/page.rb', line 69 def content_in_html(key = "main", data={}) if content = self.content.try(:[], key) content = Mustache.render(content, data) markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML, Concen.markdown_extensions) html = markdown.render content if Concen.parse_markdown_with_smartypants html = Redcarpet::Render::SmartyPants.render html end return html else return nil end end |
#first?(*args) ⇒ Boolean
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'app/models/concen/page.rb', line 165 def first?(*args) = args. children = self.parent.children children = children.published if [:only_published] if [:chronologically] children = children.asc(:publish_time) else children = children.asc(:position) end if children.first if self.id == children.first.id return true else return false end else return false end end |
#images(filename = nil) ⇒ Object
83 84 85 |
# File 'app/models/concen/page.rb', line 83 def images(filename=nil) search_grid_files(["png", "jpg", "jpeg", "gif"], filename) end |
#javascripts(filename = nil) ⇒ Object
91 92 93 |
# File 'app/models/concen/page.rb', line 91 def javascripts(filename=nil) search_grid_files(["js"], filename) end |
#last?(*args) ⇒ Boolean
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'app/models/concen/page.rb', line 185 def last?(*args) = args. children = self.parent.children children = children.published if [:only_published] if [:chronologically] children = children.asc(:publish_time) else children = children.asc(:position) end if children.last if self.id == children.last.id return true else return false end else return false end end |
#next(*args) ⇒ Object
152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'app/models/concen/page.rb', line 152 def next(*args) = args. children = self.parent.children children = children.published if [:only_published] if [:chronologically] children = children.asc(:publish_time) children.where(:publish_time.gt => self.publish_time).first else children = children.asc(:position) children.where(:position.gt => self.position).first end end |
#others(filename = nil) ⇒ Object
95 96 97 98 99 100 101 |
# File 'app/models/concen/page.rb', line 95 def others(filename=nil) excluded_ids = [] [:images, :stylesheets, :javascripts].each do |file_type| excluded_ids += self.send(file_type).map(&:_id) end self.grid_files.where(:_id.nin => excluded_ids) end |
#parse_publish_time(publish_time_string) ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'app/models/concen/page.rb', line 120 def parse_publish_time(publish_time_string) publish_time_string = publish_time_string.to_s begin Chronic.time_class = Time.zone parsed_date = Chronic.parse(publish_time_string, :now => Time.zone.now) rescue parsed_date = nil end if parsed_date self.publish_time = parsed_date elsif parsed_date = Time.zone.parse(publish_time_string) self.publish_time = parsed_date end end |
#previous(*args) ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'app/models/concen/page.rb', line 139 def previous(*args) = args. children = self.parent.children children = children.published if [:only_published] if [:chronologically] children = children.desc(:publish_time) children.where(:publish_time.lt => self.publish_time).first else children = children.desc(:position) children.where(:position.lt => self.position).first end end |
#published? ⇒ Boolean
135 136 137 |
# File 'app/models/concen/page.rb', line 135 def published? self.publish_time.present? end |
#search_grid_files(extensions, filename = nil) ⇒ Object
103 104 105 106 107 108 109 |
# File 'app/models/concen/page.rb', line 103 def search_grid_files(extensions, filename=nil) if filename self.grid_files.where(:original_filename => /.*#{filename}.*.*\.(#{extensions.join("|")}).*$/i).asc(:original_filename) else self.grid_files.where(:original_filename => /.*\.(#{extensions.join("|")}).*/i).asc(:original_filename) end end |
#stylesheets(filename = nil) ⇒ Object
87 88 89 |
# File 'app/models/concen/page.rb', line 87 def stylesheets(filename=nil) search_grid_files(["css"], filename) end |
#underscore_hash_keys(hash) ⇒ Object
111 112 113 114 115 116 117 118 |
# File 'app/models/concen/page.rb', line 111 def underscore_hash_keys(hash) new_hash = {} hash.each do |key, value| value = underscore_hash_keys(value) if value.is_a?(Hash) new_hash[key.gsub(" ","_").downcase.to_sym] = value end new_hash end |