Repository: okuramasafumi/alba (Ruby)

Issue:
`root_key_for_collection` DSL
## Is your feature request related to a problem? Please describe.

When setting a root key only for collection, it looks like this:

```ruby
root_key :_, :foos
```

Here, the first `:_` looks ugly but required.

## Describe the solution you'd like

```ruby
root_key_for_collection :foos
```

## Describe alternatives you've considered

None.

## Additional context

This is the real problem in my client work :)



Discussion:
Hello, @okuramasafumi. I would like to contribute to this. Can I work on this issue?
Hi @heka1024 absolutely, go ahead! If you need help, please let me know.
> Hi [@heka1024](https://github.com/heka1024) absolutely, go ahead! If you need help, please let me know.

is it possible to root_key_for_collection be the same of root_key ?
Hi @alexcastrodev , I'm planning to refine root_key related features a bit, including `root_key_for_collection`. 

Currently, `root_key :user` means there's no root key for user collections. This doesn't make much sense to me. I'd like to change its meaning to have `:user` as root key for both single resources and collections.
That said, currently you need to do something like `root_key :user, :user` to have the same key for both.
> Hi [@alexcastrodev](https://github.com/alexcastrodev) , I'm planning to refine root_key related features a bit, including `root_key_for_collection`.
> 
> Currently, `root_key :user` means there's no root key for user collections. This doesn't make much sense to me. I'd like to change its meaning to have `:user` as root key for both single resources and collections. That said, currently you need to do something like `root_key :user, :user` to have the same key for both.

Only using `root_key_for_collection :user` i dont need to set `root_key :user, :users`
Is it a side effect? lol 



Patch:
diff --git a/lib/alba/resource.rb b/lib/alba/resource.rb
index 1df2c9c..dbecc80 100644
--- a/lib/alba/resource.rb
+++ b/lib/alba/resource.rb
@@ -372,6 +372,15 @@ def root_key(key, key_for_collection = nil)
         @_key_for_collection = key_for_collection&.to_sym
       end
 
+      # Set root key for collection
+      #
+      # @param key [String, Symbol]
+      # @raise [NoMethodError] when key doesn't respond to `to_sym` method
+      def root_key_for_collection(key)
+        @_key = true
+        @_key_for_collection = key.to_sym
+      end
+
       # Set root key to true
       def root_key!
         @_key = true


Test patch:
diff --git a/test/usecases/root_key_collection_test.rb b/test/usecases/root_key_collection_test.rb
new file mode 100644
index 0000000..2267909
--- /dev/null
+++ b/test/usecases/root_key_collection_test.rb
@@ -0,0 +1,33 @@
+require_relative '../test_helper'
+
+class RootKeyCollectionTest < Minitest::Test
+  class User
+    attr_reader :id, :name, :created_at, :updated_at
+
+    def initialize(id, name)
+      @id = id
+      @name = name
+      @created_at = Time.now
+      @updated_at = Time.now
+    end
+  end
+
+  class UserResource
+    include Alba::Resource
+
+    root_key_for_collection :users
+
+    attributes :id, :name
+  end
+
+  def setup
+    @users = [User.new(1, 'Masafumi OKURA'), User.new(2, 'heka1024')]
+  end
+
+  def test_root_key_collection
+    assert_equal(
+      '{"users":[{"id":1,"name":"Masafumi OKURA"},{"id":2,"name":"heka1024"}]}',
+      UserResource.new(@users).serialize
+    )
+  end
+end