ardb 0.28.1 → 0.29.2

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.
Files changed (59) hide show
  1. checksums.yaml +7 -7
  2. data/.l.yml +8 -0
  3. data/.rubocop.yml +3 -0
  4. data/.t.yml +6 -0
  5. data/Gemfile +21 -8
  6. data/README.md +252 -3
  7. data/ardb.gemspec +14 -10
  8. data/bin/ardb +3 -1
  9. data/lib/ardb.rb +110 -80
  10. data/lib/ardb/adapter/base.rb +73 -47
  11. data/lib/ardb/adapter/mysql.rb +4 -17
  12. data/lib/ardb/adapter/postgresql.rb +51 -46
  13. data/lib/ardb/adapter/sqlite.rb +11 -15
  14. data/lib/ardb/adapter_spy.rb +18 -30
  15. data/lib/ardb/cli.rb +29 -24
  16. data/lib/ardb/cli/clirb.rb +19 -17
  17. data/lib/ardb/cli/commands.rb +308 -129
  18. data/lib/ardb/db_tests.rb +4 -4
  19. data/lib/ardb/default_order_by.rb +13 -21
  20. data/lib/ardb/migration.rb +15 -16
  21. data/lib/ardb/record_spy.rb +46 -61
  22. data/lib/ardb/relation_spy.rb +27 -31
  23. data/lib/ardb/require_autoloaded_active_record_files.rb +174 -58
  24. data/lib/ardb/test_helpers.rb +13 -14
  25. data/lib/ardb/use_db_default.rb +10 -19
  26. data/lib/ardb/version.rb +3 -1
  27. data/script/determine_autoloaded_active_record_files.rb +31 -24
  28. data/test/helper.rb +6 -13
  29. data/test/support/factory.rb +4 -3
  30. data/test/support/fake_schema.rb +3 -1
  31. data/test/support/postgresql/migrations/{.gitkeep → .keep} +0 -0
  32. data/test/support/postgresql/schema.rb +2 -1
  33. data/test/support/postgresql/setup_test_db.rb +17 -15
  34. data/test/support/relative_require_test_db_file.rb +1 -0
  35. data/test/support/require_test_db_file.rb +1 -0
  36. data/test/system/.keep +0 -0
  37. data/test/unit/adapter/base_tests.rb +83 -55
  38. data/test/unit/adapter/mysql_tests.rb +4 -19
  39. data/test/unit/adapter/postgresql_tests.rb +21 -30
  40. data/test/unit/adapter/sqlite_tests.rb +5 -11
  41. data/test/unit/adapter_spy_tests.rb +6 -17
  42. data/test/unit/ardb_tests.rb +81 -53
  43. data/test/unit/cli_tests.rb +232 -157
  44. data/test/unit/db_tests_tests.rb +7 -7
  45. data/test/unit/default_order_by_tests.rb +21 -20
  46. data/test/unit/migration_tests.rb +17 -18
  47. data/test/unit/record_spy_tests.rb +36 -34
  48. data/test/unit/relation_spy_tests.rb +40 -63
  49. data/test/unit/test_helpers_tests.rb +7 -15
  50. data/test/unit/use_db_default_tests.rb +22 -17
  51. metadata +117 -84
  52. data/lib/ardb/has_slug.rb +0 -107
  53. data/lib/ardb/migration_helpers.rb +0 -77
  54. data/lib/ardb/pg_json.rb +0 -90
  55. data/test/support/postgresql/pg_json_migrations/20160519133432_create_pg_json_migrate_test.rb +0 -13
  56. data/test/system/pg_json_tests.rb +0 -85
  57. data/test/unit/has_slug_tests.rb +0 -341
  58. data/test/unit/migration_helpers_tests.rb +0 -65
  59. data/test/unit/pg_json_tests.rb +0 -39
@@ -1,8 +1,9 @@
1
- require 'active_record'
2
- require 'assert'
1
+ # frozen_string_literal: true
3
2
 
4
- module Ardb
3
+ require "active_record"
4
+ require "assert"
5
5
 
6
+ module Ardb
6
7
  class DbTests < Assert::Context
7
8
  around do |block|
8
9
  ActiveRecord::Base.transaction do
@@ -11,5 +12,4 @@ module Ardb
11
12
  end
12
13
  end
13
14
  end
14
-
15
15
  end
@@ -1,59 +1,51 @@
1
- require 'much-plugin'
1
+ # frozen_string_literal: true
2
2
 
