Skip to content

Instantly share code, notes, and snippets.

@bayendor
bayendor / Archaeology.md
Last active August 29, 2015 14:18
Software Archaeology Code Screen Exercise

The cryptic Ruby function takes an array as an argument and returns an array of the items that occur more than once in the original array.

It does this using the inject method specifying an empty Hash as the accumulator. It iterates over each item in the array by referencing the item index position, and passes the resulting item into the accumulator. This builds up a hash of key/value pairs with the key being the item from the initial collection and the value representing the number of times the item appears in the array. It then uses the reject method on the resulting hash and iterates over the hash returning a new array containing the items in self for which the given block is false.

Using idiomatic Ruby a cleaner way to write this function would be:

def show_duplicates(collection)
  collection.select { |element| collection.count(element) > 1 }.uniq
end
@bayendor
bayendor / flatten_clone.rb
Last active August 29, 2015 14:19
Flatten an array in ruby without using `flatten`
def flatten_clone(array, level = -1, result = [])
array.each do |element|
if element.is_a?(Array) && level != 0
flatten_clone element, level - 1, result
else
result << element
end
end
result
end
@bayendor
bayendor / docker
Last active October 6, 2015 15:56 — forked from chernjie/logrotate.d-docker
Logrotate docker logs, copy this file to /etc/logrotate.d/docker
/data/docker/containers/*/*-json.log {
daily
rotate 7
size 100M
compress
delaycompress
missingok
}
@bayendor
bayendor / sql_limit_return.erb
Last active December 25, 2015 21:39
Code to display the SQL query from an ActiveRecord Search & to limit number of results returned. From the ransack gem demo.
<p>SQL: <%= @users.to_sql %></p>
<h2>Your first #{search_limit} results</h2>
<table>
<thead>
<th><%= sort_link @search, :id, {}, :method => action_name == 'advanced_search' ? :post : :get %></th>
<th><%= sort_link @search, :first_name, {}, :method => action_name == 'advanced_search' ? :post : :get %></th>
# ...
</thead>
@bayendor
bayendor / gist:7380393
Created November 9, 2013 01:39
Csv import model logic
# controller
def proc_csv
import = Import.find(params[:id])
if import.load_csv
flash[:notice] = "woo"
redirect_to some_url
else
flash[:error] = "ohoh"
end
end
@bayendor
bayendor / gist:7385546
Last active December 27, 2015 20:39
Checkbox in ransack search
# model CLINIC has many TREATMENTS
<%= check_box_tag "q[treatments_id_eq_ALL][]", treatment.id, {} %>
= f.input :category_name_eq, :collection=>Estate::Category.all.map(&:name), :as=>:check_boxes
@bayendor
bayendor / gist:7385660
Created November 9, 2013 13:55
Ransack multiple selects
f.collection_select :some_id_in, Model.all, :id, :name, {}, {:multiple => true}
# I have to do the following to get the select field I want:
search_form_for @search do |f|
f.collection_select :level_in, Log.levels, :to_s, :to_s, {}, { size: Log.levels.length, multiple: true }
@bayendor
bayendor / gist:7999530
Created December 17, 2013 03:24
Always use :inverse_of to connect two models in a belongs_to/has_many relationship. When you have two models in a belongs_to/has_many relationship, you should always use the :inverse_of option to tell ActiveRecord that they're two sides of the same association. By setting this option, Rails can optimize object loading so forum and forum.posts[0]…
class Forum < ActiveRecord::Base
has_many :posts, :inverse_of => :forum
end
class Post < ActiveRecord::Base
belongs_to :forum, :inverse_of => :posts
end
@bayendor
bayendor / puppet.vim
Created April 4, 2016 14:05
VIM Config settings for working with Puppet
" Set up puppet manifest and spec options
au BufRead,BufNewfile *.pp
\ set filetype=puppet
au BufRead,BufNewfile *_spec.rb
\ nmap <F8 :!rspec --color %<CR>
" Enable indentation matching for =>'s
filetype plugin indent on
" turn off auto adding comments on next line
@bayendor
bayendor / keybase.md
Created April 22, 2016 15:44
keybase.md

Keybase proof

I hereby claim:

  • I am bayendor on github.
  • I am dbayendor (https://keybase.io/dbayendor) on keybase.
  • I have a public key ASB1V69Is1m_YenJTKqCUfMSmusHP5quka8emz0F5BjJNQo

To claim this, I am signing this object: