Skip to content

Instantly share code, notes, and snippets.

@jdwolk
Last active August 29, 2015 14:07
Show Gist options
  • Save jdwolk/3d8d52dbeb411d9cb4c5 to your computer and use it in GitHub Desktop.
Save jdwolk/3d8d52dbeb411d9cb4c5 to your computer and use it in GitHub Desktop.
Pre-pre-pre-pre alpha README for forms

PoFo

Philosophy

Forms have 2 basic data flows:

In-data flow (i.e. writing)

User input -> form object -> model

When writing, forms have 2 basic purposes:

  • Validating user input
  • Normalizing user input

Out-data flow (i.e. displaying)

model -> form object -> display to user

When displaying, forms have 1 purpose:

  • Normalizing persisted input for display to user

FieldTransformers

(separate read / write transformers???)

# For use in create/update actions
class PhoneWriter < PoFo::InTransformer

  # The form object will see this version of the attribute internally.
  # Sets @value

  # User input -> form object.
  def value=(phone)
  ¦ @value = phone.gsub('-', '').gsub(' ', '')
  end

  # form object -> model
  def value
  ¦ # default returns @value
  end
end
# For use in edit action
class PhoneReader < PoFo::OutTransformer

  # form object -> display to user
  def value
  ¦ format_phone_number(@value)
  end

  # model -> form object
  def value=(phone)
  ¦ # defaults to @model.<value>
  end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment