Last active
August 29, 2015 14:17
-
-
Save maxinspace/ab8f952958eeda928079 to your computer and use it in GitHub Desktop.
Vast Tests Crashing Course 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Objective | |
#request | |
def locked? | |
locked_by.present? && | |
[LOCK_STATUSES[:uploading], LOCK_STATUSES[:locked]].include?(lock_status) | |
end | |
#command | |
def release_lock | |
assign_attributes( | |
:locked_by => nil, | |
:locked_on_name => nil, | |
:locked_on_id => nil, | |
:lock_status => LOCK_STATUSES[:unlocked] | |
) | |
end | |
#command | |
def release_lock! | |
release_lock | |
save | |
end | |
end | |
class Book | |
#request | |
def ebook? | |
file.present? | |
end | |
#request | |
def path_prefix | |
Digest::MD5.hexdigest((id || title).to_s)[0..1] | |
end | |
#command | |
def file=(data) | |
unless data.blank? | |
fn = data.original_filename.gsub('/', '_') | |
target_dir = Rails.root.join('public', 'system', path_prefix) | |
FileUtils.mkdir_p(target_dir) unless File.exists?(target_dir) | |
FileUtils.mv(data.tempfile.path, File.join(target_dir, fn)) | |
File.chmod(0644, File.join(target_dir, fn)) | |
self[:file] = fn | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Objective | |
LOCK_STATUSES = { | |
:unlocked => 0, | |
:uploading => 1, | |
:locked => 2 | |
} | |
def locked? | |
locked_by.present? && | |
[LOCK_STATUSES[:uploading], LOCK_STATUSES[:locked]].include?(lock_status) | |
end | |
def release_lock | |
assign_attributes( | |
:locked_by => nil, | |
:locked_on_name => nil, | |
:locked_on_id => nil, | |
:lock_status => LOCK_STATUSES[:unlocked] | |
) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe Objective do | |
let(:user) { create(:user) } | |
let(:objective) { create(:objective) } | |
describe '.locked?' do | |
context 'when locked_by is present' do | |
before { objective.update_attributes(locked_by: user) } | |
it 'returns true' do | |
expect(objective.locked?).to be_true | |
end | |
end | |
context 'when objective.lock_status is not unlocked' do | |
before { objective.update_attributes(lock_status: 2) } | |
it 'returns true' do | |
expect(objective.locked?).to be_true | |
end | |
end | |
context 'when objective is NOT locked' do | |
before { objective.update_attributes(lock_status: 0, locked_by: nil) } | |
it 'returns false' do | |
expect(objective.locked?).to be_falsey | |
end | |
end | |
end | |
describe '.release_lock' do | |
before do | |
objective.update_attributes(locked_by: user, lock_status: 2) | |
expect(objective.locked?).to be_true | |
end | |
it 'unlocks an objective' do | |
expect(objective.release_lock.locked?).to be_falsey | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class User | |
before_validation :set_access_token | |
def date_format | |
super.presence || "%d.%m.%Y" | |
end | |
private | |
def set_access_token | |
self.access_token ||= SecureRandom.uuid | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe User do | |
let(:user) { create(:user) } | |
let!(:token) { SecureRandom.uuid } | |
describe '.before_validation' do | |
context 'when user has set access token' do | |
before { user.update_attributes(access_token: token) } | |
it 'is valid' do | |
expect(user.valid?).to be_true | |
end | |
end | |
context 'when user does NOT have access token' do | |
before { user.update_attributes(access_token: nil) } | |
it 'is not' do | |
expect(user.valid?).to be_falsey | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment