Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow multiple meta blocks and merge them if present #424

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from

Conversation

doits
Copy link

@doits doits commented Jun 7, 2019

Useful for includes and inheritance:

module SomeModule
  extend ActiveSupport::Concern

  included do
    meta do
      {
        module_meta: true
      }
    end
  end
end

class UserSerializer
  include FastJsonapi::ObjectSerializer
  include SomeModule

  meta do
    {
      user_meta: true
    }
  end
end

class EmployeeSerializer < UserSerializer
  meta do
    {
      employee_meta: true
    }
  end
end

EmployeeSerializer.new(employee).serializable_hash

{
  data: {
    [...]
    meta: {
      module_meta: true,
      user_meta: true,
      employee_meta: true
    }
  }
}

@doits doits changed the base branch from master to dev June 7, 2019 11:17
Useful for includes and inheritance:

```rb
module SomeModule
  extend ActiveSupport::Concern

  included do
    meta do
      {
        module_meta: true
      }
    end
  end
end

class UserSerializer
  include FastJsonapi::ObjectSerializer
  include SomeModule

  meta do
    {
      user_meta: true
    }
  end
end

class EmployeeSerializer < UserSerializer
  meta do
    {
      employee_meta: true
    }
  end
end

EmployeeSerializer.new(employee).serializable_hash

{
  data: {
    [...]
    meta: {
      module_meta: true,
      user_meta: true,
      employee_meta: true
    }
  }
}
```
@doits
Copy link
Author

doits commented Jun 7, 2019

One thing to discuss is whether deep_merge or merge should be used. Only makes a difference in the exotic case of different meta blocks returning same keys though.

Otherwise I think this change makes composing serializers out of different modules and/or inheritance much more simpler, because each module or class can have meta blocks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant