peryt
peryt · 共 68 篇文章
12.3.3 scaling issue of the status feed
the problem of the implementation of last chapter is: 1. it has a line of code: following_ids = user.following_ids this will fetch all the followed users of this user, but what we need to just if user_id is included in this set. SQL already optimized things like this, check inclusion in a set of an…
2011-10-30 17:54 · ruby and rails · 评论(0)
12.3 the status feed
1. we need to get all the micropost of the users followed by current user. Micropost.from_users_followed_by(user) so we can write a test this method. describe Micropost do . . . describe "from_users_followed_by" do before(:each) do @other_user = Factory(:user, :email => Factory.next(:email)) @third…
2011-10-30 15:34 · ruby and rails · 评论(0)
12.2 a working follow button with Ajax
1. in the last chapter, in the user profile screen, the user can click on the follow button, then he is redirect back to the original profile screen. so we can ask, why do we leave that profile page at all? that is a waste of resource. right, this is exactly the problem solved by Ajax. Ajax allows …
2011-10-29 18:10 · ruby and rails · 评论(0)
12.2 a web interface for following and followers.
1.before we do the UI, we need to populate database. we will write a rake task to do this: namespace :db do desc "populate database" task :populate => :environment do Rake:Task["db:reset"].invoke make_users make_microposts make_relationships end end def make_users admin = User.create!(:name => "fdf…
2011-10-28 22:14 · ruby and rails · 评论(0)
12. following user, 12.1 relationship model
1. we need to use a relationships table to stand for the following relationship: has_many :following, :through => :relationships, :source => "followed_id" 2. then $ rails generate model Relationship follower_id:integer followed_id:integer 3. we need to add index to the table add_index :relationship…
2011-10-18 14:29 · ruby and rails · 评论(0)
11.3 manipulating microposts.
1. since all micropost actions will be done in users page, so we only need :create and :destroy actions. so the routes will be: resources :microposts, :only => [:create, :destroy] 2. we will test access control to the microposts controller first: describe MicropostsController do render_views descri…
2011-10-17 15:31 · ruby and rails · 评论(0)
11.2 show microposts.
1. add test to test the new users/show view: describe UsersController do render_views . describe "GET 'show'" do before(:each) do @user = Factory(:user) end . . it "should show the user's microposts" do mp1 = Factory(:micropost, :user => @user, :content => "Foo bar") mp2 = Factory(:micropost, :user…
2011-10-17 12:01 · ruby and rails · 评论(0)
11.1 user micropost -- a micropost model.
1. we will first generate a micropost model. $ rails generate model Micropost content:string user_id:integer note, if you the content is longer, you can use type "text" instead of string 2. next, since we expect to retrieve all microposts associated with a user, we need to add index on user_id and …
2011-10-17 10:43 · ruby and rails · 评论(0)
10.4 destroying users.
in this chapter, we will add destroy action to users controller to finish REST. first, we need to add administrative users who can use the destroy action. so we need to add a new attr of admin which is a boolean. and in the model, you can use admin? to get this attr value. 1. let's write the unit t…
2011-10-16 15:47 · ruby and rails · 评论(0)
10.3 showing users list
in this chapter, we will do user list, i.e. the index action of users controller. at the same time, we will study pagination, and how to populate many sample data into database. 1. we will make sure sign-in user can see all users list. non-signed-in user can only see users show page, not the all us…
2011-10-15 20:41 · ruby and rails · 评论(0)
10.2 protect pages.
again, we will start from TDD!!! 1. since both edit and update need the same authentication, we can put their test together: describe "authentication of edit/update pages" do before(:each) do @user = Factory(:user) end describe "for not signed-in users" do it "should redirect to sign in page" do ge…
2011-10-15 15:11 · ruby and rails · 评论(0)
10.1 updating users.
1. git checkout -b updating-users 2. in this chapter, we will make you can update user profile. a. we will use a "edit" action to render a view to edit user. b. we will use "update" action and a "PUT" request to update user profile c. we need to make sure only current user can update their informat…
2011-10-14 18:30 · ruby and rails · 评论(0)
9.4 sign out
whew!!!, last chapter is a long one! now, we are going to implement sign out. 1. no wonders, we will start from TDD. describe "DELETE 'destroy'" do it "should sign a user out" do test_sign_in(Factory(:user)) delete :destroy controller.should_not be_signed_in response.should redirect_to(root_path) e…
2011-10-13 15:21 · ruby and rails · 评论(0)
9.3 sign in success.
1. we will first finish the create action: def create user = User.authenticate(params[:session][:email], params[:session][:password]) if user.nil? flash.now[:error] = "Invalid email/password combination." @title = "Sign in" render 'new' else sign_in user redirect_to user end end for the below, we w…
2011-10-12 15:39 · ruby and rails · 评论(0)
9.1 about flash.now[:error] vs flash[:error]
There’s a subtle difference between flash and flash.now . The flash variable is designed to be used before a redirect , and it persists on the resulting page for one request—that is, it appears once, and disappears when you click on another link. Unfortunately, this means that if we don’t redirect,…
2011-10-12 15:37 · ruby and rails · 评论(0)