Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add model code for cravings

  • Loading branch information...
commit 661066a45b9cce754ea0510cf31cc01a4bf07e20 1 parent 7ffe533
@brentvatne authored
View
2  app/controllers/api_controller.rb
@@ -26,7 +26,7 @@ class App < Sinatra::Application
end
# [ { :name => 'restaurant name', :id => 1232,
- # :people => [ {:nickname => '..', # :image => 'image_url' }, { ... }, { ... } ],
+ # :people => [ {:nickname => '..', # :gravatar_id => 'id' }, { ... }, { ... } ],
# { ... } ]
get '/api/restaurants/:date' do
end
View
15 app/models/craving.rb
@@ -0,0 +1,15 @@
+require_relative 'user'
+require_relative 'restaurant'
+require_relative "../../config/data_mapper"
+
+module LunchZone
+ class Craving
+ include DataMapper::Resource
+
+ property :id, Serial
+ property :date, Date
+
+ belongs_to :user
+ belongs_to :restaurant
+ end
+end
View
9 app/models/restaurant.rb
@@ -1,3 +1,5 @@
+require_relative "user"
+require_relative "craving"
require_relative "../../config/data_mapper"
module LunchZone
@@ -6,5 +8,12 @@ class Restaurant
property :id, Serial
property :name, String, :unique => true
+
+ has n, :cravings
+ has n, :users, :through => :cravings
+
+ def users_on_date(date)
+ User.all(User.cravings.date => date)
+ end
end
end
View
13 app/models/user.rb
@@ -1,4 +1,6 @@
require 'octokit'
+require_relative "restaurant"
+require_relative "craving"
require_relative "../../config/data_mapper"
module LunchZone
@@ -12,6 +14,9 @@ class User
property :token, String
property :gravatar_id, String
+ has n, :cravings
+ has n, :restaurants, :through => :cravings
+
def self.find_or_create_from_omniauth(params)
email = params['info']['email']
user = first(:email => email)
@@ -58,6 +63,14 @@ def self.is_visible_on_github_org?(nickname)
members.include?(nickname)
end
+ def new_craving(restaurant, date = Date.today)
+ craving = Craving.new
+ craving.restaurant = restaurant
+ craving.user = self
+ craving.date = date
+ craving.save
+ end
+
def is_partnerpedia_employee?
partnerpedia_employee
end
View
32 spec/models/craving_spec.rb
@@ -0,0 +1,32 @@
+require 'spec_helper'
+require_relative '../../app/models/craving'
+
+module LunchZone
+ describe 'craving' do
+ let(:date) { Date.today }
+ let(:craving) { Craving.create(:date => date) }
+ let(:user) { User.create(:nickname => 'hungryguy',
+ :email => 'a@example.com') }
+ let(:restaurant) { Restaurant.create(:name => 'QQ Sushi') }
+
+ it 'belongs to a user' do
+ craving.user = user
+ craving.save
+ craving.reload
+
+ craving.user.should == user
+ end
+
+ it 'is for a restaurant' do
+ craving.restaurant = restaurant
+ craving.save
+ craving.reload
+
+ craving.restaurant.should == restaurant
+ end
+
+ it 'is for a particular day only' do
+ craving.date.should == date
+ end
+ end
+end
View
28 spec/models/restaurant_spec.rb
@@ -0,0 +1,28 @@
+require 'spec_helper'
+require_relative '../../app/models/restaurant'
+
+module LunchZone
+ describe 'user' do
+ let(:today) { Date.today }
+ let(:yesterday) { Date.today - (1 * 60 * 60 * 24) }
+ let(:user1) { User.create(:nickname => 'joe',
+ :email => 'a@example.com') }
+
+ let(:user2) { User.create(:nickname => 'hungryguy',
+ :email => 'b@example.com') }
+
+ let(:restaurant) { Restaurant.create(:name => 'QQ Sushi') }
+
+ before do
+ user1.new_craving(restaurant, today)
+ user2.new_craving(restaurant, today)
+
+ user1.new_craving(restaurant, yesterday)
+ end
+
+ it 'has users via cravings, for a particular day' do
+ restaurant.users_on_date(today).should == [user1, user2]
+ restaurant.users_on_date(yesterday).should == [user1]
+ end
+ end
+end
View
18 spec/models/user.rb
@@ -0,0 +1,18 @@
+require 'spec_helper'
+require_relative '../../app/models/user'
+
+module LunchZone
+ describe 'user' do
+ let(:user) { User.create(:nickname => 'hungryguy',
+ :email => 'a@example.com') }
+ let(:restaurant1) { Restaurant.create(:name => 'QQ Sushi') }
+ let(:restaurant2) { Restaurant.create(:name => 'Academic') }
+
+ it 'has restaurants via cravings' do
+ user.new_craving(restaurant1)
+ user.new_craving(restaurant2)
+
+ user.restaurants.all.should == [restaurant1, restaurant2]
+ end
+ end
+end
View
2  spec/spec_helper.rb
@@ -1,3 +1,5 @@
+APP_ENV = 'test'
+
require 'rspec'
require 'sinatra/base'
require_relative '../config/data_mapper'
Please sign in to comment.
Something went wrong with that request. Please try again.