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 / 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'])
# encoding: utf-8
class Article
attr_accessor :title, :body
def initalize(title, body)
self.title = title
self.body = body
end
end
@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
@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 / 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 / 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 / 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'