Skip to content

Instantly share code, notes, and snippets.

View marcamillion's full-sized avatar

Marc Gayle marcamillion

View GitHub Profile
1.9.2p0 :002 > f = User.first
User Load (0.4ms) SELECT "users".* FROM "users" LIMIT 1
=> #<User id: 1, email: "abc@email.com", encrypted_password: "$2a$10$LTc6zq5VorMZzsDSv7nD2eZpQkV30BaylB2Q6oY4yYjJ...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 2, current_sign_in_at: "2012-06-13 08:12:53", last_sign_in_at: "2012-06-13 08:09:08", current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", name: "Ayy Bee Cee", created_at: "2012-06-13 08:08:01", updated_at: "2012-06-13 08:12:53", confirmation_token: nil, confirmed_at: "2012-06-13 08:09:08", confirmation_sent_at: "2012-06-13 08:08:01", unconfirmed_email: nil, authentication_token: nil>
1.9.2p0 :003 > f.categories
Category Load (8.1ms) SELECT "categories".* FROM "categories" INNER JOIN "categories_users" ON "categories"."id" = "categories_users"."category_id" WHERE "categories_users"."user_id" = 1
=> []
1.9.2p0 :003 > f.categories
=> []
1.9.2p0 :007 > f.categories.create({:id => 1})
(0.1ms)
class Student < ActiveRecord::Base
has_many :grades do
def below_average
where('score < ?', 2)
end
end
end
def update
@client = current_user.clients.find(params[:id])
respond_to do |format|
if @client.update_attributes(params[:client])
format.html { redirect_to @client, notice: 'Client was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
@marcamillion
marcamillion / best_in_place_fix.js.coffee
Created December 3, 2012 16:33 — forked from mkelley33/best_in_place_fix.js.coffee
best_in_place: fix disappearing line-breaks in textarea
jQuery ->
$('[id^="best_in_place_your_object_"],[id$="address"]').on 'best_in_place:update', (event) ->
$address = $(event.target)
$address.data('bestInPlaceEditor').original_content = $address.html()
$address.html $address.html().replace(/[\n\r]+/g, '<br>')
@marcamillion
marcamillion / method_missing_override.rb
Created December 17, 2012 08:29 — forked from anonymous/gist:4316571
This allows you to do something like this: Say Advertiser is an assoc on banner, you could do something like: Advertiser.first.featured_banners And if you later add a banner type named 'Awesome' you could immediately call Advertiser.first.awesome_banner without writing new code in the model.
#Here's what I'm using. Maybe you could adapt it to your case. I have this on a Message model.
def method_missing(method_name, *args, &block)
if method_name =~ /(.*)_recipients$/
self.correspondences.where('recipient_type = ?', $1.to_s.humanize)
.collect{ |c| c.recipient }.flatten.uniq
else
super
end
end
@marcamillion
marcamillion / console.rb
Created March 27, 2013 15:50
Implementing HABTM counter_cache - between 2 models (Tag, Question)
## I imagine this could be done in the migration, but it was giving me problems so I moved it to the model
## and just did it on the command line
> Tag.reset_questions_count
@marcamillion
marcamillion / posterous_import.rb
Last active December 16, 2015 20:29 — forked from nitrogenlogic/00_posterous_import_moved.md
Added support for posts that were archived by Posterous - i.e. moved the images into subfolders belonging to dates & times that don't correspond (obviously) to the publish date of the post itself.
#!/usr/bin/env ruby
# This quick and dirty script imports posts and images exported by the
# Posterous backup feature into Octopress. Requires the escape_utils and
# nokogiri gems. Doesn't import comments.
#
# Videos and images are copied into a post-specific image directory used
# by my customized Octopress setup. Encoded videos are downloaded from
# Posterous. Images will probably need to be compressed/optimized afterward.
#
# Links to other posts in the same import will try to be converted. You will

Contract Killer 3

Revised date: 07/11/2012

Between us [company name] and you [customer name]

Summary:

We’ll always do our best to fulfil your needs and meet your expectations, but it’s important to have things written down so that we both know what’s what, who should do what and when, and what will happen if something goes wrong. In this contract you won’t find any complicated legal terms or long passages of unreadable text. We’ve no desire to trick you into signing something that you might later regret. What we do want is what’s best for both parties, now and in the future.

class Player
def play_turn(warrior)
if @health.nil?
@health = warrior.health
end
if warrior.feel.empty? && !taking_damage?(warrior) && warrior.health < 20
warrior.rest!
elsif warrior.feel.captive?
warrior.rescue!
class Player
def play_turn(warrior)
if warrior.feel.empty? && !taking_damage?(warrior) && warrior.health < 20
warrior.rest!
elsif warrior.feel.empty?
warrior.walk!
else
warrior.attack!
end
@health = warrior.health