Skip to content

Instantly share code, notes, and snippets.

View xander-miller's full-sized avatar

Xander Miller xander-miller

View GitHub Profile
@xander-miller
xander-miller / .rspec
Last active August 29, 2015 14:14
How to RSpec Controllers
--require spec_helper
--color
--format documentation
def scrub_uri
uri.strip!
unless uri.slice(0,4) == "http" do
uri = uri.prepend "http://"
end
end
<%= debug(params) if Rails.env.development? %>
<%= debug(session) if Rails.env.development? %>
@xander-miller
xander-miller / simple_form_post.html.erb
Created November 15, 2014 03:18
Untested example of post form in simple form
<%= simple_form_for [topic, post] do |f| %>
<%= f.input :title %>
<%= f.input :body, rows: 8 %>
<% if post.image? %>
<div class="form-group">
<p>Current image</p>
<%= image_tag( current_user.image.thumb.url ) %>
</div>
<% end %>
<%= f.input :image, as: :file %>
class Array
def new_any?(&block)
if block_given?
selected = self.select(&block)
else
selected = self
end
if selected.length == 0
false
class Array
def new_map
new_array = []
each do |item|
new_array << yield(item)
end
new_array
end
def new_select!(&block)
def new_map(array)
new_array = []
array.each do |item|
new_array << yield(item)
end
new_array
end
def add_two(array)
array.map { |item| "#{item} + 2 = #{ item+2 }" }
end
def merge_us(hash1, hash2)
hash1.merge(hash2)
end
def my_keys(hash)
hash.keys
end
def do_i_have?(h ={},keys)
h.keys.sort == keys.sort
class Title
attr_reader :string
def initialize(string)
@string = string
end
def fix
# A neat Ruby trick for making an array of strings
# Equivalent to: ['a', 'and', 'the', 'of']