book_reading_tracker_gem 0.1.2 → 0.1.3

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: 3e2475d002caa764860b976bcb0bbfbf73016981e39bcdefbd2df222aaad890a
4
- data.tar.gz: 27a1c29b30648aee262ecf8a4582c188be736273da1c4782394ea6b281a0a437
3
+ metadata.gz: fd081c34660a6d2eca8b027c75ab74ed89efc7a56c30e6728b649e11a8230e18
4
+ data.tar.gz: 2f3af5996bd57c2585b30837edeb3acb519185bfe11e183dfa304ffc96690d6a
5
5
  SHA512:
6
- metadata.gz: 283b60ead6bda60d5001e279a6a7fc04aaf1a208496a99881d08f11bf1a0df213ac4656b8256341c9dc188b6d18da82d8c578c9aa0ceddcdf0b011ceab18b11f
7
- data.tar.gz: 85851bda7f9f4ecf896c15408c9f5fdd5906b16c1b292915b0cefee2b6459dd94899408287b7d4808a9a69975e22ccd919a053ecc965161de87e8bdebe660bb1
6
+ metadata.gz: 6f6e60e8236d2c76e1c7a6698f755779edd51b1345655d8e70af67204415edcdbaa9172a8ac5a23fce4e337e278c378a44388f7eb99009f0589fdbfb10cacd1f
7
+ data.tar.gz: 8b140358b0d48df22bf4f87f7cc43e8d037fe8009c6ac188a2d3346c450f028c1cef08fdc824441d16e999231c84e9d259e6f89240bc3442850cc9a2030b48f4
@@ -27,7 +27,7 @@ class DatabaseConnection
27
27
 
28
28
  begin
29
29
  ActiveRecord::Base.establish_connection(database_url)
30
- ActiveRecord::Base.logger = Logger.new($stdout)
30
+ # ActiveRecord::Base.logger = Logger.new($stdout)
31
31
  puts "Successfully connected to the database in #{database_mode} mode!"
32
32
  rescue StandardError => e
33
33
  puts "Failed to connect to the database: #{e.message}"
@@ -26,8 +26,12 @@ module BookReadingTrackerGem
26
26
  end
27
27
 
28
28
  def migration_context
29
+ # bắt đầu từ thư mục chứa file database_tasks.rb, rồi đi từ đó tới ../db/migrate.
29
30
  migrations_paths = File.expand_path('../db/migrate', __dir__)
30
- @migration_context ||= ActiveRecord::MigrationContext.new(migrations_paths)
31
+
32
+ @migration_context = ActiveRecord::MigrationContext.new(migrations_paths) if @migration_context.nil?
33
+ # puts migration_context.migrations.map(&:name)
34
+ @migration_context
31
35
  end
32
36
 
33
37
  def create
@@ -90,16 +94,6 @@ module BookReadingTrackerGem
90
94
  puts "Lỗi khi migrate: #{e.message}"
91
95
  end
92
96
 
93
- def reset
94
- drop
95
- create
96
- migrate
97
- seed
98
- puts 'Reset database thành công.'
99
- rescue StandardError => e
100
- puts "Lỗi khi reset database: #{e.message}"
101
- end
102
-
103
97
  def seed
104
98
  connect
105
99
 
@@ -118,5 +112,15 @@ module BookReadingTrackerGem
118
112
  rescue StandardError => e
119
113
  puts "Lỗi khi seed dữ liệu: #{e.message}"
120
114
  end
115
+
116
+ def reset
117
+ drop
118
+ create
119
+ migrate
120
+ seed
121
+ puts 'Reset database thành công.'
122
+ rescue StandardError => e
123
+ puts "Lỗi khi reset database: #{e.message}"
124
+ end
121
125
  end
122
126
  end
@@ -7,16 +7,15 @@ require_relative '../services/category_service'
7
7
 
8
8
  module BookReadingTrackerGem
9
9
  class CLI < Thor
10
- # Quản sách
11
- desc 'add_book TITLE --author AUTHOR --pages PAGES [--description DESCRIPTION] [--isbn ISBN] [--published_year YEAR]',
10
+ desc 'add_book TITLE --author AUTHOR1 AUTHOR2 ... --pages PAGES [--description DESCRIPTION] [--isbn ISBN] [--published_year YEAR]',
12
11
  'Thêm sách mới vào danh sách'
13
- option :author, required: true
12
+ option :author, required: true, type: :array
14
13
  option :pages, required: true, type: :numeric
15
14
  option :description, required: false
16
15
  option :isbn, required: false
17
16
  option :published_year, required: false, type: :numeric
17
+
18
18
  def add_book(title)
19
- # ruby bin/book add_book "Ruby Programming" --author "David Heinemeier Hansson" --pages 300 --description "Learn Ruby with Rails" --isbn "978-0134596882" --published_year 2023
20
19
  BookService.add_book(
21
20
  title,
22
21
  options[:author],
@@ -2,6 +2,6 @@
2
2
 
3
3
  require 'active_record'
4
4
  class Author < ActiveRecord::Base
5
- has_many :book_authors67
5
+ has_many :book_authors
6
6
  has_many :books, through: :book_authors
7
7
  end
@@ -6,17 +6,24 @@ require_relative '../../../config/database_connection'
6
6
  require_relative '../utils/helpers'
7
7
  module BookReadingTrackerGem
8
8
  class BookService
9
- def self.add_book(title, author, total_pages, description = nil, isbn = nil, published_year = nil)
9
+ def self.add_book(title, authors, total_pages, description = nil, isbn = nil, published_year = nil)
10
10
  DatabaseConnection.connect
11
+
11
12
  book = Book.create!(
12
13
  title: title,
13
14
  description: description,
14
15
  isbn: isbn,
15
16
  published_year: published_year
16
17
  )
17
- book.authors.create!(author_name: author)
18
+
19
+ authors.each do |author_name|
20
+ author = Author.find_or_create_by!(author_name: author_name)
21
+ book.authors << author unless book.authors.include?(author)
22
+ end
23
+
18
24
  book.create_reading_progress!(total_pages: total_pages)
19
- puts "Add book: #{book.title} successfully"
25
+
26
+ puts "Add book: #{book.title} successfully with #{authors.size} author(s)."
20
27
  rescue StandardError => e
21
28
  puts "Error adding book: #{e.message}"
22
29
  ensure
@@ -8,7 +8,7 @@ module CommonUtils
8
8
  puts 'No data available.'
9
9
  else
10
10
  table = TTY::Table.new(header, rows)
11
- puts table.render(:ascii, resize: false)
11
+ puts table.render(:unicode, resize: false, alignments: %i[center])
12
12
  end
13
13
  end
14
14
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module BookReadingTrackerGem
4
- VERSION = '0.1.2'
4
+ VERSION = '0.1.3'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: book_reading_tracker_gem
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Doan Vo Van Trong