From 69fbc695e40af144767e4e8c50c0126373963bdd Mon Sep 17 00:00:00 2001 From: "Gregory N. Schmit" Date: Sat, 14 Dec 2024 20:32:37 -0600 Subject: [PATCH] Make fields hash allow :except key; more test refactoring. --- bin/setup | 2 +- lib/rest_framework/serializers/native_serializer.rb | 3 +++ lib/rest_framework/utils.rb | 3 ++- .../controllers/api/test/added_select_controller.rb | 10 ++++++++++ .../controllers/api/test/bare_create_controller.rb | 7 +++++++ ...ontroller.rb => fields_hash_exclude_controller.rb} | 2 +- .../api/test/fields_hash_only_except_controller.rb | 6 ++++++ ...ly_users_controller.rb => read_only_controller.rb} | 2 +- .../api/test/users_with_added_select_controller.rb | 10 ---------- .../api/test/users_with_bare_create_controller.rb | 7 ------- .../api/test/users_with_fields_hash_controller.rb | 6 ------ test/config/routes.rb | 10 +++++----- ...roller_test.rb => added_select_controller_test.rb} | 2 +- .../api/test/bare_create_controller_test.rb | 11 +++++++++++ ....rb => fields_hash_only_except_controller_test.rb} | 2 +- ...ontroller_test.rb => read_only_controller_test.rb} | 2 +- .../test/users_with_bare_create_controller_test.rb | 11 ----------- 17 files changed, 50 insertions(+), 46 deletions(-) create mode 100644 test/app/controllers/api/test/added_select_controller.rb create mode 100644 test/app/controllers/api/test/bare_create_controller.rb rename test/app/controllers/api/test/{genres_with_fields_hash_controller.rb => fields_hash_exclude_controller.rb} (61%) create mode 100644 test/app/controllers/api/test/fields_hash_only_except_controller.rb rename test/app/controllers/api/test/{read_only_users_controller.rb => read_only_controller.rb} (87%) delete mode 100644 test/app/controllers/api/test/users_with_added_select_controller.rb delete mode 100644 test/app/controllers/api/test/users_with_bare_create_controller.rb delete mode 100644 test/app/controllers/api/test/users_with_fields_hash_controller.rb rename test/test/controllers/api/test/{users_with_added_select_controller_test.rb => added_select_controller_test.rb} (85%) create mode 100644 test/test/controllers/api/test/bare_create_controller_test.rb rename test/test/controllers/api/test/{users_with_fields_hash_controller_test.rb => fields_hash_only_except_controller_test.rb} (90%) rename test/test/controllers/api/test/{read_only_users_controller_test.rb => read_only_controller_test.rb} (86%) delete mode 100644 test/test/controllers/api/test/users_with_bare_create_controller_test.rb diff --git a/bin/setup b/bin/setup index 88ecc9c..8bf88be 100755 --- a/bin/setup +++ b/bin/setup @@ -13,7 +13,7 @@ FileUtils.chdir(File.expand_path("..", __dir__)) do system!("bundle install") puts "\n== Preparing database ==" - system!("bin/rails db:migrate:reset") + system!("bin/rails db:migrate:reset db:seed") puts "\n== Removing old logs and tempfiles ==" system!("bin/rails log:clear tmp:clear") diff --git a/lib/rest_framework/serializers/native_serializer.rb b/lib/rest_framework/serializers/native_serializer.rb index f4d95cf..d1cf471 100644 --- a/lib/rest_framework/serializers/native_serializer.rb +++ b/lib/rest_framework/serializers/native_serializer.rb @@ -271,6 +271,9 @@ def _get_controller_serializer_config(fields) end elsif @model.method_defined?(f) methods << f + else + # Assume anything else is a virtual column. + columns << f end end diff --git a/lib/rest_framework/utils.rb b/lib/rest_framework/utils.rb index a435991..c0879c0 100644 --- a/lib/rest_framework/utils.rb +++ b/lib/rest_framework/utils.rb @@ -159,9 +159,10 @@ def self.parse_fields_hash(fields_hash, model, exclude_associations: nil) ) parsed_fields += fields_hash[:include].map(&:to_s) if fields_hash[:include] parsed_fields -= fields_hash[:exclude].map(&:to_s) if fields_hash[:exclude] + parsed_fields -= fields_hash[:except].map(&:to_s) if fields_hash[:except] # Warn for any unknown keys. - (fields_hash.keys - [:only, :include, :exclude]).each do |k| + (fields_hash.keys - [:only, :except, :include, :exclude]).each do |k| Rails.logger.warn("RRF: Unknown key in fields hash: #{k}") end diff --git a/test/app/controllers/api/test/added_select_controller.rb b/test/app/controllers/api/test/added_select_controller.rb new file mode 100644 index 0000000..4c661e5 --- /dev/null +++ b/test/app/controllers/api/test/added_select_controller.rb @@ -0,0 +1,10 @@ +class Api::Test::AddedSelectController < Api::TestController + include RESTFramework::ModelControllerMixin + + self.model = Movie + self.fields = {include: [:selected_value]} + + def get_recordset + return Movie.select("*, 5 as selected_value") + end +end diff --git a/test/app/controllers/api/test/bare_create_controller.rb b/test/app/controllers/api/test/bare_create_controller.rb new file mode 100644 index 0000000..c823993 --- /dev/null +++ b/test/app/controllers/api/test/bare_create_controller.rb @@ -0,0 +1,7 @@ +class Api::Test::BareCreateController < Api::TestController + include RESTFramework::ModelControllerMixin + + self.model = Movie + self.fields = %w(id name) + self.create_from_recordset = false +end diff --git a/test/app/controllers/api/test/genres_with_fields_hash_controller.rb b/test/app/controllers/api/test/fields_hash_exclude_controller.rb similarity index 61% rename from test/app/controllers/api/test/genres_with_fields_hash_controller.rb rename to test/app/controllers/api/test/fields_hash_exclude_controller.rb index c0e1ec6..8d97167 100644 --- a/test/app/controllers/api/test/genres_with_fields_hash_controller.rb +++ b/test/app/controllers/api/test/fields_hash_exclude_controller.rb @@ -1,4 +1,4 @@ -class Api::Test::GenresWithFieldsHashController < Api::TestController +class Api::Test::FieldsHashExcludeController < Api::TestController include RESTFramework::ModelControllerMixin self.fields = {exclude: [:main_movies]} diff --git a/test/app/controllers/api/test/fields_hash_only_except_controller.rb b/test/app/controllers/api/test/fields_hash_only_except_controller.rb new file mode 100644 index 0000000..96ff75c --- /dev/null +++ b/test/app/controllers/api/test/fields_hash_only_except_controller.rb @@ -0,0 +1,6 @@ +class Api::Test::FieldsHashOnlyExceptController < Api::TestController + include RESTFramework::ModelControllerMixin + + self.fields = {only: %w(id login age balance), except: %w(balance)} + self.model = User +end diff --git a/test/app/controllers/api/test/read_only_users_controller.rb b/test/app/controllers/api/test/read_only_controller.rb similarity index 87% rename from test/app/controllers/api/test/read_only_users_controller.rb rename to test/app/controllers/api/test/read_only_controller.rb index c0a613b..2878c94 100644 --- a/test/app/controllers/api/test/read_only_users_controller.rb +++ b/test/app/controllers/api/test/read_only_controller.rb @@ -1,4 +1,4 @@ -class Api::Test::ReadOnlyUsersController < Api::TestController +class Api::Test::ReadOnlyController < Api::TestController include RESTFramework::ReadOnlyModelControllerMixin class SingularManagerSerializer < RESTFramework::NativeSerializer diff --git a/test/app/controllers/api/test/users_with_added_select_controller.rb b/test/app/controllers/api/test/users_with_added_select_controller.rb deleted file mode 100644 index 899b032..0000000 --- a/test/app/controllers/api/test/users_with_added_select_controller.rb +++ /dev/null @@ -1,10 +0,0 @@ -class Api::Test::UsersWithAddedSelectController < Api::TestController - include RESTFramework::ModelControllerMixin - - self.model = User - self.native_serializer_config = {except: [:balance]} - - def get_recordset - return User.select("*, 5 as selected_value") - end -end diff --git a/test/app/controllers/api/test/users_with_bare_create_controller.rb b/test/app/controllers/api/test/users_with_bare_create_controller.rb deleted file mode 100644 index 7770662..0000000 --- a/test/app/controllers/api/test/users_with_bare_create_controller.rb +++ /dev/null @@ -1,7 +0,0 @@ -class Api::Test::UsersWithBareCreateController < Api::TestController - include RESTFramework::ModelControllerMixin - - self.fields = %w(id login) - self.create_from_recordset = false - self.model = User -end diff --git a/test/app/controllers/api/test/users_with_fields_hash_controller.rb b/test/app/controllers/api/test/users_with_fields_hash_controller.rb deleted file mode 100644 index 14628ef..0000000 --- a/test/app/controllers/api/test/users_with_fields_hash_controller.rb +++ /dev/null @@ -1,6 +0,0 @@ -class Api::Test::UsersWithFieldsHashController < Api::TestController - include RESTFramework::ModelControllerMixin - - self.fields = {only: %w(id login age balance), exclude: %w(balance)} - self.model = User -end diff --git a/test/config/routes.rb b/test/config/routes.rb index 78ecb41..befce6c 100644 --- a/test/config/routes.rb +++ b/test/config/routes.rb @@ -42,15 +42,15 @@ rest_root :test namespace :test do - rest_resources :genres_with_fields_hash rest_resources :users - rest_resources :users_with_added_select - rest_resources :users_with_bare_create, force_plural: true, only: [:create] - rest_resources :users_with_fields_hash + rest_resources :added_select + rest_resources :bare_create, force_plural: true, only: [:create] + rest_resources :fields_hash_exclude + rest_resources :fields_hash_only_except rest_resources :users_with_string_serializer rest_resources :users_with_sub_fields rest_resources :users_without_rescue_unknown_format - rest_resources :read_only_users + rest_resources :read_only rest_route :network diff --git a/test/test/controllers/api/test/users_with_added_select_controller_test.rb b/test/test/controllers/api/test/added_select_controller_test.rb similarity index 85% rename from test/test/controllers/api/test/users_with_added_select_controller_test.rb rename to test/test/controllers/api/test/added_select_controller_test.rb index 681ed40..e817eac 100644 --- a/test/test/controllers/api/test/users_with_added_select_controller_test.rb +++ b/test/test/controllers/api/test/added_select_controller_test.rb @@ -1,6 +1,6 @@ require_relative "base" -class Api::Test::UsersWithAddedSelectControllerTest < ActionController::TestCase +class Api::Test::AddedSelectControllerTest < ActionController::TestCase include BaseApi::TestControllerTests def test_only_works diff --git a/test/test/controllers/api/test/bare_create_controller_test.rb b/test/test/controllers/api/test/bare_create_controller_test.rb new file mode 100644 index 0000000..70ebfda --- /dev/null +++ b/test/test/controllers/api/test/bare_create_controller_test.rb @@ -0,0 +1,11 @@ +require_relative "base" + +class Api::Test::BareCreateControllerTest < ActionController::TestCase + include BaseApi::TestControllerTests + + def test_bare_create + post(:create, as: :json, params: {name: "Test Bare Create"}) + assert_response(:success) + assert(Movie.find_by(name: "Test Bare Create")) + end +end diff --git a/test/test/controllers/api/test/users_with_fields_hash_controller_test.rb b/test/test/controllers/api/test/fields_hash_only_except_controller_test.rb similarity index 90% rename from test/test/controllers/api/test/users_with_fields_hash_controller_test.rb rename to test/test/controllers/api/test/fields_hash_only_except_controller_test.rb index 508d991..f3bdd5d 100644 --- a/test/test/controllers/api/test/users_with_fields_hash_controller_test.rb +++ b/test/test/controllers/api/test/fields_hash_only_except_controller_test.rb @@ -1,6 +1,6 @@ require_relative "base" -class Api::Test::UsersWithFieldsHashControllerTest < ActionController::TestCase +class Api::Test::FieldsHashOnlyExceptControllerTest < ActionController::TestCase include BaseApi::TestControllerTests def test_list diff --git a/test/test/controllers/api/test/read_only_users_controller_test.rb b/test/test/controllers/api/test/read_only_controller_test.rb similarity index 86% rename from test/test/controllers/api/test/read_only_users_controller_test.rb rename to test/test/controllers/api/test/read_only_controller_test.rb index 1b83e1f..a827b80 100644 --- a/test/test/controllers/api/test/read_only_users_controller_test.rb +++ b/test/test/controllers/api/test/read_only_controller_test.rb @@ -1,6 +1,6 @@ require_relative "base" -class Api::Test::ReadOnlyUsersControllerTest < ActionController::TestCase +class Api::Test::ReadOnlyControllerTest < ActionController::TestCase include BaseApi::TestControllerTests def test_list diff --git a/test/test/controllers/api/test/users_with_bare_create_controller_test.rb b/test/test/controllers/api/test/users_with_bare_create_controller_test.rb deleted file mode 100644 index 2553883..0000000 --- a/test/test/controllers/api/test/users_with_bare_create_controller_test.rb +++ /dev/null @@ -1,11 +0,0 @@ -require_relative "base" - -class Api::Test::UsersWithBareCreateControllerTest < ActionController::TestCase - include BaseApi::TestControllerTests - - def test_bare_create - post(:create, as: :json, params: {login: "test_bare_create"}) - assert_response(:success) - assert(User.find_by(login: "test_bare_create")) - end -end