1 2 3 4 5 6 7 8 9 10 |
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
1 2 |
belongs_to :user validates_presence_of :user_id, :message, :created_at |
friendship.rb
1 2 3 4 |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
has_many :statuses, :dependent => :destroy has_many :friendships has_many :friends, :through => :friendships friendship = friendships.build(:friend_id => friend.id) friendship.save! end friendship = Friendship.find(:first, :conditions => ["user_id = ? and friend_id = ?", self.id, friend.id]) if friendship friendship.destroy end end Status.find(:all, :conditions => ["user_id in (?)", friends.map(&:id).push(self.id)], :order => "created_at desc") end return self.friends.include? friend end 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
before_filter :login_required @statuses = current_user.all_statuses end @user = User.find_by_username(params[:username]) @statuses = @user.all_statuses end @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 " else current_user.add_friend(@user) flash[:notice] = "You are now following " end redirect_to user_statuses_path(@user.username) end 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
1 2 3 4 |
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
1 2 3 4 5 |
<% 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<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> |

