baza_models 0.0.9 → 0.0.14

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 (45) hide show
  1. checksums.yaml +5 -5
  2. data/.rubocop.yml +134 -25
  3. data/.rubocop_todo.yml +47 -32
  4. data/.ruby-version +1 -0
  5. data/Gemfile +17 -19
  6. data/Gemfile.lock +126 -110
  7. data/Rakefile +5 -7
  8. data/VERSION +1 -1
  9. data/baza_models.gemspec +71 -65
  10. data/lib/baza_models/autoloader.rb +1 -0
  11. data/lib/baza_models/class_translation.rb +4 -0
  12. data/lib/baza_models/errors.rb +2 -3
  13. data/lib/baza_models/helpers/ransacker_helper.rb +2 -2
  14. data/lib/baza_models/model.rb +43 -23
  15. data/lib/baza_models/model/belongs_to_relations.rb +1 -3
  16. data/lib/baza_models/model/has_many_relations.rb +2 -2
  17. data/lib/baza_models/model/has_one_relations.rb +2 -2
  18. data/lib/baza_models/model/manipulation.rb +24 -15
  19. data/lib/baza_models/model/queries.rb +2 -0
  20. data/lib/baza_models/model/scopes.rb +1 -0
  21. data/lib/baza_models/model/validations.rb +1 -0
  22. data/lib/baza_models/query.rb +16 -14
  23. data/lib/baza_models/query/inspector.rb +2 -0
  24. data/lib/baza_models/query/pagination.rb +5 -7
  25. data/lib/baza_models/query/sql_generator.rb +3 -3
  26. data/lib/baza_models/ransacker.rb +3 -1
  27. data/lib/baza_models/ransacker/relationship_scanner.rb +4 -1
  28. data/lib/baza_models/validators/confirmation_validator.rb +1 -3
  29. data/lib/baza_models/validators/uniqueness_validator.rb +2 -4
  30. data/peak_flow.yml +4 -0
  31. data/spec/baza_models/autoloader_spec.rb +1 -1
  32. data/spec/baza_models/baza_orm_adapter_spec.rb +1 -1
  33. data/spec/baza_models/class_translation_spec.rb +4 -0
  34. data/spec/baza_models/model/has_many_relations_spec.rb +1 -1
  35. data/spec/baza_models/model/manipulation_spec.rb +3 -3
  36. data/spec/baza_models/model_spec.rb +46 -3
  37. data/spec/baza_models/query_spec.rb +14 -6
  38. data/spec/factories/organization.rb +2 -2
  39. data/spec/factories/user.rb +2 -2
  40. data/spec/spec_helper.rb +3 -6
  41. data/spec/support/database_helper.rb +6 -3
  42. data/spec/test_classes/user.rb +2 -1
  43. metadata +81 -54
  44. data/shippable.yml +0 -11
  45. data/spec/baza_models_spec.rb +0 -4
@@ -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
 
@@ -10,9 +10,11 @@ module BazaModels::Model::Manipulation
10
10
  self.updated_at = Time.now if changed? && has_attribute?(:updated_at)
11
11
 
12
12
  if new_record
13
+ @before_last_save = @data
13
14
  status = create
14
15
  else
15
16
  fire_callbacks(:before_update)
17
+ @before_last_save = @data
16
18
  db.update(table_name, @changes, id: id) if changed?
17
19
  fire_callbacks(:after_update)
18
20
  status = true
@@ -22,14 +24,15 @@ module BazaModels::Model::Manipulation
22
24
 
23
25
  @changes = {}
24
26
  @new_record = false
27
+
25
28
  reload
26
29
 
27
30
  fire_callbacks(:after_save)
28
31
  fire_callbacks(:after_create) if new_record
29
32
 
30
- return true
33
+ true
31
34
  else
32
- return false
35
+ false
33
36
  end
34
37
  end
35
38
 
@@ -44,14 +47,12 @@ module BazaModels::Model::Manipulation
44
47
 
45
48
  @data[:id] = db.insert(table_name, @changes, return_id: true)
46
49
 
47
- if @autoloads
48
- @autoloads.each do |relation_name, collection|
49
- relation = self.class.relationships.fetch(relation_name)
50
+ @autoloads&.each do |relation_name, collection|
51
+ relation = self.class.relationships.fetch(relation_name)
50
52
 
51
- collection.each do |model|
52
- model.assign_attributes(relation.fetch(:foreign_key) => id)
53
- model.create! if model.new_record?
54
- end
53
+ collection.each do |model|
54
+ model.assign_attributes(relation.fetch(:foreign_key) => id)
55
+ model.create! if model.new_record?
55
56
  end
56
57
  end
57
58
 
@@ -60,7 +61,7 @@ module BazaModels::Model::Manipulation
60
61
 
61
62
  def create!
62
63
  if create
63
- return true
64
+ true
64
65
  else
65
66
  raise BazaModels::Errors::InvalidRecord, errors.full_messages.join(". ")
66
67
  end
