Comments (2)
@jho406 here's where I left off:
require "active_support/core_ext/string"
require "rspec"
# TODO: form_with(model: nil, scope: nil, url: nil, format: nil, **options, &block)
def form_props(url: nil, scope: nil, **options)
builder = MyFormBuilder.new(url: url, scope: scope, **options)
yield builder
builder.to_h
end
class MyFormBuilder
attr_reader :scope
def initialize(url: nil, scope: nil, **options)
@scope = scope
@output = {
accept_charset: "UTF-8",
action: url || "/",
method: options [:method] || "post",
elements: {}
}
end
def text_field(method, options = {})
@output[:elements][scope ? "#{scope}_#{method}".to_sym : method.to_s.to_sym] = {
type: "text",
label: method.to_s.humanize,
name: scope ? "#{scope}[#{method}]" : method.to_s
}.merge(options)
end
def email_field(method, options = {})
@output[:elements][scope ? "#{scope}_#{method}".to_sym : method.to_s.to_sym] = {
type: "email",
label: method.to_s.humanize,
name: scope ? "#{scope}[#{method}]" : method.to_s
}.merge(options)
end
def to_h
@output
end
end
RSpec.describe "form props" do
it "builds default attributes for the form" do
props = form_props {}
expect(props).to eq(
accept_charset: "UTF-8",
action: "/",
method: "post",
elements: {}
)
end
it "sets attributes for the form" do
props = form_props(url: "/some_url", method: "get") {}
expect(props).to eq(
accept_charset: "UTF-8",
action: "/some_url",
method: "get",
elements: {}
)
end
it "builds attributes for the form elements" do
props = form_props do |my_form_builder|
my_form_builder.text_field :first_name, min: 50
my_form_builder.email_field :email, required: true, label: "Email address"
end
expect(props[:elements]).to eq(
first_name: {
type: "text",
min: 50,
label: "First name",
name: "first_name"
},
email: {
type: "email",
required: true,
label: "Email address",
name: "email"
}
)
end
it "scopes form elements" do
props = form_props(scope: "user") do |my_form_builder|
my_form_builder.text_field :first_name
my_form_builder.email_field :email
end
expect(props[:elements]).to eq(
user_first_name: {
type: "text",
label: "First name",
name: "user[first_name]"
},
user_email: {
type: "email",
label: "Email",
name: "user[email]"
}
)
end
end
from superglue.
Closing this as https://github.com/thoughtbot/form_props has been created! 🥳
from superglue.
Related Issues (9)
- Demo app is unavailable HOT 1
- bz-remote doesn't generate a payload with a form GET HOT 1
- Add a `--webpack` option to the `rails g breezy:install` and `rails g breezy:view` generators.
- Hide the `?_breezy_filter=key.path.to.node` API.
- Convert to ES6
- Add support for multifetch in BreezyTemplates
- Add support for lazy blocks that return a value instead of a new context.
- Make `Breezy.visit` accept a payload.
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from superglue.