Skip to content
Browse files

added more tests, refactored track model and controller

  • Loading branch information...
1 parent 8b1b0de commit 1f790d3f5f4fe86b9bbb044adba98a90f38700d0 @stravid stravid committed
View
17 app/controllers/tracks_controller.rb
@@ -59,24 +59,9 @@ def create
format.json do
track = Track.new(params[:track])
- track.active = true
- track.flags = 0
- track.likes = 0
-
- if track.length > 999.9
- track.length = 999.9
- end
-
+
if track.valid?
- track.clean_names
-
if track.save
- marble_run = MarbleRun.first
- marble_run.total_length += track.length
- marble_run.save
-
- Unlock.unlock_bricks
-
redirect_to Track
else
render :partial => "tracks/errors/unable_to_save.json", :status => 500
View
19 app/controllers/unlocks_controller.rb
@@ -1,17 +1,16 @@
class UnlocksController < ApplicationController
def index
- @unlocks = Unlock.where(:is_unlocked => true)
- @locks = Unlock.where(:is_unlocked => false)
+ unlocks = Unlock.all
- unlocks_array = Array.new
- locks_array = Array.new
+ unlocks_array = []
+ locks_array = []
- @unlocks.each do |unlock|
- unlocks_array.push unlock.brick_type
- end
-
- @locks.each do |lock|
- locks_array.push lock.brick_type
+ unlocks.each do |unlock|
+ if unlock.is_unlocked
+ unlocks_array.push unlock.brick_type
+ else
+ locks_array.push unlock.brick_type
+ end
end
hash = Hash.new
View
8 app/models/marble_run.rb
@@ -1,2 +1,10 @@
class MarbleRun < ActiveRecord::Base
+ def self.update_length length
+ instance = MarbleRun.first
+
+ if instance
+ instance.total_length += length
+ instance.save
+ end
+ end
end
View
93 app/models/track.rb
@@ -4,11 +4,28 @@ class Track < ActiveRecord::Base
attr_accessible :json, :username, :trackname, :length, :imagedata
- validate :check_ball
- validate :check_exit
validate :check_bricks
- scope :ordered, lambda {|*args| {:order => (args.first || 'created_at DESC')} }
+ before_save do |track|
+ track.active = true
+ track.flags = 0
+ track.likes = 0
+
+ if !track.length
+ track.length = 1.4
+ elsif track.length > 999.9
+ track.length = 999.9
+ elsif track.length < 1.4
+ track.length = 1.4
+ end
+
+ track.clean_names
+ end
+
+ after_save do |track|
+ MarbleRun.update_length track.length
+ Unlock.unlock_bricks
+ end
def json_track
hash = Hash.new
@@ -26,25 +43,6 @@ def json_track
hash
end
- def clean_names
- random_usernames = Array.new
- random_tracknames = Array.new
-
- random_usernames.push "Anonymous"
- random_usernames.push "Anonymous Architect"
-
- random_tracknames.push "Wild Ride"
- random_tracknames.push "Rollercoaster"
-
- if self.username == "YOUR NAME"
- self.username = random_usernames[rand(random_usernames.length)]
- end
-
- if self.trackname == "TRACK NAME"
- self.trackname = random_tracknames[rand(random_tracknames.length)]
- end
- end
-
def show_response
hash = Hash.new
@@ -54,30 +52,6 @@ def show_response
hash
end
- def check_ball
- hash = ActiveSupport::JSON.decode(self.json)
-
- hash['bricks'].each_value do |brick|
- if brick['type'] == 'Ball'
- return
- end
- end
-
- errors[:json] << "Doesn't include a ball!"
- end
-
- def check_exit
- hash = ActiveSupport::JSON.decode(self.json)
-
- hash['bricks'].each_value do |brick|
- if brick['type'] == 'Exit'
- return
- end
- end
-
- errors[:json] << "Doesn't include an exit!"
- end
-
def check_bricks
unlocks = Unlock.where(:is_unlocked => true)
available_bricks = Array.new
@@ -86,9 +60,20 @@ def check_bricks
available_bricks.push(unlock['brick_type'])
end
+ is_ball_included = false
+ is_exit_included = false
+
hash = ActiveSupport::JSON.decode(self.json)
hash['bricks'].each_value do |brick|
+ if brick['type'] == 'Ball'
+ is_ball_included = true
+ end
+
+ if brick['type'] == 'Exit'
+ is_exit_included = true
+ end
+
if !available_bricks.include?(brick['type'])
if brick['type'] == 'Ball'
if brick['col'] != 0 || brick['row'] != 0
@@ -109,6 +94,9 @@ def check_bricks
end
end
end
+
+ errors[:json] << "Doesn't include a ball!" unless is_ball_included
+ errors[:json] << "Doesn't include an exit!" unless is_exit_included
end
def previous
@@ -119,4 +107,17 @@ def next
self.class.first(:conditions => ["created_at < ? AND active = ?", self.created_at, true], :order => 'created_at DESC')
end
+ protected
+ def clean_names
+ random_usernames = ["Anonymous", "Anonymous Architect"]
+ random_tracknames = ["Wild Ride", "Rollercoaster"]
+
+ if self.username == "YOUR NAME"
+ self.username = random_usernames[rand(random_usernames.length)]
+ end
+
+ if self.trackname == "TRACK NAME"
+ self.trackname = random_tracknames[rand(random_tracknames.length)]
+ end
+ end
end
View
12 app/models/unlock.rb
@@ -1,12 +1,14 @@
class Unlock < ActiveRecord::Base
def self.unlock_bricks
- total_length = MarbleRun.first.total_length
+ marblerun = MarbleRun.first
- unlocks = Unlock.where("minimum_length <= ?", total_length)
+ if marblerun
+ unlocks = Unlock.where("minimum_length <= ?", marblerun.total_length)
- unlocks.each do |unlock|
- unlock.is_unlocked = true;
- unlock.save
+ unlocks.each do |unlock|
+ unlock.is_unlocked = true;
+ unlock.save
+ end
end
end
end
View
28 test/unit/marble_run_test.rb
@@ -1,8 +1,30 @@
require 'test_helper'
class MarbleRunTest < ActiveSupport::TestCase
- # Replace this with your real tests.
- test "the truth" do
- assert true
+ setup do
+ marblerun_instance = MarbleRun.new
+
+ marblerun_instance.total_length = 4
+
+ marblerun_instance.save
+ end
+
+ should "increase the total track length" do
+ MarbleRun.update_length 10
+
+ assert_equal MarbleRun.first.total_length, 14
+ end
+
+ should "decrease the total track length" do
+ MarbleRun.update_length -3
+
+ assert_equal MarbleRun.first.total_length, 1
+ end
+
+ should "in- and decrease the total track length" do
+ MarbleRun.update_length 34
+ MarbleRun.update_length -8
+
+ assert_equal MarbleRun.first.total_length, 30
end
end
View
77 test/unit/track_test.rb
@@ -97,4 +97,81 @@ class TrackTest < ActiveSupport::TestCase
assert !track.valid?, track.errors.to_s
end
end
+
+ context "after_save validations" do
+ setup do
+ marblerun = MarbleRun.new
+ marblerun.total_length = 10
+ marblerun.save
+
+ @valid_track_json = '{"bricks":{"0":{"type":"Ball","rotation":0,"row":0,"col":0},"140":{"type":"Exit","rotation":0,"row":14,"col":0}}}'
+
+ unlock = Unlock.new
+ unlock.brick_type = "Kicker"
+ unlock.minimum_length = 10
+ unlock.is_unlocked = false
+ unlock.save
+ end
+
+ should "update total_length and unlock brick" do
+ track = Track.new
+ track.json = @valid_track_json
+ track.length = 22
+ track.save
+
+ assert_equal MarbleRun.first.total_length, 32
+ assert_equal Unlock.where(:is_unlocked => true).count, 1
+ end
+ end
+
+ context "before_save validations" do
+ setup do
+ @valid_track_json = '{"bricks":{"0":{"type":"Ball","rotation":0,"row":0,"col":0},"140":{"type":"Exit","rotation":0,"row":14,"col":0}}}'
+ end
+
+ should "save the track as given" do
+ track = Track.new
+ track.json = @valid_track_json
+ track.length = 100
+ track.username = "David"
+ track.trackname = "Rollercoaster"
+ track.save
+
+ assert_equal Track.first.length, 100
+ assert_equal Track.first.username, "David"
+ assert_equal Track.first.trackname, "Rollercoaster"
+ assert_equal Track.first.active, true
+ assert_equal Track.first.flags, 0
+ assert_equal Track.first.likes, 0
+ end
+
+ should "save the track with maximum track length" do
+ track = Track.new
+ track.json = @valid_track_json
+ track.length = 1400
+ track.save
+
+ assert_equal Track.first.length, 999.9
+ end
+
+ should "save the track with minimum track length" do
+ track = Track.new
+ track.json = @valid_track_json
+ track.length = -100
+ track.save
+
+ assert_equal Track.first.length, 1.4
+ end
+
+ should "save the track with non-default names" do
+ track = Track.new
+ track.json = @valid_track_json
+ track.trackname = "TRACK NAME"
+ track.username = "YOUR NAME"
+ track.save
+
+ assert_not_equal Track.first.trackname, "TRACK NAME"
+ assert_not_equal Track.first.username, "YOUR NAME"
+ end
+ end
end
View
12 test/unit/unlock_test.rb
@@ -1,8 +1,14 @@
require 'test_helper'
class UnlockTest < ActiveSupport::TestCase
- # Replace this with your real tests.
- test "the truth" do
- assert true
+ should "unlock the Curve brick" do
+ Unlock.create!(:brick_type => "Kicker", :is_unlocked => true, :minimum_length => 0)
+ Unlock.create!(:brick_type => "Curve", :is_unlocked => false, :minimum_length => 10)
+
+ MarbleRun.create!(:total_length => 11)
+
+ Unlock.unlock_bricks
+
+ assert_equal 2, Unlock.where(:is_unlocked => true).count
end
end

0 comments on commit 1f790d3

Please sign in to comment.
Something went wrong with that request. Please try again.