OSDN Git Service

rake annotate_models
[mint/mint-server.git] / app / models / problem.rb
1 # == Schema Information
2 # Schema version: 20100224042103
3 #
4 # Table name: problems
5 #
6 #  id                       :integer       not null, primary key
7 #  unit_id                  :integer       not null
8 #  problem_group_problem_id :integer
9 #  level                    :integer       default(0), not null
10 #  amount                   :integer       default(1), not null
11 #  options                  :text          not null
12 #  lock_version             :integer       default(0), not null
13 #  created_at               :datetime
14 #  updated_at               :datetime
15 #
16
17 class Problem < ActiveRecord::Base
18
19   belongs_to :unit
20
21   has_many :learning_results
22   has_many :problem_groups_problems, :class_name => 'ProblemGroupsProblems', :dependent => :destroy
23   has_many :problem_groups, :through => :problem_groups_problems
24
25   validates_presence_of :unit_id
26
27   validates_presence_of  :level
28   validates_inclusion_of :level, :in => 0..100
29
30   validates_presence_of :amount
31   validates_inclusion_of :amount, :in => 1..100
32
33   validates_presence_of :options
34
35
36   # TODO remove this method
37   def label
38     unit.name
39   end
40
41   def generate
42     o = ActiveSupport::JSON.decode(options).symbolize_keys
43     o.each{|k, v| v.symbolize_keys! if Hash === v }
44     unit.generator(amount).generate(o).map{|v|
45       case v
46       when String then Mint::Builder.build(v)
47       else v
48       end
49     }
50   end
51
52   def solver
53     unit.solver
54   end
55
56 end