3
- module Ardb
3
+ require "much-mixin"
4
4
 
5
+ module Ardb
5
6
  module DefaultOrderBy
6
- include MuchPlugin
7
+ include MuchMixin
7
8
 
8
9
  DEFAULT_ATTRIBUTE = :order_by
9
10
  DEFAULT_SCOPE_PROC = proc{ self.class.scoped }
10
11
 
11
- plugin_included do
12
- extend ClassMethods
13
- include InstanceMethods
14
-
12
+ mixin_included do
15
13
  @ardb_default_order_by_config = {}
16
-
17
14
  end
18
15
 
19
- module ClassMethods
20
-
16
+ mixin_class_methods do
21
17
  def default_order_by(options = nil)
22
18
  options ||= {}
23
19
 
24
20
  @ardb_default_order_by_config.merge!({
25
- :attribute => options[:attribute] || DEFAULT_ATTRIBUTE,
26
- :scope_proc => options[:scope] || DEFAULT_SCOPE_PROC
21
+ attribute: options[:attribute] || DEFAULT_ATTRIBUTE,
22
+ scope_proc: options[:scope] || DEFAULT_SCOPE_PROC,
27
23
  })
28
24
 
29
- before_validation :ardb_default_order_by, :on => :create
25
+ before_validation :ardb_default_order_by, on: :create
30
26
  end
31
27
 
32
28
  def ardb_default_order_by_config
33
29
  @ardb_default_order_by_config
34
30
  end
35
-
36
31
  end
37
32
 
38
- module InstanceMethods
39
-
33
+ mixin_instance_methods do
40
34
  private
41
35
 
42
36
  def reset_order_by
43
37
  attr_name = self.class.ardb_default_order_by_config[:attribute]
44
38
  scope_proc = self.class.ardb_default_order_by_config[:scope_proc]
45
39
 
46
- current_max = self.instance_eval(&scope_proc).maximum(attr_name) || 0
47
- self.send("#{attr_name}=", current_max + 1)
40
+ current_max = instance_eval(&scope_proc).maximum(attr_name) || 0
41
+ send("#{attr_name}=", current_max + 1)
48
42
  end
49
43
 
50
44
  def ardb_default_order_by
51
45
  attr_name = self.class.ardb_default_order_by_config[:attribute]
52
- reset_order_by if self.send(attr_name).to_s.empty?
46
+ reset_order_by if send(attr_name).to_s.empty?
53
47
  true
54
48
  end
55
-
56
49
  end
57
-
58
50
  end
59
51
  end
@@ -1,8 +1,10 @@
1
- require 'fileutils'
1
+ # frozen_string_literal: true
2
2
 
3
- module Ardb
3
+ require "fileutils"
4
4
 
5
+ module Ardb
5
6
  class Migration
7
+ NoIdentifierError = Class.new(ArgumentError)
6
8
 
7
9
  attr_reader :migrations_path, :identifier
8
10
  attr_reader :class_name, :file_name, :file_path, :source
@@ -15,19 +17,20 @@ module Ardb
15
17
 
16
18
  @class_name = @identifier.classify.pluralize
17
19
  @file_name = get_file_name(@identifier)
18
- @file_path = File.join(self.migrations_path, "#{@file_name}.rb")
19
-
20
- @source = "require 'ardb/migration_helpers'\n\n" \
21
- "class #{@class_name} < ActiveRecord::Migration\n" \
22
- " include Ardb::MigrationHelpers\n\n" \
23
- " def change\n" \
24
- " end\n\n" \
25
- "end\n"
20
+ @file_path = File.join(migrations_path, "#{@file_name}.rb")
21
+
22
+ migration_version = ActiveRecord::Migration.current_version
23
+ @source =
24
+ "class #{@class_name} "\
25
+ "< ActiveRecord::Migration[#{migration_version}]\n"\
26
+ " def change\n"\
27
+ " end\n"\
28
+ "end\n"
26
29
  end
27
30
 
28
31
  def save!
29
- FileUtils.mkdir_p self.migrations_path
30
- File.open(self.file_path, 'w'){ |f| f.write(self.source) }
32
+ FileUtils.mkdir_p migrations_path
33
+ File.open(file_path, "w"){ |f| f.write(source) }
31
34
  self
32
35
  end
33
36
 
@@ -36,9 +39,5 @@ module Ardb
36
39
  def get_file_name(identifier)
37
40
  "#{Time.now.strftime("%Y%m%d%H%M%S")}_#{identifier.underscore}"
