protokoll 1.0.3 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d637a366584a1d7151edb84e2be2ee0d4d7431ef
4
- data.tar.gz: b9670f42eb552e422964a3b74e173193648534a7
3
+ metadata.gz: 371221ad1d7739cb75f52cbbebc9b41584966daa
4
+ data.tar.gz: 56f2caa8d478d7bd0bf8703bbcb7a41f370baf40
5
5
  SHA512:
6
- metadata.gz: 1915150b136264c6187e5a171c7a20702e3a3dbac6ed6112c543fcc8c14f75bef592fe15aca9d7a8576bde78953e518cb92ad38721561ff8d489bfad74a25285
7
- data.tar.gz: e198fc55718542b826176f805f8d619404b1eb3ac87bd7fd8f130697ed6fb65e75878285218c9054c2945f51baa82c7e272cb8b8b5827cce5a8031afe2e6f827
6
+ metadata.gz: 495ac927b2a9f5e5ece2854ad642c404f61a6f89720eafd2c2fe817e33b1a538ecd21c25549defd9fbfc0033d36ed19c68d38b76f8d4860e6fc3b395380eeac9
7
+ data.tar.gz: ae9cc774cc128cf4d38ea3b0e677190bdddbc83503f36d2f5245591a9e70f355ca5e69d9f84d32619ab7d27abf36b15b495cc92ba13556f6b66cdf8cf8b49bfc
data/README.md CHANGED
@@ -81,6 +81,30 @@ end
81
81
  # will produce => "2011HOUSE00001", "2011HOUSE00002"...
