Permalink
Browse files

Initial commit. Highly messy, don't use it ;)

  • Loading branch information...
0 parents commit c3ef6072f6eeb92cdd5c9e55f85d58c3d124a0a9 Jakub Stastny aka Botanicus committed Mar 26, 2010
@@ -0,0 +1,10 @@
+*~
+.*.swp
+.#*
+
+.DS_Store
+.cache
+.yardoc
+
+/*.gem
+.bundle
@@ -0,0 +1,2 @@
+= Version 0.0.1
+ * Initial import
@@ -0,0 +1,8 @@
+# encoding: utf-8
+
+# gem "nokogiri"
+gem "validatable"
+
+gem "nake"
+gem "rspec"
+gem "code-cleaner"
20 LICENSE
@@ -0,0 +1,20 @@
+Copyright (c) 2010 Jakub Stastny aka Botanicus & Pavel Kunc
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,33 @@
+h1. About
+
+h2. Presenter Pattern
+
+h2. Defining Forms
+
+h2. Saving Forms
+
+h2. Validations
+
+h2. Renderers
+
+h2. Custom Fields
+
+h2. Ideas
+
+JS validation plugin
+
+h1. Installation
+
+* Prerelease: @gem install form --development --prerelease@
+* Stable version: @gem install form --development@
+
+h1. Usage
+
+# TODO: write how to use your library
+
+h1. Links
+
+* "Source Code":http://github.com/botanicus/form
+* "Wiki":http://wiki.github.com/botanicus/form
+* "API Docs":http://rdoc.info/projects/botanicus/form
+* "Bug reporting":http://github.com/botanicus/form/issues
@@ -0,0 +1,3 @@
+- code & specs
+- release 0.0.1
+- rubyflow, twitter
@@ -0,0 +1,5 @@
+#!/usr/bin/env rip install
+
+# Syntax:
+# repository [tag or commit to install]
+# git://github.com/botanicus/rubyexts.git
No changes.
@@ -0,0 +1,6 @@
+%form{action: url(:post)}
+ %label{for: "input-title"}
+ - if form.post.errors.on(:title)
+ %input#input-title.errors{name: "post[title]", value: form.post.title}
+ - else
+ %input#input-title{name: "post[title]", value: form.post.title}
@@ -0,0 +1,10 @@
+# encoding: utf-8
+
+$LOAD_PATH.unshift File.expand_path("../lib", __FILE__)
+
+require "formidable"
+
+class BasicForm < Formidable::Form
+ text_field(:name)
+ submit("Save")
+end
@@ -0,0 +1,69 @@
+# encoding: utf-8
+
+# name=name
+# SimpleForm.new(name: "Pavel")
+# SimpleForm.new(user: )
+
+# text_field, text_area -> value = data[key]
+# data == user: {}, tags: [{name: "Ruby"}]
+# form = PostForm.new(@post.values, Author.all)
+class PostForm < Formidable::Form
+ namespace :post
+ def initialize(data, authors)
+ super(data)
+ @authors = authors
+ end
+
+ text_field(:title)
+ .validate_presence.
+ text_field(:title) do
+ validate_presence
+ end
+
+ check_box(:published)
+ text_area
+
+ group(:tydenni_menu) do
+ check_box :svickova
+ check_box :knedlo_zelo
+ validate do
+ self.fields.inject(0) do |sum, check_box|
+ sum += 1 if check_box.selected?
+ sum
+ end
+ end
+ end
+
+ select(:author) do
+ @authors.each do |author|
+ option selected: (author == data[:author])
+ end
+ end
+
+ submit(value: "Save")
+ button(type: "submit") { "Save" }
+
+ field(:password, id: "sdf") do
+ validate_length (0..10)
+ validate do
+ form.password_confirmation == value
+ end
+ end
+
+ field(:password_confirmation) do
+ validate { form.password == value }
+ end
+
+ fields_for(:author) do
+ field()
+ end
+
+ # <fieldset>
+ # <legend>User</legend>
+ # </fieldset>
+ fieldset("User") do
+ field(:name)
+ end
+
+ field(:tags, id: -> { |tag| "tag-#{tag.id}"}, array: true)
+end
@@ -0,0 +1,41 @@
+#!/usr/bin/env gem build
+# encoding: utf-8
+
+# Run ./form.gemspec or gem build form.gemspec
+# NOTE: we can't use require_relative because when we run gem build, it use eval for executing this file
+require File.expand_path("../lib/formidable/version", __FILE__)
+require "base64"
+
+Gem::Specification.new do |s|
+ s.name = "formidable"
+ s.version = Formidable::VERSION
+ s.authors = ["Jakub Stastny aka Botanicus", "Pavel Kunc"]
+ s.homepage = "http://github.com/botanicus/formidable"
+ s.summary = "" # TODO: summary
+ s.description = "" # TODO: long description
+ s.cert_chain = nil
+ s.email = Base64.decode64() # TODO: your encrypted e-mail
+ s.has_rdoc = true
+
+ # files
+ s.files = `git ls-files`.split("\n")
+
+ s.require_paths = ["lib"]
+
+ # Ruby version
+ s.required_ruby_version = ::Gem::Requirement.new(">= 1.9")
+
+ # runtime dependencies
+ s.add_dependency "validatable"
+
+ begin
+ require "changelog"
+ rescue LoadError
+ warn "You have to have changelog gem installed for post install message"
+ else
+ s.post_install_message = CHANGELOG.new.version_changes
+ end
+
+ # RubyForge
+ s.rubyforge_project = "formidable"
+end
@@ -0,0 +1,8 @@
+#!/usr/bin/env gem build
+# encoding: utf-8
+
+# You might think this is a terrible mess and guess what, you're
+# right mate! However say thanks to authors of RubyGems, not me.
+eval(File.read("form.gemspec")).tap do |specification|
+ specification.version = "#{specification.version}.pre"
+end
@@ -0,0 +1,80 @@
+# encoding: utf-8
+
+require "formidable/elements"
+require "formidable/validations"
+require "formidable/renderers/string"
+
+module Formidable
+ class Form
+ include Validations
+ Renderers::String.register(self) do |form|
+ buffer = String.new
+ buffer << "<form>"
+ form.elements.each do |element|
+ buffer += element.render
+ end
+ buffer += "</form>"
+ buffer
+
+ # form.attributes[:method]
+ # if ["GET", "POST"].include?(method)
+ # self["method"] = method
+ # elsif ["PUT", "DELETE"].include?(method)
+ # hidden_field(:_method, value: method)
+ # else
+ # raise ArgumentError, "Method can be GET, POST, PUT or DELETE, but not #{method}"
+ # end
+ end
+
+ def self.elements
+ @elements ||= Array.new
+ end
+
+ def initialize(data, options = Hash.new)
+ @data, @options = data, options
+ set_method(options[:method]) if options[:method]
+ end
+
+ def elements
+ self.class.elements
+ end
+
+ def group
+ OpenStruct.new
+ end
+
+ def validate
+ self.fields.inject(self.errors) do |errors, field|
+ unless field.valid?
+ errors[field.name] = field.errors
+ end
+ end
+ end
+
+ def save(*args)
+ raise NotImplementedError, "You have to redefine this method in subclasses!"
+ end
+ alias_method :save!, :save
+
+ def update(*args)
+ raise NotImplementedError, "You have to redefine this method in subclasses!"
+ end
+ alias_method :update!, :update
+
+ attr_writer :renderer
+ def renderer
+ @renderer ||= begin
+ ancestor = self.class.ancestors.find do |ancestor|
+ if ancestor.is_a?(Class)
+ Renderers::String.renderers.has_key?(ancestor)
+ end
+ end
+ Renderers::String.renderers[ancestor]
+ end
+ end
+
+ def render
+ renderer.call(self)
+ end
+ end
+end
Oops, something went wrong.

0 comments on commit c3ef607

Please sign in to comment.