38
41
  end
39
-
40
- NoIdentifierError = Class.new(ArgumentError)
41
-
42
42
  end
43
-
44
43
  end
@@ -1,26 +1,23 @@
1
- require 'arel'
2
- require 'much-plugin'
3
- require 'ardb/relation_spy'
1
+ # frozen_string_literal: true
4
2
 
5
- module Ardb
3
+ require "arel"
4
+ require "much-mixin"
5
+ require "ardb/relation_spy"
6
6
 
7
+ module Ardb
7
8
  module RecordSpy
8
- include MuchPlugin
9
+ include MuchMixin
9
10
 
10
11
  def self.new(&block)
11
- block ||= proc{ }
12
+ block ||= proc{}
12
13
  record_spy = Class.new{ include Ardb::RecordSpy }
13
14
  record_spy.class_eval(&block)
14
15
  record_spy
15
16
  end
16
17
 
17
- plugin_included do
18
- extend ClassMethods
19
- include InstanceMethods
20
- end
21
-
22
- module ClassMethods
18
+ CallbackType = Struct.new(:name, :options)
23
19
 
20
+ mixin_class_methods do
24
21
  attr_accessor :table_name
25
22
 
26
23
  # Associations
@@ -29,19 +26,18 @@ module Ardb
29
26
  @associations ||= []
30
27
  end
31
28
 
32
- [ :belongs_to, :has_one, :has_many ].each do |method_name|
33
-
29
+ [:belongs_to, :has_one, :has_many].each do |method_name|
34
30
  define_method(method_name) do |assoc_name, *args|
35
31
  define_method(assoc_name) do
36
- instance_variable_get("@#{assoc_name}") || (method_name == :has_many ? [] : nil)
32
+ instance_variable_get("@#{assoc_name}") ||
33
+ (method_name == :has_many ? [] : nil)
37
34
  end
38
35
  define_method("#{assoc_name}=") do |value|
39
36
  instance_variable_set("@#{assoc_name}", value)
40
37
  end
41
38
 
42
- self.associations << Association.new(method_name, assoc_name, *args)
39
+ associations << Association.new(method_name, assoc_name, *args)
43
40
  end
44
-
45
41
  end
46
42
 
47
43
  # Validations
@@ -50,7 +46,8 @@ module Ardb
50
46
  @validations ||= []
51
47
  end
52
48
 
