sequenced 3.0.0 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 19469f53054a51802c23c141597b405b2d061b9e
4
- data.tar.gz: 86e49028b4e37daf29ef770727d68e0edd096634
3
+ metadata.gz: 0706f251eb15142b2e59f24168bdfc83bbb86d51
4
+ data.tar.gz: e222ef16202f2703c315b107136687c5e484fe41
5
5
  SHA512:
6
- metadata.gz: 3e98ddf8640a706f049d2295861792cd7b5e10d21e6c4d1591ed46126bf645590fb8ab00e3b9ded0c882bd5530d60deec9e013824f86d539e73e9c56a7fb9af5
7
- data.tar.gz: cb58678c8d817b23677162008c60120f564f48b41713d9ecc9a97922ac74c1b7677c3691b5ab1661ce171e9c82c709fefcc48425773c74b5ac4d91caaf619896
6
+ metadata.gz: 6b2e5b704a339e4449d1ba8175bb72dc5e857450d693aed070304c61865d546bc3459e8412d7990ac112f8da0f3f4ff1d560f94be09c000c66a2bf4d5d6c6a9b
7
+ data.tar.gz: 601a93fee12817506cce21f0027eb89b35418cffe1cfa26183810fd59ae31adf435f609d53ffb92e1e3c9ae3cfff6e5d281b9636c4045ff762d79fde4ae46d2d
@@ -1,8 +1,14 @@
1
+ 3.1.0 (January 23, 2016)
2
+ -------------------------
3
+
4
+ * Allow multiple sequences on one record
5
+ (samphilipd, [#19](https://github.com/djreimer/sequenced/pull/19))
6
+
1
7
  3.0.0 (November 28, 2015)
2
8
  -------------------------
3
9
 
4
10
  * Make this gem thread-safe for PostgreSQL
5
- (samphilipd, [#16](https://github.com/djreimer/sequenced/pull/16))
11
+ (samphilipd, [#16](https://github.com/djreimer/sequenced/pull/16))
6
12
 
7
13
  2.0.0 (October 24, 2014)
8
14
  ------------------------
@@ -1,4 +1,4 @@
1
- Copyright 2015 Derrick Reimer
1
+ Copyright 2016 Derrick Reimer
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/README.md CHANGED
@@ -70,6 +70,18 @@ class Answer < ActiveRecord::Base
70
70
  end
71
71
  ```
72
72
 
73
+ Multiple sequences can be defined by using the macro multiple times:
74
+
75
+ ```ruby
76
+ class Answer < ActiveRecord::Base
77
+ belongs_to :account
78
+ belongs_to :question
79
+
80
+ acts_as_sequenced column: :question_answer_number, scope: :question_id
81
+ acts_as_sequenced column: :account_answer_number, scope: :account_id
82
+ end
83
+ ```
84
+
73
85
  ## Schema and data integrity
74
86
 
75
87
  **This gem is only concurrent-safe for PostgreSQL databases**. For other database systems, unexpected behavior may occur if you attempt to create records concurrently.
@@ -3,6 +3,12 @@ require 'active_support/core_ext/class/attribute_accessors'
3
3
 
4
4
  module Sequenced
5
5
  module ActsAsSequenced
6
+ DEFAULT_OPTIONS = {
7
+ column: :sequential_id,
8
+ start_at: 1
9
+ }.freeze
10
+ SequencedColumnExists = Class.new(StandardError)
11
+
6
12
  def self.included(base)
7
13
  base.extend ClassMethods
8
14
  end
@@ -11,6 +17,8 @@ module Sequenced
11
17
  # Public: Defines ActiveRecord callbacks to set a sequential ID scoped
12
18
  # on a specific class.
13
19
  #
20
+ # Can be called multiple times to add hooks for different column names.
21
+ #
14
22
  # options - The Hash of options for configuration:
15
23
  # :scope - The Symbol representing the columm on which the
16
24
  # sequential ID should be scoped (default: nil)
@@ -31,17 +39,36 @@ module Sequenced
31
39
  #
32
40
  # Returns nothing.
33
41
  def acts_as_sequenced(options = {})
34
- cattr_accessor :sequenced_options
35
- self.sequenced_options = options
42
+ unless defined?(sequenced_options)
43
+ include Sequenced::ActsAsSequenced::InstanceMethods
44
+
45
+ mattr_accessor :sequenced_options, instance_accessor: false do
46
+ []
47
+ end
48
+
49
+ before_save :set_sequential_ids
50
+ end
51
+
52
+ options = DEFAULT_OPTIONS.merge(options)
53
+ column_name = options[:column]
36
54
 
37
- before_save :set_sequential_id
38
- include Sequenced::ActsAsSequenced::InstanceMethods
55
+ if sequenced_options.any? {|options| options[:column] == column_name}
56
+ raise(SequencedColumnExists, <<-MSG.squish)
57
+ Tried to set #{column_name} as sequenced but there was already a
58
+ definition here. Did you accidentally call acts_as_sequenced
59
+ multiple times on the same column?
60
+ MSG
61
+ else
62
+ sequenced_options << options
63
+ end
39
64
  end
40
65
  end
41
66
 
42
67
  module InstanceMethods
43
- def set_sequential_id
44
- Sequenced::Generator.new(self, self.class.base_class.sequenced_options).set
68
+ def set_sequential_ids
69
+ self.class.base_class.sequenced_options.each do |options|
70
+ Sequenced::Generator.new(self, options).set
71
+ end
45
72
  end
46
73
  end
47
74
  end
@@ -5,8 +5,8 @@ module Sequenced
5
5
  def initialize(record, options = {})
6
6
  @record = record
7
7
  @scope = options[:scope]
8
- @column = (options[:column] || :sequential_id).to_sym
9
- @start_at = options[:start_at] || 1
8
+ @column = options[:column].to_sym
9
+ @start_at = options[:start_at]
10
10
  @skip = options[:skip]
11
11
  end
12
12
 
@@ -1,3 +1,3 @@
1
1
  module Sequenced
2
- VERSION = "3.0.0"
2
+ VERSION = "3.1.0"
3
3
  end
@@ -0,0 +1,4 @@
1
+ class Doppelganger < ActiveRecord::Base
2
+ acts_as_sequenced column: :sequential_id_one
3
+ acts_as_sequenced column: :sequential_id_two, start_at: 1000
4
+ end
@@ -9,7 +9,7 @@ Dummy::Application.configure do
9
9
  config.action_controller.perform_caching = true
10
10
 
11
11
  # Disable Rails's static asset server (Apache or nginx will already do this)
12
- config.serve_static_assets = false
12
+ config.serve_static_files = false
13
13
 
14
14
  # Compress JavaScripts and CSS
15
15
  config.assets.compress = true
@@ -9,7 +9,7 @@ Dummy::Application.configure do
9
9
  config.cache_classes = true
10
10
 
11
11
  # Configure static asset server for tests with Cache-Control for performance
12
- config.serve_static_assets = true
12
+ config.serve_static_files = true
13
13
  config.static_cache_control = "public, max-age=3600"
14
14
 
15
15
  # Log error messages when you accidentally call methods on nil
@@ -38,4 +38,6 @@ Dummy::Application.configure do
38
38
 
39
39
  # Required for Rails >= 4.2
40
40
  config.eager_load = true
41
+
42
+ config.active_support.test_order = :random
41
43
  end
@@ -0,0 +1,10 @@
1
+ class CreateDoppelgangers < ActiveRecord::Migration
2
+ def change
3
+ create_table :doppelgangers do |t|
4
+ t.integer :sequential_id_one
5
+ t.integer :sequential_id_two
6
+
7
+ t.timestamps null: false
8
+ end
9
+ end
10
+ end
@@ -11,22 +11,22 @@
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: 20130715002029) do
14
+ ActiveRecord::Schema.define(version: 20160118182655) do
15
15
 
16
- create_table "accounts", force: true do |t|
16
+ create_table "accounts", force: :cascade do |t|
17
17
  t.string "name"
18
18
  t.datetime "created_at"
19
19
  t.datetime "updated_at"
20
20
  end
21
21
 
22
- create_table "addresses", force: true do |t|
22
+ create_table "addresses", force: :cascade do |t|
23
23
  t.integer "account_id"
24
24
  t.string "city"
25
25
  t.datetime "created_at"
26
26
  t.datetime "updated_at"
27
27
  end
28
28
 
29
- create_table "answers", force: true do |t|
29
+ create_table "answers", force: :cascade do |t|
30
30
  t.integer "question_id"
31
31
  t.text "body"
32
32
  t.integer "sequential_id"
@@ -37,7 +37,7 @@ ActiveRecord::Schema.define(version: 20130715002029) do
37
37
  add_index "answers", ["question_id"], name: "index_answers_on_question_id"
38
38
  add_index "answers", ["sequential_id"], name: "index_answers_on_sequential_id"
39
39
 
40
- create_table "comments", force: true do |t|
40
+ create_table "comments", force: :cascade do |t|
41
41
  t.integer "question_id"
42
42
  t.text "body"
43
43
  t.integer "sequential_id"
@@ -47,7 +47,21 @@ ActiveRecord::Schema.define(version: 20130715002029) do
47
47
 
48
48
  add_index "comments", ["question_id"], name: "index_comments_on_question_id"
49
49
 
50
- create_table "emails", force: true do |t|
50
+ create_table "concurrent_badgers", force: :cascade do |t|
51
+ t.integer "sequential_id", null: false
52
+ t.integer "burrow_id"
53
+ end
54
+
55
+ add_index "concurrent_badgers", ["sequential_id", "burrow_id"], name: "unique_concurrent", unique: true
56
+
57
+ create_table "doppelgangers", force: :cascade do |t|
58
+ t.integer "sequential_id_one"
59
+ t.integer "sequential_id_two"
60
+ t.datetime "created_at", null: false
61
+ t.datetime "updated_at", null: false
62
+ end
63
+
64
+ create_table "emails", force: :cascade do |t|
51
65
  t.string "emailable_type"
52
66
  t.integer "emailable_id"
53
67
  t.integer "sequential_id"
@@ -56,7 +70,7 @@ ActiveRecord::Schema.define(version: 20130715002029) do
56
70
  t.datetime "updated_at"
57
71
  end
58
72
 
59
- create_table "invoices", force: true do |t|
73
+ create_table "invoices", force: :cascade do |t|
60
74
  t.integer "amount"
61
75
  t.integer "sequential_id"
62
76
  t.integer "account_id"
@@ -66,21 +80,41 @@ ActiveRecord::Schema.define(version: 20130715002029) do
66
80
 
67
81
  add_index "invoices", ["account_id"], name: "index_invoices_on_account_id"
68
82
 
69
- create_table "orders", force: true do |t|
83
+ create_table "monsters", force: :cascade do |t|
84
+ t.integer "sequential_id"
85
+ t.string "type"
86
+ t.datetime "created_at"
87
+ t.datetime "updated_at"
88
+ end
89
+
90
+ create_table "orders", force: :cascade do |t|
70
91
  t.string "product"
71
92
  t.integer "account_id"
72
93
  t.datetime "created_at"
73
94
  t.datetime "updated_at"
74
95
  end
75
96
 
76
- create_table "questions", force: true do |t|
97
+ create_table "policemen", force: :cascade do |t|
98
+ t.integer "sequential_id"
99
+ t.datetime "created_at"
100
+ t.datetime "updated_at"
101
+ end
102
+
103
+ create_table "products", force: :cascade do |t|
104
+ t.integer "account_id"
105
+ t.integer "sequential_id"
106
+ t.datetime "created_at"
107
+ t.datetime "updated_at"
108
+ end
109
+
110
+ create_table "questions", force: :cascade do |t|
77
111
  t.string "summary"
78
112
  t.text "body"
79
113
  t.datetime "created_at"
80
114
  t.datetime "updated_at"
81
115
  end
82
116
 
83
- create_table "ratings", force: true do |t|
117
+ create_table "ratings", force: :cascade do |t|
84
118
  t.integer "comment_id"
85
119
  t.integer "score"
86
120
  t.integer "sequential_id"
@@ -88,14 +122,14 @@ ActiveRecord::Schema.define(version: 20130715002029) do
88
122
  t.datetime "updated_at"
89
123
  end
90
124
 
91
- create_table "subscriptions", force: true do |t|
125
+ create_table "subscriptions", force: :cascade do |t|
92
126
  t.string "plan"
93
127
  t.integer "sequential_id"
94
128
  t.datetime "created_at"
95
129
  t.datetime "updated_at"
96
130
  end
97
131
 
98
- create_table "users", force: true do |t|
132
+ create_table "users", force: :cascade do |t|
99
133
  t.integer "account_id"
100
134
  t.string "name"
101
135
  t.integer "custom_sequential_id"
@@ -0,0 +1,20 @@
1
+ require 'test_helper'
2
+
3
+ class MultipleSequencesTest < ActiveSupport::TestCase
4
+ test "works with simple multiple sequences" do
5
+ doppelganger = Doppelganger.create!
6
+ assert_equal 1, doppelganger.sequential_id_one
7
+ assert_equal 1000, doppelganger.sequential_id_two
8
+ end
9
+
10
+ test "raises error on multiple definitions for the same column" do
11
+ assert_raise Sequenced::ActsAsSequenced::SequencedColumnExists do
12
+ Doppelganger.class_eval do
13
+ acts_as_sequenced column: :sequential_id_one, start_at: 99
14
+ end
15
+ end
16
+
17
+ doppelganger = Doppelganger.create!
18
+ assert_equal 1, doppelganger.sequential_id_one
19
+ end
20
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sequenced
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.0
4
+ version: 3.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Derrick Reimer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-11-28 00:00:00.000000000 Z
11
+ date: 2016-01-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -87,6 +87,7 @@ files:
87
87
  - test/dummy/app/models/answer.rb
88
88
  - test/dummy/app/models/comment.rb
89
89
  - test/dummy/app/models/concurrent_badger.rb
90
+ - test/dummy/app/models/doppelganger.rb
90
91
  - test/dummy/app/models/email.rb
91
92
  - test/dummy/app/models/invoice.rb
92
93
  - test/dummy/app/models/monster.rb
@@ -132,6 +133,7 @@ files:
132
133
  - test/dummy/db/migrate/20131226000000_create_monsters.rb
133
134
  - test/dummy/db/migrate/20140404195334_create_policemen.rb
134
135
  - test/dummy/db/migrate/20151120190645_create_concurrent_badgers.rb
136
+ - test/dummy/db/migrate/20160118182655_create_doppelgangers.rb
135
137
  - test/dummy/db/schema.rb
136
138
  - test/dummy/db/test.sqlite3
137
139
  - test/dummy/lib/assets/.gitkeep
@@ -143,6 +145,7 @@ files:
143
145
  - test/dummy/public/500.html
144
146
  - test/dummy/public/favicon.ico
145
147
  - test/dummy/script/rails
148
+ - test/multiple_sequences_test.rb
146
149
  - test/test_helper.rb
147
150
  homepage: https://github.com/djreimer/sequenced
148
151
  licenses:
@@ -180,6 +183,7 @@ test_files:
180
183
  - test/dummy/app/models/answer.rb
181
184
  - test/dummy/app/models/comment.rb
182
185
  - test/dummy/app/models/concurrent_badger.rb
186
+ - test/dummy/app/models/doppelganger.rb
183
187
  - test/dummy/app/models/email.rb
184
188
  - test/dummy/app/models/invoice.rb
185
189
  - test/dummy/app/models/monster.rb
@@ -225,6 +229,7 @@ test_files:
225
229
  - test/dummy/db/migrate/20131226000000_create_monsters.rb
226
230
  - test/dummy/db/migrate/20140404195334_create_policemen.rb
227
231
  - test/dummy/db/migrate/20151120190645_create_concurrent_badgers.rb
232
+ - test/dummy/db/migrate/20160118182655_create_doppelgangers.rb
228
233
  - test/dummy/db/schema.rb
229
234
  - test/dummy/db/test.sqlite3
230
235
  - test/dummy/log/development.log
@@ -236,5 +241,5 @@ test_files:
236
241
  - test/dummy/Rakefile
237
242
  - test/dummy/README.rdoc
238
243
  - test/dummy/script/rails
244
+ - test/multiple_sequences_test.rb
239
245
  - test/test_helper.rb
240
- has_rdoc: