Skip to content

Instantly share code, notes, and snippets.

@laurynas
laurynas / watermark.rb
Created November 20, 2010 19:27
Paperclip Watermark processor
# Based on
# https://github.com/ng/paperclip-watermarking-app/blob/master/lib/paperclip_processors/watermark.rb
# Modified by Laurynas Butkus
module Paperclip
class Watermark < Processor
# Handles watermarking of images that are uploaded.
attr_accessor :format, :whiny, :watermark_path, :position
def initialize file, options = {}, attachment = nil
@laurynas
laurynas / mail_delivery_catchall.rb
Created November 24, 2010 09:50
Add option to deliver all mails to one address. Useful in staging environment.
# Add option to deliver all mails to one address. Useful in staging environment.
# config.action_mailer.delivery_method = :catchall
module ActionMailer
class Base
def perform_delivery_catchall(mail)
original_to = mail.to
mail.to = "youremail@yourdomain"
mail.cc = ""
@laurynas
laurynas / item.rb
Created November 26, 2010 10:00
SearchLogic problem with left join in order SOLVED!
class Item < ActiveRecord::Base
named_scope :ascend_by_optional_category_title,
:joins => "LEFT JOIN categories AS optional_categories ON optional_categories.id=items.category_id",
:order => "optional_categories.title"
named_scope :descend_by_optional_category_title,
:joins => "LEFT JOIN categories AS optional_categories ON optional_categories.id=items.category_id",
:order => "optional_categories.title DESC"
@laurynas
laurynas / globalize3_hash_setter_patch.rb
Created November 26, 2010 23:11
Allow setting hash values for Globalize3 (hash keys - locale codes)
module Globalize
module ActiveRecord
class Adapter
def write_with_hash(locale, name, value)
if value.is_a?(Hash)
value.each do |locale, value|
self.write_without_hash(locale, name, value)
end
else
@laurynas
laurynas / fix_html_helper.rb
Created November 29, 2010 08:44
Fix invalid html using Nokogiri
# Close unclosed tags
def fix_invalid_html(html)
Nokogiri::HTML::DocumentFragment.parse(html).to_html
end
@laurynas
laurynas / will_paginate_jquery_mobile_renderer.rb
Created December 30, 2010 16:12
Rails3 will_paginate Jquery Mobile renderer
class WillPaginateJqueryMobileRenderer < WillPaginate::ViewHelpers::LinkRenderer
def container_attributes
super.merge({
:'data-role' => 'navbar'
})
end
def pagination
@options[:page_links] ? windowed_page_numbers : []
@laurynas
laurynas / mail_delivery_catchall.rb
Created January 6, 2011 18:32
Add option to deliver all ActionMailer mails to one address. Useful in staging environment.
# Initializer:
# require 'mail_delivery_catchall.rb' if Rails.env.staging?
# Rails 3
module ActionMailer
class Base
class << self
def deliver_mail_with_catchall(mail, &block)
@laurynas
laurynas / form_builder_multi_locale_patch.rb
Created January 14, 2011 13:33
Multi locale field builder
# depends on i18n_multi_locales_form plugin
# rails plugin install https://github.com/ZenCocoon/i18n_multi_locales_form.git
module MultiLocaleFieldBuilder
def multi_locale_field(method, opts = {})
opts = {
:type => :text_field
}.merge(opts)
type = opts.delete(:type)
module RenderRemoteHelper
# Generate javascript for rendering remote resource
# * +url+ - resource url
# * +view+ - element id to update
def render_remote(url, view = 'remote', options = {})
options[:update] = view
options[:url] = url
options[:method] = :get
render :inline => %{<span id="#{view}"><p>Loading...</p><script
@laurynas
laurynas / vodafone360_to_vcf.rb
Created January 18, 2011 20:18
Convert vodafone360 contacts html to VCF
# Convert vodafone360 contacts html to VCF
require 'rubygems'
gem 'nokogiri'
gem 'vpim'
require 'nokogiri'
require 'vpim/vcard'
Dir.glob("html/*.html").each do |file|
doc = Nokogiri.parse(File.read(file))