From bbb8502cf6b482e6fe8be8dfc61c811463c86146 Mon Sep 17 00:00:00 2001 From: PedroAugustoRamalhoDuarte Date: Sun, 15 Dec 2024 11:37:30 -0300 Subject: [PATCH 1/8] support multiple rails version in CI --- .github/workflows/ci.yml | 8 ++- Gemfile | 3 + Gemfile.lock | 148 ++++++++++++++++++++------------------- 3 files changed, 86 insertions(+), 73 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index b66cfd5..4176716 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,6 +1,6 @@ name: CI env: - RUBY_VERSION: 3.3.4 + RUBY_VERSION: 3.3.6 on: pull_request: @@ -26,8 +26,14 @@ jobs: - name: Check code run: rubocop rspec: + strategy: + fail-fast: false + matrix: + rails: [ "7.2", "8.0" ] name: RSpec runs-on: ubuntu-latest + env: + RAILS_VERSION: ${{ matrix.rails }} steps: - uses: actions/checkout@v4 - uses: ruby/setup-ruby@v1 diff --git a/Gemfile b/Gemfile index 91dbb3e..2ed3454 100644 --- a/Gemfile +++ b/Gemfile @@ -5,6 +5,9 @@ source "https://rubygems.org" # Specify your gem's dependencies in rest-api-generator.gemspec gemspec +version = ENV["RAILS_VERSION"] || "8.0" +gem "rails", "~> #{version}" + gem "rake", "~> 13.0" gem "rspec", "~> 3.0" diff --git a/Gemfile.lock b/Gemfile.lock index 3b7f1d3..0b58460 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -9,46 +9,45 @@ PATH GEM remote: https://rubygems.org/ specs: - actioncable (7.2.0) - actionpack (= 7.2.0) - activesupport (= 7.2.0) + actioncable (8.0.1) + actionpack (= 8.0.1) + activesupport (= 8.0.1) nio4r (~> 2.0) websocket-driver (>= 0.6.1) zeitwerk (~> 2.6) - actionmailbox (7.2.0) - actionpack (= 7.2.0) - activejob (= 7.2.0) - activerecord (= 7.2.0) - activestorage (= 7.2.0) - activesupport (= 7.2.0) + actionmailbox (8.0.1) + actionpack (= 8.0.1) + activejob (= 8.0.1) + activerecord (= 8.0.1) + activestorage (= 8.0.1) + activesupport (= 8.0.1) mail (>= 2.8.0) - actionmailer (7.2.0) - actionpack (= 7.2.0) - actionview (= 7.2.0) - activejob (= 7.2.0) - activesupport (= 7.2.0) + actionmailer (8.0.1) + actionpack (= 8.0.1) + actionview (= 8.0.1) + activejob (= 8.0.1) + activesupport (= 8.0.1) mail (>= 2.8.0) rails-dom-testing (~> 2.2) - actionpack (7.2.0) - actionview (= 7.2.0) - activesupport (= 7.2.0) + actionpack (8.0.1) + actionview (= 8.0.1) + activesupport (= 8.0.1) nokogiri (>= 1.8.5) - racc - rack (>= 2.2.4, < 3.2) + 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 (7.2.0) - actionpack (= 7.2.0) - activerecord (= 7.2.0) - activestorage (= 7.2.0) - activesupport (= 7.2.0) + actiontext (8.0.1) + actionpack (= 8.0.1) + activerecord (= 8.0.1) + activestorage (= 8.0.1) + activesupport (= 8.0.1) globalid (>= 0.6.0) nokogiri (>= 1.8.5) - actionview (7.2.0) - activesupport (= 7.2.0) + actionview (8.0.1) + activesupport (= 8.0.1) builder (~> 3.1) erubi (~> 1.11) rails-dom-testing (~> 2.2) @@ -58,23 +57,24 @@ GEM activemodel (>= 4.1) case_transform (>= 0.2) jsonapi-renderer (>= 0.1.1.beta1, < 0.3) - activejob (7.2.0) - activesupport (= 7.2.0) + activejob (8.0.1) + activesupport (= 8.0.1) globalid (>= 0.3.6) - activemodel (7.2.0) - activesupport (= 7.2.0) - activerecord (7.2.0) - activemodel (= 7.2.0) - activesupport (= 7.2.0) + activemodel (8.0.1) + activesupport (= 8.0.1) + activerecord (8.0.1) + activemodel (= 8.0.1) + activesupport (= 8.0.1) timeout (>= 0.4.0) - activestorage (7.2.0) - actionpack (= 7.2.0) - activejob (= 7.2.0) - activerecord (= 7.2.0) - activesupport (= 7.2.0) + activestorage (8.0.1) + actionpack (= 8.0.1) + activejob (= 8.0.1) + activerecord (= 8.0.1) + activesupport (= 8.0.1) marcel (~> 1.0) - activesupport (7.2.0) + activesupport (8.0.1) base64 + benchmark (>= 0.3) bigdecimal concurrent-ruby (~> 1.0, >= 1.3.1) connection_pool (>= 2.2.5) @@ -84,6 +84,7 @@ GEM minitest (>= 5.1) securerandom (>= 0.3) tzinfo (~> 2.0, >= 2.0.5) + uri (>= 0.13.1) addressable (2.8.7) public_suffix (>= 2.0.2, < 7.0) ammeter (1.1.7) @@ -94,6 +95,7 @@ GEM ruby-next-core (~> 1.0) ast (2.4.2) base64 (0.2.0) + benchmark (0.4.0) bigdecimal (3.1.8) builder (3.3.0) case_transform (0.2) @@ -107,7 +109,7 @@ GEM activerecord (>= 5.a) database_cleaner-core (~> 2.0.0) database_cleaner-core (2.0.1) - date (3.3.4) + date (3.4.1) diff-lcs (1.5.1) drb (2.2.1) erubi (1.13.0) @@ -137,7 +139,7 @@ GEM mini_mime (1.1.5) mini_portile2 (2.8.7) minitest (5.25.0) - net-imap (0.4.14) + net-imap (0.5.1) date net-protocol net-pop (0.1.2) @@ -146,7 +148,7 @@ GEM timeout net-smtp (0.5.0) net-protocol - nio4r (2.7.3) + nio4r (2.7.4) nokogiri (1.16.7) mini_portile2 (~> 2.8.2) racc (~> 1.4) @@ -176,20 +178,20 @@ GEM rackup (2.1.0) rack (>= 3) webrick (~> 1.8) - rails (7.2.0) - actioncable (= 7.2.0) - actionmailbox (= 7.2.0) - actionmailer (= 7.2.0) - actionpack (= 7.2.0) - actiontext (= 7.2.0) - actionview (= 7.2.0) - activejob (= 7.2.0) - activemodel (= 7.2.0) - activerecord (= 7.2.0) - activestorage (= 7.2.0) - activesupport (= 7.2.0) + rails (8.0.1) + actioncable (= 8.0.1) + actionmailbox (= 8.0.1) + actionmailer (= 8.0.1) + actionpack (= 8.0.1) + actiontext (= 8.0.1) + actionview (= 8.0.1) + activejob (= 8.0.1) + activemodel (= 8.0.1) + activerecord (= 8.0.1) + activestorage (= 8.0.1) + activesupport (= 8.0.1) bundler (>= 1.15.0) - railties (= 7.2.0) + railties (= 8.0.1) rails-dom-testing (2.2.0) activesupport (>= 5.0.0) minitest @@ -197,9 +199,9 @@ GEM rails-html-sanitizer (1.6.0) loofah (~> 2.21) nokogiri (~> 1.14) - railties (7.2.0) - actionpack (= 7.2.0) - activesupport (= 7.2.0) + railties (8.0.1) + actionpack (= 8.0.1) + activesupport (= 8.0.1) irb (~> 1.13) rackup (>= 1.0.0) rake (>= 12.2) @@ -235,21 +237,21 @@ GEM rspec-mocks (~> 3.12) rspec-support (~> 3.12) rspec-support (3.13.1) - rswag (2.14.0) - rswag-api (= 2.14.0) - rswag-specs (= 2.14.0) - rswag-ui (= 2.14.0) - rswag-api (2.14.0) - activesupport (>= 5.2, < 8.0) - railties (>= 5.2, < 8.0) - rswag-specs (2.14.0) - activesupport (>= 5.2, < 8.0) - json-schema (>= 2.2, < 5.0) - railties (>= 5.2, < 8.0) + rswag (2.16.0) + rswag-api (= 2.16.0) + rswag-specs (= 2.16.0) + rswag-ui (= 2.16.0) + rswag-api (2.16.0) + activesupport (>= 5.2, < 8.1) + railties (>= 5.2, < 8.1) + rswag-specs (2.16.0) + activesupport (>= 5.2, < 8.1) + json-schema (>= 2.2, < 6.0) + railties (>= 5.2, < 8.1) rspec-core (>= 2.14) - rswag-ui (2.14.0) - actionpack (>= 5.2, < 8.0) - railties (>= 5.2, < 8.0) + rswag-ui (2.16.0) + actionpack (>= 5.2, < 8.1) + railties (>= 5.2, < 8.1) rubocop (1.64.1) json (~> 2.3) language_server-protocol (>= 3.17.0) @@ -303,6 +305,7 @@ GEM tzinfo (2.0.6) concurrent-ruby (~> 1.0) unicode-display_width (2.5.0) + uri (1.0.2) useragent (0.16.10) webrick (1.8.1) websocket-driver (0.7.6) @@ -319,6 +322,7 @@ DEPENDENCIES ammeter (~> 1.1.5) database_cleaner panko_serializer + rails (~> 8.0) rake (~> 13.0) rest-api-generator! rspec (~> 3.0) From f93135c1cf9870814ad6e28189c432074bd68db2 Mon Sep 17 00:00:00 2001 From: PedroAugustoRamalhoDuarte Date: Sun, 15 Dec 2024 12:00:42 -0300 Subject: [PATCH 2/8] improves rails 8 support --- Gemfile | 13 +- Gemfile.lock | 140 +++++++++--------- .../rest_api_generator/resource_controller.rb | 7 +- 3 files changed, 86 insertions(+), 74 deletions(-) diff --git a/Gemfile b/Gemfile index 2ed3454..439323a 100644 --- a/Gemfile +++ b/Gemfile @@ -5,8 +5,8 @@ source "https://rubygems.org" # Specify your gem's dependencies in rest-api-generator.gemspec gemspec -version = ENV["RAILS_VERSION"] || "8.0" -gem "rails", "~> #{version}" +rails_version = ENV["RAILS_VERSION"] || "8.0" +gem "rails", "~> #{rails_version}" gem "rake", "~> 13.0" @@ -18,6 +18,13 @@ gem "sqlite3" # Serializers -gem "panko_serializer" +# TODO: waiting for https://github.com/yosiat/panko_serializer/issues/166 +if Gem::Version.new(rails_version) >= Gem::Version.new("8.0") + gem "panko_serializer", github: "yosiat/panko_serializer", branch: "master" +else + gem "panko_serializer" +end + + gem "active_model_serializers", "~> 0.10.0" diff --git a/Gemfile.lock b/Gemfile.lock index 0b58460..bba09d5 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,3 +1,12 @@ +GIT + remote: https://github.com/yosiat/panko_serializer.git + revision: a2dd88be709e3e6f623c4f20ea2af740fdb53907 + branch: master + specs: + panko_serializer (0.8.2) + activesupport + oj (> 3.11.0, < 4.0.0) + PATH remote: . specs: @@ -52,7 +61,7 @@ GEM erubi (~> 1.11) rails-dom-testing (~> 2.2) rails-html-sanitizer (~> 1.6) - active_model_serializers (0.10.14) + active_model_serializers (0.10.15) actionpack (>= 4.1) activemodel (>= 4.1) case_transform (>= 0.2) @@ -103,7 +112,7 @@ GEM concurrent-ruby (1.3.4) connection_pool (2.4.1) crass (1.0.6) - database_cleaner (2.0.2) + database_cleaner (2.1.0) database_cleaner-active_record (>= 2, < 3) database_cleaner-active_record (2.2.0) activerecord (>= 5.a) @@ -115,19 +124,20 @@ GEM erubi (1.13.0) globalid (1.2.1) activesupport (>= 6.1) - i18n (1.14.5) + i18n (1.14.6) concurrent-ruby (~> 1.0) - io-console (0.7.2) - irb (1.14.0) + io-console (0.8.0) + irb (1.14.2) rdoc (>= 4.0.0) reline (>= 0.4.2) - json (2.7.2) - json-schema (4.3.1) - addressable (>= 2.8) + json (2.9.0) + json-schema (5.1.1) + addressable (~> 2.8) + bigdecimal (~> 3.1) jsonapi-renderer (0.2.2) language_server-protocol (3.17.0.3) - logger (1.6.0) - loofah (2.22.0) + logger (1.6.3) + loofah (2.23.1) crass (~> 1.0.2) nokogiri (>= 1.12.0) mail (2.8.1) @@ -137,8 +147,8 @@ GEM net-smtp marcel (1.0.4) mini_mime (1.1.5) - mini_portile2 (2.8.7) - minitest (5.25.0) + mini_portile2 (2.8.8) + minitest (5.25.4) net-imap (0.5.1) date net-protocol @@ -149,35 +159,32 @@ GEM net-smtp (0.5.0) net-protocol nio4r (2.7.4) - nokogiri (1.16.7) + nokogiri (1.17.2) mini_portile2 (~> 2.8.2) racc (~> 1.4) - nokogiri (1.16.7-x86_64-linux) + nokogiri (1.17.2-x86_64-linux) racc (~> 1.4) - oj (3.16.5) + oj (3.16.8) bigdecimal (>= 3.0) ostruct (>= 0.2) - ostruct (0.6.0) - pagy (9.3.1) - panko_serializer (0.8.2) - activesupport - oj (> 3.11.0, < 4.0.0) - parallel (1.26.2) - parser (3.3.4.2) + ostruct (0.6.1) + pagy (9.3.3) + parallel (1.26.3) + parser (3.3.6.0) ast (~> 2.4.1) racc - psych (5.1.2) + psych (5.2.1) + date stringio public_suffix (6.0.1) racc (1.8.1) - rack (3.1.7) + rack (3.1.8) rack-session (2.0.0) rack (>= 3.0.0) rack-test (2.1.0) rack (>= 1.3) - rackup (2.1.0) + rackup (2.2.1) rack (>= 3) - webrick (~> 1.8) rails (8.0.1) actioncable (= 8.0.1) actionmailbox (= 8.0.1) @@ -196,9 +203,9 @@ GEM activesupport (>= 5.0.0) minitest nokogiri (>= 1.6) - rails-html-sanitizer (1.6.0) + rails-html-sanitizer (1.6.2) loofah (~> 2.21) - nokogiri (~> 1.14) + 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.1) actionpack (= 8.0.1) activesupport (= 8.0.1) @@ -209,23 +216,21 @@ GEM zeitwerk (~> 2.6) rainbow (3.1.1) rake (13.2.1) - rdoc (6.7.0) + rdoc (6.9.0) psych (>= 4.0.0) - regexp_parser (2.9.2) - reline (0.5.9) + regexp_parser (2.9.3) + reline (0.5.12) io-console (~> 0.5) - rexml (3.3.5) - strscan rspec (3.13.0) rspec-core (~> 3.13.0) rspec-expectations (~> 3.13.0) rspec-mocks (~> 3.13.0) - rspec-core (3.13.0) + rspec-core (3.13.2) rspec-support (~> 3.13.0) - rspec-expectations (3.13.1) + rspec-expectations (3.13.3) diff-lcs (>= 1.2.0, < 2.0) rspec-support (~> 3.13.0) - rspec-mocks (3.13.1) + rspec-mocks (3.13.2) diff-lcs (>= 1.2.0, < 2.0) rspec-support (~> 3.13.0) rspec-rails (6.0.4) @@ -236,7 +241,7 @@ GEM rspec-expectations (~> 3.12) rspec-mocks (~> 3.12) rspec-support (~> 3.12) - rspec-support (3.13.1) + rspec-support (3.13.2) rswag (2.16.0) rswag-api (= 2.16.0) rswag-specs (= 2.16.0) @@ -252,66 +257,61 @@ GEM rswag-ui (2.16.0) actionpack (>= 5.2, < 8.1) railties (>= 5.2, < 8.1) - rubocop (1.64.1) + rubocop (1.66.1) json (~> 2.3) language_server-protocol (>= 3.17.0) parallel (~> 1.10) parser (>= 3.3.0.2) rainbow (>= 2.2.2, < 4.0) - regexp_parser (>= 1.8, < 3.0) - rexml (>= 3.2.5, < 4.0) - rubocop-ast (>= 1.31.1, < 2.0) + regexp_parser (>= 2.4, < 3.0) + rubocop-ast (>= 1.32.2, < 2.0) ruby-progressbar (~> 1.7) unicode-display_width (>= 2.4.0, < 3.0) - rubocop-ast (1.32.0) + rubocop-ast (1.37.0) parser (>= 3.3.1.0) - rubocop-capybara (2.21.0) - rubocop (~> 1.41) rubocop-factory_bot (2.26.1) rubocop (~> 1.61) - rubocop-performance (1.21.0) + rubocop-performance (1.21.1) rubocop (>= 1.48.1, < 2.0) rubocop-ast (>= 1.31.1, < 2.0) - rubocop-rails (2.25.0) + rubocop-rails (2.26.1) activesupport (>= 4.2.0) rack (>= 1.1) - rubocop (>= 1.33.0, < 2.0) + rubocop (>= 1.52.0, < 2.0) rubocop-ast (>= 1.31.1, < 2.0) - rubocop-rspec (2.29.2) - rubocop (~> 1.40) - rubocop-capybara (~> 2.17) - rubocop-factory_bot (~> 2.22) - rubocop-rspec_rails (~> 2.28) - rubocop-rspec_rails (2.29.1) + rubocop-rspec (3.0.5) + rubocop (~> 1.61) + rubocop-rspec_rails (2.30.0) rubocop (~> 1.61) + rubocop-rspec (~> 3, >= 3.0.1) rubocop-shopify (2.15.1) rubocop (~> 1.51) ruby-next-core (1.0.3) ruby-progressbar (1.13.0) - securerandom (0.3.1) - sqlite3 (2.0.4) + securerandom (0.4.0) + sqlite3 (2.4.1) mini_portile2 (~> 2.8.0) - sqlite3 (2.0.4-x86_64-linux-gnu) - stringio (3.1.1) - strscan (3.1.0) - switchcop (0.1.7) - rubocop (= 1.64.1) - rubocop-performance (= 1.21.0) - rubocop-rails (= 2.25.0) - rubocop-rspec (= 2.29.2) + sqlite3 (2.4.1-x86_64-linux-gnu) + stringio (3.1.2) + switchcop (0.2.0) + rubocop (= 1.66.1) + rubocop-factory_bot (= 2.26.1) + rubocop-performance (= 1.21.1) + rubocop-rails (= 2.26.1) + rubocop-rspec (= 3.0.5) + rubocop-rspec_rails (= 2.30.0) rubocop-shopify (= 2.15.1) - thor (1.3.1) - timeout (0.4.1) + thor (1.3.2) + timeout (0.4.2) tzinfo (2.0.6) concurrent-ruby (~> 1.0) - unicode-display_width (2.5.0) + unicode-display_width (2.6.0) uri (1.0.2) - useragent (0.16.10) - webrick (1.8.1) + useragent (0.16.11) websocket-driver (0.7.6) websocket-extensions (>= 0.1.0) websocket-extensions (0.1.5) - zeitwerk (2.6.17) + zeitwerk (2.7.1) PLATFORMS ruby @@ -321,7 +321,7 @@ DEPENDENCIES active_model_serializers (~> 0.10.0) ammeter (~> 1.1.5) database_cleaner - panko_serializer + panko_serializer! rails (~> 8.0) rake (~> 13.0) rest-api-generator! diff --git a/app/controllers/rest_api_generator/resource_controller.rb b/app/controllers/rest_api_generator/resource_controller.rb index 1af2f99..1cc7c47 100644 --- a/app/controllers/rest_api_generator/resource_controller.rb +++ b/app/controllers/rest_api_generator/resource_controller.rb @@ -56,7 +56,12 @@ def resource_updated_params end def resource_params - params.require(resource_class.model_name.singular.to_sym).permit(resource_attributes) + singular_resource_name = resource_class.model_name.singular.to_sym + if Rails.gem_version < Gem::Version.new("8.0") + params.require(singular_resource_name).permit(resource_attributes) + else + params.expect(singular_resource_name => resource_attributes) + end end def resource_attributes From 9d1d1682b8a068aab50d6c96c2726f9c102ccfc4 Mon Sep 17 00:00:00 2001 From: PedroAugustoRamalhoDuarte Date: Sun, 15 Dec 2024 12:05:41 -0300 Subject: [PATCH 3/8] setup a error spec --- .../dummy/app/controllers/application_controller.rb | 2 +- spec/dummy/app/models/transaction.rb | 2 ++ .../rest_api_generator/resource_controller_spec.rb | 13 ++++++++++--- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/spec/dummy/app/controllers/application_controller.rb b/spec/dummy/app/controllers/application_controller.rb index a4ee00c..5e68af7 100644 --- a/spec/dummy/app/controllers/application_controller.rb +++ b/spec/dummy/app/controllers/application_controller.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true -class ApplicationController < ActionController::Base +class ApplicationController < ActionController::API include RestApiGenerator::ErrorHandler end diff --git a/spec/dummy/app/models/transaction.rb b/spec/dummy/app/models/transaction.rb index 34cd4e2..88cea1e 100644 --- a/spec/dummy/app/models/transaction.rb +++ b/spec/dummy/app/models/transaction.rb @@ -3,5 +3,7 @@ class Transaction < ApplicationRecord include RestApiGenerator::Filterable + validates :amount, presence: true + filter_scope :filter_by_side, ->(side) { where(side: side) } end diff --git a/spec/lib/rest_api_generator/resource_controller_spec.rb b/spec/lib/rest_api_generator/resource_controller_spec.rb index 040c45d..47297fc 100644 --- a/spec/lib/rest_api_generator/resource_controller_spec.rb +++ b/spec/lib/rest_api_generator/resource_controller_spec.rb @@ -15,13 +15,13 @@ end it "returns pagy headers" do - Transaction.create! + Transaction.create!(amount: 20) get "/transactions" expect(response.headers["Total-Count"]).to eq("1") end it "returns second page correctly" do - 21.times { Transaction.create! } + 21.times { Transaction.create!(amount: 20) } get "/transactions?page=2" expect(response.parsed_body.length).to eq(1) end @@ -30,7 +30,7 @@ describe "GET transaction" do it "returns http success" do - t = Transaction.create! + t = Transaction.create!(amount: 20) get "/transactions/#{t.id}" expect(response).to have_http_status(:success) end @@ -40,6 +40,13 @@ it "creates a new transaction" do expect { post "/transactions", params: { transaction: { amount: 20 } } }.to change(Transaction, :count).by(1) end + + context "when params are invalid" do + it "returns http unprocessable entity" do + post "/transactions", params: { transaction: { amount: nil } } + expect(response).to have_http_status(422) + end + end end describe "PATCH transaction" do From c7d8c97854237f08c8b9d85d41cce813c65463a4 Mon Sep 17 00:00:00 2001 From: PedroAugustoRamalhoDuarte Date: Sun, 15 Dec 2024 12:11:47 -0300 Subject: [PATCH 4/8] :recycle: refactor some controller methods to resource params --- .../child_resource_controller.rb | 22 +----------- .../rest_api_generator/resource_controller.rb | 30 +--------------- lib/rest_api_generator.rb | 1 + lib/rest_api_generator/resource_params.rb | 36 +++++++++++++++++++ 4 files changed, 39 insertions(+), 50 deletions(-) create mode 100644 lib/rest_api_generator/resource_params.rb diff --git a/app/controllers/rest_api_generator/child_resource_controller.rb b/app/controllers/rest_api_generator/child_resource_controller.rb index 1e66e42..10e2475 100644 --- a/app/controllers/rest_api_generator/child_resource_controller.rb +++ b/app/controllers/rest_api_generator/child_resource_controller.rb @@ -5,6 +5,7 @@ class ChildResourceController < RestApiGenerator.configuration.parent_controller include ControllerCallbacks include Orderable include Serializable + include ResourceParams before_action :set_parent_resource before_action :set_resource, only: [:show, :update, :destroy] @@ -58,27 +59,6 @@ def resource_class resource_by_controller_name end - # Params - def resource_created_params - resource_params - end - - def resource_updated_params - resource_params - end - - def resource_params - params.require(resource_class.model_name.singular.to_sym).permit(resource_attributes) - end - - def resource_attributes - resource_class.attribute_names.map(&:to_sym) - end - - def params_for_filter - params.slice(*resource_class.filter_scopes) - end - # Before actions def set_parent_resource run_callbacks :set_parent_resource do diff --git a/app/controllers/rest_api_generator/resource_controller.rb b/app/controllers/rest_api_generator/resource_controller.rb index 1cc7c47..f492041 100644 --- a/app/controllers/rest_api_generator/resource_controller.rb +++ b/app/controllers/rest_api_generator/resource_controller.rb @@ -5,6 +5,7 @@ class ResourceController < RestApiGenerator.configuration.parent_controller.cons include ControllerCallbacks include Orderable include Serializable + include ResourceParams before_action :set_resource, only: [:show, :update, :destroy] @@ -39,35 +40,6 @@ def destroy private - def params_for_filter - params.slice(*resource_class.filter_scopes) - end - - def resource_class - resource_by_controller_name - end - - def resource_created_params - resource_params - end - - def resource_updated_params - resource_params - end - - def resource_params - singular_resource_name = resource_class.model_name.singular.to_sym - if Rails.gem_version < Gem::Version.new("8.0") - params.require(singular_resource_name).permit(resource_attributes) - else - params.expect(singular_resource_name => resource_attributes) - end - end - - def resource_attributes - resource_class.attribute_names.map(&:to_sym) - end - def set_resource run_callbacks :set_resource do @resource = resource_class.find(record_id) diff --git a/lib/rest_api_generator.rb b/lib/rest_api_generator.rb index 85cb643..868d4dd 100644 --- a/lib/rest_api_generator.rb +++ b/lib/rest_api_generator.rb @@ -10,6 +10,7 @@ require_relative "rest_api_generator/orderable" require_relative "rest_api_generator/serializable" require_relative "rest_api_generator/controller_callbacks" +require_relative "rest_api_generator/resource_params" module RestApiGenerator class Error < StandardError; end diff --git a/lib/rest_api_generator/resource_params.rb b/lib/rest_api_generator/resource_params.rb new file mode 100644 index 0000000..ca46236 --- /dev/null +++ b/lib/rest_api_generator/resource_params.rb @@ -0,0 +1,36 @@ +module RestApiGenerator + module ResourceParams + extend ActiveSupport::Concern + + included do + def params_for_filter + params.slice(*resource_class.filter_scopes) + end + + def resource_class + resource_by_controller_name + end + + def resource_created_params + resource_params + end + + def resource_updated_params + resource_params + end + + def resource_params + singular_resource_name = resource_class.model_name.singular.to_sym + if Rails.gem_version < Gem::Version.new("8.0") + params.require(singular_resource_name).permit(resource_attributes) + else + params.expect(singular_resource_name => resource_attributes) + end + end + + def resource_attributes + resource_class.attribute_names.map(&:to_sym) + end + end + end +end \ No newline at end of file From 82a1c0834865f47430c87274f8df65e0de2c7f1c Mon Sep 17 00:00:00 2001 From: PedroAugustoRamalhoDuarte Date: Sun, 15 Dec 2024 12:30:00 -0300 Subject: [PATCH 5/8] upgrade ruby version --- .rubocop.yml | 2 +- Gemfile.lock | 32 +++++++++++++++++--------------- spec/dummy/.ruby-version | 2 +- 3 files changed, 19 insertions(+), 17 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index c106845..e2daf75 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -3,7 +3,7 @@ inherit_gem: AllCops: NewCops: enable - TargetRubyVersion: 2.6 + TargetRubyVersion: 3.3 Exclude: - 'tmp/**/*' - '**/*/bundle' diff --git a/Gemfile.lock b/Gemfile.lock index bba09d5..aeb659d 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -257,29 +257,29 @@ GEM rswag-ui (2.16.0) actionpack (>= 5.2, < 8.1) railties (>= 5.2, < 8.1) - rubocop (1.66.1) + rubocop (1.69.2) json (~> 2.3) language_server-protocol (>= 3.17.0) parallel (~> 1.10) parser (>= 3.3.0.2) rainbow (>= 2.2.2, < 4.0) - regexp_parser (>= 2.4, < 3.0) - rubocop-ast (>= 1.32.2, < 2.0) + regexp_parser (>= 2.9.3, < 3.0) + rubocop-ast (>= 1.36.2, < 2.0) ruby-progressbar (~> 1.7) - unicode-display_width (>= 2.4.0, < 3.0) + unicode-display_width (>= 2.4.0, < 4.0) rubocop-ast (1.37.0) parser (>= 3.3.1.0) rubocop-factory_bot (2.26.1) rubocop (~> 1.61) - rubocop-performance (1.21.1) + rubocop-performance (1.23.0) rubocop (>= 1.48.1, < 2.0) rubocop-ast (>= 1.31.1, < 2.0) - rubocop-rails (2.26.1) + rubocop-rails (2.27.0) activesupport (>= 4.2.0) rack (>= 1.1) rubocop (>= 1.52.0, < 2.0) rubocop-ast (>= 1.31.1, < 2.0) - rubocop-rspec (3.0.5) + rubocop-rspec (3.3.0) rubocop (~> 1.61) rubocop-rspec_rails (2.30.0) rubocop (~> 1.61) @@ -293,19 +293,21 @@ GEM mini_portile2 (~> 2.8.0) sqlite3 (2.4.1-x86_64-linux-gnu) stringio (3.1.2) - switchcop (0.2.0) - rubocop (= 1.66.1) - rubocop-factory_bot (= 2.26.1) - rubocop-performance (= 1.21.1) - rubocop-rails (= 2.26.1) - rubocop-rspec (= 3.0.5) - rubocop-rspec_rails (= 2.30.0) + switchcop (0.2.1) + rubocop (~> 1.69) + rubocop-factory_bot (~> 2.26) + rubocop-performance (~> 1.23) + rubocop-rails (~> 2.27) + rubocop-rspec (~> 3.3) + rubocop-rspec_rails (~> 2.30) rubocop-shopify (= 2.15.1) thor (1.3.2) timeout (0.4.2) tzinfo (2.0.6) concurrent-ruby (~> 1.0) - unicode-display_width (2.6.0) + unicode-display_width (3.1.2) + unicode-emoji (~> 4.0, >= 4.0.4) + unicode-emoji (4.0.4) uri (1.0.2) useragent (0.16.11) websocket-driver (0.7.6) diff --git a/spec/dummy/.ruby-version b/spec/dummy/.ruby-version index 6d5369b..e391e18 100644 --- a/spec/dummy/.ruby-version +++ b/spec/dummy/.ruby-version @@ -1 +1 @@ -ruby-3.3.4 +ruby-3.3.6 From 171e45de1f0570c2d63f413aebc22d63450b20b8 Mon Sep 17 00:00:00 2001 From: PedroAugustoRamalhoDuarte Date: Sun, 29 Dec 2024 10:10:02 -0300 Subject: [PATCH 6/8] upgrade panko serializer --- Gemfile | 9 +-------- Gemfile.lock | 14 ++++---------- 2 files changed, 5 insertions(+), 18 deletions(-) diff --git a/Gemfile b/Gemfile index 439323a..604e403 100644 --- a/Gemfile +++ b/Gemfile @@ -18,13 +18,6 @@ gem "sqlite3" # Serializers -# TODO: waiting for https://github.com/yosiat/panko_serializer/issues/166 -if Gem::Version.new(rails_version) >= Gem::Version.new("8.0") - gem "panko_serializer", github: "yosiat/panko_serializer", branch: "master" -else - gem "panko_serializer" -end - - +gem "panko_serializer", "~> 0.8" gem "active_model_serializers", "~> 0.10.0" diff --git a/Gemfile.lock b/Gemfile.lock index aeb659d..f93bf6c 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,12 +1,3 @@ -GIT - remote: https://github.com/yosiat/panko_serializer.git - revision: a2dd88be709e3e6f623c4f20ea2af740fdb53907 - branch: master - specs: - panko_serializer (0.8.2) - activesupport - oj (> 3.11.0, < 4.0.0) - PATH remote: . specs: @@ -169,6 +160,9 @@ GEM ostruct (>= 0.2) ostruct (0.6.1) pagy (9.3.3) + panko_serializer (0.8.2) + activesupport + oj (> 3.11.0, < 4.0.0) parallel (1.26.3) parser (3.3.6.0) ast (~> 2.4.1) @@ -323,7 +317,7 @@ DEPENDENCIES active_model_serializers (~> 0.10.0) ammeter (~> 1.1.5) database_cleaner - panko_serializer! + panko_serializer (~> 0.8) rails (~> 8.0) rake (~> 13.0) rest-api-generator! From cd4d9f33ff8d376a0c9c16bbcb7f334866711ac3 Mon Sep 17 00:00:00 2001 From: PedroAugustoRamalhoDuarte Date: Sun, 29 Dec 2024 10:10:11 -0300 Subject: [PATCH 7/8] rubocop fixes --- lib/rest_api_generator/resource_params.rb | 4 +++- spec/lib/rest_api_generator/resource_controller_spec.rb | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/rest_api_generator/resource_params.rb b/lib/rest_api_generator/resource_params.rb index ca46236..187d5d4 100644 --- a/lib/rest_api_generator/resource_params.rb +++ b/lib/rest_api_generator/resource_params.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module RestApiGenerator module ResourceParams extend ActiveSupport::Concern @@ -33,4 +35,4 @@ def resource_attributes end end end -end \ No newline at end of file +end diff --git a/spec/lib/rest_api_generator/resource_controller_spec.rb b/spec/lib/rest_api_generator/resource_controller_spec.rb index 47297fc..4735b41 100644 --- a/spec/lib/rest_api_generator/resource_controller_spec.rb +++ b/spec/lib/rest_api_generator/resource_controller_spec.rb @@ -44,7 +44,7 @@ context "when params are invalid" do it "returns http unprocessable entity" do post "/transactions", params: { transaction: { amount: nil } } - expect(response).to have_http_status(422) + expect(response).to have_http_status(:unprocessable_content) end end end From b7571346eb52e2ba173a4fd88b51a0f33fd9ddfc Mon Sep 17 00:00:00 2001 From: PedroAugustoRamalhoDuarte Date: Sun, 29 Dec 2024 10:13:32 -0300 Subject: [PATCH 8/8] upgrade ruby to 3.4 --- .github/workflows/ci.yml | 2 +- .rubocop.yml | 2 +- Gemfile | 2 +- Gemfile.lock | 14 +++++++------- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4176716..1ae436a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,6 +1,6 @@ name: CI env: - RUBY_VERSION: 3.3.6 + RUBY_VERSION: 3.4.1 on: pull_request: diff --git a/.rubocop.yml b/.rubocop.yml index e2daf75..0efa1c2 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -3,7 +3,7 @@ inherit_gem: AllCops: NewCops: enable - TargetRubyVersion: 3.3 + TargetRubyVersion: 3.4 Exclude: - 'tmp/**/*' - '**/*/bundle' diff --git a/Gemfile b/Gemfile index 604e403..2f52e52 100644 --- a/Gemfile +++ b/Gemfile @@ -18,6 +18,6 @@ gem "sqlite3" # Serializers -gem "panko_serializer", "~> 0.8" +gem "panko_serializer", "~> 0.8.3" gem "active_model_serializers", "~> 0.10.0" diff --git a/Gemfile.lock b/Gemfile.lock index f93bf6c..d3f1088 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -150,17 +150,17 @@ GEM net-smtp (0.5.0) net-protocol nio4r (2.7.4) - nokogiri (1.17.2) + nokogiri (1.18.0) mini_portile2 (~> 2.8.2) racc (~> 1.4) - nokogiri (1.17.2-x86_64-linux) + nokogiri (1.18.0-x86_64-linux-gnu) racc (~> 1.4) - oj (3.16.8) + oj (3.16.9) bigdecimal (>= 3.0) ostruct (>= 0.2) ostruct (0.6.1) pagy (9.3.3) - panko_serializer (0.8.2) + panko_serializer (0.8.3) activesupport oj (> 3.11.0, < 4.0.0) parallel (1.26.3) @@ -283,9 +283,9 @@ GEM ruby-next-core (1.0.3) ruby-progressbar (1.13.0) securerandom (0.4.0) - sqlite3 (2.4.1) + sqlite3 (2.5.0) mini_portile2 (~> 2.8.0) - sqlite3 (2.4.1-x86_64-linux-gnu) + sqlite3 (2.5.0-x86_64-linux-gnu) stringio (3.1.2) switchcop (0.2.1) rubocop (~> 1.69) @@ -317,7 +317,7 @@ DEPENDENCIES active_model_serializers (~> 0.10.0) ammeter (~> 1.1.5) database_cleaner - panko_serializer (~> 0.8) + panko_serializer (~> 0.8.3) rails (~> 8.0) rake (~> 13.0) rest-api-generator!