@@ -68,21 +69,29 @@ module BazaModels::Model::Manipulation
68
69
 
69
70
  def save!(args = {})
70
71
  if save(args)
71
- return true
72
+ true
72
73
  else
73
74
  raise BazaModels::Errors::InvalidRecord, errors.full_messages.join(". ")
74
75
  end
75
76
  end
76
77
 
77
- def update_attributes(attributes)
78
+ def update(attributes)
78
79
  assign_attributes(attributes)
79
80
  save
80
81
  end
81
82
 
82
- def update_attributes!(attributes)
83
+ def update!(attributes)
83
84
  raise BazaModels::Errors::InvalidRecord, @errors.full_messages.join(". ") unless update_attributes(attributes)
84
85
  end
85
86
 
87
+ def update_attributes(attributes)
88
+ update(attributes)
89
+ end
90
+
91
+ def update_attributes!(attributes)
92
+ update!(attributes)
93
+ end
94
+
86
95
  def assign_attributes(attributes)
87
96
  @changes.merge!(real_attributes(attributes))
88
97
  end
@@ -90,7 +99,7 @@ module BazaModels::Model::Manipulation
90
99
  def destroy
91
100
  if new_record?
92
101
  errors.add(:base, "cannot destroy new record")
93
- return false
102
+ false
94
103
  else
95
104
  fire_callbacks(:before_destroy)
96
105
 
@@ -101,7 +110,7 @@ module BazaModels::Model::Manipulation
101
110
 
102
111
  db.delete(table_name, id: id)
103
112
  fire_callbacks(:after_destroy)
104
- return true
113
+ true
105
114
  end
106
115
  end
107
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: [])
@@ -193,7 +194,7 @@ class BazaModels::Query
193
194
  new_where = "(#{args.shift})"
194
195
 
195
196
  args.each do |arg|
196
- new_where.sub!("?", @db.sqlval(arg))
197
+ new_where.sub!("?", @db.quote_value(arg))
197
198
  end
198
199
 
199
200
  new_wheres << new_where
@@ -208,7 +209,7 @@ class BazaModels::Query
208
209
  elsif arg.is_a?(TrueClass)
209
210
  arg = "1"
210
211
  else
211
- arg = @db.sqlval(arg)
212
+ arg = @db.quote_value(arg)
212
213
  end
213
214
 
214
215
  str.sub!("?", arg)
@@ -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
 
@@ -459,13 +459,15 @@ private
459
459
  sql << ", " unless first
460
460
  end
461
461
 
462
- sql << @db.sqlval(val_i)
462
+ sql << @db.quote_value(val_i)
463
463
  end
464
464
 
465
465
  sql << ")"
466
466
  sql
467
+ elsif value == nil
468
+ "IS NULL"
467
469
  else
468
- "= #{@db.sqlval(value)}"
470
+ "= #{@db.quote_value(value)}"
469
471
  end
470
472
  end
471
473
 
@@ -36,6 +36,7 @@ private
36
36
 
37
37
  relationship_pair = @model.relationships.detect { |key, _value| key == argument }
38
38
  raise "Could not find a relationship on #{@model.name} by that name: #{argument}" unless relationship_pair
39
+
39
40
  relationship = relationship_pair[1]
40
41
 
41
42
  table_name = relationship.fetch(:table_name)
@@ -60,6 +61,7 @@ private
60
61
 
61
62
  relationship_pair = @model.relationships.detect { |relationship_name, _relationship| relationship_name == key }
62
63
  raise "Could not find a relationship on #{@model.name} by that name: #{value}" unless relationship_pair
64
+
63
65
  relationship = relationship_pair[1]
64
66
 
