Scaffolded FarmbotDevice class. Wrote specs

pull/39/head
rick carlino 2014-05-09 06:48:18 -07:00
parent f35c61edaf
commit 99f6755eda
4 changed files with 42 additions and 8 deletions

View File

@ -0,0 +1,4 @@
class FarmbotDevice
include Mongoid::Document
belongs_to :user
end

View File

@ -1,7 +1,8 @@
class User
include Mongoid::Document
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
has_many :farmbot_devices, dependent: :destroy
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
@ -23,6 +24,13 @@ class User
field :current_sign_in_ip, type: String
field :last_sign_in_ip, type: String
# The username.
field :name, type: String
validates_uniqueness_of :name
validates_presence_of :name
end
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
## Confirmable
# field :confirmation_token, type: String
# field :confirmed_at, type: Time
@ -35,9 +43,3 @@ class User
# field :unlock_token, type: String
# Only if unlock strategy is :email or :both
# field :locked_at, type: Time
# The username.
field :name, type: String
validates_uniqueness_of :name
validates_presence_of :name
end

View File

@ -0,0 +1,6 @@
# Read about factories at https://github.com/thoughtbot/factory_girl
FactoryGirl.define do
factory :farmbot_device do
end
end

View File

@ -0,0 +1,22 @@
require 'spec_helper'
describe FarmbotDevice do
let(:user) { FactoryGirl.create(:user) }
let(:device){ FactoryGirl.create(:farmbot_device, user: user)}
it 'is associated with a user' do
expect(device.user).to be_kind_of(User)
expect(user.farmbot_devices).to be_kind_of(Array)
expect(user.farmbot_devices.first).to be_kind_of(FarmbotDevice)
end
it 'destroys dependant devices' do
bot_id = device.id
user_id = user.id
user.destroy
user_results = User.where(id: user_id).first
bot_results = FarmbotDevice.where(id: bot_id).first
expect(bot_results).to be_nil
expect(user_results).to be_nil
end
end