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
@@ -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("=")
@@ -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?
@@ -1,3 +1,4 @@
1
+ rvm: true
1
2
  script:
2
3
  - bundle exec rspec
3
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
@@ -162,7 +180,7 @@ describe "BazaModels::Model" do
162
180
  end
163
181
 
164
182
  it "#attribute_names" do
165
- expect(User.attribute_names).to eq %w(id organization_id email email_confirmation created_at updated_at admin)
183
+ expect(User.attribute_names).to eq %w[id organization_id email email_confirmation created_at updated_at admin]
166
184
  end
167
185
 
168
186
  it "#columns" do
@@ -212,7 +230,7 @@ describe "BazaModels::Model" do
212
230
  end
213
231
 
214
232
  describe "#to_a" do
215
- it "should not respond to it, because it will fuck up Array(ModelClass)" do
233
+ it "does not respond to it, because it will fuck up Array(ModelClass)" do
216
234
  expect { User.to_a }.to raise_error(NoMethodError)
217
235
  expect { User.to_ary }.to raise_error(NoMethodError)
218
236
  end
@@ -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
 
@@ -15,7 +15,7 @@ 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
21
  config.include FactoryBot::Syntax::Methods
@@ -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
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: baza_models
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.13
4
+ version: 0.0.14
5
5
  platform: ruby
6
6
  authors:
7
7
  - kaspernj
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-10-18 00:00:00.000000000 Z
11
+ date: 2021-01-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: array_enumerator
@@ -220,6 +220,34 @@ dependencies:
220
220
  - - ">="
221
221
  - !ruby/object:Gem::Version
222
222
  version: '0'
223
+ - !ruby/object:Gem::Dependency
224
+ name: rubocop-performance
225
+ requirement: !ruby/object:Gem::Requirement
226
+ requirements:
227
+ - - ">="
228
+ - !ruby/object:Gem::Version
229
+ version: '0'
230
+ type: :development
231
+ prerelease: false
232
+ version_requirements: !ruby/object:Gem::Requirement
233
+ requirements:
234
+ - - ">="
235
+ - !ruby/object:Gem::Version
236
+ version: '0'
237
+ - !ruby/object:Gem::Dependency
238
+ name: rubocop-rspec
239
+ requirement: !ruby/object:Gem::Requirement
240
+ requirements:
241
+ - - ">="
242
+ - !ruby/object:Gem::Version
243
+ version: '0'
244
+ type: :development
245
+ prerelease: false
246
+ version_requirements: !ruby/object:Gem::Requirement
247
+ requirements:
248
+ - - ">="
249
+ - !ruby/object:Gem::Version
250
+ version: '0'
223
251
  - !ruby/object:Gem::Dependency
224
252
  name: sqlite3
225
253
  requirement: !ruby/object:Gem::Requirement
@@ -315,7 +343,6 @@ files:
315
343
  - spec/baza_models/validators/format_validator_spec.rb
316
344
  - spec/baza_models/validators/length_validator_spec.rb
317
345
  - spec/baza_models/validators/uniqueness_validator_spec.rb
318
- - spec/baza_models_spec.rb
319
346
  - spec/factories/organization.rb
320
347
  - spec/factories/user.rb
321
348
  - spec/spec_helper.rb
@@ -1,4 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe "BazaModels" do
4
- end