65
67
  BazaModels::Query::Inspector.new(
@@ -1,10 +1,8 @@
1
1
  module BazaModels::Query::Pagination
2
2
  def current_page
3
- if @page
4
- @page
5
- else
6
- raise "Page has not been set"
7
- end
3
+ raise "Page has not been set" unless @page
4
+
5
+ @page
8
6
  end
9
7
 
10
8
  def out_of_bounds?
@@ -44,11 +42,11 @@ module BazaModels::Query::Pagination
44
42
  end
45
43
 
46
44
  def total_pages
47
- pages_count = (count.to_f / per.to_f)
45
+ pages_count = (count.to_f / per)
48
46
 
49
47
  pages_count = 1 if pages_count.nan? || pages_count == Float::INFINITY
50
48
  pages_count = pages_count.ceil
51
- pages_count = 1 if pages_count == 0
49
+ pages_count = 1 if pages_count.zero?
52
50
  pages_count
53
51
  end
54
52
  end
@@ -9,7 +9,7 @@ class BazaModels::Query::SqlGenerator
9
9
  end
10
10
  end
11
11
 
12
- def to_sql
12
+ def to_sql # rubocop:disable Metrics/CyclomaticComplexity, Metrics/AbcSize, Metrics/PerceivedComplexity
13
13
  sql = "SELECT "
14
14
 
15
15
  if @selects.empty?
@@ -68,9 +68,9 @@ class BazaModels::Query::SqlGenerator
68
68
  end
69
69
 
70
70
  if @reverse_order
71
- if order =~ /\s+desc/i
71
+ if /\s+desc/i.match?(order)
72
72
  order = order.gsub(/\s+desc/i, " ASC")
73
- elsif order =~ /\s+asc/i
73
+ elsif /\s+asc/i.match?(order)
74
74
  order = order.gsub(/\s+asc/i, " DESC")
75
75
  else
76
76
  order = "#{order} DESC"
@@ -37,8 +37,10 @@ private
37
37
  elsif key.to_s == "s"
38
38
  match = value.to_s.match(/\A([A-z_\d]+)\s+(asc|desc)\Z/)
39
39
  raise "Couldn't sort-match: #{value}" unless match
40
+
40
41
  sort_by(column_name: match[1], sort_mode: match[2])
41
- elsif ransackable_scopes && ransackable_scopes.include?(key.to_s)
42
+ elsif
43
+ ransackable_scopes&.include?(key.to_s)
42
44
  @query = @query.__send__(key, value)
43
45
  end
44
46
  end
@@ -29,6 +29,7 @@ private
29
29
 
30
30
  loop do
31
31
  break if @name_parts.empty?
32
+
32
33
  name_part = @name_parts.shift
33
34
  current_name << name_part
34
35
 
@@ -71,6 +72,7 @@ private
71
72
 
72
73
  loop do
73
74
  break if join_parts.empty?
75
+
74
76
  join_part = join_parts.shift
75
77
 
76
78
  if join_parts.length == 1
@@ -96,6 +98,7 @@ private
96
98
  case @mode
97
99
  when :cont
98
100
  return if @value.empty?
101
+
99
102
  add_query_with_symbol("LIKE", "%#{@klass.db.esc(@value)}%")
100
103
  when :eq
101
104
  add_query_with_symbol("=")
@@ -121,6 +124,6 @@ private
121
124
  add_join_parts
122
125
  @ransacker.query = @ransacker
123
126
  .query
124
- .where("#{@table_query}.#{@column_query} #{symbol} #{@klass.db.sqlval(value)}")
127
+ .where("#{@table_query}.#{@column_query} #{symbol} #{@klass.db.quote_value(value)}")
125
128
  end
126
129
  end
@@ -3,9 +3,7 @@ class BazaModels::Validators::ConfirmationValidator < BazaModels::Validators::Ba
3
3
  confirmation_attribute_name = "#{attribute_name}_confirmation"
4
4
  confirmation_value = model.__send__(confirmation_attribute_name)
5
5
 
6
- if value && !confirmation_value
7
- model.errors.add(attribute_name, "hasn't been confirmed")
8
- end
6
+ model.errors.add(attribute_name, "hasn't been confirmed") if value && !confirmation_value
9
7
 
10
8
  model.errors.add(attribute_name, "was not the same as the confirmation") if value && confirmation_value && confirmation_value != value
11
9
  end
@@ -2,10 +2,8 @@ class BazaModels::Validators::UniquenessValidator < BazaModels::Validators::Base
2
2
  def validate(model, value)
3
3
  query_same = model.class.where(attribute_name => value)
4
4
 
5
- if scope
6
- scope.each do |scope_part|
7
- query_same = query_same.where(scope_part => model.__send__(scope_part))
8
- end
5
+ scope&.each do |scope_part|
6
+ query_same = query_same.where(scope_part => model.__send__(scope_part))
9
7
  end
10
8
 
11
9
  model.errors.add(attribute_name, "isn't unique") if query_same.any?
@@ -0,0 +1,4 @@
1
+ rvm: true
2
+ script:
3
+ - bundle exec rspec
4
+ - bundle exec rake best_practice_project:run
@@ -1,6 +1,6 @@
1
1
  require "spec_helper"
2
2
 
3
- describe BazaModels::Autoloader do
3
+ describe BazaModels::Autoloader do # rubocop:disable RSpec/MultipleMemoizedHelpers
4
4
  include DatabaseHelper
5
5
 
6
6
  let!(:organization) { Organization.create! }
@@ -47,6 +47,6 @@ describe BazaModels::Query do
47
47
  end
48
48
 
49
49
  it "#column_names" do
50
- expect(User.to_adapter.column_names).to eq %w(id organization_id email email_confirmation created_at updated_at admin)
50
+ expect(User.to_adapter.column_names).to eq %w[id organization_id email email_confirmation created_at updated_at admin]
51
51
  end
52
52
  end
@@ -7,6 +7,10 @@ describe BazaModels::ClassTranslation do
7
7
  expect(User.model_name.human).to eq "User"
8
8
  end
9
9
 
10
+ it "#name" do
11
+ expect(User.model_name.name).to eq "User"
12
+ end
13
+
10
14
  it "#param_key" do
11
15
  expect(User.model_name.param_key).to eq "user"
12
16
  end