Code Monkey home page Code Monkey logo

Comments (2)

stevepolitodesign avatar stevepolitodesign commented on June 15, 2024

@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.

jho406 avatar jho406 commented on June 15, 2024

Closing this as https://github.com/thoughtbot/form_props has been created! 🥳

from superglue.

Related Issues (9)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.