Skip to content

Instantly share code, notes, and snippets.

@tatey
tatey / option_a.rb
Created October 19, 2012 04:46 — forked from coop/option_a.rb
class Page < ActiveRecord::Base
has_attached_file :image, sizes: {small: '10x10', large: '100x100'}
has_attached_file :uploaded_image
def safe_image_url
if image
image_url
else
'spinner.gif'
end
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface ErrorFormatter : NSObject
@property (strong, nonatomic) NSError *error;
- (id)initWithError:(NSError *)error;
- (UIAlertView *)alert;
class TeamFactory
class PageValue < OpenStruct.new(:page, :name)
end
def self.build_from_individual_page page, name = nil, builder = self
value = PageValue.new page, name
team = builder.build_team value
team_page = builder.build_team_page value
builder.persist! team, team_page
team
@tatey
tatey / gist:4159364
Created November 28, 2012 06:17
Short hand for an attribute writer with a default value
# When using dependency injection, I often find myself storing the dependency as
# an attribute on the object. This is fine, except when you squint at the class there
# looks to be more methods then what's really needed to understand the class.
#
# A typical class with dependency injection might look like this
class TimeParam
attr_writer :parser
def initialize value
@value = value
@tatey
tatey / gist:4376776
Last active December 10, 2015 03:48
# Person has a polymorphic relationship called contactable.
# We want the person's contactable method to return a contact
# of our choosing.
contact = Contact.new
real_person = Person.find(1)
mock_person = SimpleMock.new(real_person)
mock_person.expect(:contactable, contact)
mock_person.contact == contact # => true
module Countable
def self.included do
define_method "#{self.name}s" do
# ...
end
end
def self.name
self.class.to_s.sub('FeedItem', '').downcase
end
@tatey
tatey / gist:4458488
Last active December 10, 2015 16:08
Commit message when I upgraded rails from 2.3.8 to 2.3.9.
Upgrade rails to 2.3.9.
2.3.9 fixes a bug that we had come to depend on
(https://github.com/rails/rails/pull/7661). It's best described with an
example
2.3.8
p = Plan.new
p.member = Member.first
@tatey
tatey / new.html.erb
Created January 13, 2013 04:56
Rails ERB template for http://jsfiddle.net/tatejohnson/vLhNb/. Adding and removing children to a Rails nested form with AngularJS.
<h1>New Plan</h1>
<div ng-app>
<%= form_for @plan do |form| %>
<div ng-controller="PollCtrl" ng-init="polls = <%= @plan.polls.to_json %>">
<div ng-repeat="poll in polls">
<%= form.fields_for :polls, Poll.new, child_index: '{{$index}}' do |poll_form| %>
<%= poll_form.text_field :title, id: 'plan_poll_{{$index}}', value: '{{poll.title}}' %>
<% end %>
<a href="#" ng-click="remove($index)" ng-show="isRemovable()">Remove</a>
</div>
@tatey
tatey / gist:4592323
Created January 22, 2013 05:26
Typically we'd use hashes for this sort of things, but unlike hashes, going ChoiceTypes.doesnt_exist will raise an exception instead of returning nil.
# Known poll types. Unknown types will raise
# an exception.
#
# Poll::ChoiceTypes.time # => 'Choice::Time'
#
# @return [Struct]
ChoiceTypes = Struct.new(:time, :place, :text).new.tap do |types|
types.time = 'Choice::Time'
types.place = 'Choice::Place'
types.text = 'Choice::Text'
class RubyExample
CONSTANT = /^[0-9]+ regex awesomes$/
attr_reader :colorscheme
def initialize(attributes = {})
@colorscheme = attributes[:colorscheme]
end
def self.values