Skip to content
Snippets Groups Projects
post.rb 1 KiB
Newer Older
Arnaud Levy's avatar
Arnaud Levy committed
class Communication::Block::Template::Post < Communication::Block::Template
Arnaud Levy's avatar
wip
Arnaud Levy committed
  def build_git_dependencies
Arnaud Levy's avatar
Arnaud Levy committed
    add_dependency category unless category.nil?
Arnaud Levy's avatar
Arnaud Levy committed
    selected_posts.each do |post|
      add_dependency post
Arnaud Levy's avatar
Arnaud Levy committed
      add_dependency post.active_storage_blobs
      if post.author.present?
Arnaud Levy's avatar
Arnaud Levy committed
        add_dependency post.author
        add_dependency post.author.author
        add_dependency post.author.active_storage_blobs
Arnaud Levy's avatar
Arnaud Levy committed
      end
    end
Arnaud Levy's avatar
wip
Arnaud Levy committed
  end

  def category
    @category ||= block.about&.website.categories.find_by(id: block.data['category_id'])
  end

Arnaud Levy's avatar
Arnaud Levy committed
  def selected_posts
    @selected_posts ||= category.nil? ? free_posts : category_posts
Arnaud Levy's avatar
wip
Arnaud Levy committed
  end

  protected

  def category_posts
    quantity = block.data['posts_quantity'] || 3
    category.posts.ordered.limit(quantity)
  end

  def free_posts
Arnaud Levy's avatar
Arnaud Levy committed
    array = []
    elements.map do |element|
Arnaud Levy's avatar
wip
Arnaud Levy committed
      id = element['id']
      next if id.blank?
      post = block.about&.website.posts.find_by id: id
      next if post.nil?
Arnaud Levy's avatar
Arnaud Levy committed
      array << post
Arnaud Levy's avatar
wip
Arnaud Levy committed
    end
Arnaud Levy's avatar
Arnaud Levy committed
    array
Arnaud Levy's avatar
wip
Arnaud Levy committed
  end
end