Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

using DB to save counter and updating tests

  • Loading branch information...
commit b09d936a3197334029e348f72b540cb3e7cf3993 1 parent fa27546
Celso Dantas authored
49 lib/protokoll.rb
View
@@ -1,47 +1,6 @@
-require "protokoll/auto_increment"
-require "protokoll/extract_number"
-require "protokoll/class_variable"
-
-module Protokoll
- extend ActiveSupport::Concern
-
- module ClassMethods
-
- # Class method available in models
- #
- # == Example
- # class Order < ActiveRecord::Base
- # protokoll :number
- # end
- #
- def protokoll(column = nil, _options = {})
- ClassVariable.add_to self, :@@protokoll, :default => AutoIncrement.new
-
- prot = ClassVariable.get_from self, :@@protokoll
- return prot if column.nil?
-
- options = { :pattern => "%Y%m#####", :number_symbol => "#"}
- options.merge!(_options)
-
- prot.options = options
-
- before_create do |record|
- last = record.class.last
-
- if last.present?
- if prot.outdated?(last)
- prot.count = 0
- else
- prot.count = ExtractNumber.number(last[column], options[:pattern])
- end
- end
-
- prot.count += 1
- record[column] = prot.next_custom_number(prot.count)
- end
- end
- end
-
-end
+require "protokoll/models/custom_auto_increment"
+require "protokoll/formater"
+require "protokoll/counter"
+require "protokoll/protokoll"
ActiveRecord::Base.send :include, Protokoll
115 lib/protokoll/auto_increment.rb
View
@@ -1,115 +0,0 @@
-module Protokoll
-
- class AutoIncrement
- attr_accessor :options
- attr_accessor :count
-
- # Class initializer
- # creates counter (starts with 0) and when before
- # the model is created runs @count+1
- def initialize
- @count = 0
- end
-
- # sets the pattern to be used. Default: %Y%m##### = 20110900001, 20110900002...
- def pattern=(p)
- options[:pattern] = p
- end
-
- # get the pattern
- def pattern
- options[:pattern]
- end
-
- # sets the symbol to be used as a counter digit. Default: #
- def number_symbol=(s)
- options[:number_symbol] = s
- end
-
- # get the symbol to be used as a counter digit. Default: #
- def number_symbol
- options[:number_symbol]
- end
-
- # gets the next number.
- # it prepends the prefix + counter + sufix
- # ex:
- # "%Y####BANK"
- # %Y => prefix (year)
- # #### => counter (starts with 0001)
- # BANK => sufix
- #
- # if we are in 2011, the first model to be saved will get "20110001BANK"
- # the next model to be saved will get "20110002BANK", "20110003BANK"...
- #
- # number => is the counter
- #
- # next_custom_number(1)
- # => "20110001BANK"
- def next_custom_number(number)
- prefix(options[:pattern]).to_s +
- counter(options[:pattern], number).to_s +
- sufix(options[:pattern]).to_s
- end
-
- def prefix(pattern)
- prefx = extract_prefix(pattern)
- expand_times(prefx.to_s)
- end
-
- def counter(pattern, n)
- format_counter(digits_size(pattern), n)
- end
-
- def sufix(pattern)
- sufx = extract_sufix(pattern)
- expand_times(sufx.to_s)
- end
-
- def format_counter(zeros, value)
- "%0#{zeros}d" % value
- end
-
- def extract_prefix(pattern)
- # Company#### => Company
- symbol = options[:number_symbol]
- (pattern =~ /^(\s|\d)*[^#{symbol}]+/ and $&)
- end
-
- def extract_sufix(pattern)
- # ###Company => Company
- symbol = options[:number_symbol]
- (pattern =~ /[^#{symbol}]+$/ and $&)
- end
-
- def expand_times(pattern)
- pattern.sub!("%y", Time.now.strftime("%y"))
- pattern.sub!("%Y", Time.now.strftime("%Y"))
- pattern.sub!("%d", Time.now.strftime("%d"))
- pattern.sub!("%m", Time.now.strftime("%m"))
- pattern.sub!("%M", Time.now.strftime("%M"))
- pattern.sub("%H", Time.now.strftime("%H"))
- end
-
- def digits_size(pattern)
- symbol = options[:number_symbol]
- (pattern =~ /[#{symbol}]+/ and $&).length
- end
-
- def outdated?(record)
- Time.now.strftime(update_event).to_i > record.created_at.strftime(update_event).to_i
- end
-
- def update_event
- pattern = options[:pattern]
- event = String.new
-
- event += "%Y" if pattern.include? "%y" or pattern.include? "%Y"
- event += "%m" if pattern.include? "%m"
- event += "%H" if pattern.include? "%H"
- event += "%M" if pattern.include? "%M"
- event
- end
-
- end
-end
13 lib/protokoll/class_variable.rb
View
@@ -1,13 +0,0 @@
-module Protokoll
- module ClassVariable
- def self.add_to(klass, instance, options = nil)
- if not klass.class_variables.include? instance
- klass.class_variable_set instance, options[:default]
- end
- end
-
- def self.get_from(klass, instance)
- klass.class_variable_get(instance)
- end
- end
-end
33 lib/protokoll/counter.rb
View
@@ -0,0 +1,33 @@
+require 'active_record'
+
+module Protokoll
+ class Counter
+ def self.next(object, options)
+ element = Models::CustomAutoIncrement.find_or_create_by_model_name(object.class.to_s.underscore)
+
+ element.counter = 0 if outdated?(element, options)
+ element.counter += 1
+
+ element.save!
+
+ Formater.new.format(element.counter, options)
+ end
+
+ private
+
+ def self.outdated?(record, options)
+ Time.now.strftime(update_event(options)).to_i > record.created_at.strftime(update_event(options)).to_i
+ end
+
+ def self.update_event(options)
+ pattern = options[:pattern]
+ event = String.new
+
+ event += "%Y" if pattern.include? "%y" or pattern.include? "%Y"
+ event += "%m" if pattern.include? "%m"
+ event += "%H" if pattern.include? "%H"
+ event += "%M" if pattern.include? "%M"
+ event
+ end
+ end
+end
25 lib/protokoll/extract_number.rb
View
@@ -1,25 +0,0 @@
-module Protokoll
- module ExtractNumber
- def self.create_number_mask(format)
- format.split("").map do |i|
- (i == "#") ? 1 : 0
- end
- end
-
- def self.extract_using_mask(db_number, mask)
- result = db_number.split("").select.with_index do |n, i|
- n if mask[i] == 1
- end
- result.join.to_i
- end
-
- def self.number(str, pattern)
- mask = create_number_mask( expand_pattern(pattern) )
- extract_using_mask(str, mask)
- end
-
- def self.expand_pattern(pattern)
- pattern.sub("%Y", "0000")
- end
- end
-end
79 lib/protokoll/formater.rb
View
@@ -0,0 +1,79 @@
+
+module Protokoll
+ class Formater
+ def format(number, options)
+ @options = options
+
+ build(number)
+ end
+
+ private
+
+ # gets the next number.
+ # it prepends the prefix + counter + sufix
+ # ex:
+ # "%Y####BANK"
+ # %Y => prefix (year)
+ # #### => counter (starts with 0001)
+ # BANK => sufix
+ #
+ # if we are in 2011, the first model to be saved will get "20110001BANK"
+ # the next model to be saved will get "20110002BANK", "20110003BANK"...
+ #
+ # number => is the counter
+ #
+ # next_custom_number(1)
+ # => "20110001BANK"
+ def build(number)
+ prefix(@options[:pattern]).to_s +
+ counter(@options[:pattern], number).to_s +
+ sufix(@options[:pattern]).to_s
+ end
+
+ def prefix(pattern)
+ prefx = extract_prefix(pattern)
+ expand_times(prefx.to_s)
+ end
+
+ def counter(pattern, n)
+ format_counter(digits_size(pattern), n)
+ end
+
+ def sufix(pattern)
+ sufx = extract_sufix(pattern)
+ expand_times(sufx.to_s)
+ end
+
+ def format_counter(zeros, value)
+ "%0#{zeros}d" % value
+ end
+
+ def extract_prefix(pattern)
+ # Company#### => Company
+ symbol = @options[:number_symbol]
+ (pattern =~ /^(\s|\d)*[^#{symbol}]+/ and $&)
+ end
+
+ def extract_sufix(pattern)
+ # ###Company => Company
+ symbol = @options[:number_symbol]
+ (pattern =~ /[^#{symbol}]+$/ and $&)
+ end
+
+ def expand_times(pattern)
+ pattern.sub!("%y", Time.now.strftime("%y"))
+ pattern.sub!("%Y", Time.now.strftime("%Y"))
+ pattern.sub!("%d", Time.now.strftime("%d"))
+ pattern.sub!("%m", Time.now.strftime("%m"))
+ pattern.sub!("%M", Time.now.strftime("%M"))
+ pattern.sub("%H", Time.now.strftime("%H"))
+ end
+
+ def digits_size(pattern)
+ symbol = @options[:number_symbol]
+ (pattern =~ /[#{symbol}]+/ and $&).length
+ end
+
+
+ end
+end
8 lib/protokoll/models/custom_auto_increment.rb
View
@@ -0,0 +1,8 @@
+require "active_record/base"
+
+module Protokoll
+ module Models
+ class CustomAutoIncrement < ActiveRecord::Base
+ end
+ end
+end
34 lib/protokoll/protokoll.rb
View
@@ -0,0 +1,34 @@
+module Protokoll
+ extend ActiveSupport::Concern
+
+ module ClassMethods
+
+ # Class method available in models
+ #
+ # == Example
+ # class Order < ActiveRecord::Base
+ # protokoll :number
+ # end
+ #
+ def protokoll(column, _options = {})
+ options = { :pattern => "%Y%m#####",
+ :number_symbol => "#",
+ :column => column }
+
+ options.merge!(_options)
+
+ # Defining custom method
+ send :define_method, "reserve_#{options[:column]}!".to_sym do
+ self[column] = Counter.next(self, options)
+ end
+
+ # Signing before_create
+ before_create do |record|
+ return if record[column].present?
+
+ record[column] = Counter.next(self, options)
+ end
+ end
+ end
+
+end
2  lib/protokoll/version.rb
View
@@ -1,3 +1,3 @@
module Protokoll
- VERSION = "0.3.1"
+ VERSION = "0.4.0"
end
3  test/dummy/app/models/call.rb
View
@@ -1,3 +0,0 @@
-class Call < ActiveRecord::Base
- protokoll :number
-end
7 test/dummy/app/models/call_defined_in_tests.rb
View
@@ -0,0 +1,7 @@
+# As I need to change the class a lot, I'm defining (and redefining tons of times)
+# in the tests files.
+# I'm just keeping the model here to make easy to understand what models
+# are been used in the test.
+
+# class Call < ActiveRecord::Base
+# end
2  test/dummy/app/models/principal.rb
View
@@ -1,2 +0,0 @@
-class Principal < ActiveRecord::Base
-end
3  test/dummy/app/models/protocol.rb
View
@@ -1,3 +0,0 @@
-class Protocol < ActiveRecord::Base
- protokoll :number
-end
7 test/dummy/app/models/protocol_defined_in_tests.rb
View
@@ -0,0 +1,7 @@
+# As I need to change the class a lot, I'm defining (and redefining tons of times)
+# in the tests files.
+# I'm just keeping the model here to make easy to understand what models
+# are been used in the test.
+
+# class Protocol < ActiveRecord::Base
+# end
9 test/dummy/db/migrate/20110923101536_create_principals.rb
View
@@ -1,9 +0,0 @@
-class CreatePrincipals < ActiveRecord::Migration
- def change
- create_table :principals do |t|
- t.string :protokoll_number
-
- t.timestamps
- end
- end
-end
15 test/dummy/db/migrate/20120222164124_create_custom_auto_increments.rb
View
@@ -0,0 +1,15 @@
+class CreateCustomAutoIncrements < ActiveRecord::Migration
+ def up
+ create_table :custom_auto_increments, :force => true do |t|
+ t.string :model_name
+ t.integer :counter, :default => 0
+ t.timestamps
+ end
+
+ add_index :custom_auto_increments, :model_name
+ end
+
+ def down
+ drop_table :custom_auto_increments
+ end
+end
23 test/dummy/db/schema.rb
View
@@ -11,24 +11,33 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20110928013630) do
+ActiveRecord::Schema.define(:version => 20120222164124) do
create_table "calls", :force => true do |t|
t.string "number"
- t.datetime "created_at"
- t.datetime "updated_at"
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
end
+ create_table "custom_auto_increments", :force => true do |t|
+ t.string "model_name"
+ t.integer "counter", :default => 0
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
+ end
+
+ add_index "custom_auto_increments", ["model_name"], :name => "index_custom_auto_increments_on_model_name"
+
create_table "principals", :force => true do |t|
t.string "protokoll_number"
- t.datetime "created_at"
- t.datetime "updated_at"
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
end
create_table "protocols", :force => true do |t|
t.string "number"
- t.datetime "created_at"
- t.datetime "updated_at"
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
end
end
448 test/protokoll_test.rb
View
@@ -5,277 +5,343 @@ class ProtokollTest < ActiveSupport::TestCase
def setup
time = Time.local(2011, 9, 25, 12, 0, 0)
Timecop.travel(time)
-
- Protocol.destroy_all
- Protocol.protokoll.count = 0
end
+ def teardown
+ reset_models
+ end
+
+ def reset_models
+ ProtokollTest.send(:remove_const, 'Protocol') if defined? Protocol
+ ProtokollTest.send(:remove_const, 'Call') if defined? Call
+ end
+
+ ##
+ # Tests
+ ##
+
test "using new number_symbol with default pattern should get 20110900001" do
- Protocol.protokoll.number_symbol = "$"
- Protocol.protokoll.pattern = "%Y%m$$$$$"
-
- protocol1 = Protocol.create
- assert_equal "20110900001", protocol1.number
- end
+ class Protocol < ActiveRecord::Base
+ protokoll :number, :pattern => "%Y%m$$$$$", :number_symbol => "$"
+ end
- test "first using # should get 1" do
- Protocol.protokoll.pattern = "#"
-
protocol = Protocol.create
- assert_equal "1", protocol.number
+ assert_equal "20110900001", protocol.number
end
- test "second protocol on DB should have number should equals 2" do
- Protocol.protokoll.pattern = "#"
+ test "should get 20110900001 (format number %Y%m#####)" do
+ class Protocol < ActiveRecord::Base
+ protokoll :number
+ end
- Protocol.create
protocol = Protocol.create
- assert_equal "2", protocol.number
+ assert_equal "20110900001", protocol.number
end
- test "third protocol on DB should have number should equals 3" do
- Protocol.protokoll.pattern = "#"
-
+ test "should get 20110900002 for second save (format number %Y%m#####)" do
+ class Protocol < ActiveRecord::Base
+ protokoll :number
+ end
+
protocol1 = Protocol.create
protocol2 = Protocol.create
- protocol3 = Protocol.create
- assert_equal "3", protocol3.number
+
+ assert_equal "20110900002", protocol2.number
end
+
+ test "should get 201100002 for second save (format number %Y#####)" do
+ class Protocol < ActiveRecord::Base
+ protokoll :number, :pattern => "%Y#####"
+ end
- test "first using format A# should get A1" do
- Protocol.protokoll.pattern = "A#"
+ protocol = Protocol.create
+
+ assert_equal "201100001", protocol.number
+ end
- protocol1 = Protocol.create
- assert_equal "A1", protocol1.number
+ test "second protocol on DB should have number should equals 1" do
+ class Protocol < ActiveRecord::Base
+ protokoll :number, :pattern => "#"
+ end
+
+ protocol = Protocol.create
+ assert_equal "1", protocol.number
end
+
+ test "third protocol on DB should have number should equals 3" do
+ class Protocol < ActiveRecord::Base
+ protokoll :number, :pattern => "#"
+ end
- test "second using format A# should get A2" do
- Protocol.protokoll.pattern = "A#"
+ Protocol.new.save
+ Protocol.new.save
+ protocol3 = Protocol.new
+ protocol3.save
+
+ assert_equal "3", protocol3.number
+ end
+
+ test "first using format A# should get A1" do
+ class Protocol < ActiveRecord::Base
+ protokoll :number, :pattern => "A#"
+ end
- protocol1 = Protocol.create
- protocol2 = Protocol.create
- assert_equal "A2", protocol2.number
+ protocol1 = Protocol.new
+ protocol1.save
+
+ assert_equal "A1", protocol1.number
end
test "first using format A## should get A01" do
- Protocol.protokoll.pattern = "A##"
+ class Protocol < ActiveRecord::Base
+ protokoll :number, :pattern => "A##"
+ end
protocol1 = Protocol.create
assert_equal "A01", protocol1.number
end
-
+
test "second using format A## should get A02" do
- Protocol.protokoll.pattern = "A##"
+ class Protocol < ActiveRecord::Base
+ protokoll :number, :pattern => "A##"
+ end
protocol1 = Protocol.create
protocol2 = Protocol.create
assert_equal "A02", protocol2.number
end
-
+
test "third using format A## should get A03" do
- Protocol.protokoll.pattern = "A##"
+ class Protocol < ActiveRecord::Base
+ protokoll :number, :pattern => "A##"
+ end
protocol1 = Protocol.create
protocol2 = Protocol.create
protocol3 = Protocol.create
assert_equal "A03", protocol3.number
end
-
+
test "first use of %y# should get 111" do
- Protocol.protokoll.pattern = "%y#"
+ class Protocol < ActiveRecord::Base
+ protokoll :number, :pattern => "%y#"
+ end
protocol1 = Protocol.create
assert_equal "111", protocol1.number
end
- test "second use of %y## should get 1101" do
- Protocol.protokoll.pattern = "%y##"
+ #
+ test "first use of %y%m## should get 110901" do
+ class Protocol < ActiveRecord::Base
+ protokoll :number, :pattern => "%y%m##"
+ end
protocol1 = Protocol.create
- assert_equal "1101", protocol1.number
-
- protocol2 = Protocol.create
- assert_equal "1102", protocol2.number
+ assert_equal "110901", protocol1.number
end
+ #
+ test "using %y%m## on next month after should get 111001" do
+ class Protocol < ActiveRecord::Base
+ protokoll :number, :pattern => "%y%m##"
+ end
- test "first use of %y%m## should get 110901" do
- Protocol.protokoll.pattern = "%y%m##"
+ time = Time.local(2011, 9, 25, 12, 0, 0)
+ Timecop.travel(time)
- protocol1 = Protocol.create
- assert_equal "110901", protocol1.number
+ Protocol.create
+ Protocol.create
+
+ Timecop.travel(Time.now + 1.month)
+
+ protocol = Protocol.create
+ assert_equal "111001", protocol.number
end
-
- test "using %y%m## on next month after should get 111001" do
- Protocol.protokoll.pattern = "%y%m##"
-
- time = Time.local(2011, 9, 25, 12, 0, 0)
- Timecop.travel(time)
- Protocol.create
- Protocol.create
-
- Timecop.travel(Time.now + 1.month)
-
- protocol = Protocol.create
- assert_equal "111001", protocol.number
- end
-
test "%y%m%H#### should get 1109120001" do
- Protocol.protokoll.pattern = "%y%m%H####"
-
+ class Protocol < ActiveRecord::Base
+ protokoll :number, :pattern => "%y%m%H####"
+ end
+
protocol1 = Protocol.create
-
+
assert_equal "1109120001", protocol1.number
end
-
+
test "%y## on next year should get 1201" do
- Protocol.protokoll.pattern = "%y##"
-
- time = Time.local(2011, 9, 25, 12, 3, 0) # 2011
- protocol1 = Protocol.create
- time = Time.local(2012, 9, 25, 12, 3, 0) # 2012 - exactly 1 year after
- Timecop.travel(time)
-
- protocol2 = Protocol.create
-
- assert_equal "1201", protocol2.number
- end
-
- test "500.time create using %y%m%H#### should get 1109120500" do
- Protocol.protokoll.pattern = "%y%m%H####"
-
- 500.times { Protocol.create }
-
- assert_equal "1109120500", Protocol.last.number
- end
-
- test "PROT%H%m%y#### should get PROT%H%m%y0001" do
- Protocol.protokoll.pattern = "PROT%H%m%y####"
-
- protocol1 = Protocol.create
-
- assert_equal "PROT1209110001", protocol1.number
- end
-
- test "PROT%Y%m%H#### should get PROT201109120001" do
- Protocol.protokoll.pattern = "PROT%Y%m%H####"
-
- protocol1 = Protocol.create
-
- assert_equal "PROT201109120001", protocol1.number
- end
-
- test "use of sufix ####PROTO should get 0001PROTO" do
- Protocol.protokoll.pattern = "####PROTO"
-
- protocol1 = Protocol.create
-
- assert_equal "0001PROTO", protocol1.number
- end
-
- test "use of sufix %Y%M####PROTO on 12:03 should get 2011030001PROTO" do
- Protocol.protokoll.pattern = "%Y%M####PROTO"
-
- time = Time.local(2011, 9, 25, 12, 3, 0)
- Timecop.travel(time)
-
- protocol1 = Protocol.create
-
- assert_equal "2011030001PROTO", protocol1.number
+ class Protocol < ActiveRecord::Base
+ protokoll :number, :pattern => "%y##"
+ end
+
+ time = Time.local(2011, 9, 25, 12, 3, 0) # 2011
+ protocol1 = Protocol.create
+ time = Time.local(2012, 9, 25, 12, 3, 0) # 2012 - exactly 1 year after
+ Timecop.travel(time)
+
+ protocol2 = Protocol.create
+
+ assert_equal "1201", protocol2.number
end
-
+
+ test "500.time create using %y%m%H#### should get 1109120500" do
+ class Protocol < ActiveRecord::Base
+ protokoll :number, :pattern => "%y%m%H####"
+ end
+
+ 500.times { Protocol.create }
+
+ assert_equal "1109120500", Protocol.last.number
+ end
+
+ test "PROT%H%m%y#### should get PROT%H%m%y0001" do
+ class Protocol < ActiveRecord::Base
+ protokoll :number, :pattern => "PROT%H%m%y####"
+ end
+
+ protocol1 = Protocol.create
+
+ assert_equal "PROT1209110001", protocol1.number
+ end
+
+ test "PROT%Y%m%H#### should get PROT201109120001" do
+ class Protocol < ActiveRecord::Base
+ protokoll :number, :pattern => "PROT%Y%m%H####"
+ end
+
+ protocol1 = Protocol.create
+
+ assert_equal "PROT201109120001", protocol1.number
+ end
+
+ test "use of sufix ####PROTO should get 0001PROTO" do
+ class Protocol < ActiveRecord::Base
+ protokoll :number, :pattern => "####PROTO"
+ end
+
+ protocol1 = Protocol.create
+
+ assert_equal "0001PROTO", protocol1.number
+ end
+
+ test "use of sufix %Y%M####PROTO on 12:03 should get 2011030001PROTO" do
+ class Protocol < ActiveRecord::Base
+ protokoll :number, :pattern => "%Y%M####PROTO"
+ end
+
+ time = Time.local(2011, 9, 25, 12, 3, 0)
+ Timecop.travel(time)
+
+ protocol1 = Protocol.create
+
+ assert_equal "2011030001PROTO", protocol1.number
+ end
+
test "use of sufix %Y%M####PROTO on 12:15 should get 2011150001PROTO" do
- Protocol.protokoll.pattern = "%Y%M####PROTO"
-
+ class Protocol < ActiveRecord::Base
+ protokoll :number, :pattern => "%Y%M####PROTO"
+ end
+
time = Time.local(2011, 9, 25, 12, 15, 0)
Timecop.travel(time)
-
+
protocol1 = Protocol.create
-
+
assert_equal "2011150001PROTO", protocol1.number
end
-
+
test "using 2 models" do
- Protocol.protokoll.pattern = "%Y##"
- Call.protokoll.pattern = "%Y##"
+ class Protocol < ActiveRecord::Base
+ protokoll :number, :pattern => "%Y##"
+ end
+
+ class Call < ActiveRecord::Base
+ protokoll :number, :pattern => "%Y##"
+ end
protocol = Protocol.create
call = Call.create
assert_equal "201101", protocol.number
assert_equal "201101", call.number
end
-
- test "updating model should not get a different number" do
- Protocol.protokoll.pattern = "%Y##"
+
+ test "using 2 models with different patterns" do
+ class Protocol < ActiveRecord::Base
+ protokoll :number, :pattern => "%YA#"
+ end
+
+ class Call < ActiveRecord::Base
+ protokoll :number, :pattern => "%YB#"
+ end
+
+ protocol = Protocol.create
+ call = Call.create
+ assert_equal "2011A1", protocol.number
+ assert_equal "2011B1", call.number
+ end
+
+ test "updating model should not get a different number" do
+ class Protocol < ActiveRecord::Base
+ protokoll :number, :pattern => "%Y##"
+ end
protocol = Protocol.new
-
+
protocol.save
assert_equal "201101", protocol.number
protocol.save
assert_equal "201101", protocol.number
end
+
+ test "reserve_number should set number to instance" do
+ class Protocol < ActiveRecord::Base
+ protokoll :number, :pattern => "%Y##"
+ end
+
+ protocol = Protocol.new
+ protocol.reserve_number!
+
+ assert_equal "201101", protocol.number
+ protocol.save
+ assert_equal "201101", protocol.number
+
+ protocol = Protocol.new
+ protocol.save
+ assert_equal "201102", protocol.number
+ end
+
+ test "reserve_number should assure number if reserved" do
+ class Protocol < ActiveRecord::Base
+ protokoll :number, :pattern => "%Y##"
+ end
+
+ protocol1 = Protocol.new
+ protocol1.reserve_number!
+
+ protocol2 = Protocol.new
+ protocol2.save
+
+ assert_equal "201101", protocol1.number
+ assert_equal "201102", protocol2.number
+ end
+
+ test "reserve_number should assure number if reserved if next month" do
+ class Protocol < ActiveRecord::Base
+ protokoll :number, :pattern => "%Y%m##"
+ end
+
+ protocol1 = Protocol.new
+ protocol1.reserve_number!
+
+ Timecop.travel(Time.now + 1.month)
+
+ protocol2 = Protocol.new
+ protocol2.save!
+
+ assert_equal "20110901", protocol1.number
+ assert_equal "20111001", protocol2.number
+ end
- # test "reserve_number should set number to instance" do
- # Protocol.protokoll.pattern = "%Y##"
- #
- # protocol = Protocol.new
- # protocol.reserve_number!
- #
- # assert_equal "201101", protocol.number
- # protocol.save
- # assert_equal "201101", protocol.number
- #
- # protocol = Protocol.new
- # protocol.save
- # assert_equal "201102", protocol.number
- # end
- #
- # test "reserve_number should assure number if reserved" do
- # Protocol.protokoll.pattern = "%Y##"
- #
- # protocol1 = Protocol.new
- # protocol1.reserve_number!
- #
- # protocol2 = Protocol.new
- # protocol2.save
- #
- # assert_equal "201101", protocol1.number
- # assert_equal "201102", protocol2.number
- # end
- #
- # test "reserve_number should assure number if reserved if next month" do
- # Protocol.protokoll.pattern = "%Y%m##"
- #
- # protocol1 = Protocol.create
- # protocol1.reserve_number!
- #
- # Timecop.travel(Time.now + 1.month)
- #
- # protocol2 = Protocol.new
- # protocol2.save!
- #
- # assert_equal "20110901", protocol1.number
- # assert_equal "20111001", protocol2.number
- # end
-
- ## Can't reproduce this on test environmet!
- ## if you know how to do that, contact-me!
- # test "Start application with populated db." do
- # Protocol.protokoll.pattern = "%Y%M####PROTO"
- #
- # Protocol.create
- # Protocol.create
- # Protocol.create
- #
- # # restart application or reload Protocol model
- # # how do I do that?
- #
- # protocol = Protocol.create
- #
- # assert_equal "2011090004PROTO", protocol.number
- # end
end
Please sign in to comment.
Something went wrong with that request. Please try again.