ru.Bee 2.7.5 → 2.7.7

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bec1d1078b350a96e452be2ffbc136feb1e29fb3d05ec45269332b9eb896a3fb
4
- data.tar.gz: 3dc67677e2d98194c0f66679f2fac5dbb8b2e58a10e7919b65a8bafd2172267b
3
+ metadata.gz: b96c111963e69a5ad1fb05d5b1b4e226122015a51a34ceb69ee93f1fe23a2426
4
+ data.tar.gz: f210ad9397a97e65b36fa924dd1c65ea8bc5b88a5bcf7d4643cb4408c6ce0706
5
5
  SHA512:
6
- metadata.gz: fa8e128042bb5c8dbd246e59ac45c9c1d802f993150ce7b41ca903eaf09da88b36ad32a1a85163e524ce29a54dd7efaaada3ddc6a571005a2c10c939912020e5
7
- data.tar.gz: a09c8a1c7181baef32d3cc5812520b984655797ed5ee40636d4ce8ba6f7898833b40fed3e68579d63e065c8a2f139cdfdb782f0140235d839ec81c2d9a555e36
6
+ metadata.gz: b30676699627fcc0f65bf02462ca12138f4226004d5927e6bbb64ad8caa37fc9f6da1ed7c26f40e577392b164bd696d62f89104fa1223347635f0a4fefb11b3c
7
+ data.tar.gz: 8ef41f6739a0c1771c7fab2e484fa1485e38f9a46e1a7fa8509ec40b95630c243008b92d9fa46299aa434480f62da0d6726fc08d9b08b44f4326fe4cc472c660
data/lib/db/test.db CHANGED
Binary file
data/lib/rubee/cli/db.rb CHANGED
@@ -12,7 +12,7 @@ module Rubee
12
12
  _, file_name = argv[1]&.split(':')
13
13
  file_names = if file_name == 'all'
14
14
  lib = Rubee::PROJECT_NAME == 'rubee' ? '/lib' : ''
15
- Dir.glob(".#{lib}/db/*.rb").map do |file|
15
+ Dir.glob(".#{lib}/db/*.rb").sort.map do |file|
16
16
  File.basename(file, '.rb')
17
17
  end.reject { |file| file == 'structure' }
18
18
  else
@@ -20,7 +20,7 @@ module Rubee
20
20
  end
21
21
  file_names.each do |file|
22
22
  color_puts("Run #{file} file for #{ENV['RACK_ENV']} env", color: :cyan)
23
- Object.const_get(file.split('_').map(&:capitalize).join).new.call
23
+ Object.const_get(file.gsub(/^\d+_/, '').split('_').map(&:capitalize).join).new.call
24
24
  end
25
25
  color_puts("Migration for #{file_name} completed", color: :green)
26
26
  unless Rubee::PROJECT_NAME == 'rubee'
@@ -17,6 +17,10 @@ module Rubee
17
17
  }
18
18
 
19
19
  class << self
20
+ def env
21
+ ENV['RACK_ENV']
22
+ end
23
+
20
24
  def setup(env, app = :app)
21
25
  unless @configuraiton[app.to_sym]
