├── .github └── workflows │ └── tests.yml ├── .gitignore ├── .gitignore.swp ├── CHANGELOG.rdoc ├── Gemfile ├── MIT-LICENSE ├── README.md ├── Rakefile ├── lib ├── generators │ └── protokoll │ │ └── migration │ │ ├── migration_generator.rb │ │ ├── scope_by_generator.rb │ │ └── templates │ │ ├── add_scope_by_to_custom_auto_increments.rb │ │ └── create_custom_auto_increments.rb ├── protokoll.rb ├── protokoll │ ├── counter.rb │ ├── formater.rb │ ├── models │ │ └── custom_auto_increment.rb │ ├── protokoll.rb │ └── version.rb └── tasks │ └── protokoll_tasks.rake ├── protokoll.gemspec └── test ├── dummy ├── Rakefile ├── app │ ├── assets │ │ ├── config │ │ │ └── manifest.js │ │ ├── javascripts │ │ │ └── application.js │ │ └── stylesheets │ │ │ └── application.css │ ├── controllers │ │ └── application_controller.rb │ ├── helpers │ │ └── application_helper.rb │ ├── mailers │ │ └── .gitkeep │ ├── models │ │ ├── .gitkeep │ │ ├── call_defined_in_tests.rb │ │ └── protocol_defined_in_tests.rb │ └── views │ │ └── layouts │ │ └── application.html.erb ├── config.ru ├── config │ ├── application.rb │ ├── boot.rb │ ├── database.yml │ ├── environment.rb │ ├── environments │ │ ├── development.rb │ │ ├── production.rb │ │ └── test.rb │ ├── initializers │ │ ├── backtrace_silencers.rb │ │ ├── inflections.rb │ │ ├── mime_types.rb │ │ ├── secret_token.rb │ │ ├── session_store.rb │ │ └── wrap_parameters.rb │ ├── locales │ │ └── en.yml │ └── routes.rb ├── db │ ├── migrate │ │ ├── 20110923024431_create_protocols.rb │ │ ├── 20110928013630_create_calls.rb │ │ ├── 20120222164124_create_custom_auto_increments.rb │ │ └── 20160310030821_add_scope_by_to_custom_auto_increments.rb │ ├── schema.rb │ └── seeds.rb ├── lib │ └── assets │ │ └── .gitkeep ├── log │ └── .gitkeep ├── public │ ├── 404.html │ ├── 422.html │ ├── 500.html │ └── favicon.ico └── script │ └── rails ├── protokoll_test.rb └── test_helper.rb /.github/workflows/tests.yml: -------------------------------------------------------------------------------- 1 | name: Tests 2 | on: [push, pull_request] 3 | jobs: 4 | tests: 5 | strategy: 6 | fail-fast: false 7 | matrix: 8 | # Due to https://github.com/actions/runner/issues/849, we have to use quotes for '3.0' 9 | ruby: [2.7, '3.0', 3.1, head] 10 | runs-on: ubuntu-latest 11 | steps: 12 | - uses: actions/checkout@v2 13 | - uses: ruby/setup-ruby@v1 14 | with: 15 | ruby-version: ${{ matrix.ruby }} 16 | bundler-cache: true # runs 'bundle install' and caches installed gems automatically 17 | - run: bundle exec rake -f test/dummy/Rakefile db:migrate db:test:prepare; bundle exec rake 18 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .bundle/ 2 | log/*.log 3 | pkg/ 4 | test/dummy/db/*.sqlite3 5 | test/dummy/log/*.log 6 | test/dummy/tmp/ 7 | .idea/ 8 | protokoll-*.gem 9 | rdoc/ 10 | .DS_Store/ 11 | .DS_Store 12 | lib/.DS_Store 13 | Gemfile.lock 14 | .ruby-gemset 15 | .ruby-version 16 | -------------------------------------------------------------------------------- /.gitignore.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/celsodantas/protokoll/8eefacb627b0b81298650268cc7d240915bd89bb/.gitignore.swp -------------------------------------------------------------------------------- /CHANGELOG.rdoc: -------------------------------------------------------------------------------- 1 | == v2.0.1 2 | * improving support for ActiveType 'Presenters' (thanks @Bogadon) 3 | == v2.0.0 4 | * addin the ability to dinamicaly scope the counter (thanks @emaxi) 5 | This will require a migration to users using the old version of the gem 6 | == v1.0.3 7 | * gemspec fix to accept Rails >4 #26, previous PR didn't fix it. 8 | == v1.0.2 9 | * accepting Rails >4 #21 10 | * removing debugger gem and dropping ruby 1.9.3 support 11 | == v1.0.1 12 | * BUG FIX: time comparison with UTC (#14) 13 | * Adding code announcement for bad or missing patterns (thanks @sammyjopeters) 14 | 15 | == v1.0.0 16 | * BUG FIX: column rename (#17) 17 | * bump to 1.0.0 to let people know about the API incompability 18 | 19 | == v0.6.0 20 | * BUG FIX: LocalJumpError (#11) 21 | * Added "day" pattern (#8) 22 | 23 | == v0.4.0 24 | * Adding support to database driven counter and not in memory anymore. 25 | * Simplifying code 26 | * new API: 27 | reserve_ # use to get the auto generated number without 28 | # saving the model in the database 29 | 30 | == v0.3.1 31 | * BUG FIX: Updates were updating the number count. oO 32 | now the number is only incremented when you create the model. 33 | 34 | == v0.3.0 35 | * BUG FIX: If using %y%m and only year changes, counter was not restarting. 36 | 37 | == v0.2.0 38 | * Performance improvement 39 | * Refactoring 40 | 41 | == v0.1.1 42 | * Updating gem description 43 | 44 | == v0.1.0 45 | * Working! 46 | 47 | == v0.0.1 48 | * Initial code 49 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source "http://rubygems.org" 2 | 3 | # Declare your gem's dependencies in protocol_number_plugin.gemspec. 4 | # Bundler will treat runtime dependencies like base dependencies, and 5 | # development dependencies will be added by default to the :development group. 6 | gemspec 7 | 8 | # Declare any dependencies that are still in development here instead of in 9 | # your gemspec. These might include edge Rails or gems from your path or 10 | # Git. Remember to move these dependencies to your gemspec before releasing 11 | # your gem to rubygems.org. 12 | 13 | gem 'timecop' 14 | -------------------------------------------------------------------------------- /MIT-LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2012 CELSO DANTAS 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining 4 | a copy of this software and associated documentation files (the 5 | "Software"), to deal in the Software without restriction, including 6 | without limitation the rights to use, copy, modify, merge, publish, 7 | distribute, sublicense, and/or sell copies of the Software, and to 8 | permit persons to whom the Software is furnished to do so, subject to 9 | the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be 12 | included in all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 18 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 19 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 20 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Protokoll [![Travis](https://api.travis-ci.org/celsodantas/protokoll.png)](http://travis-ci.org/celsodantas/protokoll) [![Gem Version](https://badge.fury.io/rb/protokoll.svg)](http://badge.fury.io/rb/protokoll) 2 | 3 | 4 | ## Description 5 | 6 | Protokoll is a simple Rails 4 pluggin to simplify the management of a custom autoincrement value for a model. 7 | 8 | If you want to create an autoincrement information on the database, just like those callcenter registration number (2011000001, 2011000002, 20110000003 and on) this gem is for you! If you want to create just an custom autoincrement value, this gem is for you too! =) 9 | 10 | All those tricky things to control like every month you have to reset the counter are gone! All you have to do is define a String column and let _Protokoll_ handle the rest: 11 | 12 | 13 | ```ruby 14 | # creating an autoincrement column based on Time 15 | class Call < ActiveRecord::Base 16 | protokoll :registry_number # by default it uses "%Y%m#####" 17 | end 18 | 19 | Time.local(2011) 20 | call01 = Call.create 21 | call01.registry_number 22 | => "201100001" 23 | 24 | call02 = Call.create 25 | call02.registry_number 26 | => "201100002" 27 | 28 | Time.local(2012) 29 | 30 | call03 = Call.create 31 | call03.registry_number 32 | => "201200001" # restarts counter because it is the first item of the year 33 | ``` 34 | 35 | If you want to use your own pattern, just do this: 36 | 37 | ```ruby 38 | class Call < ActiveRecord::Base 39 | protokoll :registry_number, :pattern => "some#####thing" 40 | end 41 | 42 | # this will produce 43 | call01 = Call.create 44 | call01.registry_number 45 | => "some00001thing" 46 | 47 | call02 = Call.create 48 | call02.registry_number 49 | => "some00002thing" 50 | ``` 51 | 52 | Or use any time based format. You can use in the pattern any combination of: 53 | 54 | ```ruby 55 | # assume it's 2011/01/01 12:00 56 | "%Y" for year # => appends 2011 57 | "%y" for year # => appends 11 58 | "%m" for month # => appends 01 59 | "%d" for day # => appends 01 60 | "%H" for hour # => appends 12 61 | "%M" for minute # => appends 00 62 | "#" for the autoincrement number (use as long as you want) 63 | ``` 64 | 65 | Using the Time formating string ("%y", "%m", ...) is totally optional. It's fine to use "CALL####", "BUY###N" or any combination you like. 66 | 67 | Ex: 68 | ```ruby 69 | # :number must be a String 70 | class Car < ActiveRecord::Base 71 | protokoll :number, :pattern => "CAR%y#####" 72 | end 73 | 74 | # will produce => "CAR1100001", "CAR1100002"... 75 | 76 | # :sell_number must be a String 77 | class House < ActiveRecord::Base 78 | protokoll :sell_number, :pattern => "%YHOUSE#####" 79 | end 80 | 81 | # will produce => "2011HOUSE00001", "2011HOUSE00002"... 82 | ``` 83 | 84 | It's possible to pass :scope_by option as a simple method, Proc.new or lambda 85 | 86 | Ex: 87 | ```ruby 88 | # :manufacturer should be a Car's instance method(like an ActiveRecord column) 89 | class Car < ActiveRecord::Base 90 | protokoll :code, :scope_by => :manufacturer 91 | end 92 | # will scope Cars by manufacturers, for example "Ford", "Chevrolet" 93 | 94 | # :manufacturer and :year should be Car's instance methods(like ActiveRecord columns) 95 | class Car < ActiveRecord::Base 96 | protokoll :code, :scope_by => lambda { |o| "#{o.manufacturer}-#{o.year}" } 97 | end 98 | # will scope Cars by for example "Ford-2016" 99 | 100 | # :manufacturer and :year should be Car's instance methods(like ActiveRecord columns) 101 | class Car < ActiveRecord::Base 102 | protokoll :code, :scope_by => Proc.new{ "#{manufacturer}-#{model}" } 103 | end 104 | # will scope Cars by for example "Ford-Mustang", "Chevrolet-Camaro" 105 | ``` 106 | 107 | 108 | ## reserve_number! 109 | 110 | object.reserve_number! 111 | 112 | Add a new intance method colled: "your_instance#reserve_#{column_name}!". 113 | With it you can reserve a number without the need to save it to the database. Ex: 114 | 115 | ```ruby 116 | car = Car.new 117 | car.number 118 | # => nil 119 | car.reserve_number! 120 | car.number 121 | # => "CAR1100001" 122 | # if you save it, the object will preserve the number: "CAR1100001" 123 | ``` 124 | 125 | It just increases the counter so any other object that gets saved or uses the #reserve_#{column_name} will get the next available number. 126 | 127 | ## Installation 128 | 129 | Just add to the Gemfile: 130 | 131 | ```ruby 132 | gem 'protokoll' 133 | ``` 134 | 135 | And run _bundle install_ on the Rails application folder 136 | 137 | bundle install 138 | 139 | Run the generator 140 | 141 | rails g protokoll:migration 142 | 143 | Optional: If scope_by will be used run next generator as well 144 | 145 | rails g protokoll:migration:scope_by 146 | 147 | and migrate your database 148 | 149 | rake db:migrate 150 | 151 | 152 | ## Questions & Sugestions 153 | This is my _first_ public gem, so if you have any questions os sugestions, feel free to contact me (use github msg system for that). It will be awesome to hear feedback to improve the code. 154 | 155 | The gem is still on early _alpha_ so you may find bugs on it. And if you do, please use the _Issues_ here in Github and I'd love to fix it. 156 | 157 | This piece of software is free to use. 158 | 159 | Check the wiki if you are having any issues while upgrading from version `0.x` 160 | 161 | ### Running tests in Development 162 | 163 | You need to clone the project 164 | 165 | git clone git@github.com:celsodantas/protokoll.git 166 | cd protokoll 167 | 168 | Then prepare the test database: 169 | 170 | bundle 171 | cd test/dummy && rake db:migrate && rake db:test:prepare && cd ../.. 172 | 173 | Now run the rake test to run the tests: 174 | 175 | rake test 176 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env rake 2 | begin 3 | require 'bundler/setup' 4 | rescue LoadError 5 | puts 'You must `gem install bundler` and `bundle install` to run rake tasks' 6 | end 7 | begin 8 | require 'rdoc/task' 9 | rescue LoadError 10 | require 'rdoc/rdoc' 11 | require 'rake/rdoctask' 12 | RDoc::Task = Rake::RDocTask 13 | end 14 | 15 | RDoc::Task.new(:rdoc) do |rdoc| 16 | rdoc.rdoc_dir = 'rdoc' 17 | rdoc.title = 'Protokoll' 18 | rdoc.options << '--line-numbers' 19 | rdoc.rdoc_files.include('README.rdoc') 20 | rdoc.rdoc_files.include('lib/**/*.rb') 21 | end 22 | 23 | 24 | 25 | Bundler::GemHelper.install_tasks 26 | 27 | require 'rake/testtask' 28 | 29 | Rake::TestTask.new(:test) do |t| 30 | t.libs << 'lib' 31 | t.libs << 'test' 32 | t.pattern = 'test/**/*_test.rb' 33 | t.verbose = false 34 | end 35 | 36 | 37 | task :default => :test 38 | -------------------------------------------------------------------------------- /lib/generators/protokoll/migration/migration_generator.rb: -------------------------------------------------------------------------------- 1 | require 'rails/generators' 2 | 3 | module Protokoll 4 | module Generators 5 | class MigrationGenerator < ::Rails::Generators::Base 6 | 7 | include Rails::Generators::Migration 8 | 9 | desc "Generate protokoll migration" 10 | def create_migration_file 11 | migration_name = "create_custom_auto_increments.rb" 12 | migration_template migration_name, File.join('db', 'migrate', migration_name) 13 | end 14 | 15 | def self.source_root 16 | @source_root ||= File.expand_path(File.join(File.dirname(__FILE__), 'templates')) 17 | end 18 | 19 | def self.next_migration_number(path) 20 | Time.now.utc.strftime("%Y%m%d%H%M%S") 21 | end 22 | end 23 | end 24 | end -------------------------------------------------------------------------------- /lib/generators/protokoll/migration/scope_by_generator.rb: -------------------------------------------------------------------------------- 1 | require 'rails/generators' 2 | 3 | module Protokoll 4 | module Generators 5 | module Migration 6 | class ScopeByGenerator < ::Rails::Generators::Base 7 | 8 | include Rails::Generators::Migration 9 | 10 | desc "Generate protokoll's scope_by migration" 11 | def create_migration_file 12 | migration_name = "add_scope_by_to_custom_auto_increments.rb" 13 | migration_template migration_name, File.join('db', 'migrate', migration_name) 14 | end 15 | 16 | def self.source_root 17 | @source_root ||= File.expand_path(File.join(File.dirname(__FILE__), 'templates')) 18 | end 19 | 20 | def self.next_migration_number(path) 21 | Time.now.utc.strftime("%Y%m%d%H%M%S") 22 | end 23 | end 24 | end 25 | end 26 | end 27 | -------------------------------------------------------------------------------- /lib/generators/protokoll/migration/templates/add_scope_by_to_custom_auto_increments.rb: -------------------------------------------------------------------------------- 1 | class AddScopeByToCustomAutoIncrements < ActiveRecord::Migration 2 | def up 3 | add_column :custom_auto_increments, :counter_model_scope, :string 4 | add_index :custom_auto_increments, [:counter_model_name, :counter_model_scope], 5 | :unique => true, :name => :counter_model_name_scope 6 | end 7 | 8 | def down 9 | remove_index :custom_auto_increments, name: :counter_model_name_scope 10 | remove_column :custom_auto_increments, :counter_model_scope, :string 11 | end 12 | end 13 | -------------------------------------------------------------------------------- /lib/generators/protokoll/migration/templates/create_custom_auto_increments.rb: -------------------------------------------------------------------------------- 1 | class CreateCustomAutoIncrements < ActiveRecord::Migration 2 | def up 3 | create_table :custom_auto_increments, :force => true do |t| 4 | t.string :counter_model_name 5 | t.integer :counter, :default => 0 6 | t.timestamps 7 | end 8 | 9 | add_index :custom_auto_increments, :counter_model_name 10 | end 11 | 12 | def down 13 | drop_table :custom_auto_increments 14 | end 15 | end 16 | -------------------------------------------------------------------------------- /lib/protokoll.rb: -------------------------------------------------------------------------------- 1 | require "protokoll/models/custom_auto_increment" 2 | require "protokoll/formater" 3 | require "protokoll/counter" 4 | require "protokoll/protokoll" 5 | 6 | ActiveRecord::Base.send :include, Protokoll -------------------------------------------------------------------------------- /lib/protokoll/counter.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'active_record' 4 | 5 | module Protokoll 6 | class Counter 7 | def self.next(object, options) 8 | element = Models::CustomAutoIncrement.find_or_create_by(build_attrs(object, options)) 9 | 10 | element.counter = options[:start] if outdated?(element, options) || element.counter == 0 11 | element.counter += 1 12 | 13 | element.touch unless element.changed? 14 | element.save! if element.changed? 15 | 16 | element.save! 17 | 18 | Formater.new.format(element.counter, options) 19 | end 20 | 21 | private 22 | 23 | def self.build_attrs(object, options) 24 | attrs = {counter_model_name: object.model_name.name.underscore} 25 | return attrs unless options[:scope_by] 26 | 27 | scope_by = options[:scope_by].respond_to?(:call) ? 28 | object.instance_eval(&options[:scope_by]) : 29 | object.send(options[:scope_by]) 30 | 31 | attrs.merge(counter_model_scope: scope_by) 32 | end 33 | 34 | def self.outdated?(record, options) 35 | event = update_event(options) 36 | return false if event.empty? 37 | 38 | Time.now.utc.strftime(event).to_i > record.updated_at.strftime(event).to_i 39 | end 40 | 41 | def self.update_event(options) 42 | pattern = options[:pattern] 43 | event = String.new 44 | 45 | event << "%Y" if pattern.include? "%y" or pattern.include? "%Y" 46 | event << "%m" if pattern.include? "%m" 47 | event << "%H" if pattern.include? "%H" 48 | event << "%M" if pattern.include? "%M" 49 | event << "%d" if pattern.include? "%d" 50 | event 51 | end 52 | end 53 | end 54 | -------------------------------------------------------------------------------- /lib/protokoll/formater.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | module Protokoll 4 | class Formater 5 | def format(number, options) 6 | @options = options 7 | 8 | build(number) 9 | end 10 | 11 | private 12 | 13 | # gets the next number. 14 | # it prepends the prefix + counter + sufix 15 | # ex: 16 | # "%Y####BANK" 17 | # %Y => prefix (year) 18 | # #### => counter (starts with 0001) 19 | # BANK => sufix 20 | # 21 | # if we are in 2011, the first model to be saved will get "20110001BANK" 22 | # the next model to be saved will get "20110002BANK", "20110003BANK"... 23 | # 24 | # number => is the counter 25 | # 26 | # next_custom_number(1) 27 | # => "20110001BANK" 28 | def build(number) 29 | prefix(@options[:pattern]).to_s + 30 | counter(@options[:pattern], number).to_s + 31 | sufix(@options[:pattern]).to_s 32 | end 33 | 34 | def prefix(pattern) 35 | prefx = extract_prefix(pattern) 36 | expand_times(prefx.to_s) 37 | end 38 | 39 | def counter(pattern, n) 40 | format_counter(digits_size(pattern), n) 41 | end 42 | 43 | def sufix(pattern) 44 | sufx = extract_sufix(pattern) 45 | expand_times(sufx.to_s) 46 | end 47 | 48 | def format_counter(zeros, value) 49 | "%0#{zeros}d" % value 50 | end 51 | 52 | def extract_prefix(pattern) 53 | # Company#### => Company 54 | symbol = @options[:number_symbol] 55 | (pattern =~ /^(\s|\d)*[^#{symbol}]+/ and $&) 56 | end 57 | 58 | def extract_sufix(pattern) 59 | # ###Company => Company 60 | symbol = @options[:number_symbol] 61 | (pattern =~ /[^#{symbol}]+$/ and $&) 62 | end 63 | 64 | def expand_times(pattern) 65 | pat = pattern.dup # pattern is a frozen string. 66 | pat.sub!("%y", Time.now.strftime("%y")) 67 | pat.sub!("%Y", Time.now.strftime("%Y")) 68 | pat.sub!("%d", Time.now.strftime("%d")) 69 | pat.sub!("%m", Time.now.strftime("%m")) 70 | pat.sub!("%M", Time.now.strftime("%M")) 71 | pat.sub("%H", Time.now.strftime("%H")) 72 | end 73 | 74 | def digits_size(pattern) 75 | symbol = @options[:number_symbol] 76 | (pattern =~ /[#{symbol}]+/ and $&).length 77 | end 78 | 79 | 80 | end 81 | end 82 | -------------------------------------------------------------------------------- /lib/protokoll/models/custom_auto_increment.rb: -------------------------------------------------------------------------------- 1 | require "active_record/base" 2 | 3 | module Protokoll 4 | module Models 5 | class CustomAutoIncrement < ActiveRecord::Base 6 | end 7 | end 8 | end -------------------------------------------------------------------------------- /lib/protokoll/protokoll.rb: -------------------------------------------------------------------------------- 1 | module Protokoll 2 | extend ActiveSupport::Concern 3 | 4 | module ClassMethods 5 | 6 | # Class method available in models 7 | # 8 | # == Example 9 | # class Order < ActiveRecord::Base 10 | # protokoll :number 11 | # end 12 | # 13 | def protokoll(column, _options = {}) 14 | options = { :pattern => "%Y%m#####", 15 | :number_symbol => "#", 16 | :column => column, 17 | :start => 0, 18 | :scope_by => nil } 19 | 20 | options.merge!(_options) 21 | raise ArgumentError.new("pattern can't be nil!") if options[:pattern].nil? 22 | raise ArgumentError.new("pattern requires at least one counter symbol #{options[:number_symbol]}") unless pattern_includes_symbols?(options) 23 | 24 | # Defining custom method 25 | send :define_method, "reserve_#{options[:column]}!".to_sym do 26 | self[column] = Counter.next(self, options) 27 | end 28 | 29 | # Signing before_create 30 | before_create do |record| 31 | unless record[column].present? 32 | record[column] = Counter.next(self, options) 33 | end 34 | end 35 | end 36 | 37 | private 38 | 39 | def pattern_includes_symbols?(options) 40 | options[:pattern].count(options[:number_symbol]) > 0 41 | end 42 | end 43 | 44 | end 45 | -------------------------------------------------------------------------------- /lib/protokoll/version.rb: -------------------------------------------------------------------------------- 1 | module Protokoll 2 | VERSION = "2.0.1" 3 | end 4 | -------------------------------------------------------------------------------- /lib/tasks/protokoll_tasks.rake: -------------------------------------------------------------------------------- 1 | # desc "Explaining what the task does" 2 | # task :protocol_number_plugin do 3 | # # Task goes here 4 | # end 5 | -------------------------------------------------------------------------------- /protokoll.gemspec: -------------------------------------------------------------------------------- 1 | $:.push File.expand_path("../lib", __FILE__) 2 | 3 | # Maintain your gem's version: 4 | require "protokoll/version" 5 | 6 | # Describe your gem and declare its dependencies: 7 | Gem::Specification.new do |s| 8 | s.name = "protokoll" 9 | s.version = Protokoll::VERSION 10 | s.authors = ["Celso Dantas"] 11 | s.email = ["celsodantas@gmail.com"] 12 | s.homepage = "https://github.com/celsodantas/protokoll" 13 | s.summary = "A simple Rails gem to create custom autoincrement Time base values to a database column" 14 | s.description = "Rails 4 gem to enable creation of a custom autoincrement Time based string on a model defined by a pattern. ex. 20110001, 20110002, 20110003, 20120001, 20120002..." 15 | s.license = 'MIT' 16 | 17 | s.files = Dir["{app,config,db,lib}/**/*"] + ["MIT-LICENSE", "Rakefile", "README.md"] 18 | s.test_files = Dir["test/**/*"] 19 | 20 | s.add_dependency("rails", ">= 4.0") 21 | 22 | s.add_development_dependency "sqlite3", '~> 1' 23 | end 24 | -------------------------------------------------------------------------------- /test/dummy/Rakefile: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env rake 2 | # Add your own tasks in files placed in lib/tasks ending in .rake, 3 | # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake. 4 | 5 | require File.expand_path('../config/application', __FILE__) 6 | 7 | Dummy::Application.load_tasks 8 | -------------------------------------------------------------------------------- /test/dummy/app/assets/config/manifest.js: -------------------------------------------------------------------------------- 1 | //= link_tree ../images 2 | //= link_directory ../javascripts .js 3 | //= link_directory ../stylesheets .css 4 | -------------------------------------------------------------------------------- /test/dummy/app/assets/javascripts/application.js: -------------------------------------------------------------------------------- 1 | // This is a manifest file that'll be compiled into including all the files listed below. 2 | // Add new JavaScript/Coffee code in separate files in this directory and they'll automatically 3 | // be included in the compiled file accessible from http://example.com/assets/application.js 4 | // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the 5 | // the compiled file. 6 | // 7 | //= require jquery 8 | //= require jquery_ujs 9 | //= require_tree . 10 | -------------------------------------------------------------------------------- /test/dummy/app/assets/stylesheets/application.css: -------------------------------------------------------------------------------- 1 | /* 2 | * This is a manifest file that'll automatically include all the stylesheets available in this directory 3 | * and any sub-directories. You're free to add application-wide styles to this file and they'll appear at 4 | * the top of the compiled file, but it's generally better to create a new file per style scope. 5 | *= require_self 6 | *= require_tree . 7 | */ -------------------------------------------------------------------------------- /test/dummy/app/controllers/application_controller.rb: -------------------------------------------------------------------------------- 1 | class ApplicationController < ActionController::Base 2 | protect_from_forgery 3 | end 4 | -------------------------------------------------------------------------------- /test/dummy/app/helpers/application_helper.rb: -------------------------------------------------------------------------------- 1 | module ApplicationHelper 2 | end 3 | -------------------------------------------------------------------------------- /test/dummy/app/mailers/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/celsodantas/protokoll/8eefacb627b0b81298650268cc7d240915bd89bb/test/dummy/app/mailers/.gitkeep -------------------------------------------------------------------------------- /test/dummy/app/models/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/celsodantas/protokoll/8eefacb627b0b81298650268cc7d240915bd89bb/test/dummy/app/models/.gitkeep -------------------------------------------------------------------------------- /test/dummy/app/models/call_defined_in_tests.rb: -------------------------------------------------------------------------------- 1 | # As I need to change the class a lot, I'm defining (and redefining tons of times) 2 | # in the tests files. 3 | # I'm just keeping the model here to make easy to understand what models 4 | # are been used in the test. 5 | 6 | # class Call < ActiveRecord::Base 7 | # end 8 | -------------------------------------------------------------------------------- /test/dummy/app/models/protocol_defined_in_tests.rb: -------------------------------------------------------------------------------- 1 | # As I need to change the class a lot, I'm defining (and redefining tons of times) 2 | # in the tests files. 3 | # I'm just keeping the model here to make easy to understand what models 4 | # are been used in the test. 5 | 6 | # class Protocol < ActiveRecord::Base 7 | # end 8 | -------------------------------------------------------------------------------- /test/dummy/app/views/layouts/application.html.erb: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Dummy 5 | <%= stylesheet_link_tag "application" %> 6 | <%= javascript_include_tag "application" %> 7 | <%= csrf_meta_tags %> 8 | 9 | 10 | 11 | <%= yield %> 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /test/dummy/config.ru: -------------------------------------------------------------------------------- 1 | # This file is used by Rack-based servers to start the application. 2 | 3 | require ::File.expand_path('../config/environment', __FILE__) 4 | run Dummy::Application 5 | -------------------------------------------------------------------------------- /test/dummy/config/application.rb: -------------------------------------------------------------------------------- 1 | require File.expand_path('../boot', __FILE__) 2 | 3 | require 'rails/all' 4 | 5 | Bundler.require 6 | require "protokoll" 7 | 8 | module Dummy 9 | class Application < Rails::Application 10 | # Settings in config/environments/* take precedence over those specified here. 11 | # Application configuration should go into files in config/initializers 12 | # -- all .rb files in that directory are automatically loaded. 13 | 14 | # Custom directories with classes and modules you want to be autoloadable. 15 | # config.autoload_paths += %W(#{config.root}/extras) 16 | 17 | # Only load the plugins named here, in the order given (default is alphabetical). 18 | # :all can be used as a placeholder for all plugins not explicitly named. 19 | # config.plugins = [ :exception_notification, :ssl_requirement, :all ] 20 | 21 | # Activate observers that should always be running. 22 | # config.active_record.observers = :cacher, :garbage_collector, :forum_observer 23 | 24 | # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone. 25 | # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC. 26 | # config.time_zone = 'Central Time (US & Canada)' 27 | 28 | # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded. 29 | # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s] 30 | # config.i18n.default_locale = :de 31 | 32 | # Configure the default encoding used in templates for Ruby 1.9. 33 | config.encoding = "utf-8" 34 | 35 | # Configure sensitive parameters which will be filtered from the log file. 36 | config.filter_parameters += [:password] 37 | end 38 | end 39 | 40 | -------------------------------------------------------------------------------- /test/dummy/config/boot.rb: -------------------------------------------------------------------------------- 1 | require 'rubygems' 2 | gemfile = File.expand_path('../../../../Gemfile', __FILE__) 3 | 4 | if File.exist?(gemfile) 5 | ENV['BUNDLE_GEMFILE'] = gemfile 6 | require 'bundler' 7 | Bundler.setup 8 | end 9 | 10 | $:.unshift File.expand_path('../../../../lib', __FILE__) -------------------------------------------------------------------------------- /test/dummy/config/database.yml: -------------------------------------------------------------------------------- 1 | # SQLite version 3.x 2 | # gem install sqlite3 3 | # 4 | # Ensure the SQLite 3 gem is defined in your Gemfile 5 | # gem 'sqlite3' 6 | development: 7 | adapter: sqlite3 8 | database: db/development.sqlite3 9 | pool: 5 10 | timeout: 5000 11 | 12 | # Warning: The database defined as "test" will be erased and 13 | # re-generated from your development database when you run "rake". 14 | # Do not set this db to the same as development or production. 15 | test: 16 | adapter: sqlite3 17 | database: db/test.sqlite3 18 | pool: 5 19 | timeout: 5000 20 | 21 | production: 22 | adapter: sqlite3 23 | database: db/production.sqlite3 24 | pool: 5 25 | timeout: 5000 26 | -------------------------------------------------------------------------------- /test/dummy/config/environment.rb: -------------------------------------------------------------------------------- 1 | # Load the rails application 2 | require File.expand_path('../application', __FILE__) 3 | 4 | # Initialize the rails application 5 | Dummy::Application.initialize! 6 | -------------------------------------------------------------------------------- /test/dummy/config/environments/development.rb: -------------------------------------------------------------------------------- 1 | Dummy::Application.configure do 2 | # Settings specified here will take precedence over those in config/application.rb 3 | 4 | # In the development environment your application's code is reloaded on 5 | # every request. This slows down response time but is perfect for development 6 | # since you don't have to restart the web server when you make code changes. 7 | config.cache_classes = false 8 | 9 | # Show full error reports and disable caching 10 | config.consider_all_requests_local = true 11 | config.action_controller.perform_caching = false 12 | 13 | # Don't care if the mailer can't send 14 | config.action_mailer.raise_delivery_errors = false 15 | 16 | # Print deprecation notices to the Rails logger 17 | config.active_support.deprecation = :log 18 | 19 | # Only use best-standards-support built into browsers 20 | config.action_dispatch.best_standards_support = :builtin 21 | 22 | config.eager_load = false 23 | end 24 | -------------------------------------------------------------------------------- /test/dummy/config/environments/production.rb: -------------------------------------------------------------------------------- 1 | Dummy::Application.configure do 2 | # Settings specified here will take precedence over those in config/application.rb 3 | 4 | # Code is not reloaded between requests 5 | config.cache_classes = true 6 | 7 | # Full error reports are disabled and caching is turned on 8 | config.consider_all_requests_local = false 9 | config.action_controller.perform_caching = true 10 | 11 | # Specifies the header that your server uses for sending files 12 | # config.action_dispatch.x_sendfile_header = "X-Sendfile" # for apache 13 | # config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for nginx 14 | 15 | # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies. 16 | # config.force_ssl = true 17 | 18 | # See everything in the log (default is :info) 19 | # config.log_level = :debug 20 | 21 | # Use a different logger for distributed setups 22 | # config.logger = SyslogLogger.new 23 | 24 | # Use a different cache store in production 25 | # config.cache_store = :mem_cache_store 26 | 27 | # Enable serving of images, stylesheets, and JavaScripts from an asset server 28 | # config.action_controller.asset_host = "http://assets.example.com" 29 | 30 | # Precompile additional assets (application.js, application.css, and all non-JS/CSS are already added) 31 | # config.assets.precompile += %w( search.js ) 32 | 33 | # Disable delivery errors, bad email addresses will be ignored 34 | # config.action_mailer.raise_delivery_errors = false 35 | 36 | # Enable threaded mode 37 | # config.threadsafe! 38 | 39 | # Enable locale fallbacks for I18n (makes lookups for any locale fall back to 40 | # the I18n.default_locale when a translation can not be found) 41 | config.i18n.fallbacks = true 42 | 43 | # Send deprecation notices to registered listeners 44 | config.active_support.deprecation = :notify 45 | 46 | config.eager_load = turned 47 | end 48 | -------------------------------------------------------------------------------- /test/dummy/config/environments/test.rb: -------------------------------------------------------------------------------- 1 | Dummy::Application.configure do 2 | # Settings specified here will take precedence over those in config/application.rb 3 | 4 | # The test environment is used exclusively to run your application's 5 | # test suite. You never need to work with it otherwise. Remember that 6 | # your test database is "scratch space" for the test suite and is wiped 7 | # and recreated between test runs. Don't rely on the data there! 8 | config.cache_classes = true 9 | 10 | # Configure static asset server for tests with Cache-Control for performance 11 | config.serve_static_assets = true 12 | config.static_cache_control = "public, max-age=3600" 13 | 14 | # Show full error reports and disable caching 15 | config.consider_all_requests_local = true 16 | config.action_controller.perform_caching = false 17 | 18 | # Raise exceptions instead of rendering exception templates 19 | config.action_dispatch.show_exceptions = false 20 | 21 | # Disable request forgery protection in test environment 22 | config.action_controller.allow_forgery_protection = false 23 | 24 | # Tell Action Mailer not to deliver emails to the real world. 25 | # The :test delivery method accumulates sent emails in the 26 | # ActionMailer::Base.deliveries array. 27 | config.action_mailer.delivery_method = :test 28 | 29 | # Use SQL instead of Active Record's schema dumper when creating the test database. 30 | # This is necessary if your schema can't be completely dumped by the schema dumper, 31 | # like if you have constraints or database-specific column types 32 | # config.active_record.schema_format = :sql 33 | 34 | # Print deprecation notices to the stderr 35 | config.active_support.deprecation = :stderr 36 | 37 | config.eager_load = false 38 | 39 | config.active_support.test_order = :random 40 | end 41 | -------------------------------------------------------------------------------- /test/dummy/config/initializers/backtrace_silencers.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | # You can add backtrace silencers for libraries that you're using but don't wish to see in your backtraces. 4 | # Rails.backtrace_cleaner.add_silencer { |line| line =~ /my_noisy_library/ } 5 | 6 | # You can also remove all the silencers if you're trying to debug a problem that might stem from framework code. 7 | # Rails.backtrace_cleaner.remove_silencers! 8 | -------------------------------------------------------------------------------- /test/dummy/config/initializers/inflections.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | # Add new inflection rules using the following format 4 | # (all these examples are active by default): 5 | # ActiveSupport::Inflector.inflections do |inflect| 6 | # inflect.plural /^(ox)$/i, '\1en' 7 | # inflect.singular /^(ox)en/i, '\1' 8 | # inflect.irregular 'person', 'people' 9 | # inflect.uncountable %w( fish sheep ) 10 | # end 11 | -------------------------------------------------------------------------------- /test/dummy/config/initializers/mime_types.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | # Add new mime types for use in respond_to blocks: 4 | # Mime::Type.register "text/richtext", :rtf 5 | # Mime::Type.register_alias "text/html", :iphone 6 | -------------------------------------------------------------------------------- /test/dummy/config/initializers/secret_token.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | # Your secret key for verifying the integrity of signed cookies. 4 | # If you change this key, all old signed cookies will become invalid! 5 | # Make sure the secret is at least 30 characters and all random, 6 | # no regular words or you'll be exposed to dictionary attacks. 7 | Dummy::Application.config.secret_token = 'aa22b894ee478051a668893ff64c9ec5f00a06f3cd4d649c88cff58bb6778941d7ab8f34f7a33c030f48c08ca721399305f440ce1b51bb7040821ee831209dcd' 8 | -------------------------------------------------------------------------------- /test/dummy/config/initializers/session_store.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | Dummy::Application.config.session_store :cookie_store, key: '_dummy_session' 4 | 5 | # Use the database for sessions instead of the cookie-based default, 6 | # which shouldn't be used to store highly confidential information 7 | # (create the session table with "rails generate session_migration") 8 | # Dummy::Application.config.session_store :active_record_store 9 | -------------------------------------------------------------------------------- /test/dummy/config/initializers/wrap_parameters.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | # 3 | # This file contains settings for ActionController::ParamsWrapper which 4 | # is enabled by default. 5 | 6 | # Enable parameter wrapping for JSON. You can disable this by setting :format to an empty array. 7 | ActiveSupport.on_load(:action_controller) do 8 | wrap_parameters format: [:json] 9 | end 10 | 11 | # Disable root element in JSON by default. 12 | ActiveSupport.on_load(:active_record) do 13 | self.include_root_in_json = false 14 | end 15 | -------------------------------------------------------------------------------- /test/dummy/config/locales/en.yml: -------------------------------------------------------------------------------- 1 | # Sample localization file for English. Add more files in this directory for other locales. 2 | # See https://github.com/svenfuchs/rails-i18n/tree/master/rails%2Flocale for starting points. 3 | 4 | en: 5 | hello: "Hello world" 6 | -------------------------------------------------------------------------------- /test/dummy/config/routes.rb: -------------------------------------------------------------------------------- 1 | Dummy::Application.routes.draw do 2 | # The priority is based upon order of creation: 3 | # first created -> highest priority. 4 | 5 | # Sample of regular route: 6 | # match 'products/:id' => 'catalog#view' 7 | # Keep in mind you can assign values other than :controller and :action 8 | 9 | # Sample of named route: 10 | # match 'products/:id/purchase' => 'catalog#purchase', :as => :purchase 11 | # This route can be invoked with purchase_url(:id => product.id) 12 | 13 | # Sample resource route (maps HTTP verbs to controller actions automatically): 14 | # resources :products 15 | 16 | # Sample resource route with options: 17 | # resources :products do 18 | # member do 19 | # get 'short' 20 | # post 'toggle' 21 | # end 22 | # 23 | # collection do 24 | # get 'sold' 25 | # end 26 | # end 27 | 28 | # Sample resource route with sub-resources: 29 | # resources :products do 30 | # resources :comments, :sales 31 | # resource :seller 32 | # end 33 | 34 | # Sample resource route with more complex sub-resources 35 | # resources :products do 36 | # resources :comments 37 | # resources :sales do 38 | # get 'recent', :on => :collection 39 | # end 40 | # end 41 | 42 | # Sample resource route within a namespace: 43 | # namespace :admin do 44 | # # Directs /admin/products/* to Admin::ProductsController 45 | # # (app/controllers/admin/products_controller.rb) 46 | # resources :products 47 | # end 48 | 49 | # You can have the root of your site routed with "root" 50 | # just remember to delete public/index.html. 51 | # root :to => 'welcome#index' 52 | 53 | # See how all your routes lay out with "rake routes" 54 | 55 | # This is a legacy wild controller route that's not recommended for RESTful applications. 56 | # Note: This route will make all actions in every controller accessible via GET requests. 57 | # match ':controller(/:action(/:id(.:format)))' 58 | end 59 | -------------------------------------------------------------------------------- /test/dummy/db/migrate/20110923024431_create_protocols.rb: -------------------------------------------------------------------------------- 1 | class CreateProtocols < ActiveRecord::Migration[4.2] 2 | def change 3 | create_table :protocols do |t| 4 | t.string :number 5 | t.string :context 6 | t.string :context_2 7 | 8 | t.timestamps 9 | end 10 | end 11 | end 12 | -------------------------------------------------------------------------------- /test/dummy/db/migrate/20110928013630_create_calls.rb: -------------------------------------------------------------------------------- 1 | class CreateCalls < ActiveRecord::Migration[4.2] 2 | def change 3 | create_table :calls do |t| 4 | t.string :number 5 | t.timestamps 6 | end 7 | end 8 | end 9 | -------------------------------------------------------------------------------- /test/dummy/db/migrate/20120222164124_create_custom_auto_increments.rb: -------------------------------------------------------------------------------- 1 | class CreateCustomAutoIncrements < ActiveRecord::Migration[4.2] 2 | def up 3 | create_table :custom_auto_increments, :force => true do |t| 4 | t.string :counter_model_name 5 | t.integer :counter, :default => 0 6 | t.timestamps 7 | end 8 | 9 | add_index :custom_auto_increments, :counter_model_name 10 | end 11 | 12 | def down 13 | drop_table :custom_auto_increments 14 | end 15 | end 16 | -------------------------------------------------------------------------------- /test/dummy/db/migrate/20160310030821_add_scope_by_to_custom_auto_increments.rb: -------------------------------------------------------------------------------- 1 | class AddScopeByToCustomAutoIncrements < ActiveRecord::Migration[4.2] 2 | def up 3 | add_column :custom_auto_increments, :counter_model_scope, :string 4 | add_index :custom_auto_increments, [:counter_model_name, :counter_model_scope], 5 | :unique => true, :name => :counter_model_name_scope 6 | end 7 | 8 | def down 9 | remove_index :custom_auto_increments, name: :counter_model_name_scope 10 | remove_column :custom_auto_increments, :counter_model_scope, :string 11 | end 12 | end 13 | -------------------------------------------------------------------------------- /test/dummy/db/schema.rb: -------------------------------------------------------------------------------- 1 | # This file is auto-generated from the current state of the database. Instead 2 | # of editing this file, please use the migrations feature of Active Record to 3 | # incrementally modify your database, and then regenerate this schema definition. 4 | # 5 | # This file is the source Rails uses to define your schema when running `bin/rails 6 | # db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to 7 | # be faster and is potentially less error prone than running all of your 8 | # migrations from scratch. Old migrations may fail to apply correctly if those 9 | # migrations use external dependencies or application code. 10 | # 11 | # It's strongly recommended that you check this file into your version control system. 12 | 13 | ActiveRecord::Schema[7.0].define(version: 2016_03_10_030821) do 14 | create_table "calls", force: :cascade do |t| 15 | t.string "number" 16 | t.datetime "created_at", precision: nil 17 | t.datetime "updated_at", precision: nil 18 | end 19 | 20 | create_table "custom_auto_increments", force: :cascade do |t| 21 | t.string "counter_model_name" 22 | t.integer "counter", default: 0 23 | t.datetime "created_at", precision: nil 24 | t.datetime "updated_at", precision: nil 25 | t.string "counter_model_scope" 26 | t.index ["counter_model_name", "counter_model_scope"], name: "counter_model_name_scope", unique: true 27 | t.index ["counter_model_name"], name: "index_custom_auto_increments_on_counter_model_name" 28 | end 29 | 30 | create_table "protocols", force: :cascade do |t| 31 | t.string "number" 32 | t.string "context" 33 | t.string "context_2" 34 | t.datetime "created_at", precision: nil 35 | t.datetime "updated_at", precision: nil 36 | end 37 | 38 | end 39 | -------------------------------------------------------------------------------- /test/dummy/db/seeds.rb: -------------------------------------------------------------------------------- 1 | # Seed file 2 | -------------------------------------------------------------------------------- /test/dummy/lib/assets/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/celsodantas/protokoll/8eefacb627b0b81298650268cc7d240915bd89bb/test/dummy/lib/assets/.gitkeep -------------------------------------------------------------------------------- /test/dummy/log/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/celsodantas/protokoll/8eefacb627b0b81298650268cc7d240915bd89bb/test/dummy/log/.gitkeep -------------------------------------------------------------------------------- /test/dummy/public/404.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | The page you were looking for doesn't exist (404) 5 | 17 | 18 | 19 | 20 | 21 |
22 |

