OSDN Git Service

add {p,q,r}_min to quadratic equation model
authorokimoto <okimoto@good-day.co.jp>
Thu, 18 Mar 2010 02:21:59 +0000 (11:21 +0900)
committerokimoto <okimoto@good-day.co.jp>
Thu, 18 Mar 2010 02:21:59 +0000 (11:21 +0900)
app/models/quadratic_equation_edit_form.rb
spec/models/quadratic_equation_edit_form_spec.rb

index 608946d..3095927 100644 (file)
@@ -17,28 +17,40 @@ class QuadraticEquationEditForm < ActiveForm
   acts_as_problem
 
   column :answer_type
+  column :r_min, :type => :integer
   column :r_max, :type => :integer
+  column :p_min, :type => :integer
   column :p_max, :type => :integer
+  column :q_min, :type => :integer
   column :q_max, :type => :integer
 
   validates_presence_of :answer_type
   validates_inclusion_of :answer_type, :in => %w[rational irrational imaginary]
 
+  validates_presence_of :r_min
   validates_presence_of :r_max
+  validates_presence_of :p_min
   validates_presence_of :p_max
+  validates_presence_of :q_min
   validates_presence_of :q_max
 
   with_options :only_integer => true, :allow_blank => true do |problem|
+    problem.validates_numericality_of :r_min, :greater_than_or_equal_to => 1
     problem.validates_numericality_of :r_max, :greater_than_or_equal_to => 1
+    problem.validates_numericality_of :p_min, :greater_than_or_equal_to => 0
     problem.validates_numericality_of :p_max, :greater_than_or_equal_to => 0
+    problem.validates_numericality_of :q_min, :greater_than_or_equal_to => 0
     problem.validates_numericality_of :q_max, :greater_than_or_equal_to => 0
   end
 
   def options_hash
     {
       :answer_type => answer_type,
+      :r_min       => r_min,
       :r_max       => r_max,
+      :p_min       => p_min,
       :p_max       => p_max,
+      :q_min       => q_min,
       :q_max       => q_max,
     }
   end
@@ -46,8 +58,11 @@ class QuadraticEquationEditForm < ActiveForm
   def set_options(options)
     hash = ActiveSupport::JSON.decode(options)
     self.answer_type = hash["answer_type"]
+    self.r_min       = hash["r_min"]
     self.r_max       = hash["r_max"]
+    self.p_min       = hash["p_min"]
     self.p_max       = hash["p_max"]
+    self.q_min       = hash["q_min"]
     self.q_max       = hash["q_max"]
   end
 
index 6881693..764bc5a 100644 (file)
@@ -7,8 +7,11 @@ describe QuadraticEquationEditForm do
       :level       => 1,
       :amount      => 2,
       :answer_type => "rational",
+      :r_min       => 1,
       :r_max       => 10,
+      :p_min       => 0,
       :p_max       => 20,
+      :q_min       => 0,
       :q_max       => 30,
     }
     @klass = QuadraticEquationEditForm
@@ -37,8 +40,11 @@ describe QuadraticEquationEditForm do
     end
 
     [
+     [:p_min, -1, 0],
      [:p_max, -1, 0],
+     [:q_min, -1, 0],
      [:q_max, -1, 0],
+     [:r_min, 0, 1],
      [:r_max, 0, 1],
     ].each do |target, ng, ok|
       context target do
@@ -59,8 +65,11 @@ describe QuadraticEquationEditForm do
     it{
       should == {
         :answer_type => "rational",
+        :r_min       => 1,
         :r_max       => 10,
+        :p_min       => 0,
         :p_max       => 20,
+        :q_min       => 0,
         :q_max       => 30,
       }
     }
@@ -71,16 +80,22 @@ describe QuadraticEquationEditForm do
       subject{
         options = {
           :answer_type => "irrational",
+          :r_min       => 1,
           :r_max       => 11,
+          :p_min       => 2,
           :p_max       => 21,
+          :q_min       => 2,
           :q_max       => 31,
         }
         @basic.set_options(options.to_json)
         @basic
       }
       its(:answer_type){ should == "irrational" }
+      its(:r_min){ should == 1  }
       its(:r_max){ should == 11 }
+      its(:p_min){ should == 2  }
       its(:p_max){ should == 21 }
+      its(:q_min){ should == 2  }
       its(:q_max){ should == 31 }
     end
     context "set nothing" do