Skip to content

Instantly share code, notes, and snippets.

View stephenrichards's full-sized avatar

Stephen Richards stephenrichards

  • Ministry of Justice
  • London
View GitHub Profile
@stephenrichards
stephenrichards / thin_controller.rb
Created December 12, 2018 10:22
Thin Controller
class ThinController < ApplicationController
def index
@cards = OysterCard.order(balance: :desc)
end
def show
end
def swipe_out
@stephenrichards
stephenrichards / fat_controller.rb
Created December 12, 2018 10:20
Fat controller
class ThinController < ApplicationController
def swipe_out
@card = OysteCard.find(params[:id])
authorize @card # uses a Pundit policy to determine whether or not the current user is authorised to update this card
@journey_end_station = Station.find(params[:swipe_out][journey_end_station_id])
if @card.journey_start_station.nil?
flash[:alert] << "No start of journey recorded - you have been charged the maximum fare"
@card.balance -= MAXIMUM_FARE
else
class OysterCardsController < ApplicationController
def index
@cards = OysterCard.order(balance: :desc)
end
def show
end
def swipe_out
class Person < ApplicationRecord
validates_with PersonValidator
end
@stephenrichards
stephenrichards / list_routes.rb
Created March 21, 2018 13:59
Llist all controllers and actions
Rails.application.routes.routes.each do |route|
d = route.defaults
next unless d.key?(:controller)
next if d[:internal] == true
puts "#{d[:controller]}/#{d[:action]}"
end
@stephenrichards
stephenrichards / db_counter.rb
Created February 1, 2017 10:40
Count record in each table in database
# use as DbCounter.new.run
class DbRrecordCounter
def initialize
@conn = ActiveRecord::Base.connection
end
def run
@stephenrichards
stephenrichards / pop.rake
Created January 25, 2017 11:12
Rakefile to create people with similar names for peoplefinder test
namespace :db do
task :pop => :environment do
include FactoryGirl::Syntax::Methods
Person.where('surname like ?', 'Me%').map(&:destroy)
names = [
%w{ Nicky Meadhurst },
%w{ Nicola Meadhurst },
%w{ Nicky Medhurst },
%w{ Nicola Medhurst },
%w{ Jane Meadhurst },
/*
Query CCR database
Get bills on cases for AGFS scheme 10 with a caclulation date > 2/1/2015
*/
select
c.id,
s.psty_highest_level,
b.bity_bill_type,
["46.51.128.0/18",
"46.51.192.0/20",
"46.137.0.0/17",
"46.137.128.0/18",
"52.16.0.0/15",
"52.18.0.0/15",
"52.30.0.0/15",
"52.48.0.0/14",
"52.92.40.0/21",
"52.93.0.0/24",
require 'rest-client'
require 'ap'
response = RestClient.get 'https://ip-ranges.amazonaws.com/ip-ranges.json', accept: :json
hash = JSON.parse(response.body)
regions = hash['prefixes']
ranges = []
regions.each do |region|
ranges << region['ip_prefix'] if region['region'] == 'eu-west-1'