diff --git a/.gitignore b/.gitignore index 4c2454b..4fa5c84 100644 --- a/.gitignore +++ b/.gitignore @@ -11,6 +11,7 @@ /spec/dummy/db/*.sqlite3* /spec/dummy/db/schema.rb /spec/dummy/db/api_logger_schema.rb +/spec/tmp/* .env # rspec failure tracking diff --git a/Appraisals b/Appraisals index ec138ea..f4fe915 100644 --- a/Appraisals +++ b/Appraisals @@ -3,6 +3,7 @@ appraise "rails-6.1" do gem "rails", "~> 6.1.0" gem "rspec-rails", "~> 6.1.0" gem "sqlite3", "~> 1.7.3" + gem "ammeter", "~> 1.1" end appraise "rails-7.0" do @@ -10,22 +11,26 @@ appraise "rails-7.0" do gem "rails", "~> 7.0.0" gem "rspec-rails", "~> 7.1.0" gem "sqlite3", "~> 1.7.3" + gem "ammeter", "~> 1.1" end appraise "rails-7.1" do gem "rails", "~> 7.1.0" gem "rspec-rails", "~> 7.1.0" gem "sqlite3", "~> 1.7.3" + gem "ammeter", "~> 1.1" end appraise "rails-7.2" do gem "rails", "~> 7.2.0" gem "rspec-rails", "~> 7.1.0" gem "sqlite3", "~> 1.7.3" + gem "ammeter", "~> 1.1" end appraise "rails-8.0" do gem "rails", "~> 8.0.0" gem "rspec-rails", "~> 7.1.0" gem "sqlite3", "~> 2.1.0" + gem "ammeter", "~> 1.1" end diff --git a/gemfiles/rails_6.1.gemfile b/gemfiles/rails_6.1.gemfile index 70ca4f8..be643ed 100644 --- a/gemfiles/rails_6.1.gemfile +++ b/gemfiles/rails_6.1.gemfile @@ -6,5 +6,6 @@ gem "concurrent-ruby", "1.3.4" gem "rails", "~> 6.1.0" gem "rspec-rails", "~> 6.1.0" gem "sqlite3", "~> 1.7.3" +gem "ammeter", "~> 1.1" gemspec path: "../" diff --git a/gemfiles/rails_6.1.gemfile.lock b/gemfiles/rails_6.1.gemfile.lock index ed25ad0..8d35c1f 100644 --- a/gemfiles/rails_6.1.gemfile.lock +++ b/gemfiles/rails_6.1.gemfile.lock @@ -70,6 +70,10 @@ GEM minitest (>= 5.1) tzinfo (~> 2.0) zeitwerk (~> 2.3) + ammeter (1.1.7) + activesupport (>= 3.0) + railties (>= 3.0) + rspec-rails (>= 2.2) appraisal (2.5.0) bundler rake @@ -224,6 +228,7 @@ PLATFORMS arm64-darwin DEPENDENCIES + ammeter (~> 1.1) appraisal (~> 2.5) concurrent-ruby (= 1.3.4) mysql2 (~> 0.5.6) diff --git a/gemfiles/rails_7.0.gemfile b/gemfiles/rails_7.0.gemfile index e1abb0b..8a44e08 100644 --- a/gemfiles/rails_7.0.gemfile +++ b/gemfiles/rails_7.0.gemfile @@ -6,5 +6,6 @@ gem "concurrent-ruby", "1.3.4" gem "rails", "~> 7.0.0" gem "rspec-rails", "~> 7.1.0" gem "sqlite3", "~> 1.7.3" +gem "ammeter", "~> 1.1" gemspec path: "../" diff --git a/gemfiles/rails_7.0.gemfile.lock b/gemfiles/rails_7.0.gemfile.lock index ca5c119..2de3e4e 100644 --- a/gemfiles/rails_7.0.gemfile.lock +++ b/gemfiles/rails_7.0.gemfile.lock @@ -1,7 +1,7 @@ PATH remote: .. specs: - rails_api_logger (0.10.0) + rails_api_logger (0.10.1) activejob (>= 6.0) activerecord (>= 6.0) nokogiri @@ -76,6 +76,10 @@ GEM i18n (>= 1.6, < 2) minitest (>= 5.1) tzinfo (~> 2.0) + ammeter (1.1.7) + activesupport (>= 3.0) + railties (>= 3.0) + rspec-rails (>= 2.2) appraisal (2.5.0) bundler rake @@ -223,6 +227,7 @@ PLATFORMS arm64-darwin DEPENDENCIES + ammeter (~> 1.1) appraisal (~> 2.5) concurrent-ruby (= 1.3.4) mysql2 (~> 0.5.6) diff --git a/gemfiles/rails_7.1.gemfile b/gemfiles/rails_7.1.gemfile index 4a192ed..2c515a3 100644 --- a/gemfiles/rails_7.1.gemfile +++ b/gemfiles/rails_7.1.gemfile @@ -5,5 +5,6 @@ source "https://rubygems.org" gem "rails", "~> 7.1.0" gem "rspec-rails", "~> 7.1.0" gem "sqlite3", "~> 1.7.3" +gem "ammeter", "~> 1.1" gemspec path: "../" diff --git a/gemfiles/rails_7.1.gemfile.lock b/gemfiles/rails_7.1.gemfile.lock index 9ee8826..3b86f2d 100644 --- a/gemfiles/rails_7.1.gemfile.lock +++ b/gemfiles/rails_7.1.gemfile.lock @@ -85,6 +85,10 @@ GEM minitest (>= 5.1) mutex_m tzinfo (~> 2.0) + ammeter (1.1.7) + activesupport (>= 3.0) + railties (>= 3.0) + rspec-rails (>= 2.2) appraisal (2.5.0) bundler rake @@ -257,6 +261,7 @@ PLATFORMS arm64-darwin DEPENDENCIES + ammeter (~> 1.1) appraisal (~> 2.5) mysql2 (~> 0.5.6) pg (~> 1.5.4) diff --git a/gemfiles/rails_7.2.gemfile b/gemfiles/rails_7.2.gemfile index 8e684aa..7b20641 100644 --- a/gemfiles/rails_7.2.gemfile +++ b/gemfiles/rails_7.2.gemfile @@ -5,5 +5,6 @@ source "https://rubygems.org" gem "rails", "~> 7.2.0" gem "rspec-rails", "~> 7.1.0" gem "sqlite3", "~> 1.7.3" +gem "ammeter", "~> 1.1" gemspec path: "../" diff --git a/gemfiles/rails_7.2.gemfile.lock b/gemfiles/rails_7.2.gemfile.lock index 238a8c7..0245cb8 100644 --- a/gemfiles/rails_7.2.gemfile.lock +++ b/gemfiles/rails_7.2.gemfile.lock @@ -1,7 +1,7 @@ PATH remote: .. specs: - rails_api_logger (0.10.0) + rails_api_logger (0.10.1) activejob (>= 6.0) activerecord (>= 6.0) nokogiri @@ -82,6 +82,10 @@ GEM minitest (>= 5.1) securerandom (>= 0.3) tzinfo (~> 2.0, >= 2.0.5) + ammeter (1.1.7) + activesupport (>= 3.0) + railties (>= 3.0) + rspec-rails (>= 2.2) appraisal (2.5.0) bundler rake @@ -257,6 +261,7 @@ PLATFORMS arm64-darwin DEPENDENCIES + ammeter (~> 1.1) appraisal (~> 2.5) mysql2 (~> 0.5.6) pg (~> 1.5.4) diff --git a/gemfiles/rails_8.0.gemfile b/gemfiles/rails_8.0.gemfile index 52a3a6e..808d8d9 100644 --- a/gemfiles/rails_8.0.gemfile +++ b/gemfiles/rails_8.0.gemfile @@ -5,5 +5,6 @@ source "https://rubygems.org" gem "rails", "~> 8.0.0" gem "rspec-rails", "~> 7.1.0" gem "sqlite3", "~> 2.1.0" +gem "ammeter", "~> 1.1" gemspec path: "../" diff --git a/gemfiles/rails_8.0.gemfile.lock b/gemfiles/rails_8.0.gemfile.lock new file mode 100644 index 0000000..c733645 --- /dev/null +++ b/gemfiles/rails_8.0.gemfile.lock @@ -0,0 +1,284 @@ +PATH + remote: .. + specs: + rails_api_logger (0.10.1) + activejob (>= 6.0) + activerecord (>= 6.0) + nokogiri + railties (>= 6.0) + zeitwerk (>= 2.0.0) + +GEM + remote: https://rubygems.org/ + specs: + actioncable (8.0.2) + actionpack (= 8.0.2) + activesupport (= 8.0.2) + nio4r (~> 2.0) + websocket-driver (>= 0.6.1) + zeitwerk (~> 2.6) + actionmailbox (8.0.2) + actionpack (= 8.0.2) + activejob (= 8.0.2) + activerecord (= 8.0.2) + activestorage (= 8.0.2) + activesupport (= 8.0.2) + mail (>= 2.8.0) + actionmailer (8.0.2) + actionpack (= 8.0.2) + actionview (= 8.0.2) + activejob (= 8.0.2) + activesupport (= 8.0.2) + mail (>= 2.8.0) + rails-dom-testing (~> 2.2) + actionpack (8.0.2) + actionview (= 8.0.2) + activesupport (= 8.0.2) + nokogiri (>= 1.8.5) + rack (>= 2.2.4) + rack-session (>= 1.0.1) + rack-test (>= 0.6.3) + rails-dom-testing (~> 2.2) + rails-html-sanitizer (~> 1.6) + useragent (~> 0.16) + actiontext (8.0.2) + actionpack (= 8.0.2) + activerecord (= 8.0.2) + activestorage (= 8.0.2) + activesupport (= 8.0.2) + globalid (>= 0.6.0) + nokogiri (>= 1.8.5) + actionview (8.0.2) + activesupport (= 8.0.2) + builder (~> 3.1) + erubi (~> 1.11) + rails-dom-testing (~> 2.2) + rails-html-sanitizer (~> 1.6) + activejob (8.0.2) + activesupport (= 8.0.2) + globalid (>= 0.3.6) + activemodel (8.0.2) + activesupport (= 8.0.2) + activerecord (8.0.2) + activemodel (= 8.0.2) + activesupport (= 8.0.2) + timeout (>= 0.4.0) + activestorage (8.0.2) + actionpack (= 8.0.2) + activejob (= 8.0.2) + activerecord (= 8.0.2) + activesupport (= 8.0.2) + marcel (~> 1.0) + activesupport (8.0.2) + base64 + benchmark (>= 0.3) + bigdecimal + concurrent-ruby (~> 1.0, >= 1.3.1) + connection_pool (>= 2.2.5) + drb + i18n (>= 1.6, < 2) + logger (>= 1.4.2) + minitest (>= 5.1) + securerandom (>= 0.3) + tzinfo (~> 2.0, >= 2.0.5) + uri (>= 0.13.1) + ammeter (1.1.7) + activesupport (>= 3.0) + railties (>= 3.0) + rspec-rails (>= 2.2) + appraisal (2.5.0) + bundler + rake + thor (>= 0.14.0) + ast (2.4.3) + base64 (0.3.0) + benchmark (0.4.1) + bigdecimal (3.2.2) + builder (3.3.0) + concurrent-ruby (1.3.5) + connection_pool (2.5.3) + crass (1.0.6) + date (3.4.1) + diff-lcs (1.6.2) + drb (2.2.3) + erb (5.0.2) + erubi (1.13.1) + globalid (1.2.1) + activesupport (>= 6.1) + i18n (1.14.7) + concurrent-ruby (~> 1.0) + io-console (0.8.1) + irb (1.15.2) + pp (>= 0.6.0) + rdoc (>= 4.0.0) + reline (>= 0.4.2) + json (2.13.0) + language_server-protocol (3.17.0.5) + lint_roller (1.1.0) + logger (1.7.0) + loofah (2.24.1) + crass (~> 1.0.2) + nokogiri (>= 1.12.0) + mail (2.8.1) + mini_mime (>= 0.1.1) + net-imap + net-pop + net-smtp + marcel (1.0.4) + mini_mime (1.1.5) + minitest (5.25.5) + mysql2 (0.5.6) + net-imap (0.5.9) + date + net-protocol + net-pop (0.1.2) + net-protocol + net-protocol (0.2.2) + timeout + net-smtp (0.5.1) + net-protocol + nio4r (2.7.4) + nokogiri (1.18.8-arm64-darwin) + racc (~> 1.4) + parallel (1.27.0) + parser (3.3.8.0) + ast (~> 2.4.1) + racc + pg (1.5.9) + pp (0.6.2) + prettyprint + prettyprint (0.2.0) + prism (1.4.0) + psych (5.2.6) + date + stringio + racc (1.8.1) + rack (3.1.16) + rack-session (2.1.1) + base64 (>= 0.1.0) + rack (>= 3.0.0) + rack-test (2.2.0) + rack (>= 1.3) + rackup (2.2.1) + rack (>= 3) + rails (8.0.2) + actioncable (= 8.0.2) + actionmailbox (= 8.0.2) + actionmailer (= 8.0.2) + actionpack (= 8.0.2) + actiontext (= 8.0.2) + actionview (= 8.0.2) + activejob (= 8.0.2) + activemodel (= 8.0.2) + activerecord (= 8.0.2) + activestorage (= 8.0.2) + activesupport (= 8.0.2) + bundler (>= 1.15.0) + railties (= 8.0.2) + rails-dom-testing (2.3.0) + activesupport (>= 5.0.0) + minitest + nokogiri (>= 1.6) + rails-html-sanitizer (1.6.2) + loofah (~> 2.21) + nokogiri (>= 1.15.7, != 1.16.7, != 1.16.6, != 1.16.5, != 1.16.4, != 1.16.3, != 1.16.2, != 1.16.1, != 1.16.0.rc1, != 1.16.0) + railties (8.0.2) + actionpack (= 8.0.2) + activesupport (= 8.0.2) + irb (~> 1.13) + rackup (>= 1.0.0) + rake (>= 12.2) + thor (~> 1.0, >= 1.2.2) + zeitwerk (~> 2.6) + rainbow (3.1.1) + rake (12.3.3) + rdoc (6.14.2) + erb + psych (>= 4.0.0) + regexp_parser (2.10.0) + reline (0.6.1) + io-console (~> 0.5) + rspec-core (3.13.5) + rspec-support (~> 3.13.0) + rspec-expectations (3.13.5) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.13.0) + rspec-mocks (3.13.5) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.13.0) + rspec-rails (7.1.1) + actionpack (>= 7.0) + activesupport (>= 7.0) + railties (>= 7.0) + rspec-core (~> 3.13) + rspec-expectations (~> 3.13) + rspec-mocks (~> 3.13) + rspec-support (~> 3.13) + rspec-support (3.13.4) + rubocop (1.75.8) + json (~> 2.3) + language_server-protocol (~> 3.17.0.2) + lint_roller (~> 1.1.0) + parallel (~> 1.10) + parser (>= 3.3.0.2) + rainbow (>= 2.2.2, < 4.0) + regexp_parser (>= 2.9.3, < 3.0) + rubocop-ast (>= 1.44.0, < 2.0) + ruby-progressbar (~> 1.7) + unicode-display_width (>= 2.4.0, < 4.0) + rubocop-ast (1.46.0) + parser (>= 3.3.7.2) + prism (~> 1.4) + rubocop-performance (1.25.0) + lint_roller (~> 1.1) + rubocop (>= 1.75.0, < 2.0) + rubocop-ast (>= 1.38.0, < 2.0) + ruby-progressbar (1.13.0) + securerandom (0.4.1) + sqlite3 (2.1.1-arm64-darwin) + standard (1.50.0) + language_server-protocol (~> 3.17.0.2) + lint_roller (~> 1.0) + rubocop (~> 1.75.5) + standard-custom (~> 1.0.0) + standard-performance (~> 1.8) + standard-custom (1.0.2) + lint_roller (~> 1.0) + rubocop (~> 1.50) + standard-performance (1.8.0) + lint_roller (~> 1.1) + rubocop-performance (~> 1.25.0) + stringio (3.1.7) + thor (1.3.2) + timeout (0.4.3) + tzinfo (2.0.6) + concurrent-ruby (~> 1.0) + unicode-display_width (3.1.4) + unicode-emoji (~> 4.0, >= 4.0.4) + unicode-emoji (4.0.4) + uri (1.0.3) + useragent (0.16.11) + websocket-driver (0.8.0) + base64 + websocket-extensions (>= 0.1.0) + websocket-extensions (0.1.5) + zeitwerk (2.7.3) + +PLATFORMS + arm64-darwin + +DEPENDENCIES + ammeter (~> 1.1) + appraisal (~> 2.5) + mysql2 (~> 0.5.6) + pg (~> 1.5.4) + rack + rails (~> 8.0.0) + rails_api_logger! + rake (~> 12.0) + rspec-rails (~> 7.1.0) + sqlite3 (~> 2.1.0) + standard (~> 1.31) + +BUNDLED WITH + 2.5.4 diff --git a/lib/generators/templates/create_rails_api_logger_table.rb.tt b/lib/generators/templates/create_rails_api_logger_table.rb.tt index 99580d1..3f904b9 100644 --- a/lib/generators/templates/create_rails_api_logger_table.rb.tt +++ b/lib/generators/templates/create_rails_api_logger_table.rb.tt @@ -1,6 +1,7 @@ class CreateRailsApiLoggerTable < ActiveRecord::Migration[<%= ActiveRecord::Migration.current_version %>] + <% pk_type = Rails.application.config.generators.options[:active_record].dig(:primary_key_type) %> def change - create_table :inbound_request_logs do |t| + create_table :inbound_request_logs<%= ", id: :#{pk_type}" if pk_type %> do |t| t.string :method t.string :path t.text :request_body @@ -8,11 +9,11 @@ class CreateRailsApiLoggerTable < ActiveRecord::Migration[<%= ActiveRecord::Migr t.integer :response_code t.timestamp :started_at t.timestamp :ended_at - t.references :loggable, index: true, polymorphic: true + t.references :loggable, index: true, polymorphic: true<%= ", type: :#{pk_type}" if pk_type %> t.timestamps null: false end - create_table :outbound_request_logs do |t| + create_table :outbound_request_logs<%= ", id: :#{pk_type}" if pk_type %> do |t| t.string :method t.string :path t.text :request_body @@ -20,7 +21,7 @@ class CreateRailsApiLoggerTable < ActiveRecord::Migration[<%= ActiveRecord::Migr t.integer :response_code t.timestamp :started_at t.timestamp :ended_at - t.references :loggable, index: true, polymorphic: true + t.references :loggable, index: true, polymorphic: true<%= ", type: :#{pk_type}" if pk_type %> t.timestamps null: false end end diff --git a/lib/rails_api_logger.rb b/lib/rails_api_logger.rb index 9559453..1c27a9b 100644 --- a/lib/rails_api_logger.rb +++ b/lib/rails_api_logger.rb @@ -2,6 +2,7 @@ require "rails_api_logger/engine" require_relative "../app/models/rails_api_logger/loggable" require_relative "../app/middlewares/rails_api_logger/middleware" +require_relative "../app/controllers/inbound_requests_logger" require "rails" require "nokogiri" diff --git a/spec/generators/install_generator_spec.rb b/spec/generators/install_generator_spec.rb new file mode 100644 index 0000000..e0845a4 --- /dev/null +++ b/spec/generators/install_generator_spec.rb @@ -0,0 +1,84 @@ +# frozen_string_literal: true + +require "rails_helper" +require "ammeter/init" +require "generators/rails_api_logger/install_generator" + +# rubocop:disable Metrics/BlockLength +RSpec.describe RailsApiLogger::Generators::InstallGenerator, type: :generator do + destination File.expand_path("../tmp", __dir__) + + before do + prepare_destination + end + + context "with default primary key (bigint)" do + before do + allow(Rails.application.config.generators).to receive_message_chain(:options, :[], :dig) + .with(:active_record) + .with(:primary_key_type) + .and_return(nil) + end + + it "generates migration without explicit id type" do + run_generator + + migration = migration_file("db/migrate/create_rails_api_logger_table.rb") + + expect(migration).to contain("create_table :inbound_request_logs do") + expect(migration).to contain("create_table :outbound_request_logs do") + expect(migration).to contain("t.references :loggable, index: true, polymorphic: true") + expect(migration).not_to contain("id: :uuid") + expect(migration).not_to contain("type: :uuid") + end + end + + context "with UUID primary key" do + before do + allow(Rails.application.config.generators).to receive_message_chain(:options, :[], :dig) + .with(:active_record) + .with(:primary_key_type) + .and_return(:uuid) + end + + it "generates migration with uuid id type" do + run_generator + + migration = migration_file("db/migrate/create_rails_api_logger_table.rb") + + expect(Pathname.new(migration)).to exist + expect(migration).to contain("create_table :inbound_request_logs, id: :uuid do") + expect(migration).to contain("create_table :outbound_request_logs, id: :uuid do") + expect(migration).to contain("t.references :loggable, index: true, polymorphic: true, type: :uuid") + end + end + + context "with custom primary key type" do + before do + allow(Rails.application.config.generators).to receive_message_chain(:options, :[], :dig) + .with(:active_record) + .with(:primary_key_type) + .and_return(:bigserial) + end + + it "generates migration with custom id type" do + run_generator + + migration = migration_file("db/migrate/create_rails_api_logger_table.rb") + + expect(Pathname.new(migration)).to exist + expect(migration).to contain("create_table :inbound_request_logs, id: :bigserial do") + expect(migration).to contain("create_table :outbound_request_logs, id: :bigserial do") + expect(migration).to contain("t.references :loggable, index: true, polymorphic: true, type: :bigserial") + end + end + + private + + def migration_file(_file) + migration_path = Dir.glob("#{destination_root}/db/migrate/*_create_rails_api_logger_table.rb").first + + file(migration_path) + end +end +# rubocop:enable Metrics/BlockLength