baza_models 0.0.13 → 0.0.14

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +134 -25
  3. data/.rubocop_todo.yml +47 -32
  4. data/Gemfile +3 -1
  5. data/Gemfile.lock +55 -48
  6. data/Rakefile +2 -4
  7. data/VERSION +1 -1
  8. data/baza_models.gemspec +9 -4
  9. data/lib/baza_models/autoloader.rb +1 -0
  10. data/lib/baza_models/errors.rb +2 -3
  11. data/lib/baza_models/model.rb +22 -21
  12. data/lib/baza_models/model/belongs_to_relations.rb +1 -3
  13. data/lib/baza_models/model/has_many_relations.rb +2 -2
  14. data/lib/baza_models/model/has_one_relations.rb +2 -2
  15. data/lib/baza_models/model/manipulation.rb +20 -18
  16. data/lib/baza_models/model/queries.rb +2 -0
  17. data/lib/baza_models/model/scopes.rb +1 -0
  18. data/lib/baza_models/model/validations.rb +1 -0
  19. data/lib/baza_models/query.rb +12 -10
  20. data/lib/baza_models/query/inspector.rb +2 -0
  21. data/lib/baza_models/query/pagination.rb +5 -7
  22. data/lib/baza_models/query/sql_generator.rb +3 -3
  23. data/lib/baza_models/ransacker.rb +3 -1
  24. data/lib/baza_models/ransacker/relationship_scanner.rb +3 -0
  25. data/lib/baza_models/validators/confirmation_validator.rb +1 -3
  26. data/lib/baza_models/validators/uniqueness_validator.rb +2 -4
  27. data/peak_flow.yml +1 -0
  28. data/spec/baza_models/autoloader_spec.rb +1 -1
  29. data/spec/baza_models/baza_orm_adapter_spec.rb +1 -1
  30. data/spec/baza_models/model/has_many_relations_spec.rb +1 -1
  31. data/spec/baza_models/model/manipulation_spec.rb +3 -3
  32. data/spec/baza_models/model_spec.rb +22 -4
  33. data/spec/baza_models/query_spec.rb +14 -6
  34. data/spec/spec_helper.rb +1 -1
  35. data/spec/support/database_helper.rb +6 -3
  36. data/spec/test_classes/user.rb +2 -1
  37. metadata +30 -3
  38. data/spec/baza_models_spec.rb +0 -4
data/Rakefile CHANGED
@@ -1,12 +1,10 @@
1
- # encoding: utf-8
2
-
3
1
  require "rubygems"
4
2
  require "bundler"
5
3
  begin
6
4
  Bundler.setup(:default, :development)
7
5
  rescue Bundler::BundlerError => e
8
- $stderr.puts e.message
9
- $stderr.puts "Run `bundle install` to install missing gems"
6
+ warn e.message
7
+ warn "Run `bundle install` to install missing gems"
10
8
  exit e.status_code
11
9
  end
12
10
  require "rake"
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.13
1
+ 0.0.14
@@ -2,16 +2,16 @@
2
2
  # DO NOT EDIT THIS FILE DIRECTLY
3
3
  # Instead, edit Juwelier::Tasks in Rakefile, and run 'rake gemspec'
4
4
  # -*- encoding: utf-8 -*-
5
- # stub: baza_models 0.0.13 ruby lib
5
+ # stub: baza_models 0.0.14 ruby lib
6
6
 
7
7
  Gem::Specification.new do |s|
8
8
  s.name = "baza_models".freeze
9
- s.version = "0.0.13"
9
+ s.version = "0.0.14"
10
10
 
11
11
  s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
12
12
  s.require_paths = ["lib".freeze]
13
13
  s.authors = ["kaspernj".freeze]
14
- s.date = "2019-10-18"
14
+ s.date = "2021-01-02"
15
15
  s.description = "ActiveRecord like models for the Baza database framework".freeze
16
16
  s.email = "k@spernj.org".freeze
