baza_models 0.0.11 → 0.0.15

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 (44) hide show
  1. checksums.yaml +5 -5
  2. data/.github/dependabot.yml +13 -0
  3. data/.rubocop.yml +134 -25
  4. data/.rubocop_todo.yml +47 -32
  5. data/.ruby-version +1 -0
  6. data/Gemfile +15 -13
  7. data/Gemfile.lock +126 -105
  8. data/Rakefile +5 -7
  9. data/VERSION +1 -1
  10. data/baza_models.gemspec +43 -55
  11. data/lib/baza_models/autoloader.rb +1 -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/belongs_to_relations.rb +1 -3
  15. data/lib/baza_models/model/has_many_relations.rb +2 -2
  16. data/lib/baza_models/model/has_one_relations.rb +2 -2
  17. data/lib/baza_models/model/manipulation.rb +20 -18
  18. data/lib/baza_models/model/queries.rb +2 -0
  19. data/lib/baza_models/model/scopes.rb +1 -0
  20. data/lib/baza_models/model/validations.rb +1 -0
  21. data/lib/baza_models/model.rb +24 -27
  22. data/lib/baza_models/query/inspector.rb +2 -0
  23. data/lib/baza_models/query/pagination.rb +5 -7
  24. data/lib/baza_models/query/sql_generator.rb +3 -3
  25. data/lib/baza_models/query.rb +16 -14
  26. data/lib/baza_models/ransacker/relationship_scanner.rb +4 -1
  27. data/lib/baza_models/ransacker.rb +3 -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/model/has_many_relations_spec.rb +1 -1
  34. data/spec/baza_models/model/manipulation_spec.rb +3 -3
  35. data/spec/baza_models/model_spec.rb +31 -4
  36. data/spec/baza_models/query_spec.rb +14 -6
  37. data/spec/factories/organization.rb +2 -2
  38. data/spec/factories/user.rb +2 -2
  39. data/spec/spec_helper.rb +3 -3
  40. data/spec/support/database_helper.rb +6 -3
  41. data/spec/test_classes/user.rb +2 -1
  42. metadata +70 -42
  43. data/shippable.yml +0 -11
  44. data/spec/baza_models_spec.rb +0 -4
@@ -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
 
@@ -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
@@ -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
@@ -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?
data/peak_flow.yml ADDED
@@ -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
@@ -45,7 +45,7 @@ describe BazaModels::Model::HasManyRelations do
45
45
  end
46
46
  end
47
47
 
48
- context "#<<" do
48
+ describe "#<<" do
49
49
  it "adds models to persisted parent" do
50
50
  organization.save!
51
51
  organization.users << User.create!(email: "test@example.com")
@@ -10,17 +10,17 @@ describe BazaModels::Model::Manipulation do
10
10
  it "#created_at" do
11
11
  expect(user.created_at).to eq nil
12
12
  user.save!
13
- expect(user.created_at).to_not eq nil
13
+ expect(user.created_at).not_to eq nil
14
14
  end
15
15
 
16
16
  it "#updated_at" do
17
17
  expect(user.updated_at).to eq nil
18
18
  user.save!
19
- expect(user.updated_at).to_not eq nil
19
+ expect(user.updated_at).not_to eq nil
20
20
  old_updated_at = user.updated_at
21
21
  sleep 1
22
22
  user.email = "test2@example.com"
23
23
  user.save!
24
- expect(user.updated_at).to_not eq old_updated_at
24
+ expect(user.updated_at).not_to eq old_updated_at
25
25
  end
26
26
  end
@@ -22,8 +22,8 @@ describe "BazaModels::Model" do
22
22
  expect(user.id).to eq nil
23
23
  expect(user.to_param).to eq nil
24
24
  user.save!
25
- expect(user.id).to_not eq nil
26
- expect(user.to_param).to_not eq nil
25
+ expect(user.id).not_to eq nil
26
+ expect(user.to_param).not_to eq nil
27
27
  expect(user.to_param).to eq user.id.to_s
28
28
  end
29
29
 
