Skip to content

Instantly share code, notes, and snippets.

View Agowan's full-sized avatar

Niklas Lundgren Agowan

  • Boulebar
  • Stockholm Sweden
View GitHub Profile
@Agowan
Agowan / bug-report.rb
Created September 11, 2012 16:46
Squeel bug report (duplicated "order by" when using default_scope inherited to a STI sub class)
# encoding: utf-8
gem 'sqlite3', '1.3.6'
gem 'activerecord', '3.2.8'
gem 'squeel', '1.0.11'
require 'active_record'
require 'squeel'
require 'squeel/version'
@Agowan
Agowan / test.rb
Created September 11, 2012 18:09
Without using squeel, default scope with sorting inherited to STI sub class
# encoding: utf-8
gem 'sqlite3', '1.3.6'
gem 'activerecord', '3.2.8'
require 'active_record'
require 'logger'
puts "Active Record #{ActiveRecord::VERSION::STRING}"
@Agowan
Agowan / album_form.rb
Last active April 5, 2016 20:50
A simple way to use has_many between form objects using Virtus.The problem I had was to get validations working with fields_for in the view, but still have the flexibility and full control using virtus instead of active record models.And I added a way of checking for sanitised args in rails 4.
# encoding: utf-8
class AlbumForm < BaseForm
has_many :songs, class_name: 'SongForm'
validates :songs, form_collection: true
end
@Agowan
Agowan / context.rb
Last active August 29, 2015 13:56
A way of encapsulate custom sql within objects and of course it's possible to make really clean with more inheritance.
# encoding: utf-8
class Context
def sanitize_sql(array)
self.class.sanitize_sql(array)
end
def select_all(sql)
self.class.select_all(sql)
@Agowan
Agowan / context.rb
Last active August 29, 2015 13:58
A simple way of handling custom sql queries without involving ActiveRecord. (Using postgresql)
# encoding: utf-8
class Context
class Context::Result
def initialize(*args, &block)
args.extract_options!.each do |attr, value|
send("#{attr}=", numeric?(value)) if respond_to?("#{attr}=")
end
# encoding: utf-8
class Article
attr_accessor :title, :body
def initalize(title, body)
self.title = title
self.body = body
end
end
@Agowan
Agowan / heroku_scaler.rake
Created April 15, 2015 06:39
How to schedule heroku prcesses.
# encoding: utf-8
require 'heroku-api'
namespace :heroku do
desc "Scale down the processes during business hours"
task :spin_down => :environment do
app = ENV['HEROKU_APP']
heroku = Heroku::API.new(:api_key => ENV['HEROKU_API_KEY'])
@Agowan
Agowan / email.rb
Created May 6, 2015 12:32
Store raw email in attachment with rails
# encoding: UTF-8
class Email < ActiveRecord::Base
has_attached_file :raw
after_save :remove_file
# ===========================================================================
# Public instance methods
@Agowan
Agowan / string_file.rb
Created May 6, 2015 12:38
Override StringIO content type
# encoding: utf-8
class StringFile < StringIO
def initialize(file_content, file_name, file_type)
super(file_content)
@file_name = file_name
@file_type = file_type
end
def data
@data ||= File.read pdf_template_path
end
def pdf_template_path
@tmp_path ||= @pdf_template.template.tap { |u| u.cache_stored_file! }.path
end
def cleanup
File.delete(pdf_template_path) if File.exist?(pdf_template_path)