Skip to content
Snippets Groups Projects
post.rb 1.12 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?
    add_dependency selected_posts
Arnaud Levy's avatar
Arnaud Levy committed
    selected_posts.each do |post|
Arnaud Levy's avatar
Arnaud Levy committed
      add_dependency post.active_storage_blobs
      if post.author.present?
        add_dependency [post.author, post.author.author]
Arnaud Levy's avatar
Arnaud Levy committed
        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: data['category_id'])
Arnaud Levy's avatar
wip
Arnaud Levy committed
  end

Arnaud Levy's avatar
Arnaud Levy committed
  def selected_posts
Arnaud Levy's avatar
Arnaud Levy committed
    @selected_posts ||= send "selected_posts_#{kind}"
Arnaud Levy's avatar
wip
Arnaud Levy committed
  end

  protected

alexisben's avatar
alexisben committed
  def kind
    @kind ||= data['kind'] || 'all'
  end

Arnaud Levy's avatar
Arnaud Levy committed
  def selected_posts_all
alexisben's avatar
alexisben committed
    quantity = data['posts_quantity'] || 3
    block.about&.website.posts.ordered.limit(quantity)
  end

Arnaud Levy's avatar
Arnaud Levy committed
  def selected_posts_category
    quantity = data['posts_quantity'] || 3
Arnaud Levy's avatar
wip
Arnaud Levy committed
    category.posts.ordered.limit(quantity)
  end

Arnaud Levy's avatar
Arnaud Levy committed
  def selected_posts_free
Arnaud Levy's avatar
Arnaud Levy committed
    elements.map { |element| post(element['id']) }
            .compact
Arnaud Levy's avatar
wip
Arnaud Levy committed
  end
Arnaud Levy's avatar
Arnaud Levy committed

  def post(id)
    return if id.blank?
    block.about&.website.posts.find_by id: id
  end
Arnaud Levy's avatar
wip
Arnaud Levy committed
end