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

Add support to look up superclass schemas in inherited controllers #61

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

keymastervn
Copy link

I am having two classes A and B with the following definitions

class A < ApplicationController
  schema(:index) do
    optional(:user_id).value(:string)
  end
  
  def index
    # something
  end
end
class B < A
end
# routes.rb
namespace :api do
  namespace :v1 do
    namespace :public do
      resources :a, only: [:index]
    end

    namespace :enterprise do 
      resources :b, only: [:index]
    end
  end
end

The problem is, B couldn't inherit the schema(:index) defined in the parent controller A, so I have to go with the reuse schema approach. It is even trickier because our ApplicationController has a callback to verify input and exits early

before_action do
  if safe_params && safe_params.failure?
    raise InvalidParams, safe_params.errors(full: true).messages.join(', ')
  end
end

In order to get the dry-schema object from the parent class, this PR checks if the parent class is including the SafeParams module, it then merges the parent schemas to resolve the safe_params for the action.

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