rails delete_if using hashes to ignore current article (Middleman)
Asked Answered
N

1

6

I've got an easy one for you guys.

I want to have a featured content section where the current article is EXCLUDED

So this works using Middleman Blog with delete_if:

<% blog(content).articles.delete_if{|item| item == current_article}.each do |article| %>
  <%= article_content %>
<% end %>

However I'm using Middleman proxies so I don't have access to the current_article method...

I've got an YAML structure that holds the following mock data (amongst others) with the folder setup like: data > site > caseStudy > RANDOM-ID423536.yaml (Generated by a CMS)

enter image description here

Inside each yaml file you'll find stuff like:

:id: 2k1YccJrQsKE2siSO6o6ac
:title: Heyplace

My config.rb looks like this

data.site.caseStudy.each do | id, this |
  proxy "/cases/#{this.slug}/index.html", "/cases/cases-template.html", :locals => { this: this }, :ignore => true
end

My template file holds the following

<%= partial(:'partials/footer', :locals => {:content => data.site.caseStudy, :title => 'See more projects'}) %>

My loop that gets content

<% content.each do |id, this| %>
  <%= partial "partials/tile" %>
<% end %>

So I guess my question is, how can I use delete_if in this instance so that the current article isnt being displayed?

I have tried stuff like, but to no avail:

<% content.delete_if{|e| e.title == content.title}.each do |id, this| %>
  <%= partial "partials/tile" %>
<% end %>

Any help is appreciated! Thank you!

Solved, ended up doing

<% content.reject{ | id, item| item == this }.each do |id, this| %>
<%= partial "partials/tile", :locals => { :this => this, :dir => "/#{this.content_type_id.downcase}/", :secondary => 'View' } %>
<% end %>

Courtesy of @David Litvak

Northwester answered 19/4, 2017 at 23:13 Comment(0)
M
4

I'm the maintainer of contentful_middleman. Looks like you could just send the this to the partial as local, then iterate through your content and make sure that you exclude the one that matches the id of this.

Miramontes answered 19/4, 2017 at 23:31 Comment(2)
Hi David, thanks for taking your time to help me. Do you know roughly how I would do that with hashes (from contentful)? I'm not super familiar with how to do that. With arrays it's a bit easierNorthwester
The following kind of worked, but now they are getting deleted in the cache. I have to reload the page a couple of times to get the tiles to reappear. Very strange <% content.reject{|key, value| key >= this.id }.sort_by{ |index| rand }.each do |id, this| %> <%= partial "partials/tile" %> <% end %>Northwester

© 2022 - 2024 — McMap. All rights reserved.