Giter VIP home page Giter VIP logo

custom_form_builder's Introduction

Custom Form Builder

The idea is to create a custom form builder, which always adds a label to a field. Then you don’t have to add the label specifically. This is taken from Chad Fowler, Rails Recipes, 2012. (Chapter 32)

The interesting bit is the following metaprogramming class, which is homed in the app/helpers directory.

class LabeledFormBuilder < ActionView::Helpers::FormBuilder

(field_helpers -
  %w(check_box radio_button hidden_field label)).each do |selector|
    src = <<-END_SRC
      def #{selector}(field, options = {})
        @template.content_tag("p", label(field) + ": " + super,
         :class => @template.cycle("", "alt-row"))
      end
    END_SRC
    class_eval src, __FILE__, __LINE__
end

end

and then do something like .alt-row { background: #fab444; }

The inner loop generates some code, which is then class_eval’d to incorporate it into the FormBuilder as new code. That is, to create new definitions of things like check_box and label.

A number of questions arise about this:

1) What is the dash symbol doing in the line with field_helpers? 2) What do __FILE__ and __LINE__ do? 3) what is the super token doing here?

One answer to the first question: The dash removes from the array. But, if we are defining these, why would we want them removed from ‘field_helpers’?

__FILE__ is current relative path __LINE__ is the current line These last 2 items are added to be able to provide correct file name and correct line number.

Fowler also notes that you can do an initializer, config/initializers/ custom_form_builder.rb:

require ‘labeled_form_builder’ ActionView::Base.default_form_builder = LabeledFormBuilder

to always get it. All form_for calls will then get this one.

custom_form_builder's People

Contributors

sploiber avatar

Watchers

 avatar  avatar

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.