@@ -62,12 +62,30 @@ describe "BazaModels::Model" do
62
62
  end
63
63
  end
64
64
 
65
+ it "#update" do
66
+ user.save!
67
+ expect(user.update(email: "newemail@example.com")).to eq true
68
+ expect(user.email).to eq "newemail@example.com"
69
+ end
70
+
71
+ it "#update!" do
72
+ user.save!
73
+ user.update!(email: "newemail@example.com")
74
+ expect(user.email).to eq "newemail@example.com"
75
+ end
76
+
65
77
  it "#update_attributes" do
66
78
  user.save!
67
79
  expect(user.update_attributes(email: "newemail@example.com")).to eq true
68
80
  expect(user.email).to eq "newemail@example.com"
69
81
  end
70
82
 
83
+ it "#update_attributes!" do
84
+ user.save!
85
+ user.update_attributes!(email: "newemail@example.com")
86
+ expect(user.email).to eq "newemail@example.com"
87
+ end
88
+
71
89
  it "#before_save, #after_save" do
72
90
  expect(user.before_save_called).to eq nil
73
91
  expect(user.after_save_called).to eq nil
@@ -152,17 +170,19 @@ describe "BazaModels::Model" do
152
170
 
153
171
  it "#each" do
154
172
  user.save!
173
+ found = []
155
174
  count = 0
156
175
  User.each do |user|
157
- expect(user).to eq user
176
+ found << user
158
177
  count += 1
159
178
  end
160
179
 
161
180
  expect(count).to eq 1
181
+ expect(found).to eq [user]
162
182
  end
163
183
 
164
184
  it "#attribute_names" do
165
- expect(User.attribute_names).to eq %w(id organization_id email email_confirmation created_at updated_at admin)
185
+ expect(User.attribute_names).to eq %w[id organization_id email email_confirmation created_at updated_at admin]
166
186
  end
167
187
 
168
188
  it "#columns" do
@@ -211,6 +231,13 @@ describe "BazaModels::Model" do
211
231
  expect(user.email).to eq "test@example.com"
212
232
  end
213
233
 
234
+ describe "#to_a" do
235
+ it "does not respond to it, because it will fuck up Array(ModelClass)" do
236
+ expect { User.to_a }.to raise_error(NoMethodError)
237
+ expect { User.to_ary }.to raise_error(NoMethodError)
238
+ end
239
+ end
240
+
214
241
  describe "#attribute_before_last_save" do
215
242
  it "returns the value as it was before the save" do
216
243
  user.save!
@@ -7,7 +7,7 @@ describe BazaModels::Query do
7
7
  let(:role_user) { Role.new(user: user, role: "user") }
8
8
  let(:role_admin) { Role.new(user: user, role: "administrator") }
9
9
 
10
- context "#average" do
10
+ describe "#average" do
11
11
  it "calculates the average" do
12
12
  5.times do |n|
13
13
  User.create! id: n + 1, email: "user#{n}@example.com"
@@ -18,7 +18,7 @@ describe BazaModels::Query do
18
18
  end
19
19
  end
20
20
 
21
- context "#where" do
21
+ describe "#where" do
22
22
  before do
23
23
  user.save!
24
24
  end
@@ -29,6 +29,14 @@ describe BazaModels::Query do
29
29
  expect(query.to_a).to eq [user]
30
30
  end
31
31
 
32
+ it "supports hashes with nils" do
33
+ user.update!(email_confirmation: nil)
34
+
35
+ query = User.where(users: {email_confirmation: nil})
36
+ expect(query.to_sql).to eq "SELECT `users`.* FROM `users` WHERE `users`.`email_confirmation` IS NULL"
37
+ expect(query.to_a).to eq [user]
38
+ end
39
+
32
40
  it "supports strings" do
33
41
  query = User.where("email = 'test@example.com'")
34
42
  expect(query.to_sql).to eq "SELECT `users`.* FROM `users` WHERE (email = 'test@example.com')"
@@ -49,7 +57,7 @@ describe BazaModels::Query do
49
57
  end
50
58
  end
