Wrap text
Theme:
Clean (Pastie)
All Hallow's Eve
Blackboard
Brilliance Black
Cobalt
Espresso Libre
IDLE
Mac Classic
MagicWB (Amiga)
Pastels on Dark
Slate
Slush and Poppies
Sunburst
Sunburst (Josh)
Twilight
Vibrant Ink
Report abuse
rails app
cd app
script/ generate nifty_authentication
script/ generate nifty_layout
script/ generate model status user_id: integer message: string created_at: datetime
script/ generate model friendship user_id: integer friend_id: integer
script/ generate controller statuses
rm public / index.html
rake db: migrate
status.rb
belongs_to :user
validates_presence_of :user_id , :message , :created_at
friendship.rb
belongs_to :user
belongs_to :friend , :class_name => 'User'
validates_uniqueness_of :friend_id , :scope => :user_id
validates_presence_of :user_id , :friend_id
user.rb
has_many :statuses , :dependent => :destroy
has_many :friendships
has_many :friends , :through => :friendships
def add_friend (friend )
friendship = friendships.build(:friend_id => friend.id)
friendship.save!
end
def remove_friend (friend )
friendship = Friendship .find(:first , :conditions => ["user_id = ? and friend_id = ?" , self .id, friend.id])
if friendship
friendship.destroy
end
end
def all_statuses
Status .find(:all , :conditions => ["user_id in (?)" , friends.map(& :id ).push(self .id)], :order => "created_at desc" )
end
def is_friend? (friend )
return self .friends.include? friend
end
def friends_of
Friendship .find(:all , :conditions => ["friend_id = ?" , self .id]).map{|f |f.user}
end
validates_exclusion_of :username , :in => %w(following) , :message => "Sorry, cannot use this username"
statuses_controller.rb
before_filter :login_required
def index
@statuses = current_user.all_statuses
end
def show
@user = User .find_by_username(params[:username ])
@statuses = @user .all_statuses
end
def toggle_follow
@user = User .find_by_username(params[:username ])
if current_user.is_friend? @user
current_user.remove_friend(@user )
flash[:notice ] = "You are no longer following #{@user .username} "
else
current_user.add_friend(@user )
flash[:notice ] = "You are now following #{@user .username} "
end
redirect_to user_statuses_path(@user .username)
end
def create
status = current_user.statuses.build(params[:status ])
status.message = status.message[0 ..140 ]
status.created_at = Time .now
status.save!
redirect_to root_path
end
routes.rb
map.root :controller => "statuses"
map.resources :statuses
map.toggle_follow '/:username/toggle_follow' , :controller => 'statuses' , :action => 'toggle_follow'
map.user_statuses '/:username' , :controller => 'statuses' , :action => 'show'
layouts/application.html.erb
< % if current_user %>
<div style="float: right;">
Logged in as < %= current_user.username %> | <%= link_to "Logout", logout_path %>
< / div>< br / >
< % end %>
statuses/index.html.erb
< h1> Welcome < %= current_user.username %></h1>
< % form_for Status .new do |f | %>
<%= f.text_area :message %>
< %= f.submit "Post" %>
<% end %>
< ul>
< % @statuses .each do |status | %>
<li> < %= link_to status.user.username, user_statuses_path(status.user.username) %> - <%= h status.message %> - < %= distance_of_time_in_words_to_now(status.created_at) %> ago</li>
< % end %>
</ul>
< %= pluralize(current_user.statuses.length, "post" ) %>
<%= current_user.friends.count %> following
< %= pluralize(current_user.friends_of.length, "follower" ) %>
statuses/show.html.erb
< h1>< %= @user .username %></h1>
< %- form_tag toggle_follow_path do - %>
<% if current_user.is_friend? @user %>
< %= submit_tag "Following" %>
<% else %>
< %= submit_tag "Not Following" %>
<% end %>
< %- end - %>
<ul>
< % @statuses .each do |status | %>
<li> < %= h status.message %> - <%= distance_of_time_in_words_to_now(status.created_at) %> ago< / li>
< % end %>
</ul>