Skip to content

Instantly share code, notes, and snippets.

@xander-miller
xander-miller / .rspec
Last active Aug 29, 2015
How to RSpec Controllers
View .rspec
--require spec_helper
--color
--format documentation
View scrub_uri.rb
def scrub_uri
uri.strip!
unless uri.slice(0,4) == "http" do
uri = uri.prepend "http://"
end
end
View debug_boxes.html.erb
<%= debug(params) if Rails.env.development? %>
<%= debug(session) if Rails.env.development? %>
@xander-miller
xander-miller / simple_form_post.html.erb
Created Nov 15, 2014
Untested example of post form in simple form
View simple_form_post.html.erb
<%= 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 %>
View new_anyy.rb
class Array
def new_any?(&block)
if block_given?
selected = self.select(&block)
else
selected = self
end
if selected.length == 0
false
View monkey_patchy.rb
class Array
def new_map
new_array = []
each do |item|
new_array << yield(item)
end
new_array
end
def new_select!(&block)
View yieldy.rb
def new_map(array)
new_array = []
array.each do |item|
new_array << yield(item)
end
new_array
end
View mapy.rb
def add_two(array)
array.map { |item| "#{item} + 2 = #{ item+2 }" }
end
View hashy.rb
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
View loopy3.rb
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']