Skip to content

Commit 98f018c

Browse files
committed
production -> test
2 parents 2ce839e + dd13405 commit 98f018c

File tree

2 files changed

+24
-50
lines changed

2 files changed

+24
-50
lines changed

dashboard/app/controllers/script_levels_controller.rb

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,7 @@ def load_script_level
8282
if params[:chapter]
8383
@script_level = @script.get_script_level_by_chapter(params[:chapter])
8484
elsif params[:stage_id]
85-
if @script.cached?
86-
@script_level = @script.script_levels.select{|sl| sl.stage.position == params[:stage_id].to_i && sl.position == params[:id].to_i}.first
87-
else
88-
@script_level = @script.get_script_level_by_stage_and_position(params[:stage_id], params[:id])
89-
end
85+
@script_level = @script.get_script_level_by_stage_and_position(params[:stage_id], params[:id])
9086
else
9187
@script_level = @script.get_script_level_by_id(params[:id])
9288
end
@@ -96,15 +92,13 @@ def load_script_level
9692
def present_level
9793
@level = @script_level.level
9894
@game = @level.game
99-
if @script.cached?
100-
@stage = @script.stages.to_a.find{|stage| stage.id == @script_level.stage_id}
101-
if @stage
102-
@game_script_levels = @script.script_levels.to_a.select{|sl| sl.stage_id == @script_level.stage_id}
103-
else
104-
@game_script_levels = @script.script_levels.to_a.select{|sl| sl.level.game_id == @script_level.level.game_id}
105-
end
95+
96+
@stage = @script_level.stage # this should be included
97+
cached_script = Script.get_from_cache(@script.id)
98+
if @stage
99+
@game_script_levels = cached_script.script_levels.to_a.select{|sl| sl.stage_id == @script_level.stage_id}
106100
else
107-
@stage = @script_level.stage
101+
@game_script_levels = cached_script.script_levels.to_a.select{|sl| sl.level.game_id == @script_level.level.game_id}
108102
end
109103

110104
set_videos_and_blocks_and_callouts_and_instructions

dashboard/app/models/script.rb

Lines changed: 17 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -30,40 +30,16 @@ class Script < ActiveRecord::Base
3030
FLAPPY_NAME = 'flappy'
3131
TWENTY_HOUR_NAME = '20-hour'
3232

33-
def self.twenty_hour_script
34-
@@twenty_hour_script ||= Script.includes(script_levels: {level: [:game, :concepts] }).find(TWENTY_HOUR_ID)
35-
end
36-
37-
def self.frozen_script
38-
@@frozen_script ||= Script.includes([{script_levels: [{level: [:game, :concepts] }, :stage]}, :stages]).find_by_name(FROZEN_NAME)
39-
end
40-
41-
def self.hoc_script
42-
@@hoc_script ||= Script.includes([{script_levels: [{level: [:game, :concepts] }, :stage]}, :stages]).find_by_name(HOC_NAME)
43-
end
44-
45-
def self.flappy_script
46-
@@flappy_script ||= Script.includes([{script_levels: [{level: [:game, :concepts] }, :stage]}, :stages]).find(FLAPPY_ID)
47-
end
48-
49-
def self.playlab_script
50-
@@playlab_script ||= Script.includes([{script_levels: [{level: [:game, :concepts] }, :stage]}, :stages]).find_by_name(PLAYLAB_NAME)
51-
end
52-
53-
def cached?
54-
self.equal?(Script.twenty_hour_script) ||
55-
self.equal?(Script.frozen_script) ||
56-
self.equal?(Script.hoc_script) ||
57-
self.equal?(Script.flappy_script) ||
58-
self.equal?(Script.playlab_script)
33+
def Script.twenty_hour_script
34+
Script.get_from_cache(Script::TWENTY_HOUR_ID)
5935
end
6036

6137
def Script.should_be_cached?(id)
6238
Script.script_cache.has_key?(id.to_s)
6339
end
6440

6541
def should_be_cached?
66-
Script.script_cache.has_key?(id.to_s)
42+
Script.should_be_cached?(id.to_s)
6743
end
6844

6945
def starting_level
@@ -90,10 +66,13 @@ def self.script_cache_to_redis
9066

9167
def self.script_cache_from_db
9268
{}.tap do |cache|
93-
[twenty_hour_script, frozen_script, hoc_script, flappy_script, playlab_script].each do |script|
94-
cache[script.name] = script
95-
cache[script.id.to_s] = script
96-
end
69+
[FLAPPY_ID, TWENTY_HOUR_ID, PLAYLAB_NAME, HOC_NAME, FROZEN_NAME].each do |id|
70+
find_by = (id.to_i.to_s == id.to_s) ? :id : :name
71+
script = Script.includes([{script_levels: [{level: [:game, :concepts] }, :stage]}, :stages]).
72+
find_by(find_by => id)
73+
cache[script.name] = script
74+
cache[script.id.to_s] = script
75+
end
9776
end
9877
end
9978

@@ -167,7 +146,13 @@ def get_script_level_by_id(script_level_id)
167146
end
168147

169148
def get_script_level_by_stage_and_position(stage_position, puzzle_position)
170-
self.stages.find_by!(position: stage_position).script_levels.find_by!(position: puzzle_position)
149+
if should_be_cached?
150+
Script.get_from_cache(id).script_levels.to_a.find do |sl|
151+
sl.stage.position == stage_position.to_i && sl.position == puzzle_position.to_i
152+
end
153+
else
154+
self.stages.find_by!(position: stage_position).script_levels.find_by!(position: puzzle_position)
155+
end
171156
end
172157

173158
def get_script_level_by_chapter(chapter)
@@ -372,11 +357,6 @@ def self.update_i18n(custom_i18n)
372357
private
373358
def Script.clear_cache
374359
# only call this in a test!
375-
@@twenty_hour_script = nil
376-
@@frozen_script = nil
377-
@@hoc_script = nil
378-
@@flappy_script = nil
379-
@@playlab_script = nil
380360
@@script_cache = nil
381361
end
382362
end

0 commit comments

Comments
 (0)