Skip to content

Instantly share code, notes, and snippets.

@caike
caike / topic.rb
Created January 23, 2011 04:59
moved from custom validation to before_save callback returning false
class Topic < ActiveRecord::Base
before_create :set_start_trending
before_save :check_content
before_save :set_hot_topic, :if => Proc.new { |topic| topic.mentions >= HOT_TOPIC_MENTIONS }
scope :trending_top, lambda { |num| where("started_trending > ?", 1.day.ago).order('mentions desc').limit(num) }
HOT_TOPIC_MENTIONS = 1000
@caike
caike / content_moderator.rb
Created January 23, 2011 05:02
checks if content is suitable
class ContentModerator
def self.is_suitable?(content)
# checks against a web service
# or local dictionary
end
end
@caike
caike / tweets_controller.rb
Created January 23, 2011 15:01
avoiding n+1 query with :include
class TweetsController < ApplicationController
def index
# @tweets = Tweet.all
# Tweet Load (0.4ms) SELECT `tweets`.* FROM `tweets` ORDER BY created_at desc
# User Load (0.4ms) SELECT `users`.* FROM `users` WHERE (`users`.`id` = 1) LIMIT 1
# User Load (0.4ms) SELECT `users`.* FROM `users` WHERE (`users`.`id` = 2) LIMIT 1
# User Load (0.3ms) SELECT `users`.* FROM `users` WHERE (`users`.`id` = 3) LIMIT 1
@caike
caike / _form.html.erb
Created January 23, 2011 15:42
serializing preferences
# in the user form partial
<h3>Preferences</h3>
<% User::PREFERENCES.each do |preference| %>
<div class="field">
<%= check_box_tag "user[preferences[#{preference.to_s}]]", "1", @user.preferences ? @user.preferences[preference] : false %>
<%= label_tag :attribute, preference.to_s.titleize %>
</div>
<% end %>
@caike
caike / user.rb
Created January 23, 2011 16:09
memoizing calculated value
class User < ActiveRecord::Base
has_many :tweets
has_one :account_setting, :dependent => :destroy
accepts_nested_attributes_for :account_setting
validates_presence_of :first_name, :last_name, :email
validates_uniqueness_of :email
validates_presence_of :age
validates_exclusion_of :age, :in => 0..18, :message => 'underage'
@caike
caike / long_running_task.rake
Created January 23, 2011 17:14
find_each example
desc 'Long running task involving all tweets'
task :long_running_task => :environment do
ActiveRecord::Base.logger = Logger.new STDOUT
# defaults to 1000
Tweet.find_each() do |tweet|
p "long running task for #{tweet}"
end
@caike
caike / topics_controller.rb
Created January 23, 2011 17:24
selecting specific fields for a model
class TopicsController < ApplicationController
def index
@topics = Topic.select([:id, :name]).order('mentions desc')
#@topics = Topic.order('mentions desc')
end
def show
@topic = Topic.find(params[:id])
end
@caike
caike / index.html.erb
Created January 23, 2011 17:38
law of demeter
<% @tweets.each do |tweet| %>
<tr>
<td><%= tweet.status %></td>
<!--<td><%#= tweet.user.display_name %></td>-->
<td>
<em>
<%#= (tweet.user.account_setting && tweet.user.account_setting.location_on_tweets?) ? 'available' : 'unavailable'%>
<%= tweet.has_location_data? ? 'available' : 'unavailable' %>
</em>
</td>
@caike
caike / time_difference.rb
Created February 16, 2011 01:36
difference in seconds
before = Time.now.to_i
sleep 5
now = Time.now.to_i
p "#{now - before} seconds have passed"
@caike
caike / machine.js
Created February 19, 2011 04:30
vending machine
function Machine(){
this.totalAmount = 0;
this.items = {'twix': 0, 'coke': 0, 'tostitos': 0};
this.insertMoney = function(amount){
this.totalAmount += amount;
}
this.total = function(){