This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<% @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 %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Topic < ActiveRecord::Base | |
(...) | |
def to_param | |
"#{id}-#{name.parameterize}" | |
end | |
(...) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |