Skip to content

Commit

Permalink
Make fields hash allow :except key; more test refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
gregschmit committed Dec 15, 2024
1 parent a768a87 commit 69fbc69
Show file tree
Hide file tree
Showing 17 changed files with 50 additions and 46 deletions.
2 changes: 1 addition & 1 deletion bin/setup
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
3 changes: 3 additions & 0 deletions lib/rest_framework/serializers/native_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion lib/rest_framework/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 10 additions & 0 deletions test/app/controllers/api/test/added_select_controller.rb
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions test/app/controllers/api/test/bare_create_controller.rb
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Api::Test::GenresWithFieldsHashController < Api::TestController
class Api::Test::FieldsHashExcludeController < Api::TestController
include RESTFramework::ModelControllerMixin

self.fields = {exclude: [:main_movies]}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Api::Test::ReadOnlyUsersController < Api::TestController
class Api::Test::ReadOnlyController < Api::TestController
include RESTFramework::ReadOnlyModelControllerMixin

class SingularManagerSerializer < RESTFramework::NativeSerializer
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

10 changes: 5 additions & 5 deletions test/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
11 changes: 11 additions & 0 deletions test/test/controllers/api/test/bare_create_controller_test.rb
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down

This file was deleted.

0 comments on commit 69fbc69

Please sign in to comment.