22
26
  @configuraiton[app.to_sym] = {
@@ -29,6 +33,14 @@ module Rubee
29
33
  end
30
34
  end
31
35
 
36
+ envs.each do |envi|
37
+ next if respond_to? "#{envi}?"
38
+
39
+ define_singleton_method "#{envi}?" do
40
+ envi.to_s == ENV['RACK_ENV']
41
+ end
42
+ end
43
+
32
44
  yield(self)
33
45
  end
34
46
 
@@ -129,8 +141,8 @@ module Rubee
129
141
  &.[](method_name.to_s.delete_prefix('get_').to_sym)
130
142
  end
131
143
 
132
- def envs
133
- @configuraiton.keys
144
+ def envs(app_name = :app)
145
+ @configuraiton[app_name.to_sym].keys
134
146
  end
135
147
  end
136
148
  end
@@ -100,9 +100,10 @@ module Rubee
100
100
  end
101
101
 
102
102
  def generate_db_file
103
+ timestamp = Time.now.strftime('%Y%m%d%H%M%S')
103
104
  prefix = @namespace == "" ? "" : "#{@app_name.snakeize}_"
104
105
  table_name = "#{prefix}#{@plural_name}"
105
- db_file = File.join(Rubee::APP_ROOT, Rubee::LIB, "db/create_#{table_name}.rb")
106
+ db_file = File.join(Rubee::APP_ROOT, Rubee::LIB, "db/#{timestamp}_create_#{table_name}.rb")
106
107
  if File.exist?(db_file)
107
108
  puts "DB file for #{table_name} already exists. Remove it if you want to regenerate"
108
109
  return
@@ -208,6 +208,14 @@ module Rubee
208
208
  ::Rubee::AssocArray.new([], self, query_dataset.where(**args))
209
209
  end
210
210
 
211
+ def find_first(args, options = {})
212
+ where(args, options).order(:id).limit(1).last
213
+ end
214
+
215
+ def find_last(args, options = {})
216
+ where(args, options).order(id: :desc).limit(1).last
217
+ end
218
+
211
219
  def order(args, options = {})
212
220
  query_dataset = options[:__query_dataset] || dataset
213
221
 
data/lib/rubee.rb CHANGED
@@ -20,7 +20,7 @@ module Rubee
20
20
  RUBEE_SUPPORT = { "Rubee::Support::Hash" => Hash, "Rubee::Support::String" => String }
21
21
  end
22
22
 
23
- VERSION = '2.7.5'
23
+ VERSION = '2.7.7'
24
24
 
25
25
  require_relative 'rubee/router'
26
26
  require_relative 'rubee/logger'
@@ -119,6 +119,26 @@ describe 'Comment model' do
119
119
  end
120
120
  end
121
121
 
122
+ describe 'find_first' do
123
+ it 'finds first record' do
124
+ Comment.destroy_all
125
+ comment_1 = Comment.create(text: 'test123123')
126
+ comment_2 = Comment.create(text: 'test123123')
127
+ _(Comment.find_first(text: 'test123123').id).must_equal(comment_1.id)
128
+ Comment.destroy_all
129
+ end
130
+ end
131
+
132
+ describe 'find_last' do
133
+ it 'finds last record' do
134
+ Comment.destroy_all
135
+ comment_1 = Comment.create(text: 'test123123')
136
+ comment_2 = Comment.create(text: 'test123123')
137
+ _(Comment.find_last(text: 'test123123').id).must_equal(comment_2.id)
138
+ Comment.destroy_all
139
+ end
140
+ end
141
+
122
142
  describe 'method' do
123
143
  it 'updates existing model' do
124
144
  comment = Comment.new(text: 'test 1')
@@ -4,8 +4,10 @@ describe 'Rubee::Generator' do
4
4
  describe 'generates Sequel schema lines' do
5
5
  after do
6
6
  File.delete('lib/app/models/apple.rb') if File.exist?('lib/app/models/apple.rb')
7
- File.delete('lib/db/create_apples.rb') if File.exist?('lib/db/create_apples.rb')
7
+ # Delete any migration file with timestamp prefix
8
+ Dir.glob('lib/db/*_create_apples.rb').each { |f| File.delete(f) }
8
9
  end
10
+
9
11
  it 'for string with just name' do
10
12
  generator = Rubee::Generator.new(nil, nil, nil, nil)
11
13
 
@@ -30,23 +32,30 @@ describe 'Rubee::Generator' do
30
32
  describe 'generates Sequel file' do
31
33
  after do
32
34
  File.delete('lib/app/models/apple.rb') if File.exist?('lib/app/models/apple.rb')
33
- File.delete('lib/db/create_apples.rb') if File.exist?('lib/db/create_apples.rb')
35
+ # Delete any migration file with timestamp prefix
36
+ Dir.glob('lib/db/*_create_apples.rb').each { |f| File.delete(f) }
34
37
  end
35
38
 
36
39
  it 'not without a model' do
37
40
  generator = Rubee::Generator.new(nil, nil, nil, nil)
38
41
  generator.call
39
42
 
40
- _(File.exist?('lib/db/create_apples.rb')).must_equal(false)
43
+ # Check no migration files exist with any timestamp
44
+ _(Dir.glob('lib/db/*_create_apples.rb').empty?).must_equal(true)
41
45
  end
42
46
 
43
47
  it 'with a model only' do
44
48
  generator = Rubee::Generator.new('apple', nil, 'apples', nil)
45
49
  generator.call
46
50
 
47
- _(File.exist?('lib/db/create_apples.rb')).must_equal(true)
51
+ # Find the migration file with timestamp prefix
52
+ migration_files = Dir.glob('lib/db/*_create_apples.rb')
53
+ _(migration_files.size).must_equal(1)
54
+
55
+ migration_file = migration_files.first
56
+ _(File.exist?(migration_file)).must_equal(true)
48
57
 
49
- lines = File.readlines('lib/db/create_apples.rb').map(&:chomp).join("\n")
58
+ lines = File.readlines(migration_file).map(&:chomp).join("\n")
50
59
 
51
60
  _(lines.include?('class CreateApples')).must_equal(true)
52
61
  _(lines.include?('def call')).must_equal(true)
@@ -61,9 +70,11 @@ describe 'Rubee::Generator' do
61
70
  'apples', nil)
