New Rails Project for Fun

Today i push a new rails project to heroku called lunch. I also push to github. Every week day i and colleague have to discuss which lunch to eat. It will be very helpful if a site can random pick a lunch restaurant for us. So i create a new rails project for this purpose. The basic idea is simple, everybody can share a restaurant and add others’ restaurants to favorites list. Then site pick a restaurant for you. I can study new stuff when i practice rails each time. I try to write down here.

image store in S3

Because the heroku doesn’t allow to store images, i move the paperclip default storage to S3. It’s very easy to implement, just follow this tutorial

social network signin

This time i integrate three social networks(facebook, twitter, google). I am stuck in google app setting for a while. Because they all have different callback url setting. Finally i resolved and here are screenshots for these sites. These samples are for 127.0.0.1:3000 testing

google

list

twitter

list

facebook

list

MVP design

I add a app/presenters folder and make controller logic more impact. Add folder path to application.rb that controller could access them.

application.rb
1
2
config.paths.add "app/presenters", glob: "**/*.rb"
config.autoload_paths += Dir["#{Rails.root}/app/presenters/*"]

Google map support

Use gmaps4rails gem. github link. Good video tutorial link

rspec testing

devise signin

Just create a user and call signin.

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
FactoryGirl.define do

  factory :user do
    name Faker::Name.name
    sequence(:email) { |n| "example#{n}@example.com" } # create different email each user.
    password 'a1234567'
    password_confirmation 'a1234567'

    factory :user_with_restaurant do
      after(:create) do |user|
        restaurant = FactoryGirl.build(:restaurant)
        restaurant.creator_id = user.id
        restaurant.save!
        user.favor_restaurant << restaurant
      end
    end

  end

end

# It's good practice to put in before block
before(:each) do
  @user = create(:user)
  sign_in @user
end

Use expect to assert result

For example restaurant is the return object from controller.

xx_controller.rb
1
2
3
  def new
    @restaurant = Restaurant.new
  end
xx_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
restaurant = assigns(:restaurant)
expect(response.status).to eq 302 # check http status code
expect(response).to render_template :index # check render template
expect(restaurant).to be_a_new(Restaurant) # check restaurant is new object
expect(flash[:notice]).to be # check flash[:notice] exist
expect(flash[:warn]).to be_nil # check flash[:warn] nil

# we could even use block 
# check restaurant is added by 1 after post to create controller
expect {
  post :create, restaurant: attributes_for(:restaurant)
}.to change(Restaurant, :count).by(1)

Comments