Skip to content

Instantly share code, notes, and snippets.

# app/views/layouts/application.html.erb
<%= render_stylesheets content_for(:stylesheets) %>
# app/helpers/layout_helper.rb
def render_stylesheets(tag_html = nil)
html = []
if tag_html.present?
html << tag_html
else

Mature engineers have an innate sense of anticipation, even if they don’t know they do.

how long will it last before it’s rewritten? Once it’s in production, how will its execution affect resource usage? How much so I expect CPU/memory/disk/network to increase or decrease? Will others be able to understand this code? Am I making it as easy as I can for others to extend or introspect this work?

@ilake
ilake / validaation.rb
Last active May 14, 2017 22:16
Mastering Rails Validations: Contexts
# http://blog.arkency.com/2014/04/mastering-rails-validations-contexts/
# http://blog.arkency.com/2014/05/mastering-rails-validations-objectify/
# The idea was, that it should not be possible to delete user who already took part of some important business activity.
class User < ActiveRecord::Base
has_many :invoices
validate :does_not_have_any_invoice, on: :destroy
def destroy
transaction do
@ilake
ilake / tips_for_asset_pipeline.md
Last active August 29, 2015 14:01
12 Tips for the Rails Asset Pipeline

http://www.reinteractive.net/posts/116-12-tips-for-the-rails-asset-pipeline

The asset pipeline is not quite your assets folder

  • Every file that is not a Javascript file or CSS file that is in the app/assets folder will be copied by Rails into the public/assets folder when you compile your assets.

Don't fall back in staging or production

  • config.assets.compile
  • If it is set to "true" (which it is by default in development) then Rails will try to find a Javascript or CSS file by first looking in the public/assets directory and if it can't find it, will hunt through your app/assets folder looking for the file. If it finds it in app/assets it will go ahead and compile on the fly and then serve this asset up.
@ilake
ilake / render.rb
Last active October 20, 2015 18:47
render view not in controller
# http://makandracards.com/makandra/17751-render-a-view-from-a-model-in-rails
# https://github.com/yappbox/render_anywhere
# http://stackoverflow.com/questions/18263220/rendering-partials-view-in-a-rake-task-background-job-model-in-rails-4
# http://ruby-china.org/topics/12193
# http://alphahydrae.com/2013/06/rails-3-rendering-views-outside-a-controller/
def render_to_string(options = {})
av = ActionView::Base.new(ActionController::Base.view_paths)
av.extend TranslationHelper
av.render(:template => "contacts/index.pdf.erb",
@ilake
ilake / pricne.rb
Last active August 29, 2015 14:01
princely study
# lib/princely/asset_support.rb
# Rails.application.assets.find_asset is useful to find asset.
# you could get the content through
# Rails.application.assets.find_asset('analytics.css.sass').body
# get pathname through
# Rails.application.assets.find_asset('analytics.css.sass').pathname
def asset_file_path(asset)
# Remove /assets/ from generated names and try and find a matching asset
Rails.application.assets.find_asset(asset.gsub(%r{/assets/}, "")).try(:pathname) || asset
end
# Set the prefix to ^A.
unbind C-b
set -g prefix ^a
set -g base-index 1
# other ^A
unbind ^A
bind ^A last-window
#!/usr/bin/env ruby
# require 'rubygems'
require 'mechanize'
require 'debugger'
require 'sidekiq'
# Sidekiq server is multi-threaded so our Redis connection pool size defaults to concurrency (-c)
Sidekiq.configure_server do |config|
config.redis = { :namespace => 'treehouse', :url => 'redis://127.0.0.1:6379' }
end
# The user order I want
ids = [1, 3, 5, 9, 6, 2]
indexed_people = Person.find(ids).index_by(&:id)
people_in_order = indexed_people.values_at(*ids)
@ilake
ilake / Money.md
Last active December 25, 2015 08:19
require "i18n" rescue LoadError
module Money::Currency::Loader
  extend self

  DATA_PATH = File.expand_path("../../../../config", __FILE__)