62
71
  generator.call
63
72
 
64
- _(File.exist?('lib/db/create_apples.rb')).must_equal(true)
73
+ migration_files = Dir.glob('lib/db/*_create_apples.rb')
74
+ _(migration_files.size).must_equal(1)
65
75
 
66
- lines = File.readlines('lib/db/create_apples.rb').map(&:chomp).join("\n")
76
+ migration_file = migration_files.first
77
+ lines = File.readlines(migration_file).map(&:chomp).join("\n")
67
78
 
68
79
  _(lines.include?('class CreateApples')).must_equal(true)
69
80
  _(lines.include?('def call')).must_equal(true)
@@ -79,9 +90,9 @@ describe 'Rubee::Generator' do
79
90
  [{ name: 'id', type: :bigint }, { name: 'colour', type: :string }, { name: 'weight', type: :integer }], 'apples', nil)
80
91
  generator.call
81
92
 
82
- _(File.exist?('lib/db/create_apples.rb')).must_equal(true)
83
-
84
- lines = File.readlines('lib/db/create_apples.rb').map(&:chomp).join("\n")
93
+ migration_files = Dir.glob('lib/db/*_create_apples.rb')
94
+ migration_file = migration_files.first
95
+ lines = File.readlines(migration_file).map(&:chomp).join("\n")
85
96
 
86
97
  _(lines.include?('class CreateApples')).must_equal(true)
87
98
  _(lines.include?('def call')).must_equal(true)
@@ -98,9 +109,9 @@ describe 'Rubee::Generator' do
98
109
  [{ name: ['blue_id', 'shoe_id'], type: :foreign_key, table: 'blue_and_shoe_join_tb' }], 'apples', nil)
99
110
  generator.call
100
111
 
101
- _(File.exist?('lib/db/create_apples.rb')).must_equal(true)
102
-
103
- lines = File.readlines('lib/db/create_apples.rb').map(&:chomp).join("\n")
112
+ migration_files = Dir.glob('lib/db/*_create_apples.rb')
113
+ migration_file = migration_files.first
114
+ lines = File.readlines(migration_file).map(&:chomp).join("\n")
104
115
 
105
116
  _(lines.include?('class CreateApples')).must_equal(true)
106
117
  _(lines.include?('def call')).must_equal(true)
@@ -115,9 +126,9 @@ describe 'Rubee::Generator' do
115
126
  generator = Rubee::Generator.new('apple', [{ name: 'blue_id', type: :foreign_key }], 'apples', nil)
116
127
  generator.call
117
128
 
118
- _(File.exist?('lib/db/create_apples.rb')).must_equal(true)
119
-
120
- lines = File.readlines('lib/db/create_apples.rb').map(&:chomp).join("\n")
129
+ migration_files = Dir.glob('lib/db/*_create_apples.rb')
130
+ migration_file = migration_files.first
131
+ lines = File.readlines(migration_file).map(&:chomp).join("\n")
121
132
 
122
133
  _(lines.include?('class CreateApples')).must_equal(true)
