Class: Concen::Page

Inherits:
Object
  • Object
show all
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, :ancestor_slugs, :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, :ancestor_slugs]

Instance Method Summary collapse

Instance Method Details

#authors_as_userObject



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'app/models/concen/page.rb', line 222

def authors_as_user
  users = []
  for author in self.authors
    if author.is_a?(String)
      if user = User.where(:username => author).first
        users << user
      elsif user = User.where(:email => author).first
        users << user
      elsif user = User.where(:full_name => author).first
        users << user
      end
    else
      users << user if User.where(:_id => author).first
    end
  end
  return users
end

#content_in_html(key = "main", data = {}) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'app/models/concen/page.rb', line 73

def content_in_html(key = "main", data={})
  html = nil

  if content = self.content.try(:[], key)
    # Parse mustache first.
    content = Mustache.render(content, data)

    # Parse markdown.
    markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML, Concen.markdown_extensions)
    html = markdown.render content

    # Parse smartypants.
    if Concen.parse_markdown_with_smartypants
      # Temporary hack to fix smartypants bug in Redcarpet 2.0.0b5.
      html.gsub!("&#39;", "'")

      html = Redcarpet::Render::SmartyPants.render html
    end
  end

  return html
end

#first?(*args) ⇒ Boolean

Returns:

  • (Boolean)


182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'app/models/concen/page.rb', line 182

def first?(*args)
  options = args.extract_options!
			children = self.parent.children
			children = children.published if options[:only_published]
			if options[: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



96
97
98
# File 'app/models/concen/page.rb', line 96

def images(filename=nil)
  search_grid_files(["png", "jpg", "jpeg", "gif"], filename)
end

#javascripts(filename = nil) ⇒ Object



104
105
106
# File 'app/models/concen/page.rb', line 104

def javascripts(filename=nil)
  search_grid_files(["js"], filename)
end

#last?(*args) ⇒ Boolean

Returns:

  • (Boolean)


202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'app/models/concen/page.rb', line 202

def last?(*args)
  options = args.extract_options!
			children = self.parent.children
			children = children.published if options[:only_published]
			if options[: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



169
170
171
172
173
174
175
176
177
178
179
180
# File 'app/models/concen/page.rb', line 169

def next(*args)
			options = args.extract_options!
			children = self.parent.children
			children = children.published if options[:only_published]
			if options[: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



108
109
110
111
112
113
114
# File 'app/models/concen/page.rb', line 108

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



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

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



156
157
158
159
160
161
162
163
164
165
166
167
# File 'app/models/concen/page.rb', line 156

def previous(*args)
  options = args.extract_options!
			children = self.parent.children
			children = children.published if options[:only_published]
			if options[: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

Returns:

  • (Boolean)


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

def published?
  self.publish_time.present?
end

#search_grid_files(extensions, filename = nil) ⇒ Object



116
117
118
119
120
121
122
# File 'app/models/concen/page.rb', line 116

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



100
101
102
# File 'app/models/concen/page.rb', line 100

def stylesheets(filename=nil)
  search_grid_files(["css"], filename)
end

#underscore_hash_keys(hash) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
# File 'app/models/concen/page.rb', line 124

def underscore_hash_keys(hash)
  if hash.is_a? 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
  else
    return nil
  end
end