51
59
 
52
- context "#ids" do
60
+ describe "#ids" do
53
61
  it "returns the ids of the models" do
54
62
  5.times do |n|
55
63
  User.create! id: n + 1, email: "user#{n}@example.com"
@@ -60,7 +68,7 @@ describe BazaModels::Query do
60
68
  end
61
69
  end
62
70
 
63
- context "#joins" do
71
+ describe "#joins" do
64
72
  before do
65
73
  user.save!
66
74
  role_admin.save!
@@ -94,7 +102,7 @@ describe BazaModels::Query do
94
102
  end
95
103
  end
96
104
 
97
- context "#group, #order" do
105
+ describe "#group, #order" do
98
106
  before do
99
107
  user
100
108
  role_user
@@ -109,7 +117,7 @@ describe BazaModels::Query do
109
117
  roles = Role.group(:role).order(:role).to_a
110
118
 
111
119
  expect(roles.length).to eq 2
112
- expect(roles.map(&:role)).to eq %w(administrator user)
120
+ expect(roles.map(&:role)).to eq %w[administrator user]
113
121
  end
114
122
  end
115
123
 
@@ -1,5 +1,5 @@
1
- FactoryGirl.define do
1
+ FactoryBot.define do
2
2
  factory :organization do
3
- name "Test organization"
3
+ name { "Test organization" }
4
4
  end
5
5
  end
@@ -1,7 +1,7 @@
1
- FactoryGirl.define do
1
+ FactoryBot.define do
2
2
  factory :user do
3
3
  organization
4
4
 
5
- email "user@example.com"
5
+ email { "user@example.com" }
6
6
  end
7
7
  end
data/spec/spec_helper.rb CHANGED
@@ -3,7 +3,7 @@ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
3
 
4
4
  require "rspec"
5
5
  require "baza_models"
6
- require "factory_girl"
6
+ require "factory_bot"
7
7
 
8
8
  Dir.foreach("spec/test_classes") do |file|
9
9
  require "test_classes/#{file}" if file.end_with?(".rb")
@@ -15,8 +15,8 @@ end
15
15
 
16
16
  # Requires supporting files with custom matchers and macros, etc,
17
17
  # in ./support/ and its subdirectories.
18
- Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
18
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].sort.each { |f| require f }
19
19
 
20
20
  RSpec.configure do |config|
21
- config.include FactoryGirl::Syntax::Methods
21
+ config.include FactoryBot::Syntax::Methods
22
22
  end
@@ -53,7 +53,8 @@ module DatabaseHelper
53
53
  {name: :updated_at, type: :datetime},
54
54
  {name: :admin, type: :tinyint}
55
55
  ],
56
- indexes: [:organization_id, :email])
56
+ indexes: [:organization_id, :email]
57
+ )
57
58
 
58
59
  @db.tables.create(
59
60
  :user_passports,
@@ -64,7 +65,8 @@ module DatabaseHelper
64
65
  ],
65
66
  indexes: [
66
67
  :user_id
67
- ])
68
+ ]
69
+ )
68
70
 
69
71
  @db.tables.create(
70
72
  :persons,
@@ -72,7 +74,8 @@ module DatabaseHelper
72
74
  {name: :id, type: :int, primarykey: true, autoincr: true},
73
75
  {name: :user_id, type: :int}
74
76
  ],
75
- indexes: [:user_id])
77
+ indexes: [:user_id]
78
+ )
76
79
 
77
80
  @db.tables.create(
78
81
  :roles,
@@ -24,11 +24,12 @@ class User < BazaModels::Model
24
24
  # Used to test callbacks.
25
25
  BazaModels::Model::CALLBACK_TYPES.each do |callback_type|
26
26
  attr_reader "#{callback_type}_called"
27
+
27
28
  __send__(callback_type, :add_callback, callback_type)
28
29
  end
29
30
 
30
31
  def self.ransackable_scopes(_auth_object = nil)
31
- %i(created_at_since)
32
+ %i[created_at_since]
32
33
  end
33
34
 
34
35
  private