Skip to content

Instantly share code, notes, and snippets.

# A user has many tweets.. and sometimes we want to retweet.
class TweetsController < ApplicationController
def retweet
@tweet = Tweet.find(params[:id])
if @tweet.user == current_user
flash[:notice] = "Sorry, you can't retweet your own tweets"
elsif @tweet.retweets.where(:user_id => current_user.id).present?
# Even when you create objects they should be properly scoped... Remember this example?
t = Tweet.new
t.status = "RT #{@tweet.user.name}: #{@tweet.status}"
t.original_tweet = @tweet
t.user = current_user
t.save
# A simplified version of this, properly scoped might look like this:
class AccountSetting < ActiveRecord::Base
belongs_to :user
# attr_protected :user_id
# or you can also do
attr_accessible :public_email, :language, :time_zone,
:location_on_tweets, :show_media, :protect_tweets
end
class AddIndexesToTables < ActiveRecord::Migration
def self.up
add_index :tweets, :user_id
add_index :tweets, :tweet_id
add_index :tweets, :created_at
add_index :users, :email, :unique => true
add_index :account_settings, :user_id
end
@caike
caike / topic.rb
Created January 22, 2011 02:12
before_filter
class Admin::TopicsController < ApplicationController
# this could be an example of a before filter
before_filter :check_security_token
def create
topic = Topic.new(params[:topic])
# move this to AR callback before_create ?
class AddDefaultTimeZoneToUsers < ActiveRecord::Migration
def self.up
change_column_default :account_settings, :time_zone, 'EST'
end
def self.down
change_column :account_settings, :time_zone, :string, nil
end
end
@caike
caike / index.html.erb
Created January 22, 2011 21:42
tweets#index
<% @presenter.tweets.each do |tweet| %>
<tr>
<td><%= tweet.status %></td>
<!--<td><%#= tweet.user.display_name %></td>-->
<td><%= tweet.user %></td>
<td><%= link_to 'Show', tweet %></td>
<td><%= link_to 'Edit', edit_tweet_path(tweet) %></td>
<td><%= link_to 'Destroy', tweet, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
@caike
caike / topic.rb
Created January 22, 2011 22:02
SEO friendly topic url
class Topic < ActiveRecord::Base
(...)
def to_param
"#{id}-#{name.parameterize}"
end
(...)
@caike
caike / topic.rb
Created January 23, 2011 04:41
AR call back with conditional
class Topic < ActiveRecord::Base
before_create :set_start_trending
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
def to_param
@caike
caike / topic.rb
Created January 23, 2011 04:51
custom validator checking if trending topic content is suitable
class Topic < ActiveRecord::Base
before_create :set_start_trending
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
validate :appropriate_content