Skip to content

Instantly share code, notes, and snippets.

@rywall
rywall / gist:2039089
Created March 14, 2012 19:59
Identity Map Bug Fix
gem 'rails', :git => 'git://github.com/rails/rails.git', :ref => 'c1f397f82c7b3f352500832a399b5dbdc500b9c8'# change as required
require 'active_record'
require 'logger'
# Show ActiveRecord log output
ActiveRecord::Base.logger = Logger.new(STDOUT)
# Print out what version we're running
puts "Active Record #{ActiveRecord::VERSION::STRING}"
@rywall
rywall / gist:2130349
Created March 20, 2012 02:35
Only store in IM if all attributes are present
gem 'rails', :git => 'git://github.com/rails/rails.git', :ref => 'c1f397f82c7b3f352500832a399b5dbdc500b9c8'# change as required
require 'active_record'
require 'logger'
# Show ActiveRecord log output
ActiveRecord::Base.logger = Logger.new(STDOUT)
# Print out what version we're running
puts "Active Record #{ActiveRecord::VERSION::STRING}"
[50, 100, 200, 500, 1000, 5000].each do |buffer|
puts "Calculating population within #{buffer} m of each shape_geometry"
Shape.find_each do |s|
execute "UPDATE shapes SET pop#{buffer}m =
(SELECT SUM((ST_SummaryStats(ST_Clip(rast,1,ST_Buffer(geom::geography, #{buffer})::geometry))).sum)
FROM shapes, population_tiles
WHERE id = #{s.id} AND ST_Intersects(ST_Buffer(geom::geography, #{buffer})::geometry,rast)
GROUP BY id)
WHERE id = #{s.id}"
end
DISPERSION_FACTORS = {50 => 0.5341, 100 => 0.3610, 200 => 0.5145, 500 => 0.9159, 1000 => 0.9088, 5000 => 4.662}
# Calculate the intake fraction where iF = SUM(popN * DispN) / 1000
# where popN is the population in an N meter buffer minus the previous population buffer
def intake_fraction
fraction = 0
inner_pop = 0
DISPERSION_FACTORS.each do |buffer, value|
pop_in_buffer = send("pop#{buffer}m").to_i
fraction += (pop_in_buffer - inner_pop) * value * 0.62 * 14.5
# Calculate the intake fraction where iF = SUM(popDensN * DispN)
# where popDensN is the population density in an N meter buffer minus the previous buffer
def intake_fraction
fraction = 0
inner_pop = 0
inner_area = 0
DISPERSION_FACTORS.each do |buffer, value|
pop_in_buffer = send("pop#{buffer}m").to_i
area_in_buffer = send("area#{buffer}m").to_i
class CreateModels < ActiveRecord::Migration
def change
create_table :items do |t|
end
end
end
class Item < ActiveRecord::Base
end
@rywall
rywall / test.rb
Created December 4, 2014 01:52
hm:t association with order clause that references join table cannot be eager loaded
unless File.exist?('Gemfile')
File.write('Gemfile', <<-GEMFILE)
source 'https://rubygems.org'
gem 'rails', github: 'rails/rails'
gem 'arel', github: 'rails/arel'
gem 'rack', github: 'rack/rack'
gem 'i18n', github: 'svenfuchs/i18n'
gem 'sqlite3'
GEMFILE
@rywall
rywall / en.yml
Created August 6, 2015 23:45
RRN CMS Translation
en:
new: "New"
edit: "Edit"
delete: "Delete"
cancel: "Cancel"
import: "Import"
error: "Error"
toggle_navigation: "Toggle Navigation"
@rywall
rywall / blinder.command
Last active May 17, 2018 13:08
Ruby Script to randomize filenames
#!/usr/bin/env ruby
require 'csv'
Dir.chdir File.dirname(__FILE__)
class String
def green; "\e[32m#{self}\e[0m" end
def yellow; "\e[33m#{self}\e[0m" end
end