Skip to content

Commit

Permalink
[Fix] Only warn about only and except option
Browse files Browse the repository at this point in the history
  • Loading branch information
okuramasafumi committed Nov 30, 2023
1 parent b0372c7 commit e733a8d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 8 deletions.
11 changes: 5 additions & 6 deletions lib/alba/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,11 @@ def serializable_hash
private

def _to_json(root_key, meta, options)
options.reject! { |k, _| %i[layout prefixes template status].include?(k) } # Rails specific guard
names = options.filter_map { |k, v| k unless v.nil? }
unless names.empty?
names.sort!
names.map! { |s| "\"#{s}\"" }
message = "You passed #{names.join(', ')} options but ignored. Please refer to the document: https://github.com/okuramasafumi/alba/blob/main/docs/rails.md"
confusing_options = options.keys.select { |k| k.to_sym == :only || k.to_sym == :except }
unless confusing_options.empty?
confusing_options.sort!
confusing_options.map! { |s| "\"#{s}\"" }
message = "You passed #{confusing_options.join(' and ')} options but ignored. Please refer to the document: https://github.com/okuramasafumi/alba/blob/main/docs/rails.md"
Kernel.warn(message)
end
serialize(root_key: root_key, meta: meta)
Expand Down
3 changes: 1 addition & 2 deletions test/resource_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ def test_to_json_with_various_arguments # rubocop:disable Minitest/MultipleAsser
meta: {this: :meta}
)
end
message = "You passed \"except\", \"include\", \"methods\", \"only\", \"root\" options but ignored. Please refer to the document: https://github.com/okuramasafumi/alba/blob/main/docs/rails.md\n"
assert_output('', message) { result = execute2.call }
assert_output('', "You passed \"except\" and \"only\" options but ignored. Please refer to the document: https://github.com/okuramasafumi/alba/blob/main/docs/rails.md\n") { result = execute2.call }
assert_equal(
'{"foo":{"id":1,"bar_size":1,"bars":[{"id":1}]},"meta":{"this":"meta"}}',
result
Expand Down

0 comments on commit e733a8d

Please sign in to comment.