class Article

Public Instance Methods

as_indexed_json(*) click to toggle source
# File lib/buweb/article.rb, line 91
def as_indexed_json(*)
  {
    title: title,
    slug: slug,
    body: body,
    sanitized_body: sanitized_body,
    audience: audience.to_a.map{|x| x.to_s.downcase.strip.presence }.compact,
    teaser: teaser,
    subtitle: subtitle,
    topics: topics,
    topic_slugs: topics.to_a.map(&:parameterize),
    press_release: press_release,
    author_ids: authors.map(&:id).map(&:to_s), # Use map since authors already get loaded
    author_names: authors.map(&:name), # name is a method that needs to be called
    author_slugs: authors.map(&:slug),
    site_id: site.try(:id).try(:to_s),
    categories: site_categories.to_a.map(&:to_s),
    category_slugs: site_categories.to_a.map(&:slug),
    image_x_small_url: image.x_small.url,
    image_small_url: image.small.url,
    image_medium_url: image.medium.url,
    image_large_url: image.large.url,
    image_x_large_url: image.x_large.url,
    image_xx_large_url: image.xx_large.url,
    image_url: image.url,
    publish_at: publish_at,
    external_url: external_url
  }
end
dont_index?() click to toggle source
# File lib/buweb/article.rb, line 136
def dont_index?
  !published?
end
set_slug() click to toggle source

sets slug from title

# File lib/buweb/article.rb, line 82
def set_slug
  return unless title?
  appendage = nil
  while ::Article.where(slug: "#{title.parameterize}#{appendage}").present?
    appendage.nil? ? appendage = 1 : appendage += 1
  end
  self.slug = "#{title.parameterize}#{appendage}"
end
to_s() click to toggle source
# File lib/buweb/article.rb, line 77
def to_s
  title
end
topics_string() click to toggle source
# File lib/buweb/article.rb, line 69
def topics_string
  topics.join(', ') if topics.present?
end
topics_string=(string) click to toggle source
# File lib/buweb/article.rb, line 73
def topics_string=(string)
  self.topics = string.to_s.split(',').map(&:titleize).map(&:strip)
end

Private Instance Methods

author_or_ws_author() click to toggle source
# File lib/buweb/article.rb, line 152
def author_or_ws_author
  return if ws_author.present? || authors.present?
  errors.add :base, 'Must have at least one author or '\
                    'a legacy author from ws.'
end
increment_ws_id() click to toggle source
# File lib/buweb/article.rb, line 158
def increment_ws_id
  return unless ws_id.blank? && !imported
  self.ws_id = Article.where(imported: false).max(:ws_id).to_i + 1
end
non_imported_site_presence() click to toggle source
# File lib/buweb/article.rb, line 142
def non_imported_site_presence
  return unless !imported && site_id.blank?
  errors.add :base, 'Site is required.'
end
sanitized_body() click to toggle source
# File lib/buweb/article.rb, line 163
def sanitized_body
  body.gsub(%r{</?[^>]+?>}, ' ').squeeze(' ') if body.present?
end
site_categories_type() click to toggle source
# File lib/buweb/article.rb, line 147
def site_categories_type
  return unless site_categories&.ne(type: 'Article').present?
  errors.add :base, 'All categories must be for articles.'
end