53
- [ :validates_acceptance_of,
49
+ [
50
+ :validates_acceptance_of,
54
51
  :validates_confirmation_of,
55
52
  :validates_exclusion_of,
56
53
  :validates_format_of,
@@ -59,30 +56,29 @@ module Ardb
59
56
  :validates_numericality_of,
60
57
  :validates_presence_of,
61
58
  :validates_size_of,
62
- :validates_uniqueness_of
59
+ :validates_uniqueness_of,
63
60
  ].each do |method_name|
64
61
  type = method_name.to_s.match(/\Avalidates_(.+)_of\Z/)[1]
65
62
 
66
63
  define_method(method_name) do |*args|
67
- self.validations << Validation.new(type, *args)
64
+ validations << Validation.new(type, *args)
68
65
  end
69
-
70
66
  end
71
67
 
72
68
  def validates_associated(*args)
73
- self.validations << Validation.new(:associated, *args)
69
+ validations << Validation.new(:associated, *args)
74
70
  end
75
71
 
76
72
  def validates_with(*args)
77
- self.validations << Validation.new(:with, *args)
73
+ validations << Validation.new(:with, *args)
78
74
  end
79
75
 
80
76
  def validates_each(*args, &block)
81
- self.validations << Validation.new(:each, *args, &block)
77
+ validations << Validation.new(:each, *args, &block)
82
78
  end
83
79
 
84
80
  def validate(method_name = nil, &block)
85
- self.validations << Validation.new(:custom, method_name, &block)
81
+ validations << Validation.new(:custom, method_name, &block)
86
82
  end
87
83
 
88
84
  def callbacks
@@ -91,7 +87,8 @@ module Ardb
91
87
 
92
88
  # Callbacks
93
89
 
94
- [ :before_validation,
90
+ [
91
+ :before_validation,
95
92
  :after_validation,
96
93
  :before_create,
97
94
  :around_create,
@@ -108,13 +105,11 @@ module Ardb
108
105
  :after_commit,
109
106
  :after_rollback,
110
107
  :after_initialize,
111
- :after_find
108
+ :after_find,
112
109
  ].each do |method_name|
113
-
114
110
  define_method(method_name) do |*args, &block|
115
- self.callbacks << Callback.new(method_name, *args, &block)
111
+ callbacks << Callback.new(method_name, *args, &block)
116
112
  end
117
-
118
113
  end
119
114
 
120
115
  def custom_callback_types
@@ -122,23 +117,21 @@ module Ardb
122
117
  end
123
118
 
124
119
  def define_model_callbacks(*args)
125
- options = args.last.kind_of?(Hash) ? args.pop : {}
120
+ options = args.last.is_a?(Hash) ? args.pop : {}
126
121
  types = options[:only] || [:before, :around, :after]
127
122
  metaclass = class << self; self; end
128
123
 
129
124
  args.each do |name|
130
- self.custom_callback_types << CallbackType.new(name, options)
125
+ custom_callback_types << CallbackType.new(name, options)
131
126
  types.each do |type|
132
127
  method_name = "#{type}_#{name}"
133
128
  metaclass.send(:define_method, method_name) do |*args, &block|
134
- self.callbacks << Callback.new(method_name, *args, &block)
129
+ callbacks << Callback.new(method_name, *args, &block)
135
130
  end
136
131
  end
137
132
  end
138
133
  end
139
134
 
140
- CallbackType = Struct.new(:name, :options)
141
-
142
135
  # Scopes
143
136
 
144
137
  attr_writer :relation_spy
@@ -147,14 +140,15 @@ module Ardb
147
140
  end
148
141
 
149
142
  def arel_table
150
- @arel_table ||= Arel::Table.new(self.table_name)
143
+ @arel_table ||= Arel::Table.new(table_name)
151
144
  end
152
145
 
153
146
  def scoped
154
- self.relation_spy
147
+ relation_spy
155
148
  end
156
149
 
157
- [ :select,
150
+ [
151
+ :select,
158
152
  :from,
159
153
  :includes,
160
154
  :joins,
@@ -168,23 +162,19 @@ module Ardb
168
162
  :offset,
169
163
  :merge,
170
164
  :except,
171
- :only
165
+ :only,
172
166
  ].each do |method_name|
173
-
174
167
  define_method(method_name) do |*args|
175
- self.relation_spy.send(method_name, *args)
168
+ relation_spy.send(method_name, *args)
176
169
  end
177
-
178
170
  end
179
-
180
171
  end
181
172
 
182
- module InstanceMethods
183
-
173
+ mixin_instance_methods do
184
174
  attr_accessor :id
185
175
 
186
176
  def update_column(col, value)
187
- self.send("#{col}=", value)
177
+ send("#{col}=", value)
188
178
  end
189
179
 
190
180
  def manually_run_callbacks
@@ -192,10 +182,9 @@ module Ardb
192
182
  end
193
183
 
194
184
  def run_callbacks(name, &block)
195
- self.manually_run_callbacks << name
196
- block.call if block
185
+ manually_run_callbacks << name
186
+ block&.call
197
187
  end
198
-
199
188
  end
200
189
 
201
190
  class Association
@@ -212,8 +201,8 @@ module Ardb
212
201
  attr_reader :type, :args, :options, :block
213
202
 
214
203
  def initialize(type, *args, &block)
215
- @type = type.to_sym
216
- @options = args.last.kind_of?(::Hash) ? args.pop : {}
204
+ @type = type.to_sym
205
+ @options = args.last.is_a?(::Hash) ? args.pop : {}
217
206
  @args = args
218
207
  @block = block
219
208
  end
@@ -221,21 +210,17 @@ module Ardb
221
210
 
222
211
  class Validation
223
212
  attr_reader :type, :args, :options, :method_name, :block
224
- alias :columns :args
225
- alias :associations :args
226
- alias :classes :args
213
+ alias_method :columns, :args
214
+ alias_method :associations, :args
215
+ alias_method :classes, :args
227
216
 
228
217
  def initialize(type, *args, &block)
229
- @type = type.to_sym
230
- @options = args.last.kind_of?(::Hash) ? args.pop : {}
218
+ @type = type.to_sym
219
+ @options = args.last.is_a?(::Hash) ? args.pop : {}
231
220
  @args = args
232
221
  @block = block
233
- if @type == :custom
234
- @method_name = @args.first
235
- end
222
+ @method_name = @args.first if @type == :custom
236
223
  end
237
224
  end
238
-
239
225
  end
240
-
241
226
  end
@@ -1,7 +1,7 @@
1
- module Ardb
1
+ # frozen_string_literal: true
2
2
 
3
+ module Ardb
3
4
  class RelationSpy
4
-
5
5
  attr_reader :applied
6
6
  attr_accessor :limit_value, :offset_value
7
7
  attr_accessor :pluck_values, :maximum_values, :minimum_values
@@ -23,7 +23,7 @@ module Ardb
23
23
  end
24
24
 
25
25
  def ==(other)
26
- other.kind_of?(self.class) ? @applied == other.applied : super
26
+ other.is_a?(self.class) ? @applied == other.applied : super
27
27
  end
28
28
 
29
29
  def to_sql
@@ -39,52 +39,50 @@ module Ardb
39
39
 
40
40
  # ActiveRecord::QueryMethods
41
41
 
42
- [ :select,
43
- :from,
44
- :includes,
45
- :joins,
46
- :where,
47
- :group,
48
- :having,
49
- :order,
50
- :reverse_order,
51
- :readonly
52
- ].each do |type|
53
-
42
+ [:select,
43
+ :from,
44
+ :includes,
45
+ :joins,
46
+ :where,
47
+ :group,
48
+ :having,
49
+ :order,
50
+ :reverse_order,
51
+ :readonly,
52
+ ].each do |type|
54
53
  define_method(type) do |*args|
55
54
  @applied << AppliedExpression.new(type, args)
56
55
  self
57
56
  end
58
-
59
57
  end
60
58
 
61
59
  def limit(value)
62
60
  @limit_value = value ? value.to_i : nil
63
- @applied << AppliedExpression.new(:limit, [ value ])
61
+ @applied << AppliedExpression.new(:limit, [value])
64
62
  self
65
63
  end
66
64
 
67
65
  def offset(value)
68
66
  @offset_value = value ? value.to_i : nil
69
- @applied << AppliedExpression.new(:offset, [ value ])
67
+ @applied << AppliedExpression.new(:offset, [value])
70
68
  self
71
69
  end
72
70
 
73
71
  # ActiveRecord::SpawnMethods
74
72
 
75
73
  def merge(other)
76
- return self if self.equal?(other)
77
- if other.kind_of?(self.class)
78
- other.applied.each{ |a| self.send(a.type, *a.args) }
74
+ return self if equal?(other)
75
+ if other.is_a?(self.class)
76
+ other.applied.each{ |a| send(a.type, *a.args) }
79
77
  else
80
- @applied << AppliedExpression.new(:merge, [ other ])
78
+ @applied << AppliedExpression.new(:merge, [other])
81
79
  end
82
80
  self
83
81
  end
84
82
 
85
83
  def except(*skips)
86
84
  skips = skips.map(&:to_sym)
87
- self.dup.tap do |r|
85
+ dup.tap do |r|
88
86
  r.applied.reject!{ |a| skips.include?(a.type) }
89
87
  r.limit_value = nil if skips.include?(:limit)
90
88
  r.offset_value = nil if skips.include?(:offset)
@@ -93,7 +91,7 @@ module Ardb
93
91
 
94
92
  def only(*onlies)
95
93
  onlies = onlies.map(&:to_sym)
96
- self.dup.tap do |r|
94
+ dup.tap do |r|
97
95
  r.applied.reject!{ |a| !onlies.include?(a.type) }
98
96
  r.limit_value = nil unless onlies.include?(:limit)
99
97
  r.offset_value = nil unless onlies.include?(:offset)
@@ -108,19 +106,19 @@ module Ardb
108
106
  end
109
107
 
110
108
  def first
111
- self.all.first
109
+ all.first
112
110
  end
113
111
 
114
112
  def first!
115
- self.first || raise(NotFoundError)
113
+ first || raise(NotFoundError)
116
114
  end
117
115
 
118
116
  def last
119
- self.all.last
117
+ all.last
120
118
  end
121
119
 
122
120
  def last!
123
- self.last || raise(NotFoundError)
121
+ last || raise(NotFoundError)
124
122
  end
125
123
 
126
124
  def all
@@ -147,12 +145,10 @@ module Ardb
147
145
 
148
146
  class AppliedExpression < Struct.new(:type, :args)
149
147
  def to_sql
150
- "#{self.type}: #{self.args.inspect}"
148
+ "#{type}: #{args.inspect}"
151
149
  end
152
150
  end
153
151
 
154
152
  NotFoundError = Class.new(RuntimeError)
155
-
156
153
  end
157
-
158
154
  end