I’m displaying records in a grid format from an ActiveRecord list using each_slice. What I want to do is randomly insert items into this grid.
My code currently looks something like:
<%=things.each_slice(3) do |row|%>
<div class="row">
<%row.each do |listing|%>
<%=show_cell(listing)%>
<%end%>
</div>
<%end%>
Which produces a grid like so:
+----+----+----+
|cell|cell|cell|
+----+----+----+
|cell|cell|cell|
+----+----+----+
|cell|cell|cell|
+----+----+----+
What I want to do is have something like
+----+----+----+
|cell|cell|cell|
+----+----+----+
|cell|xxxx|cell|
+----+----+----+
|cell|cell|xxxx|
+----+----+----+
Where the cells come from the things collection of records, and the xxxx’s are randomly inserted (10% chance)
Is there a “ruby” way of doing this without getting rid of the slicing method, and manually keeping a count of when to create new rows?
Make a new array of mixed Things and Xxxs:
Then you can do:
<%=mixed_stuff.each_slice(3) do |row|%> etc…
Then in the show_cell(listing) method you can do
if listing.is_a?(Thing)... else listing.is_a?(Xxx)...