Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rails delete_if using hashes to ignore current article (Middleman)

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

like image 457
umbriel Avatar asked Apr 19 '17 23:04

umbriel


1 Answers

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.

like image 66
David Litvak Avatar answered Nov 04 '22 14:11

David Litvak