Skip to content

Instantly share code, notes, and snippets.

View marcamillion's full-sized avatar

Marc Gayle marcamillion

View GitHub Profile
@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
@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 / 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 / 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>')
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" }
class Student < ActiveRecord::Base
has_many :grades do
def below_average
where('score < ?', 2)
end
end
end
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)
Interactive ruby ready.
> food = ['chi­cken', 'stea­k', 'beef­', 'pork­']
=> ["chicken", "steak", "beef", "pork"]
> food.each do |x|
.. puts x
.. end
=> #<SyntaxError: <script>:1: syntax error, unexpected end-of-file
food = ['chicken', 'steak', 'beef', 'pork'];food.each do |x|;
#(Element:0x8427df88 {
name = "a",
attributes = [
#(Attr:0x841fe3a0 {
name = "href",
value = "http://google.com"
})],
children = [ #(Text "Google Home Page")]
})
# Get a URL's content to play with
content = contents[contents.keys.first]
# => [#<Nokogiri::XML::Element:0x48a9a86 name="p" children=[#<Nokogiri::XML::Text:0x48a9810 "The autobiography of Yohwan Lim, ">, #<Nokogiri::XML::Element:0x48a96ee name="i" children=[#<Nokogiri::XML::Text:0x48a9536 "Crazy As Me">]>, #<Nokogiri::XML::Text:0x48a931a " was released in Korea by BookRoad Publishers in October 25, 2004. This is my translation of the book, except the following four sections which were translated by BinaryStar of Teamliquid.net, which I have made minor changes: \"Hope on the Road Not Taken,\" \"Chapter One: The Game-crazed Kid,\" \"The Birth of the Emperor,\" and \"The Little Prince with Three Sisters.\"">]>, #<Nokogiri::XML::Element:0x48a9194 name="p" children=[#<Nokogiri::XML::Text:0x48a9022 "As of October 4, 2004:">]>, #<Nokogiri::XML::Element:0x48a8e92 name="p">, #<Nokogiri::XML::Element:0x48a8ce4 name="p" children=[#<Nokogiri::XML::Text:0x48a8af0 "The addition of e-sports organizations to major compani