Arguments extractor for Rails actions.
MIT License
Arguments extractor for Rails actions.
Add this line to your Rails application's Gemfile:
gem 'action_extractor'
And then execute:
bundle install
class ArticlesController < ApplicationController
extract(
article_id: {
from: :path,
schema: {
type: 'integer',
},
},
body: {
from: :form_data,
schema: {
type: 'string',
},
},
request_id: {
from: :header,
name: 'X-Request-Id',
schema: {
format: 'uuid',
type: 'string',
},
},
title: {
from: :form_data,
schema: {
type: 'string',
},
},
).on \
def update(
article_id:,
body:,
request_id:,
title:
)
article = Article.find(article_id)
if article.update(
body: body,
title: title,
request_id: request_id,
)
redirect_to article
else
@article = article
render :edit, status: 400
end
end
end
extract(...).on(action_name)
By using this DSL method, we can state what kind of input value the subsequent action is expecting.
Note that the argument names passed here will also be used at the action call.
:from
This value represents where the data is comming from.
Supported values are:
:form_data
:header
:path
:query
:name
This value represents what name of value to extract from the data source.
In many cases, this option is not necessary because the argument name is used as the key for extraction instead. This is useful for the case when the names overlap or when extracting from data source with different naming convention (e.g. HTTP headers).
:schema
It's currently unfinished, but we intend to use this value for the following purposes:
This gem was inspired by the following libraries:
The gem is available as open source under the terms of the MIT License.