OSDN Git Service

Add Unit model, and many fixes to use it
authorokimoto <okimoto@good-day.co.jp>
Wed, 24 Feb 2010 13:12:35 +0000 (22:12 +0900)
committerokimoto <okimoto@good-day.co.jp>
Wed, 24 Feb 2010 13:12:35 +0000 (22:12 +0900)
app/models/problem.rb
app/models/problem_group.rb
app/models/unit.rb
db/seeds.rb
spec/factories/problem.rb
spec/factories/problem_group.rb
spec/factories/unit.rb [new file with mode: 0644]
spec/models/learning_result_spec.rb
spec/models/problem_group_spec.rb
spec/models/problem_spec.rb
spec/models/report_spec.rb

index 3ed8d6e..e95ebef 100644 (file)
@@ -39,7 +39,7 @@ class Problem < ActiveRecord::Base
   def generate
     o = ActiveSupport::JSON.decode(options).symbolize_keys
     o.each{|k, v| v.symbolize_keys! if Hash === v }
-    generator.generate(o).map{|v|
+    unit.generator(amount).generate(o).map{|v|
       case v
       when String then Mint::Builder.build(v)
       else v
@@ -48,13 +48,7 @@ class Problem < ActiveRecord::Base
   end
 
   def solver
-    Mint::Solver::Maxima::Factory.create(unit)
-  end
-
-  private
-
-  def generator
-    Mint::Generator::Factory.create(unit, amount)
+    unit.solver
   end
 
 end
index d5d1bed..949df7d 100644 (file)
@@ -14,7 +14,7 @@
 
 class ProblemGroup < ActiveRecord::Base
   has_many :problem_groups_problems, :class_name => 'ProblemGroupsProblems', :dependent => :destroy
-  has_many :problems, :through => :problem_groups_problems, :order => 'unit, level ASC'
+  has_many :problems, :through => :problem_groups_problems
   has_many :learning_results
   has_many :reports
 
index bb3e993..6e8a4af 100644 (file)
@@ -5,7 +5,16 @@ class Unit < ActiveRecord::Base
   validates_presence_of :name
   validates_length_of :name, :maximum => 40
 
-  validates_presence_of :symbol
-  validates_length_of   :symbol, :maximum => 40, :allow_nil => true
-  validates_format_of   :symbol, :with => /\A[a-z_]+\z/
+  validates_presence_of   :symbol
+  validates_length_of     :symbol, :maximum => 40, :allow_nil => true
+  validates_format_of     :symbol, :with => /\A[a-z_]+\z/
+  validates_uniqueness_of :symbol
+
+  def generator(amount)
+    Mint::Generator::Factory.create(symbol, amount)
+  end
+
+  def solver
+    Mint::Solver::Maxima::Factory.create(symbol)
+  end
 end
index 440f20b..315ba09 100644 (file)
@@ -42,13 +42,13 @@ ActiveRecord::Base.transaction do
    ["ordinary_arithmetic"              , "整数の四則演算"],
    ["decimal_arithmetic"               , "小数の四則演算"],
    ["fractional_arithmetic"            , "分数の四則演算"],
+   ["square_root_arithmetic"           , "根号を含む数の計算"],
+   ["complex_number_arithmetic"        , "複素数の計算"],
    ["expansion"                        , "式の展開"],
    ["factorization"                    , "因数分解"],
-   ["complex_number_arithmetic"        , "複素数の計算"],
    ["fractional_expression_arithmetic" , "分数式の計算"],
-   ["linear_function_graph"            , "一次関数のグラフ"],
    ["partial_fraction_expansion"       , "部分分数の展開"],
-   ["square_root_arithmetic"           , "根号を含む数の計算"],
+   ["linear_function_graph"            , "一次関数のグラフ"],
   ].each do |symbol, name|
     Unit.create!(:symbol => symbol, :name => name)
   end
index 731e426..f25ddcf 100644 (file)
@@ -1,13 +1,12 @@
 Factory.define :problem do |m|
   m.label "label"
-  m.unit "unit"
   m.level 1
   m.amount 2
   m.options({ :term_number => 2, :operators => %w[+ - * div] }.to_json)
 end
 
-Factory.define :ordinary_arithmetic, :parent => :problem do |m|
-  m.unit "ordinary_arithmetic"
+Factory.define :ordinary_arithmetic_level_1, :parent => :problem do |m|
+  m.unit{|u| u.association(:ordinary_arithmetic) }
   m.level 1
   m.options({
               :minus => true, :min => 0, :max => 100,
@@ -16,8 +15,8 @@ Factory.define :ordinary_arithmetic, :parent => :problem do |m|
             }.to_json)
 end
 
-Factory.define :decimal_arithmetic, :parent => :problem do |m|
-  m.unit "decimal_arithmetic"
+Factory.define :decimal_arithmetic_level_1, :parent => :problem do |m|
+  m.unit{|u| u.association "decimal_arithmetic" }
   m.level 1
   m.options({
               :minus => true, :digits => 2, :min => 0, :max => 10.0,
@@ -26,8 +25,8 @@ Factory.define :decimal_arithmetic, :parent => :problem do |m|
             }.to_json)
 end
 
-Factory.define :fractional_arithmetic, :parent => :problem do |m|
-  m.unit "fractional_arithmetic"
+Factory.define :fractional_arithmetic_level_1, :parent => :problem do |m|
+  m.unit{ u.association "fractional_arithmetic" }
   m.level 1
   m.options({
               :minus => true,
@@ -36,8 +35,8 @@ Factory.define :fractional_arithmetic, :parent => :problem do |m|
             }.to_json)
 end
 
-Factory.define :factorization, :parent => :problem do |m|
-  m.unit "factorization"
+Factory.define :factorization_level_1, :parent => :problem do |m|
+  m.unit{|u| u.association  "factorization" }
   m.level 1
   m.options({
               :order_min => 2, :order_max => 3, :x => %w[x y z],
@@ -45,8 +44,8 @@ Factory.define :factorization, :parent => :problem do |m|
             }.to_json)
 end
 
-Factory.define :expansion, :parent => :problem do |m|
-  m.unit "expansion"
+Factory.define :expansion_level_1, :parent => :problem do |m|
+  m.unit{|u| u.association "expansion" }
   m.level 1
   m.options({
               :order_min => 2, :order_max => 3, :x => %w[x y z],
index c564a24..8cb98cd 100644 (file)
@@ -1,4 +1,4 @@
 Factory.define :problem_group do |m|
   m.label 'label'
-  m.problems{|n| [n.association(:ordinary_arithmetic)] }
+  m.problems{|n| [n.association(:ordinary_arithmetic_level_1)] }
 end
diff --git a/spec/factories/unit.rb b/spec/factories/unit.rb
new file mode 100644 (file)
index 0000000..fda864e
--- /dev/null
@@ -0,0 +1,24 @@
+# -*- coding: utf-8 -*-
+Factory.define :unit do |u|
+  u.name 'unit'
+  u.symbol 'unit'
+end
+
+[
+ ["ordinary_arithmetic"              , "整数の四則演算"],
+ ["decimal_arithmetic"               , "小数の四則演算"],
+ ["fractional_arithmetic"            , "分数の四則演算"],
+ ["expansion"                        , "式の展開"],
+ ["factorization"                    , "因数分解"],
+ ["complex_number_arithmetic"        , "複素数の計算"],
+ ["fractional_expression_arithmetic" , "分数式の計算"],
+ ["partial_fraction_expansion"       , "部分分数の展開"],
+ ["square_root_arithmetic"           , "根号を含む数の計算"],
+ ["linear_function_graph"            , "一次関数のグラフ"],
+].each do |symbol, name|
+  Factory.define symbol.to_sym, :parent => :unit do |u|
+    u.name name
+    u.symbol symbol
+  end
+end
+
index ce38cb2..0727083 100644 (file)
@@ -38,7 +38,7 @@ describe LearningResult do
     describe "before_save" do
       before do
         user = Factory(:user)
-        problem = Factory(:ordinary_arithmetic)
+        problem = Factory(:ordinary_arithmetic_level_1)
         solver = mock('solver')
         answer = mock('answer')
         answer.should_receive(:normalize).at_least(1).and_return("2")
index 7cf50af..0ee3a54 100644 (file)
@@ -8,7 +8,7 @@ describe ProblemGroup do
     }
     @klass = ProblemGroup
     @basic = @klass.new(@valid_attributes)
-    @basic.problems << Factory.build(:ordinary_arithmetic)
+    @basic.problems << Factory.build(:ordinary_arithmetic_level_1)
   end
 
   describe "basic" do
@@ -37,7 +37,7 @@ describe ProblemGroup do
   describe "association" do
     describe "has_many problms" do
       before do
-        @basic.problems << Factory(:expansion)
+        @basic.problems << Factory(:expansion_level_1)
         @basic.save!
         @basic.reload
       end
index 7e2f327..4016646 100644 (file)
@@ -4,7 +4,7 @@ describe Problem do
   before(:each) do
     @valid_attributes = {
       :label   => "label",
-      :unit    => "unit",
+      :unit_id => 1,
       :level   => 1,
       :amount  => 2,
       :options => "options",
@@ -37,29 +37,9 @@ describe Problem do
       end
     end
 
-    describe "unit" do
+    describe "unit_id" do
       describe "is nil" do
-        subject{ @basic.unit = nil; @basic }
-        it{ should_not be_valid }
-      end
-      describe "is empty" do
-        subject{ @basic.unit = ''; @basic }
-        it{ should_not be_valid }
-      end
-      describe "length 40" do
-        subject{ @basic.unit = "a"*40; @basic }
-        it{ should be_valid }
-      end
-      describe "length 41" do
-        subject{ @basic.unit = "a"*41; @basic }
-        it{ should_not be_valid }
-      end
-      describe "valid format" do
-        subject{ @basic.unit = "a"; @basic }
-        it{ should be_valid }
-      end
-      describe "invalid format" do
-        subject{ @basic.unit = "AAAAA"; @basic }
+        subject{ @basic.unit_id = nil; @basic }
         it{ should_not be_valid }
       end
     end
@@ -132,6 +112,7 @@ describe Problem do
         Mint::Builder.should_receive(:build).at_least(1).and_return(@expression)
         @basic.options = { }.to_json
         @problem = @basic.dup
+        @problem.unit = Factory(:ordinary_arithmetic)
       end
       it{ @problem.generate.first.should == @expression }
     end
@@ -142,6 +123,7 @@ describe Problem do
         Mint::Generator::Factory.should_receive(:create).and_return(generator)
         @basic.options = { }.to_json
         @problem = @basic.dup
+        @problem.unit = Factory(:linear_function_graph)
       end
       it{ @problem.generate.first.should == { } }
     end
index b82a8f3..b90e61f 100644 (file)
@@ -75,7 +75,8 @@ describe Report do
         user = Factory(:user)
         admin = Factory(:admin)
         @problem_group = Factory(:problem_group)
-        @problem_group2 = Factory(:problem_group, :label => "2", :problems => [Factory(:decimal_arithmetic)])
+        @problem_group2 = Factory(:problem_group, :label => "2",
+                                  :problems => [Factory(:decimal_arithmetic_level_1)])
         Factory(:report, :user => user, :problem_group => @problem_group)
         Factory(:report, :user => user, :problem_group => @problem_group)
         Factory(:report, :user => user, :problem_group => @problem_group)