Created
May 20, 2013 14:10
-
-
Save agius/5612491 to your computer and use it in GitHub Desktop.
An application_helper function to output stuff in a metro-like format for bootstrap.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def metroify(collection, partial, opts = {}) | |
| return "" unless collection.present? | |
| output = "" | |
| coll_copy = collection.dup | |
| renderize = lambda do |obj, tile| | |
| locals = {tile: tile} | |
| locals = locals.merge(opts[:locals]) if opts[:locals] | |
| render_opts = opts.dup.merge(partial: partial, object: obj, locals: locals) | |
| render(render_opts).to_s | |
| end | |
| begin | |
| row_height = [2, 4, 4, 4].sample | |
| if row_height == 2 | |
| output << "<div class='row'>" | |
| 6.times do | |
| obj = coll_copy.shift | |
| next unless obj.present? | |
| output << "<div class='span2 tile2'>" << renderize.call(obj, 'tile2') << "</div>" | |
| end | |
| output << "</div>" | |
| else | |
| output << "<div class='row'>" | |
| 3.times do | |
| colset_type = ['big_square', 'wide_top', 'wide_bottom', 'tiles'].sample | |
| case colset_type | |
| when 'big_square' | |
| output << "<div class='span4 tile4'>" | |
| obj = coll_copy.shift | |
| output << renderize.call(obj, 'tile4') if obj.present? | |
| output << "</div>" | |
| when 'wide_top' | |
| output << "<div class='span4'>" | |
| output << "<div class='row'>" | |
| output << "<div class='span4 tile4-wide'>" | |
| obj = coll_copy.shift | |
| output << renderize.call(obj, 'tile4-wide') if obj.present? | |
| output << "</div>" | |
| output << "</div>" | |
| output << "<div class='row'>" | |
| 2.times do | |
| output << "<div class='span2 tile2'>" | |
| obj = coll_copy.shift | |
| output << renderize.call(obj, 'tile2') | |
| output << "</div>" | |
| end | |
| output << "</div>" | |
| output << "</div>" | |
| when 'wide_bottom' | |
| output << "<div class='span4'>" | |
| output << "<div class='row'>" | |
| 2.times do | |
| output << "<div class='span2 tile2'>" | |
| obj = coll_copy.shift | |
| output << renderize.call(obj, 'tile2') | |
| output << "</div>" | |
| end | |
| output << "</div>" | |
| output << "<div class='row'>" | |
| output << "<div class='span4 tile4-wide'>" | |
| obj = coll_copy.shift | |
| output << renderize.call(obj, 'tile4-wide') | |
| output << "</div>" | |
| output << "</div>" | |
| output << "</div>" | |
| when 'tiles' | |
| output << "<div class='span4'>" | |
| 2.times do | |
| output << "<div class='row'>" | |
| 2.times do | |
| output << "<div class='span2 tile2'>" | |
| obj = coll_copy.shift | |
| output << renderize.call(obj, 'tile2') | |
| output << "</div>" | |
| end | |
| output << "</div>" | |
| end | |
| output << "</div>" | |
| end | |
| end | |
| output << "</div>" | |
| end | |
| end while coll_copy.present? | |
| output.html_safe | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment