Skip to content

Instantly share code, notes, and snippets.

View reggieb's full-sized avatar

Rob Nichols reggieb

View GitHub Profile
@reggieb
reggieb / picking_serializer.rb
Created May 24, 2017 12:46
Playing with ActiveModelSerializers to generate a redux friendly data structure.
class PickingSerializer < ActiveModel::Serializer
attribute :data do
{
result: object.id,
entities: {
orders: { object.id => OrderSerializer.new(object) },
line_items: object.line_items.each_with_object({}) do |line_item, h|
h[line_item.id] = LineItemSerializer.new(line_item)
end
#!/bin/bash
###
# Script for downgrading Elasticsearch to version 1.3.9
#
# Before running this script, please reset your dependency cache in Project Settings > Admin.
#
# Add the line below to your Setup commands in Project Settings (without the # at the beginning):
#
# wget https://gist.githubusercontent.com/reggieb/c16ce698f482904ad0a8f81bd1fe7af4/raw/1fc5a9883ee85f74b6fc8415213deac69b002e55/elasticsearch-downgrade-semaphore.sh ; sudo bash elasticsearch-downgrade-semaphore.sh
@reggieb
reggieb / array_to_h.rb
Created March 13, 2017 12:24
Add a `to_h` method to pre 2.1 ruby
unless [].respond_to? :to_h
class Array
def to_h
Hash[self]
end
end
end
def glyphicon(name)
content_tag(
'span', '', class: "glyphicon glyphicon-#{name}", 'aria-hidden' => 'true'
)
end
require 'elasticsearch'
# Set up client bypassing Elasticsearch's Faraday imported proxy settings (which ignore system's no_proxy)
# So transport_options needed to access local elasticsearch instance
client = Elasticsearch::Client.new log: true, transport_options: { proxy: '' }
# Retrieve data - setup instructions here:
# https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-elasticsearch-on-ubuntu-16-04
client.get index: 'tutorial', type: 'helloworld', id: 1
class Address
attr_accessor :address, :flat, :house_number, :house_name, :street, :street2, :street3, :town, :county
def initialize(address)
self.address = address
end
def parts
@parts ||= address.split(/\s?\,\s?/).collect(&:strip)
end
@reggieb
reggieb / contents_of.rb
Created September 20, 2016 12:01
A module to simplify grabbing the contents from files in rails apps
module ContentsOf
def contents_of(file_path, location: Rails.root)
File.read File.expand_path(file_path, location)
end
end
# Resistence settings for cartidge loads
settings = [1.1, 33.0, 100.0, 1000.0]
# Combination of resistors switched in via panel under dino
# Equation is: 1/Rtotal = 1/R1 + 1/R2 ... + 1/Rn
(1..4).collect do |i|
settings.combination(i).each do |combination|
label = combination.inspect
fractions = combination.collect{|c| 1/c}
@reggieb
reggieb / use_uuid.rb
Last active January 3, 2021 20:55
UUID concern for rails models - To allow rails paths to be of the form `foo/63950bdb-6c3b-4744-ad6f-f5df5aed1a76` rather than `foo/1`
require 'active_support/concern'
module UseUuid
extend ActiveSupport::Concern
included do
before_save :generate_uuid
end
def to_param
uuid
@reggieb
reggieb / parent_child.rb
Last active June 29, 2016 10:07
How to define method to be over-ridden in child classes
class Parent
def self.to_be_over_ridden
raise "Class method `#{__method__}` must be defined in #{name}"
end
def to_be_over_ridden
raise "Instance method `#{__method__}` must be defined in #{self.class.name}"
end
end