The page you were looking for doesn't exist.

23 |

You may have mistyped the address or the page may have moved.

24 |
25 | 26 | 27 | -------------------------------------------------------------------------------- /test/dummy/public/422.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | The change you wanted was rejected (422) 5 | 17 | 18 | 19 | 20 | 21 |
22 |

The change you wanted was rejected.

23 |

Maybe you tried to change something you didn't have access to.

24 |
25 | 26 | 27 | -------------------------------------------------------------------------------- /test/dummy/public/500.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | We're sorry, but something went wrong (500) 5 | 17 | 18 | 19 | 20 | 21 |
22 |

We're sorry, but something went wrong.

23 |

We've been notified about this issue and we'll take a look at it shortly.

24 |
25 | 26 | 27 | -------------------------------------------------------------------------------- /test/dummy/public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/celsodantas/protokoll/8eefacb627b0b81298650268cc7d240915bd89bb/test/dummy/public/favicon.ico -------------------------------------------------------------------------------- /test/dummy/script/rails: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | # This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application. 3 | 4 | APP_PATH = File.expand_path('../../config/application', __FILE__) 5 | require File.expand_path('../../config/boot', __FILE__) 6 | require 'rails/commands' 7 | -------------------------------------------------------------------------------- /test/protokoll_test.rb: -------------------------------------------------------------------------------- 1 | require 'test_helper' 2 | 3 | class ProtokollTest < ActiveSupport::TestCase 4 | 5 | def setup 6 | time = Time.local(2011, 9, 25, 12, 0, 0) 7 | Timecop.travel(time) 8 | end 9 | 10 | def teardown 11 | reset_models 12 | end 13 | 14 | def reset_models 15 | ProtokollTest.send(:remove_const, 'Protocol') if defined? Protocol 16 | ProtokollTest.send(:remove_const, 'Call') if defined? Call 17 | end 18 | 19 | ## 20 | # Tests 21 | ## 22 | 23 | test "using new number_symbol with default pattern should get 20110900001" do 24 | class Protocol < ActiveRecord::Base 25 | protokoll :number, :pattern => "%Y%m$$$$$", :number_symbol => "$" 26 | end 27 | 28 | protocol = Protocol.create 29 | assert_equal "20110900001", protocol.number 30 | end 31 | 32 | test "should get 20110900001 (format number %Y%m#####)" do 33 | class Protocol < ActiveRecord::Base 34 | protokoll :number 35 | end 36 | 37 | protocol = Protocol.create 38 | assert_equal "20110900001", protocol.number 39 | end 40 | 41 | test "should get 20110900002 for second save (format number %Y%m#####)" do 42 | class Protocol < ActiveRecord::Base 43 | protokoll :number 44 | end 45 | 46 | Protocol.create 47 | protocol2 = Protocol.create 48 | 49 | assert_equal "20110900002", protocol2.number 50 | end 51 | 52 | test "should get 201100002 for second save (format number %Y#####)" do 53 | class Protocol < ActiveRecord::Base 54 | protokoll :number, :pattern => "%Y#####" 55 | end 56 | 57 | protocol = Protocol.create 58 | 59 | assert_equal "201100001", protocol.number 60 | end 61 | 62 | test "second protocol on DB should have number should equals 1" do 63 | class Protocol < ActiveRecord::Base 64 | protokoll :number, :pattern => "#" 65 | end 66 | 67 | protocol = Protocol.create 68 | assert_equal "1", protocol.number 69 | end 70 | 71 | test "third protocol on DB should have number should equals 3" do 72 | class Protocol < ActiveRecord::Base 73 | protokoll :number, :pattern => "#" 74 | end 75 | 76 | Protocol.new.save 77 | Protocol.new.save 78 | protocol3 = Protocol.new 79 | protocol3.save 80 | 81 | assert_equal "3", protocol3.number 82 | end 83 | 84 | test "first using format A# should get A1" do 85 | class Protocol < ActiveRecord::Base 86 | protokoll :number, :pattern => "A#" 87 | end 88 | 89 | protocol1 = Protocol.new 90 | protocol1.save 91 | 92 | assert_equal "A1", protocol1.number 93 | end 94 | 95 | test "first using format A## should get A01" do 96 | class Protocol < ActiveRecord::Base 97 | protokoll :number, :pattern => "A##" 98 | end 99 | 100 | protocol1 = Protocol.create 101 | assert_equal "A01", protocol1.number 102 | end 103 | 104 | test "second using format A## should get A02" do 105 | class Protocol < ActiveRecord::Base 106 | protokoll :number, :pattern => "A##" 107 | end 108 | 109 | Protocol.create 110 | protocol2 = Protocol.create 111 | assert_equal "A02", protocol2.number 112 | end 113 | 114 | test "third using format A## should get A03" do 115 | class Protocol < ActiveRecord::Base 116 | protokoll :number, :pattern => "A##" 117 | end 118 | 119 | Protocol.create 120 | Protocol.create 121 | protocol3 = Protocol.create 122 | assert_equal "A03", protocol3.number 123 | end 124 | 125 | test "first use of %y# should get 111" do 126 | class Protocol < ActiveRecord::Base 127 | protokoll :number, :pattern => "%y#" 128 | end 129 | 130 | protocol1 = Protocol.create 131 | assert_equal "111", protocol1.number 132 | end 133 | 134 | 135 | test "first use of %y%m## should get 110901" do 136 | class Protocol < ActiveRecord::Base 137 | protokoll :number, :pattern => "%y%m##" 138 | end 139 | 140 | protocol1 = Protocol.create 141 | assert_equal "110901", protocol1.number 142 | end 143 | 144 | test "using %y%m## on next month after should get 111001" do 145 | class Protocol < ActiveRecord::Base 146 | protokoll :number, :pattern => "%y%m##" 147 | end 148 | 149 | time = Time.local(2011, 9, 25, 12, 0, 0) 150 | Timecop.travel(time) 151 | 152 | Protocol.create 153 | Protocol.create 154 | 155 | Timecop.travel(Time.now + 1.month) 156 | 157 | protocol = Protocol.create 158 | assert_equal "111001", protocol.number 159 | end 160 | 161 | test "%y%m%H#### should get 1109120001" do 162 | class Protocol < ActiveRecord::Base 163 | protokoll :number, :pattern => "%y%m%H####" 164 | end 165 | 166 | protocol1 = Protocol.create 167 | 168 | assert_equal "1109120001", protocol1.number 169 | end 170 | 171 | test "%y## on next year should get 1201" do 172 | class Protocol < ActiveRecord::Base 173 | protokoll :number, :pattern => "%y##" 174 | end 175 | 176 | time = Time.local(2011, 9, 25, 12, 3, 0) # 2011 177 | Protocol.create 178 | time = Time.local(2012, 9, 25, 12, 3, 0) # 2012 - exactly 1 year after 179 | Timecop.travel(time) 180 | 181 | protocol2 = Protocol.create 182 | 183 | assert_equal "1201", protocol2.number 184 | end 185 | 186 | test "500.time create using %y%m%H#### should get 1109120500" do 187 | class Protocol < ActiveRecord::Base 188 | protokoll :number, :pattern => "%y%m%H####" 189 | end 190 | 191 | 500.times { Protocol.create } 192 | 193 | assert_equal "1109120500", Protocol.last.number 194 | end 195 | 196 | test "PROT%H%m%y#### should get PROT%H%m%y0001" do 197 | class Protocol < ActiveRecord::Base 198 | protokoll :number, :pattern => "PROT%H%m%y####" 199 | end 200 | 201 | protocol1 = Protocol.create 202 | 203 | assert_equal "PROT1209110001", protocol1.number 204 | end 205 | 206 | test "PROT%Y%m%H#### should get PROT201109120001" do 207 | class Protocol < ActiveRecord::Base 208 | protokoll :number, :pattern => "PROT%Y%m%H####" 209 | end 210 | 211 | protocol1 = Protocol.create 212 | 213 | assert_equal "PROT201109120001", protocol1.number 214 | end 215 | 216 | test "use of sufix ####PROTO should get 0001PROTO" do 217 | class Protocol < ActiveRecord::Base 218 | protokoll :number, :pattern => "####PROTO" 219 | end 220 | 221 | protocol1 = Protocol.create 222 | 223 | assert_equal "0001PROTO", protocol1.number 224 | end 225 | 226 | test "use of sufix %Y%M####PROTO on 12:03 should get 2011030001PROTO" do 227 | class Protocol < ActiveRecord::Base 228 | protokoll :number, :pattern => "%Y%M####PROTO" 229 | end 230 | 231 | time = Time.local(2011, 9, 25, 12, 3, 0) 232 | Timecop.travel(time) 233 | 234 | protocol1 = Protocol.create 235 | 236 | assert_equal "2011030001PROTO", protocol1.number 237 | end 238 | 239 | test "use of sufix %Y%M####PROTO on 12:15 should get 2011150001PROTO" do 240 | class Protocol < ActiveRecord::Base 241 | protokoll :number, :pattern => "%Y%M####PROTO" 242 | end 243 | 244 | time = Time.local(2011, 9, 25, 12, 15, 0) 245 | Timecop.travel(time) 246 | 247 | protocol1 = Protocol.create 248 | 249 | assert_equal "2011150001PROTO", protocol1.number 250 | end 251 | 252 | test "using 2 models" do 253 | class Protocol < ActiveRecord::Base 254 | protokoll :number, :pattern => "%Y##" 255 | end 256 | 257 | class Call < ActiveRecord::Base 258 | protokoll :number, :pattern => "%Y##" 259 | end 260 | 261 | protocol = Protocol.create 262 | call = Call.create 263 | assert_equal "201101", protocol.number 264 | assert_equal "201101", call.number 265 | end 266 | 267 | test "using 2 models with different patterns" do 268 | class Protocol < ActiveRecord::Base 269 | protokoll :number, :pattern => "%YA#" 270 | end 271 | 272 | class Call < ActiveRecord::Base 273 | protokoll :number, :pattern => "%YB#" 274 | end 275 | 276 | protocol = Protocol.create 277 | call = Call.create 278 | assert_equal "2011A1", protocol.number 279 | assert_equal "2011B1", call.number 280 | end 281 | 282 | test "updating model should not get a different number" do 283 | class Protocol < ActiveRecord::Base 284 | protokoll :number, :pattern => "%Y##" 285 | end 286 | 287 | protocol = Protocol.new 288 | 289 | protocol.save 290 | assert_equal "201101", protocol.number 291 | 292 | protocol.save 293 | assert_equal "201101", protocol.number 294 | end 295 | 296 | test "reserve_number should set number to instance" do 297 | class Protocol < ActiveRecord::Base 298 | protokoll :number, :pattern => "%Y##" 299 | end 300 | 301 | protocol = Protocol.new 302 | protocol.reserve_number! 303 | 304 | assert_equal "201101", protocol.number 305 | protocol.save 306 | assert_equal "201101", protocol.number 307 | 308 | protocol = Protocol.new 309 | protocol.save 310 | assert_equal "201102", protocol.number 311 | end 312 | 313 | test "reserve_number should assure number if reserved" do 314 | class Protocol < ActiveRecord::Base 315 | protokoll :number, :pattern => "%Y##" 316 | end 317 | 318 | protocol1 = Protocol.new 319 | protocol1.reserve_number! 320 | 321 | protocol2 = Protocol.new 322 | protocol2.save 323 | 324 | assert_equal "201101", protocol1.number 325 | assert_equal "201102", protocol2.number 326 | end 327 | 328 | test "reserve_number should assure number if reserved if next month" do 329 | class Protocol < ActiveRecord::Base 330 | protokoll :number, :pattern => "%Y%m##" 331 | end 332 | 333 | protocol1 = Protocol.new 334 | protocol1.reserve_number! 335 | 336 | Timecop.travel(Time.now + 1.month) 337 | 338 | protocol2 = Protocol.new 339 | protocol2.save! 340 | 341 | assert_equal "20110901", protocol1.number 342 | assert_equal "20111001", protocol2.number 343 | end 344 | 345 | test "starting shift should work" do 346 | class Protocol < ActiveRecord::Base 347 | protokoll :number, :pattern => "%Y%m##", :start => 33 348 | end 349 | 350 | protocol = Protocol.new 351 | protocol.save 352 | 353 | assert_equal "20110934", protocol.number 354 | end 355 | 356 | test "counter should not be reset after first reset call while in the non reset window" do 357 | class Protocol < ActiveRecord::Base 358 | protokoll :number, :pattern => "%Y%m##" 359 | end 360 | 361 | protocol1 = Protocol.create 362 | 363 | Timecop.travel(Time.now + 1.month) 364 | protocol2 = Protocol.create 365 | 366 | Timecop.travel(2.day) 367 | protocol3 = Protocol.create 368 | 369 | assert_equal "20110901", protocol1.number 370 | assert_equal "20111001", protocol2.number 371 | assert_equal "20111002", protocol3.number 372 | end 373 | 374 | test "counter should consider day in pattern" do 375 | class Protocol < ActiveRecord::Base 376 | protokoll :number, :pattern => "%Y%m%d##" 377 | end 378 | 379 | protocol1 = Protocol.create! 380 | protocol2 = Protocol.create! 381 | 382 | Timecop.travel(1.day) 383 | 384 | protocol3 = Protocol.create! 385 | 386 | assert_equal "2011092501", protocol1.number 387 | assert_equal "2011092502", protocol2.number 388 | assert_equal "2011092601", protocol3.number 389 | end 390 | 391 | test "counter should consider instance method scope given" do 392 | class Protocol < ActiveRecord::Base 393 | protokoll :number, :scope_by => :context 394 | end 395 | 396 | protocol1 = Protocol.create! context: 'scenario_1' 397 | protocol2 = Protocol.create! context: 'scenario_2' 398 | protocol3 = Protocol.create! context: 'scenario_1' 399 | 400 | assert_equal "20110900001", protocol1.number 401 | assert_equal "20110900001", protocol2.number 402 | assert_equal "20110900002", protocol3.number 403 | end 404 | 405 | test "counter should consider Proc scope given" do 406 | class Protocol < ActiveRecord::Base 407 | protokoll :number, :scope_by => Proc.new { "#{context}-#{context_2}" } 408 | end 409 | 410 | protocol1 = Protocol.create! context: 'scenario_1', context_2: 'case1' 411 | protocol2 = Protocol.create! context: 'scenario_2', context_2: 'case1' 412 | protocol3 = Protocol.create! context: 'scenario_1', context_2: 'case1' 413 | protocol4 = Protocol.create! context: 'scenario_1', context_2: 'case2' 414 | 415 | assert_equal "20110900001", protocol1.number 416 | assert_equal "20110900001", protocol2.number 417 | assert_equal "20110900002", protocol3.number 418 | assert_equal "20110900001", protocol4.number 419 | end 420 | 421 | test "counter should consider lambda scope given" do 422 | class Protocol < ActiveRecord::Base 423 | protokoll :number, :scope_by => lambda { |o| "#{o.context}-#{o.context_2}" } 424 | end 425 | 426 | protocol1 = Protocol.create! context: 'scenario_1', context_2: 'case1' 427 | protocol2 = Protocol.create! context: 'scenario_2', context_2: 'case1' 428 | protocol3 = Protocol.create! context: 'scenario_1', context_2: 'case1' 429 | protocol4 = Protocol.create! context: 'scenario_1', context_2: 'case2' 430 | 431 | assert_equal "20110900001", protocol1.number 432 | assert_equal "20110900001", protocol2.number 433 | assert_equal "20110900002", protocol3.number 434 | assert_equal "20110900001", protocol4.number 435 | end 436 | 437 | 438 | test "rejects empty patterns" do 439 | 440 | assert_raise ArgumentError do 441 | class Protocol < ActiveRecord::Base 442 | protokoll :number, :pattern => nil 443 | end 444 | end 445 | end 446 | 447 | test "rejects invalid patterns" do 448 | 449 | assert_raise ArgumentError do 450 | class Protocol < ActiveRecord::Base 451 | protokoll :number, :pattern => "%ydodgyPattern" 452 | end 453 | end 454 | end 455 | 456 | end 457 | 458 | 459 | -------------------------------------------------------------------------------- /test/test_helper.rb: -------------------------------------------------------------------------------- 1 | # Configure Rails Environment 2 | ENV["RAILS_ENV"] = "test" 3 | 4 | require File.expand_path("../dummy/config/environment.rb", __FILE__) 5 | require "rails/test_help" 6 | 7 | require 'rubygems' 8 | require "timecop" 9 | 10 | Rails.backtrace_cleaner.remove_silencers! 11 | 12 | # Load support files 13 | Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f } 14 | --------------------------------------------------------------------------------