Skip to content

Instantly share code, notes, and snippets.

View pier-oliviert's full-sized avatar

Pier-Olivier Thibault pier-oliviert

View GitHub Profile
class Post
end
class Tag
def posts
post_scope = ->(tag) { where '? = ANY (posts.tags)', tag.id }
options = {}
reflection = ActiveRecord::Reflection::HasManyReflection.new(:posts, post_scope, options, self.class)
association = PostsAssociation.new(self, reflection)
@posts ||= ActiveRecord::Associations::CollectionProxy.new(Post, association)
end
private
var div = document.createElement('div')
div.innerHTML = "<script></script>"
var s = div.children[0]
s.src = "//connect.facebook.net/en_US/all.js#xfbml=1"
// true
s instanceof HTMLScriptElement
// Will NOT load the script
document.head.appendChild(s)
link_to meal_ingredients_path(meal) do |link|
if meal.ingredients.need > 0
"Liste d'épicerie &mdash; il vous manque #{meal.ingredients.need} ingrédients"
else
link[:class] << 'complete'
"Vous avez tous les ingrédients!"
end
end
where("logs.products ?| ARRAY[:ids]", ids: products.map(&:id).uniq.map(&:to_s))
<!-- This work for a new contact, but will not for an existing contact -->
<%= form_for [contact.client, contact] do |f| %>
<% end %>
<!-- This work for an existing contact, but will not for a new contact -->
<%= form_for [contact] do |f| %>
<% end %>
class FollowersController < ApplicationController
before_action :authenticate!
def index
@pictures = current_user.followers.pictures
end
end
@pier-oliviert
pier-oliviert / popup.coffee
Created November 28, 2014 16:19
How to handle a popup with fixed position's content behind, like Facebook's dialog.
class Popup
loaded: =>
@on 'dates:index', @show
show: (e) =>
@scrollTop = document.body.scrollTop
document.body.appendChild(e.HTML)
main = document.body.querySelector('main')
main.classList.add('overlayed')
main.style.top = "-#{@scrollTop}px"
e.HTML = "<%= j content_tag(:div, render('list', posts: @posts), class: %w(popup)) %>".toHTML()
module ContentTagHelper
def content_tag(*args)
if block_given?
tag = Tag.new(args[0], args[1] || {})
old_buf = @output_buffer
@output_buffer = ActionView::OutputBuffer.new
value = yield(tag)
content = tag.render(@output_buffer.presence || value)
@output_buffer = old_buf
content