17
17
  s.extra_rdoc_files = [
@@ -92,7 +92,6 @@ Gem::Specification.new do |s|
92
92
  "spec/baza_models/validators/format_validator_spec.rb",
93
93
  "spec/baza_models/validators/length_validator_spec.rb",
94
94
  "spec/baza_models/validators/uniqueness_validator_spec.rb",
95
- "spec/baza_models_spec.rb",
96
95
  "spec/factories/organization.rb",
97
96
  "spec/factories/user.rb",
98
97
  "spec/spec_helper.rb",
@@ -127,6 +126,8 @@ Gem::Specification.new do |s|
127
126
  s.add_development_dependency(%q<rdoc>.freeze, [">= 0"])
128
127
  s.add_development_dependency(%q<rspec>.freeze, [">= 0"])
129
128
  s.add_development_dependency(%q<rubocop>.freeze, [">= 0"])
129
+ s.add_development_dependency(%q<rubocop-performance>.freeze, [">= 0"])
130
+ s.add_development_dependency(%q<rubocop-rspec>.freeze, [">= 0"])
130
131
  s.add_development_dependency(%q<sqlite3>.freeze, [">= 0"])
131
132
  else
132
133
  s.add_dependency(%q<array_enumerator>.freeze, [">= 0"])
@@ -144,6 +145,8 @@ Gem::Specification.new do |s|
144
145
  s.add_dependency(%q<rdoc>.freeze, [">= 0"])
145
146
  s.add_dependency(%q<rspec>.freeze, [">= 0"])
146
147
  s.add_dependency(%q<rubocop>.freeze, [">= 0"])
148
+ s.add_dependency(%q<rubocop-performance>.freeze, [">= 0"])
149
+ s.add_dependency(%q<rubocop-rspec>.freeze, [">= 0"])
147
150
  s.add_dependency(%q<sqlite3>.freeze, [">= 0"])
148
151
  end
149
152
  else
@@ -162,6 +165,8 @@ Gem::Specification.new do |s|
162
165
  s.add_dependency(%q<rdoc>.freeze, [">= 0"])
163
166
  s.add_dependency(%q<rspec>.freeze, [">= 0"])
164
167
  s.add_dependency(%q<rubocop>.freeze, [">= 0"])
168
+ s.add_dependency(%q<rubocop-performance>.freeze, [">= 0"])
169
+ s.add_dependency(%q<rubocop-rspec>.freeze, [">= 0"])
165
170
  s.add_dependency(%q<sqlite3>.freeze, [">= 0"])
166
171
  end
167
172
  end
@@ -125,6 +125,7 @@ private
125
125
  orig_model = @models.detect { |array_model| array_model.id == orig_model_id }
126
126
 
127
127
  raise "Already autoloaded?" if orig_model.autoloads.key?(autoload_name)
128
+
128
129
  orig_model.autoloads[autoload_name] = model
129
130
 
130
131
  result.fetch(:models) << model
@@ -1,5 +1,6 @@
1
1
  class BazaModels::Errors
2
2
  class InvalidRecord < RuntimeError; end
3
+
3
4
  class RecordNotFound < RuntimeError; end
4
5
 
5
6
  def initialize
@@ -18,9 +19,7 @@ class BazaModels::Errors
18
19
  errors.each do |error|
19
20
  message = ""
20
21
 
21
- unless attribute_name == :base
22
- message << "#{StringCases.snake_to_camel(attribute_name)} "
23
- end
22
+ message << "#{StringCases.snake_to_camel(attribute_name)} " unless attribute_name == :base
24
23
 
25
24
  message << error
26
25
  messages << message
@@ -16,6 +16,7 @@ class BazaModels::Model
16
16
 
17
17
  attr_accessor :data, :db
18
18
  attr_reader :changes, :errors
19
+ attr_writer :db, :table_name
19
20
 
20
21
  # Define all callback methods.
21
22
  CALLBACK_TYPES = [
@@ -75,9 +76,7 @@ class BazaModels::Model
75
76
  end
76
77
  end
77
78
 
78
- # rubocop:disable Style/TrivialAccessors
79
79
  def new_record?
80
- # rubocop:enable Style/TrivialAccessors
81
80
  @new_record
82
81
  end
83
82
 
@@ -87,21 +86,23 @@ class BazaModels::Model
87
86
 
88
87
  def db
89
88
  return @db if @db
89
+
90
90
  @db ||= self.class.db
91
91
  end
92
92
 
93
- attr_writer :db
94
-
95
93
  def self.attribute_names
96
94
  init_model
97
95
  @table.columns.map(&:name).map(&:clone)
98
96
  end
99
97
 
100
98
  def self.db
101
- @db = nil if @db && @db.closed?
99
+ @db = nil if
100
+ @db&.closed?
102
101
  return @db if @db
102
+
103
103
  @db ||= BazaModels.primary_db
104
104
  raise "No Baza database has been configured" unless @db
105
+
105
106
  @db
106
107
  end
107
108
 
@@ -144,8 +145,6 @@ class BazaModels::Model
144
145
  attr_writer :db, :table_name
145
146
  end
146
147
 
147
- attr_writer :table_name
148
-
149
148
  def autoloads
150
149
  @autoloads ||= {}
151
150
  @autoloads
@@ -187,7 +186,7 @@ class BazaModels::Model
187
186
  end
188
187
 
189
188
  def to_param
190
- id.to_s if id
189
+ id&.to_s
191
190
  end
192
191
 
193
192
  def to_key
@@ -201,6 +200,7 @@ class BazaModels::Model
201
200
  def reload
202
201
  @data = db.single(table_name, {id: id}, limit: 1)
203
202
  raise BazaModels::Errors::RecordNotFound unless @data
203
+
204
204
  @changes = {}
205
205
  self
206
206
  end
@@ -233,15 +233,15 @@ class BazaModels::Model
233
233
  return false unless self.class == other.class
234
234
 
235
235
  if new_record? && other.new_record?
236
- return merged_data == other.__send__(:merged_data)
236
+ merged_data == other.__send__(:merged_data)
237
237
  else
238
- return id == other.id
238
+ id == other.id
239
239
  end
240
240
  end
241
241
 
242
- # rubocop:disable Style/PredicateName
242
+ # rubocop:disable Naming/PredicateName
243
243
  def has_attribute?(name)
244
- # rubocop:enable Style/PredicateName
244
+ # rubocop:enable Naming/PredicateName
245
245
  self.class.column_names.include?(name.to_s)
246
246
  end
247
247
 
@@ -260,6 +260,7 @@ class BazaModels::Model
260
260
 
261
261
  def read_attribute(attribute_name)
262
262
  return @changes.fetch(attribute_name) if @changes.key?(attribute_name)
263
+
263
264
  @data.fetch(attribute_name)
264
265
  end
265
266
 
@@ -271,6 +272,7 @@ class BazaModels::Model
271
272
  changed = false
272
273
  @changes.each do |key, value|
273
274
  next if @data.fetch(key) == value
275
+
274
276
  changed = true
275
277
  break
276
278
  end
@@ -283,10 +285,7 @@ protected
283
285
  class << self
284
286
  attr_reader :__blank_attributes
285
287
  end
286
-
287
- # rubocop:disable Style/TrivialAccessors
288
288
  def self.model_initialized?
289
- # rubocop:enable Style/TrivialAccessors
290
289
  @model_initialized
291
290
  end
292
291
 
@@ -359,12 +358,11 @@ protected
359
358
  attributes.each do |attribute_name, attribute_value|
360
359
  belongs_to_relations = self.class.instance_variable_get(:@belongs_to_relations)
361
360
 
362
- if belongs_to_relations
363
- belongs_to_relations.each do |relation|
364
- if attribute_name.to_s == relation[:relation_name].to_s
365
- attribute_name = :"#{attribute_name}_id"
366
- attribute_value = attribute_value.id if attribute_value
367
- end
361
+
362
+ belongs_to_relations&.each do |relation|
363
+ if attribute_name.to_s == relation[:relation_name].to_s
364
+ attribute_name = :"#{attribute_name}_id"
365
+ attribute_value = attribute_value.id if attribute_value
368
366
  end
369
367
  end
370
368
 
@@ -385,16 +383,19 @@ protected
385
383
 
386
384
  def attribute_before_last_save(attribute_name)
387
385
  return @before_last_save.fetch(attribute_name) if @before_last_save.key?(attribute_name)
386
+
388
387
  @data.fetch(attribute_name)
389
388
  end
390
389
 
391
390
  def will_save_change_to_attribute?(attribute_name)
392
391
  return true if @changes.key?(attribute_name) && @changes[attribute_name] != data[attribute_name]
392
+
393
393
  false
394
394
  end
395
395
 
396
396
  def method_missing(method_name, *args, &blk)
397
397
  return @data.fetch(method_name) if @data.key?(method_name)
398
+
398
399
  super
399
400
  end
400
401
  end
@@ -25,9 +25,7 @@ module BazaModels::Model::BelongsToRelations
25
25
  @relationships[relation_name] = relation
26
26
 
27
27
  define_method(relation_name) do
28
- if (model = @changes[relation_name])
29
- model
30
- elsif (model = autoloads[relation_name])
28
+ if (model = @changes[relation_name]) || (model = autoloads[relation_name])
31
29
  model
32
30
  else
33
31
  if relation[:class_name]
@@ -4,9 +4,9 @@ module BazaModels::Model::HasManyRelations
4
4
  end
5
5
 
6
6
  module ClassMethods
7
- # rubocop:disable Style/PredicateName
7
+ # rubocop:disable Naming/PredicateName
8
8
  def has_many(relation_name, *all_args)
9
- # rubocop:enable Style/PredicateName
9
+ # rubocop:enable Naming/PredicateName
10
10
 
11
11
  args = all_args.pop
12
12
 
@@ -4,9 +4,9 @@ module BazaModels::Model::HasOneRelations
4
4
  end
5
5
 
6
6
  module ClassMethods
7
- # rubocop:disable Style/PredicateName
7
+ # rubocop:disable Naming/PredicateName
8
8
  def has_one(relation_name, *all_args)
9
- # rubocop:enable Style/PredicateName
9
+ # rubocop:enable Naming/PredicateName
10
10
 
11
11
  args = all_args.pop
12
12
 
@@ -30,9 +30,9 @@ module BazaModels::Model::Manipulation
30
30
  fire_callbacks(:after_save)
31
31
  fire_callbacks(:after_create) if new_record
32
32
 
33
- return true
33
+ true
34
34
  else
35
- return false
35
+ false
36
36
  end
37
37
  end
38
38
 
@@ -47,14 +47,12 @@ module BazaModels::Model::Manipulation
47
47
 
48
48
  @data[:id] = db.insert(table_name, @changes, return_id: true)
49
49
 
50
- if @autoloads
51
- @autoloads.each do |relation_name, collection|
52
- relation = self.class.relationships.fetch(relation_name)
50
+ @autoloads&.each do |relation_name, collection|
51
+ relation = self.class.relationships.fetch(relation_name)
53
52
 
54
- collection.each do |model|
55
- model.assign_attributes(relation.fetch(:foreign_key) => id)
56
- model.create! if model.new_record?
57
- end
53
+ collection.each do |model|
54
+ model.assign_attributes(relation.fetch(:foreign_key) => id)
55
+ model.create! if model.new_record?
58
56
  end
59
57
  end
60
58
 
@@ -63,7 +61,7 @@ module BazaModels::Model::Manipulation
63
61
 
64
62
  def create!
65
63
  if create
66
- return true
64
+ true
67
65
  else
68
66
  raise BazaModels::Errors::InvalidRecord, errors.full_messages.join(". ")
69
67
  end
@@ -71,25 +69,29 @@ module BazaModels::Model::Manipulation
71
69
 
72
70
  def save!(args = {})
73
71
  if save(args)
74
- return true
72
+ true
75
73
  else
76
74
  raise BazaModels::Errors::InvalidRecord, errors.full_messages.join(". ")
77
75
  end
78
76
  end
79
77
 
80
- def update_attributes(attributes)
78
+ def update(attributes)
81
79
  assign_attributes(attributes)
82
80
  save
83
81
  end
84
82
 
85
- def update_attributes!(attributes)
86
- update!(attributes)
87
- end
88
-
89
83
  def update!(attributes)
90
84
  raise BazaModels::Errors::InvalidRecord, @errors.full_messages.join(". ") unless update_attributes(attributes)
91
85
  end
92
86
 
87
+ def update_attributes(attributes)
88
+ update(attributes)
89
+ end
90
+
91
+ def update_attributes!(attributes)
92
+ update!(attributes)
93
+ end
94
+
93
95
  def assign_attributes(attributes)
94
96
  @changes.merge!(real_attributes(attributes))
95
97
  end
@@ -97,7 +99,7 @@ module BazaModels::Model::Manipulation
97
99
  def destroy
98
100
  if new_record?
99
101
  errors.add(:base, "cannot destroy new record")
100
- return false
102
+ false
101
103
  else
102
104
  fire_callbacks(:before_destroy)
103
105
 
@@ -108,7 +110,7 @@ module BazaModels::Model::Manipulation
108
110
 
109
111
  db.delete(table_name, id: id)
110
112
  fire_callbacks(:after_destroy)
111
- return true
113
+ true
112
114
  end
113
115
  end
114
116
 
@@ -7,6 +7,7 @@ module BazaModels::Model::Queries
7
7
  def find(id)
8
8
  row = db.select(table_name, {id: id}, limit: 1).fetch
9
9
  raise BazaModels::Errors::RecordNotFound, "Record not found by ID: #{id}" unless row
10
+
10
11
  new(row, init: true)
11
12
  end
12
13
 
@@ -23,6 +24,7 @@ module BazaModels::Model::Queries
23
24
  def find_or_initialize_by(data)
24
25
  model = find_by(data)
25
26
  return model if model
27
+
26
28
  new(data)
27
29
  end
28
30
 
@@ -9,6 +9,7 @@ module BazaModels::Model::Scopes
9
9
  name = name.to_sym
10
10
 
11
11
  raise "Such a scope already exists" if @scopes.key?(name)
12
+
12
13
  @scopes[name] = {blk: blk}
13
14
 
14
15
  (class << self; self; end).__send__(:define_method, name) do
@@ -22,6 +22,7 @@ module BazaModels::Model::Validations
22
22
 
23
23
  validators[attribute_name].each do |validator|
24
24
  next unless validator.fire?(self)
25
+
25
26
  validator.validate(self, attribute_value)
26
27
  end
27
28
  end
@@ -69,7 +69,8 @@ class BazaModels::Query
69
69
  end
70
70
 
71
71
  def count
72
- if @previous_model && @previous_model.new_record?
72
+ if
73
+ @previous_model&.new_record?
73
74
  autoloaded_cache_or_create.length
74
75
  else
75
76
  query = clone(selects: [])
@@ -283,7 +284,7 @@ class BazaModels::Query
283
284
  end
284
285
 
285
286
  if @includes.empty?
286
- return array_enum
287
+ array_enum
287
288
  else
288
289
  array = array_enum.to_a
289
290
 
@@ -296,14 +297,12 @@ class BazaModels::Query
296
297
  autoloader.autoload
297
298
  end
298
299
 
299
- return array
300
+ array
300
301
  end
301
302
  end
302
303
 
303
- def each
304
- to_enum.each do |model|
305
- yield model
306
- end
304
+ def each(&blk)
305
+ to_enum.each(&blk)
307
306
  end
308
307
 
309
308
  def find_each
@@ -376,6 +375,7 @@ class BazaModels::Query
376
375
 
377
376
  def sanitize_sql(value)
378
377
  return value if value.is_a?(Array) || value.is_a?(Integer) || value.is_a?(Integer)
378
+
379
379
  "'#{@db.esc(value)}'"
380
380
  end
381
381
 
@@ -413,9 +413,7 @@ private
413
413
  end
414
414
 
415
415
  def autoloaded_on_previous_model?
416
- if @previous_model && @relation
417
- return true if @previous_model.autoloads.include?(@relation.fetch(:relation_name))
418
- end
416
+ return true if @previous_model && @relation && @previous_model.autoloads.include?(@relation.fetch(:relation_name))
419
417
 
420
418
  false
421
419
  end
@@ -439,11 +437,13 @@ private
439
437
 
440
438
  def key_convert(key, value)
441
439
  return "#{key}_id" if value.is_a?(BazaModels::Model)
440
+
442
441
  key
443
442
  end
444
443
 
445
444
  def value_convert(value)
446
445
  return value.id if value.is_a?(BazaModels::Model)
446
+
447
447
  value
448
448
  end
449
449
 
@@ -464,6 +464,8 @@ private
464
464
 
465
465
  sql << ")"
466
466
  sql
467
+ elsif value == nil
468
+ "IS NULL"
467
469
  else
468
470
  "= #{@db.quote_value(value)}"
469
471
  end