123
134
  _(lines.include?('def call')).must_equal(true)
@@ -126,12 +137,24 @@ describe 'Rubee::Generator' do
126
137
  _(lines.include?('foreign_key :blue_id')).must_equal(true)
127
138
  _(lines.include?('end')).must_equal(true)
128
139
  end
140
+
141
+ it 'generates migration file with timestamp prefix' do
142
+ generator = Rubee::Generator.new('apple', nil, 'apples', nil)
143
+ generator.call
144
+
145
+ migration_files = Dir.glob('lib/db/*_create_apples.rb')
146
+ _(migration_files.size).must_equal(1)
147
+
148
+ # Verify timestamp prefix format (YYYYMMDDHHMMSS_create_apples.rb)
149
+ filename = File.basename(migration_files.first)
150
+ _(filename).must_match(/^\d{14}_create_apples\.rb$/)
151
+ end
129
152
  end
130
153
 
131
154
  describe 'generates Model file' do
132
155
  after do
133
156
  File.delete('lib/app/models/apple.rb') if File.exist?('lib/app/models/apple.rb')
134
- File.delete('lib/db/create_apples.rb') if File.exist?('lib/db/create_apples.rb')
157
+ Dir.glob('lib/db/*_create_apples.rb').each { |f| File.delete(f) }
135
158
  end
136
159
 
137
160
  it 'not without a model' do
data/readme.md CHANGED
@@ -221,6 +221,12 @@ This will generate the following files:
221
221
  ```bash
222
222
  rubee db run:all
223
223
  ```
224
+ For supporting ordering in migrations, while you create files manually,
225
+ you would benefit by prefixing the migration file with a number.
226
+ So that will ensure that the migration is run in the proper order and won't be interupted
227
+ with related errors.
228
+ In case you create migration file over `rubee generate /{get} {path}` command, the timestamp
229
+ prefix will be added automatically.
224
230
 
225
231
  4. Fill the generated files with the logic you need and run the server again.
226
232
 
@@ -374,7 +380,16 @@ Get all records scoped by a field
374
380
  irb(main):005> User.where(email: "ok23@ok.com")
375
381
  => [#<User:0x000000010cfaa5c0 @email="ok23@ok.com", @id=2, @password="123">]
376
382
  ```
377
-
383
+ Get the first record. It is a shortcut for `User.where(email: "ok23@ok.com").order(:id).limit(1).last`
384
+ ```ruby
385
+ irb(main):006> User.find_first(email: "ok23@ok.com")
386
+ => #<User:0x000000010cfaa5c0 @email="ok23@ok.com", @id=2, @password="123">
387
+ ```
388
+ Get the last record. It is a shortcut for `User.where(email: "ok23@ok.com").order(id: :desc).limit(1).last`
389
+ ```ruby
390
+ irb(main):007> User.find_last(email: "ok23@ok.com")
391
+ => #<User:0x000000010cfaa5c0 @email="ok23@ok.com", @id=2, @password="123">
392
+ ```
378
393
  Get all records
379
394
  ```ruby
380
395
  irb(main):001> User.all
@@ -719,6 +734,9 @@ Generates:
719
734
  ```bash
720
735
  rubee db run:create_cabbages
721
736
  ```
737
+ If you migration file prefixed with timestamp or mannually added number, you don't need to include It
738
+ in the command `rubee db run:202603101300_create_cabbages`, just `rubee db run:create_cabbages` will be
739
+ sufficient enough
722
740
 
723
741
  5. Fill the controller with content
724
742
  ```ruby
@@ -1039,6 +1057,15 @@ Available extensions:
1039
1057
  "test".plural? # => false
1040
1058
  ```
1041
1059
 
1060
+ In the code base, out of the box, you can check current environtment with:
1061
+ ```ruby
1062
+ Rubee::Configuration.env
1063
+ # => :development
1064
+
1065
+ Rubee::Configuration.development?
1066
+ # => true
1067
+ ```
1068
+
1042
1069
  [Back to content](#content)
1043
1070
 
1044
1071
  ## JWT based authentication
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ru.Bee
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.7.5
4
+ version: 2.7.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Oleg Saltykov