Skip to content

Instantly share code, notes, and snippets.

View lulalala's full-sized avatar

lulalala lulalala

View GitHub Profile
@lulalala
lulalala / banner.rb
Last active January 14, 2019 12:58 — forked from matheusvetor/banner.rb
Carrierwave image model validation on image dimensions/height/width.
class Banner < ActiveRecord::Base
attr_accessor :image_width, :image_height
mount_uploader :image, ImageUploader
validate :check_dimensions, :on => :create
def check_dimensions
if !image_cache.nil? && (image.width < 300 || image.height < 300)
errors.add :image, "Dimension too small."
end
end
@lulalala
lulalala / nokogiri_ext.rb
Created June 28, 2013 03:54
Every web crawler using Nokogiri should have this. This simplifies the page fetching call, and adds redirected url automatically so you can reference that url later (like when you have to compute absolute url out of relative href values).
require 'open-uri'
class Nokogiri::HTML::Document
# Use open-uri to get document and set url accordingly.
def self.get(url)
raw = open(url)
parse(raw, raw.base_uri.try(:to_s)) # handles redirected url
end
end
module Nokogiri::HTML
def self.get(url)
@lulalala
lulalala / ConditionalValidations.rb
Last active December 17, 2015 13:39 — forked from ibanez270dx/ConditionalValidations.rb
Add association conditional validation
#
# At CoverHound, we use conditional validations all over the form. However, there is no proper way to do
# this in Rails. Instead, we can provide an array of attributes (validated_fields attribute)
# and ensure they are the only ones to get validated.
#
module ConditionalValidations
attr_accessor :validated_fields
def field_is_required?(field)
@lulalala
lulalala / redirect_if_mobile
Created May 6, 2013 09:25
Javascript to redirect to mobile subdomain, when using Ernie Miller's methods: http://erniemiller.org/2011/01/05/mobile-devices-and-rails-maintaining-your-sanity/
if (document.cookie.indexOf("full_site=1") === -1 && navigator.userAgent.match(/iPhone|iPod|iPad|Android/i)) {
window.location = document.URL.replace(/:\/\//, '://m.');
}
@lulalala
lulalala / wretch_to_mt.xslt
Last active December 12, 2015 01:38
能把無名小站的 xml 備份轉換成 Moveable Type 格式的 xslt 檔案
<xsl:stylesheet version="1.0"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xhtml="http://www.w3.org/1999/xhtml"
>
<!-- lulalala 2008/10/16 created -->
<xsl:output method="text" encoding="UTF-8"/>
@lulalala
lulalala / rspecgrep.sh
Created November 2, 2012 09:36
rspec with grep to run selected specs
function rspecgrep()
{
SPK_FILES=`find spec -type f -name "*_spec.rb" | grep $1`
echo $SPK_FILES
time rspec $SPK_FILES --format documentation
}
@lulalala
lulalala / gist:3652906
Created September 6, 2012 08:15
台灣住址行政區域分類
class TaiwaneseAddress
def initialize(address)
@original_address = address.clone()
@levels = prepare_level()
@levels.each do |level|
level.keys.each do |region|
if match = address[/^(.{1,4}#{region})/,1]
level[region] = match
@lulalala
lulalala / gist:3607450
Created September 3, 2012 06:56
Mysql nested query slower than two distinct query
SELECT `topics`.* FROM `topics` WHERE `topics`.`id` IN (
SELECT DISTINCT `topic_comments`.`topic_id` FROM `topic_comments` WHERE (created_at > '2011-11-08 04:56:58') ORDER BY `topic_comments`.`created_at` DESC)
LIMIT 10 OFFSET 0;
SELECT DISTINCT `topic_comments`.`topic_id` FROM `topic_comments` WHERE (created_at > '2011-11-08 04:56:58') ORDER BY `topic_comments`.`created_at` DESC;
select topics.* from topics where topics.id in (45756,45334,44759,44799,44769,44697,43755,43445,43591,43754,42620,42643,42683,41333,42277,41678,41868,41610,41530,525,41336,41066,41067,41068,40596,40800,40575,40264,40292,40038,39744,39348,40082,39859,39702,40039,39532,38440,38658,8346,38218,37760,33297,37123,37353,37002,37089,36948,36870,36617,36727,16621,25468,25465,16972,8741,9604,17498,14175,14898,26513,26143,26058,10957,8799,35054,36728,36430,36393,36395,35816,7642,36278,36279,36097,24377,33299,35087,35053,32413,34660,34489,34510,33823,34077,33873,33557,32656,32600,33123,30970,32880,32924,116,29133,32274,32409,32104,32148,30
@lulalala
lulalala / gist:3377911
Created August 17, 2012 10:50
Update pacman
pacman -S gnupg
pacman -Sy
pacman -S systemd-tools --force
pacman -S pacman --force
sudo pacman-key --init; sudo pacman-key --populate archlinux
@lulalala
lulalala / ender_duplicate_remove
Created August 16, 2012 08:18
Remove duplicate ender
set = Set.new()
i = 0
Delayed::Job.order('id DESC').where('queue = "ender"').find_each do |d|
if not (set.add?( d.handler[/\d+/].to_i ))
d.destroy
i += 1
end
end