-
Notifications
You must be signed in to change notification settings - Fork 482
/
applab.rb
131 lines (117 loc) · 3.29 KB
/
applab.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# == Schema Information
#
# Table name: levels
#
# id :integer not null, primary key
# game_id :integer
# name :string(255) not null
# created_at :datetime
# updated_at :datetime
# level_num :string(255)
# ideal_level_source_id :integer
# solution_level_source_id :integer
# user_id :integer
# properties :text(65535)
# type :string(255)
# md5 :string(255)
# published :boolean default(FALSE), not null
# notes :text(65535)
#
# Indexes
#
# index_levels_on_game_id (game_id)
# index_levels_on_name (name)
#
require 'cdo/shared_constants'
class Applab < Blockly
before_save :update_json_fields, :validate_maker_if_needed
serialized_attrs %w(
free_play
show_turtle_before_run
autocomplete_palette_apis_only
execute_palette_apis_only
text_mode_at_start
design_mode_at_start
hide_design_mode
beginner_mode
start_html
submittable
log_conditions
data_tables
data_properties
hide_view_data_button
show_debug_watch
expand_debugger
watchers_prepopulated
fail_on_lint_errors
debugger_disabled
makerlab_enabled
teacher_markdown
)
# List of possible skins, the first is used as a default.
def self.skins
['applab']
end
# List of possible palette categories
def self.palette_categories
%w(uicontrols canvas data turtle control math variables functions) +
maker_palette_categories
end
def self.maker_palette_categories
%w(Maker Circuit)
end
def self.create_from_level_builder(params, level_params)
create!(
level_params.merge(
user: params[:user],
game: Game.applab,
level_num: 'custom',
properties: {
code_functions: JSON.parse(palette),
edit_code: true
}
)
)
end
def xml_blocks
%w()
end
def update_palette
if code_functions.present? && code_functions.is_a?(String)
self.code_functions = JSON.parse(code_functions)
end
true
rescue JSON::ParserError => e
errors.add(:code_functions, "#{e.class.name}: #{e.message}")
return false
end
def parse_json_property_field(property_field)
value = properties[property_field]
if value.present? && value.is_a?(String)
properties[property_field] = JSON.parse value
end
true
rescue JSON::ParserError => e
errors.add(property_field, "#{e.class.name}: #{e.message}")
return false
end
def update_json_fields
palette_result = update_palette
log_conditions_result = parse_json_property_field('log_conditions')
success = palette_result && log_conditions_result
throw :abort unless success
end
def validate_maker_if_needed
maker_enabled = properties['makerlab_enabled'] == 'true'
starting_category = properties['palette_category_at_start']
if Applab.maker_palette_categories.include?(starting_category) && !maker_enabled
raise ArgumentError.new(
"Selected '#{starting_category}' as the palette category at start, " \
"but 'Enable Maker APIs' is not checked."
)
end
end
def self.palette
SharedConstants::APPLAB_BLOCKS
end
end