82
82
  ```
83
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
+
84
108
  ## reserve_number!
85
109
 
86
110
  object.reserve_number!
@@ -116,6 +140,10 @@ Run the generator
116
140
 
117
141
  rails g protokoll:migration
118
142
 
143
+ Optional: If scope_by will be used run next generator as well
144
+
145
+ rails g protokoll:migration:scope_by
146
+
119
147
  and migrate your database
120
148
 
121
149
  rake db:migrate
@@ -0,0 +1,26 @@
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
@@ -0,0 +1,12 @@
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
@@ -3,7 +3,7 @@ require 'active_record'
3
3
  module Protokoll
4
4
  class Counter
5
5
  def self.next(object, options)
6
- element = Models::CustomAutoIncrement.find_or_create_by(counter_model_name: object.class.to_s.underscore)
6
+ element = Models::CustomAutoIncrement.find_or_create_by(build_attrs(object, options))
7
7
 
8
8
  element.counter = options[:start] if outdated?(element, options) || element.counter == 0
9
9
  element.counter += 1
@@ -18,6 +18,17 @@ module Protokoll
18
18
 
19
19
  private
20
20
 
21
+ def self.build_attrs(object, options)
22
+ attrs = {counter_model_name: object.class.to_s.underscore}
23
+ return attrs unless options[:scope_by]
24
+
25
+ scope_by = options[:scope_by].respond_to?(:call) ?
26
+ object.instance_eval(&options[:scope_by]) :
27
+ object.send(options[:scope_by])
28
+
29
+ attrs.merge(counter_model_scope: scope_by)
30
+ end
31
+
21
32
  def self.outdated?(record, options)
22
33
  Time.now.utc.strftime(update_event(options)).to_i > record.updated_at.strftime(update_event(options)).to_i
23
34
  end
@@ -14,7 +14,8 @@ module Protokoll
14
14
  options = { :pattern => "%Y%m#####",
15
15
  :number_symbol => "#",
16
16
  :column => column,
17
- :start => 0 }
17
+ :start => 0,
18
+ :scope_by => nil }
18
19
 
19
20
  options.merge!(_options)
20
21
  raise ArgumentError.new("pattern can't be nil!") if options[:pattern].nil?
@@ -1,3 +1,3 @@
1
1
  module Protokoll
2
- VERSION = "1.0.3"
2
+ VERSION = "2.0.0"
3
3
  end
@@ -2,6 +2,8 @@ class CreateProtocols < ActiveRecord::Migration
2
2
  def change
3
3
  create_table :protocols do |t|
4
4
  t.string :number
5
+ t.string :context
6
+ t.string :context_2
5
7
 
6
8
  t.timestamps
7
9
  end
@@ -1,7 +1,7 @@
1
1
  class CreateCustomAutoIncrements < ActiveRecord::Migration
2
2
  def up
3
3
  create_table :custom_auto_increments, :force => true do |t|
4
- t.string :counter_model_name
4
+ t.string :counter_model_name
5
5
  t.integer :counter, :default => 0
6
6
  t.timestamps
7
7
  end
@@ -0,0 +1,12 @@
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
@@ -11,7 +11,7 @@
11
11
  #
12
12
  # It's strongly recommended that you check this file into your version control system.
13
13
 
14
- ActiveRecord::Schema.define(version: 20120222164124) do
14
+ ActiveRecord::Schema.define(version: 20160310030821) do
15
15
 
16
16
  create_table "calls", force: :cascade do |t|
17
17
  t.string "number"
@@ -21,15 +21,19 @@ ActiveRecord::Schema.define(version: 20120222164124) do
21
21
 
22
22
  create_table "custom_auto_increments", force: :cascade do |t|
23
23
  t.string "counter_model_name"
24
- t.integer "counter", default: 0
24
+ t.integer "counter", default: 0
25
25
  t.datetime "created_at"
26
26
  t.datetime "updated_at"
27
+ t.string "counter_model_scope"
27
28
  end
28
29
 
30
+ add_index "custom_auto_increments", ["counter_model_name", "counter_model_scope"], name: "counter_model_name_scope", unique: true
29
31
  add_index "custom_auto_increments", ["counter_model_name"], name: "index_custom_auto_increments_on_counter_model_name"
30
32
 
31
33
  create_table "protocols", force: :cascade do |t|
32
34
  t.string "number"
35
+ t.string "context"
36
+ t.string "context_2"
33
37
  t.datetime "created_at"
34
38
  t.datetime "updated_at"
35
39
  end
@@ -388,6 +388,53 @@ class ProtokollTest < ActiveSupport::TestCase
388
388
  assert_equal "2011092601", protocol3.number
389
389
  end
390
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
+
391
438
  test "rejects empty patterns" do
392
439
 
393
440
  assert_raise ArgumentError do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: protokoll
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Celso Dantas
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-04-28 00:00:00.000000000 Z
11
+ date: 2016-09-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -50,6 +50,8 @@ files:
50
50
  - README.md
51
51
  - Rakefile
52
52
  - lib/generators/protokoll/migration/migration_generator.rb
53
+ - lib/generators/protokoll/migration/scope_by_generator.rb
54
+ - lib/generators/protokoll/migration/templates/add_scope_by_to_custom_auto_increments.rb
53
55
  - lib/generators/protokoll/migration/templates/create_custom_auto_increments.rb
54
56
  - lib/protokoll.rb
55
57
  - lib/protokoll/counter.rb
@@ -86,6 +88,7 @@ files:
86
88
  - test/dummy/db/migrate/20110923024431_create_protocols.rb
87
89
  - test/dummy/db/migrate/20110928013630_create_calls.rb
88
90
  - test/dummy/db/migrate/20120222164124_create_custom_auto_increments.rb
91
+ - test/dummy/db/migrate/20160310030821_add_scope_by_to_custom_auto_increments.rb
89
92
  - test/dummy/db/schema.rb
90
93
  - test/dummy/db/seeds.rb
91
94
  - test/dummy/db/test.sqlite3
@@ -118,7 +121,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
118
121
  version: '0'
119
122
  requirements: []
120
123
  rubyforge_project:
121
- rubygems_version: 2.4.5.1
124
+ rubygems_version: 2.5.1
122
125
  signing_key:
123
126
  specification_version: 4
124
127
  summary: A simple Rails gem to create custom autoincrement Time base values to a database
@@ -151,6 +154,7 @@ test_files:
151
154
  - test/dummy/db/migrate/20110923024431_create_protocols.rb
152
155
  - test/dummy/db/migrate/20110928013630_create_calls.rb
153
156
  - test/dummy/db/migrate/20120222164124_create_custom_auto_increments.rb
157
+ - test/dummy/db/migrate/20160310030821_add_scope_by_to_custom_auto_increments.rb
154
158
  - test/dummy/db/schema.rb
155
159
  - test/dummy/db/seeds.rb
156
160
  - test/dummy/db/test.sqlite3