funky_form 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +9 -0
- data/Gemfile +10 -0
- data/README.rdoc +62 -0
- data/Rakefile +13 -0
- data/funky_form.gemspec +23 -0
- data/lib/funky_form/class_methods.rb +21 -0
- data/lib/funky_form/instance_methods.rb +16 -0
- data/lib/funky_form/version.rb +3 -0
- data/lib/funky_form.rb +21 -0
- data/test/dummy/Rakefile +7 -0
- data/test/dummy/app/controllers/application_controller.rb +3 -0
- data/test/dummy/app/controllers/posts_controller.rb +41 -0
- data/test/dummy/app/forms/post_form.rb +12 -0
- data/test/dummy/app/helpers/application_helper.rb +2 -0
- data/test/dummy/app/models/post.rb +2 -0
- data/test/dummy/app/views/layouts/application.html.erb +19 -0
- data/test/dummy/app/views/orders/new.html.erb +10 -0
- data/test/dummy/app/views/posts/_form.html.erb +13 -0
- data/test/dummy/app/views/posts/edit.html.erb +3 -0
- data/test/dummy/app/views/posts/index.html.erb +1 -0
- data/test/dummy/app/views/posts/new.html.erb +3 -0
- data/test/dummy/config/application.rb +45 -0
- data/test/dummy/config/boot.rb +10 -0
- data/test/dummy/config/database.yml +22 -0
- data/test/dummy/config/environment.rb +5 -0
- data/test/dummy/config/environments/development.rb +26 -0
- data/test/dummy/config/environments/production.rb +49 -0
- data/test/dummy/config/environments/test.rb +35 -0
- data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/test/dummy/config/initializers/inflections.rb +10 -0
- data/test/dummy/config/initializers/mime_types.rb +5 -0
- data/test/dummy/config/initializers/secret_token.rb +7 -0
- data/test/dummy/config/initializers/session_store.rb +8 -0
- data/test/dummy/config/locales/en.yml +5 -0
- data/test/dummy/config/routes.rb +3 -0
- data/test/dummy/config.ru +4 -0
- data/test/dummy/db/development.sqlite3 +0 -0
- data/test/dummy/db/migrate/20120306162814_create_posts.rb +12 -0
- data/test/dummy/db/schema.rb +25 -0
- data/test/dummy/public/404.html +26 -0
- data/test/dummy/public/422.html +26 -0
- data/test/dummy/public/500.html +26 -0
- data/test/dummy/public/favicon.ico +0 -0
- data/test/dummy/public/stylesheets/.gitkeep +0 -0
- data/test/dummy/script/rails +6 -0
- data/test/funky_form_test.rb +95 -0
- data/test/integration/posts_test.rb +60 -0
- data/test/integration_test_helper.rb +19 -0
- data/test/test_helper.rb +17 -0
- metadata +132 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.rdoc
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
= FunkyForm
|
2
|
+
The purpose of form objects is to take user-entered data and perform work on it.
|
3
|
+
|
4
|
+
== Installation
|
5
|
+
Add to your Gemfile
|
6
|
+
gem "funky_form"
|
7
|
+
|
8
|
+
== Examples
|
9
|
+
|
10
|
+
==== app/models/post.rb
|
11
|
+
class Post < ActiveRecord::Base
|
12
|
+
end
|
13
|
+
|
14
|
+
==== app/forms/post_form.rb
|
15
|
+
class PostForm
|
16
|
+
include FunkyForm
|
17
|
+
|
18
|
+
model Post
|
19
|
+
|
20
|
+
attribute :id, Integer
|
21
|
+
attribute :title, String
|
22
|
+
attribute :body, String
|
23
|
+
|
24
|
+
validates :title, :presence => true, :length => {:maximum => 30}
|
25
|
+
validates :body, :presence => true, :length => {:within => 10..30}
|
26
|
+
end
|
27
|
+
|
28
|
+
==== app/controllers/posts_controller.rb
|
29
|
+
class PostsController < ApplicationController
|
30
|
+
def new
|
31
|
+
@post_form = PostForm.new
|
32
|
+
end
|
33
|
+
|
34
|
+
def create
|
35
|
+
@post_form = PostForm.new(params[:post])
|
36
|
+
|
37
|
+
if @post_form.valid?
|
38
|
+
Post.create(@post_form.attributes)
|
39
|
+
flash[:notice] = "Successfully created"
|
40
|
+
redirect_to :posts
|
41
|
+
else
|
42
|
+
flash[:alert] = "Validation errors"
|
43
|
+
render "new"
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
==== app/views/posts/new.html.erb
|
49
|
+
<h1>New Post</h1>
|
50
|
+
<%= form_for @post_form do |f| %>
|
51
|
+
<p>
|
52
|
+
<%= f.label :title %>
|
53
|
+
<%= f.text_field :title %>
|
54
|
+
</p>
|
55
|
+
|
56
|
+
<p>
|
57
|
+
<%= f.label :body %>
|
58
|
+
<%= f.text_area :body %>
|
59
|
+
</p>
|
60
|
+
|
61
|
+
<p><%= f.submit %></p>
|
62
|
+
<% end %>
|
data/Rakefile
ADDED
data/funky_form.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "funky_form/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "funky_form"
|
7
|
+
s.version = FunkyForm::VERSION
|
8
|
+
s.authors = ["Indrek Juhkam"]
|
9
|
+
s.email = ["indrek@urgas.eu"]
|
10
|
+
s.homepage = ""
|
11
|
+
s.summary = %q{Simple form objects in ruby}
|
12
|
+
s.description = %q{}
|
13
|
+
|
14
|
+
s.rubyforge_project = "funky_form"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
s.add_dependency "virtus", "~> 0.5.4"
|
22
|
+
s.add_dependency "activemodel", "~> 3.2.0"
|
23
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module FunkyForm
|
2
|
+
module ClassMethods
|
3
|
+
def model_name(model_name = nil)
|
4
|
+
if model_name
|
5
|
+
@model_name = model_name
|
6
|
+
else
|
7
|
+
@model_name ||= super()
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
private
|
12
|
+
|
13
|
+
def model(klass = nil)
|
14
|
+
if klass.respond_to?(:model_name)
|
15
|
+
model_name klass.model_name
|
16
|
+
elsif klass.is_a?(String)
|
17
|
+
model_name ActiveModel::Name.new(self, nil, klass)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module FunkyForm
|
2
|
+
module InstanceMethods
|
3
|
+
# @param [#to_hash, #attributes] target
|
4
|
+
def initialize(target = {})
|
5
|
+
if target.respond_to?(:attributes)
|
6
|
+
super(target.attributes)
|
7
|
+
else
|
8
|
+
super
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def persisted?
|
13
|
+
defined?(id) && !id.nil?
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
data/lib/funky_form.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require "virtus"
|
2
|
+
require "active_model"
|
3
|
+
|
4
|
+
module FunkyForm
|
5
|
+
# Extends class with FunkyForm methods
|
6
|
+
#
|
7
|
+
# @param [Class] descendant
|
8
|
+
def self.included(descendant)
|
9
|
+
super
|
10
|
+
descendant.send(:include, Virtus)
|
11
|
+
descendant.send(:include, ActiveModel::Validations)
|
12
|
+
descendant.send(:include, ActiveModel::Conversion)
|
13
|
+
descendant.send(:include, InstanceMethods)
|
14
|
+
descendant.extend(ClassMethods)
|
15
|
+
end
|
16
|
+
private_class_method :included
|
17
|
+
end
|
18
|
+
|
19
|
+
require_relative "funky_form/version"
|
20
|
+
require_relative "funky_form/class_methods"
|
21
|
+
require_relative "funky_form/instance_methods"
|
data/test/dummy/Rakefile
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
# Add your own tasks in files placed in lib/tasks ending in .rake,
|
2
|
+
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
|
3
|
+
|
4
|
+
require File.expand_path('../config/application', __FILE__)
|
5
|
+
require 'rake'
|
6
|
+
|
7
|
+
Dummy::Application.load_tasks
|
@@ -0,0 +1,41 @@
|
|
1
|
+
class PostsController < ApplicationController
|
2
|
+
def index
|
3
|
+
@posts = Post.all
|
4
|
+
end
|
5
|
+
|
6
|
+
def new
|
7
|
+
@post_form = PostForm.new
|
8
|
+
end
|
9
|
+
|
10
|
+
def create
|
11
|
+
@post_form = PostForm.new(params[:post])
|
12
|
+
|
13
|
+
if @post_form.valid?
|
14
|
+
Post.create(@post_form.attributes)
|
15
|
+
flash[:notice] = "Successfully created"
|
16
|
+
redirect_to :posts
|
17
|
+
else
|
18
|
+
flash[:alert] = "Validation errors"
|
19
|
+
render "new"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def edit
|
24
|
+
@post = Post.find(params[:id])
|
25
|
+
@post_form = PostForm.new(@post)
|
26
|
+
end
|
27
|
+
|
28
|
+
def update
|
29
|
+
@post = Post.find(params[:id])
|
30
|
+
@post_form = PostForm.new(params[:post])
|
31
|
+
|
32
|
+
if @post_form.valid?
|
33
|
+
@post.update_attributes(@post_form.attributes)
|
34
|
+
flash[:notice] = "Successfully updated"
|
35
|
+
redirect_to :posts
|
36
|
+
else
|
37
|
+
flash[:alert] = "Validation errors"
|
38
|
+
render "edit"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
class PostForm
|
2
|
+
include FunkyForm
|
3
|
+
|
4
|
+
model Post
|
5
|
+
|
6
|
+
attribute :id, Integer
|
7
|
+
attribute :title, String
|
8
|
+
attribute :body, String
|
9
|
+
|
10
|
+
validates :title, :presence => true, :length => {:maximum => 30}
|
11
|
+
validates :body, :presence => true, :length => {:within => 10..30}
|
12
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
<html>
|
3
|
+
<head>
|
4
|
+
<title>Dummy</title>
|
5
|
+
<%= stylesheet_link_tag :all %>
|
6
|
+
<%= javascript_include_tag :defaults %>
|
7
|
+
<%= csrf_meta_tag %>
|
8
|
+
</head>
|
9
|
+
<body>
|
10
|
+
<header>
|
11
|
+
<%= notice %>
|
12
|
+
<%= alert %>
|
13
|
+
</div>
|
14
|
+
|
15
|
+
<article>
|
16
|
+
<%= yield %>
|
17
|
+
</article>
|
18
|
+
</body>
|
19
|
+
</html>
|
@@ -0,0 +1 @@
|
|
1
|
+
This is the default view
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require File.expand_path('../boot', __FILE__)
|
2
|
+
|
3
|
+
require "active_model/railtie"
|
4
|
+
require "active_record/railtie"
|
5
|
+
require "action_controller/railtie"
|
6
|
+
require "action_view/railtie"
|
7
|
+
require "action_mailer/railtie"
|
8
|
+
|
9
|
+
Bundler.require
|
10
|
+
require "funky_form"
|
11
|
+
|
12
|
+
module Dummy
|
13
|
+
class Application < Rails::Application
|
14
|
+
# Settings in config/environments/* take precedence over those specified here.
|
15
|
+
# Application configuration should go into files in config/initializers
|
16
|
+
# -- all .rb files in that directory are automatically loaded.
|
17
|
+
|
18
|
+
# Custom directories with classes and modules you want to be autoloadable.
|
19
|
+
config.autoload_paths += %W(#{config.root}/lib)
|
20
|
+
|
21
|
+
# Only load the plugins named here, in the order given (default is alphabetical).
|
22
|
+
# :all can be used as a placeholder for all plugins not explicitly named.
|
23
|
+
# config.plugins = [ :exception_notification, :ssl_requirement, :all ]
|
24
|
+
|
25
|
+
# Activate observers that should always be running.
|
26
|
+
# config.active_record.observers = :cacher, :garbage_collector, :forum_observer
|
27
|
+
|
28
|
+
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
|
29
|
+
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
|
30
|
+
# config.time_zone = 'Central Time (US & Canada)'
|
31
|
+
|
32
|
+
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
|
33
|
+
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
|
34
|
+
# config.i18n.default_locale = :de
|
35
|
+
|
36
|
+
# JavaScript files you want as :defaults (application.js is always included).
|
37
|
+
# config.action_view.javascript_expansions[:defaults] = %w(jquery rails)
|
38
|
+
|
39
|
+
# Configure the default encoding used in templates for Ruby 1.9.
|
40
|
+
config.encoding = "utf-8"
|
41
|
+
|
42
|
+
# Configure sensitive parameters which will be filtered from the log file.
|
43
|
+
config.filter_parameters += [:password]
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# SQLite version 3.x
|
2
|
+
# gem install sqlite3
|
3
|
+
development:
|
4
|
+
adapter: sqlite3
|
5
|
+
database: db/development.sqlite3
|
6
|
+
pool: 5
|
7
|
+
timeout: 5000
|
8
|
+
|
9
|
+
# Warning: The database defined as "test" will be erased and
|
10
|
+
# re-generated from your development database when you run "rake".
|
11
|
+
# Do not set this db to the same as development or production.
|
12
|
+
test:
|
13
|
+
adapter: sqlite3
|
14
|
+
database: db/test.sqlite3
|
15
|
+
pool: 5
|
16
|
+
timeout: 5000
|
17
|
+
|
18
|
+
production:
|
19
|
+
adapter: sqlite3
|
20
|
+
database: db/production.sqlite3
|
21
|
+
pool: 5
|
22
|
+
timeout: 5000
|
@@ -0,0 +1,26 @@
|
|
1
|
+
Dummy::Application.configure do
|
2
|
+
# Settings specified here will take precedence over those in config/application.rb
|
3
|
+
|
4
|
+
# In the development environment your application's code is reloaded on
|
5
|
+
# every request. This slows down response time but is perfect for development
|
6
|
+
# since you don't have to restart the webserver when you make code changes.
|
7
|
+
config.cache_classes = false
|
8
|
+
|
9
|
+
# Log error messages when you accidentally call methods on nil.
|
10
|
+
config.whiny_nils = true
|
11
|
+
|
12
|
+
# Show full error reports and disable caching
|
13
|
+
config.consider_all_requests_local = true
|
14
|
+
config.action_view.debug_rjs = true
|
15
|
+
config.action_controller.perform_caching = false
|
16
|
+
|
17
|
+
# Don't care if the mailer can't send
|
18
|
+
config.action_mailer.raise_delivery_errors = false
|
19
|
+
|
20
|
+
# Print deprecation notices to the Rails logger
|
21
|
+
config.active_support.deprecation = :log
|
22
|
+
|
23
|
+
# Only use best-standards-support built into browsers
|
24
|
+
config.action_dispatch.best_standards_support = :builtin
|
25
|
+
end
|
26
|
+
|
@@ -0,0 +1,49 @@
|
|
1
|
+
Dummy::Application.configure do
|
2
|
+
# Settings specified here will take precedence over those in config/application.rb
|
3
|
+
|
4
|
+
# The production environment is meant for finished, "live" apps.
|
5
|
+
# Code is not reloaded between requests
|
6
|
+
config.cache_classes = true
|
7
|
+
|
8
|
+
# Full error reports are disabled and caching is turned on
|
9
|
+
config.consider_all_requests_local = false
|
10
|
+
config.action_controller.perform_caching = true
|
11
|
+
|
12
|
+
# Specifies the header that your server uses for sending files
|
13
|
+
config.action_dispatch.x_sendfile_header = "X-Sendfile"
|
14
|
+
|
15
|
+
# For nginx:
|
16
|
+
# config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect'
|
17
|
+
|
18
|
+
# If you have no front-end server that supports something like X-Sendfile,
|
19
|
+
# just comment this out and Rails will serve the files
|
20
|
+
|
21
|
+
# See everything in the log (default is :info)
|
22
|
+
# config.log_level = :debug
|
23
|
+
|
24
|
+
# Use a different logger for distributed setups
|
25
|
+
# config.logger = SyslogLogger.new
|
26
|
+
|
27
|
+
# Use a different cache store in production
|
28
|
+
# config.cache_store = :mem_cache_store
|
29
|
+
|
30
|
+
# Disable Rails's static asset server
|
31
|
+
# In production, Apache or nginx will already do this
|
32
|
+
config.serve_static_assets = false
|
33
|
+
|
34
|
+
# Enable serving of images, stylesheets, and javascripts from an asset server
|
35
|
+
# config.action_controller.asset_host = "http://assets.example.com"
|
36
|
+
|
37
|
+
# Disable delivery errors, bad email addresses will be ignored
|
38
|
+
# config.action_mailer.raise_delivery_errors = false
|
39
|
+
|
40
|
+
# Enable threaded mode
|
41
|
+
# config.threadsafe!
|
42
|
+
|
43
|
+
# Enable locale fallbacks for I18n (makes lookups for any locale fall back to
|
44
|
+
# the I18n.default_locale when a translation can not be found)
|
45
|
+
config.i18n.fallbacks = true
|
46
|
+
|
47
|
+
# Send deprecation notices to registered listeners
|
48
|
+
config.active_support.deprecation = :notify
|
49
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
Dummy::Application.configure do
|
2
|
+
# Settings specified here will take precedence over those in config/application.rb
|
3
|
+
|
4
|
+
# The test environment is used exclusively to run your application's
|
5
|
+
# test suite. You never need to work with it otherwise. Remember that
|
6
|
+
# your test database is "scratch space" for the test suite and is wiped
|
7
|
+
# and recreated between test runs. Don't rely on the data there!
|
8
|
+
config.cache_classes = true
|
9
|
+
|
10
|
+
# Log error messages when you accidentally call methods on nil.
|
11
|
+
config.whiny_nils = true
|
12
|
+
|
13
|
+
# Show full error reports and disable caching
|
14
|
+
config.consider_all_requests_local = true
|
15
|
+
config.action_controller.perform_caching = false
|
16
|
+
|
17
|
+
# Raise exceptions instead of rendering exception templates
|
18
|
+
config.action_dispatch.show_exceptions = false
|
19
|
+
|
20
|
+
# Disable request forgery protection in test environment
|
21
|
+
config.action_controller.allow_forgery_protection = false
|
22
|
+
|
23
|
+
# Tell Action Mailer not to deliver emails to the real world.
|
24
|
+
# The :test delivery method accumulates sent emails in the
|
25
|
+
# ActionMailer::Base.deliveries array.
|
26
|
+
config.action_mailer.delivery_method = :test
|
27
|
+
|
28
|
+
# Use SQL instead of Active Record's schema dumper when creating the test database.
|
29
|
+
# This is necessary if your schema can't be completely dumped by the schema dumper,
|
30
|
+
# like if you have constraints or database-specific column types
|
31
|
+
# config.active_record.schema_format = :sql
|
32
|
+
|
33
|
+
# Print deprecation notices to the stderr
|
34
|
+
config.active_support.deprecation = :stderr
|
35
|
+
end
|
@@ -0,0 +1,7 @@
|
|
1
|
+
# Be sure to restart your server when you modify this file.
|
2
|
+
|
3
|
+
# You can add backtrace silencers for libraries that you're using but don't wish to see in your backtraces.
|
4
|
+
# Rails.backtrace_cleaner.add_silencer { |line| line =~ /my_noisy_library/ }
|
5
|
+
|
6
|
+
# You can also remove all the silencers if you're trying to debug a problem that might stem from framework code.
|
7
|
+
# Rails.backtrace_cleaner.remove_silencers!
|
@@ -0,0 +1,10 @@
|
|
1
|
+
# Be sure to restart your server when you modify this file.
|
2
|
+
|
3
|
+
# Add new inflection rules using the following format
|
4
|
+
# (all these examples are active by default):
|
5
|
+
# ActiveSupport::Inflector.inflections do |inflect|
|
6
|
+
# inflect.plural /^(ox)$/i, '\1en'
|
7
|
+
# inflect.singular /^(ox)en/i, '\1'
|
8
|
+
# inflect.irregular 'person', 'people'
|
9
|
+
# inflect.uncountable %w( fish sheep )
|
10
|
+
# end
|
@@ -0,0 +1,7 @@
|
|
1
|
+
# Be sure to restart your server when you modify this file.
|
2
|
+
|
3
|
+
# Your secret key for verifying the integrity of signed cookies.
|
4
|
+
# If you change this key, all old signed cookies will become invalid!
|
5
|
+
# Make sure the secret is at least 30 characters and all random,
|
6
|
+
# no regular words or you'll be exposed to dictionary attacks.
|
7
|
+
Dummy::Application.config.secret_token = '55b15712589fe9aed29ca8b8eba74b1a831ecbf34b810da7c7b6153d8e7ca8e5fe85c05ad6e42d484cf0eff9fb6b511573b85337177968b3855f6de527c544a5'
|
@@ -0,0 +1,8 @@
|
|
1
|
+
# Be sure to restart your server when you modify this file.
|
2
|
+
|
3
|
+
Dummy::Application.config.session_store :cookie_store, :key => '_dummy_session'
|
4
|
+
|
5
|
+
# Use the database for sessions instead of the cookie-based default,
|
6
|
+
# which shouldn't be used to store highly confidential information
|
7
|
+
# (create the session table with "rails generate session_migration")
|
8
|
+
# Dummy::Application.config.session_store :active_record_store
|
Binary file
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
# This file is auto-generated from the current state of the database. Instead
|
3
|
+
# of editing this file, please use the migrations feature of Active Record to
|
4
|
+
# incrementally modify your database, and then regenerate this schema definition.
|
5
|
+
#
|
6
|
+
# Note that this schema.rb definition is the authoritative source for your
|
7
|
+
# database schema. If you need to create the application database on another
|
8
|
+
# system, you should be using db:schema:load, not running all the migrations
|
9
|
+
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
|
10
|
+
# you'll amass, the slower it'll run and the greater likelihood for issues).
|
11
|
+
#
|
12
|
+
# It's strongly recommended to check this file into your version control system.
|
13
|
+
|
14
|
+
ActiveRecord::Schema.define(:version => 20120306162814) do
|
15
|
+
|
16
|
+
create_table "posts", :force => true do |t|
|
17
|
+
t.integer "author_id"
|
18
|
+
t.string "title"
|
19
|
+
t.text "body"
|
20
|
+
t.integer "priority"
|
21
|
+
t.datetime "created_at", :null => false
|
22
|
+
t.datetime "updated_at", :null => false
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
<html>
|
3
|
+
<head>
|
4
|
+
<title>The page you were looking for doesn't exist (404)</title>
|
5
|
+
<style type="text/css">
|
6
|
+
body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
|
7
|
+
div.dialog {
|
8
|
+
width: 25em;
|
9
|
+
padding: 0 4em;
|
10
|
+
margin: 4em auto 0 auto;
|
11
|
+
border: 1px solid #ccc;
|
12
|
+
border-right-color: #999;
|
13
|
+
border-bottom-color: #999;
|
14
|
+
}
|
15
|
+
h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
|
16
|
+
</style>
|
17
|
+
</head>
|
18
|
+
|
19
|
+
<body>
|
20
|
+
<!-- This file lives in public/404.html -->
|
21
|
+
<div class="dialog">
|
22
|
+
<h1>The page you were looking for doesn't exist.</h1>
|
23
|
+
<p>You may have mistyped the address or the page may have moved.</p>
|
24
|
+
</div>
|
25
|
+
</body>
|
26
|
+
</html>
|
@@ -0,0 +1,26 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
<html>
|
3
|
+
<head>
|
4
|
+
<title>The change you wanted was rejected (422)</title>
|
5
|
+
<style type="text/css">
|
6
|
+
body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
|
7
|
+
div.dialog {
|
8
|
+
width: 25em;
|
9
|
+
padding: 0 4em;
|
10
|
+
margin: 4em auto 0 auto;
|
11
|
+
border: 1px solid #ccc;
|
12
|
+
border-right-color: #999;
|
13
|
+
border-bottom-color: #999;
|
14
|
+
}
|
15
|
+
h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
|
16
|
+
</style>
|
17
|
+
</head>
|
18
|
+
|
19
|
+
<body>
|
20
|
+
<!-- This file lives in public/422.html -->
|
21
|
+
<div class="dialog">
|
22
|
+
<h1>The change you wanted was rejected.</h1>
|
23
|
+
<p>Maybe you tried to change something you didn't have access to.</p>
|
24
|
+
</div>
|
25
|
+
</body>
|
26
|
+
</html>
|
@@ -0,0 +1,26 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
<html>
|
3
|
+
<head>
|
4
|
+
<title>We're sorry, but something went wrong (500)</title>
|
5
|
+
<style type="text/css">
|
6
|
+
body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
|
7
|
+
div.dialog {
|
8
|
+
width: 25em;
|
9
|
+
padding: 0 4em;
|
10
|
+
margin: 4em auto 0 auto;
|
11
|
+
border: 1px solid #ccc;
|
12
|
+
border-right-color: #999;
|
13
|
+
border-bottom-color: #999;
|
14
|
+
}
|
15
|
+
h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
|
16
|
+
</style>
|
17
|
+
</head>
|
18
|
+
|
19
|
+
<body>
|
20
|
+
<!-- This file lives in public/500.html -->
|
21
|
+
<div class="dialog">
|
22
|
+
<h1>We're sorry, but something went wrong.</h1>
|
23
|
+
<p>We've been notified about this issue and we'll take a look at it shortly.</p>
|
24
|
+
</div>
|
25
|
+
</body>
|
26
|
+
</html>
|
File without changes
|
File without changes
|
@@ -0,0 +1,6 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.
|
3
|
+
|
4
|
+
APP_PATH = File.expand_path('../../config/application', __FILE__)
|
5
|
+
require File.expand_path('../../config/boot', __FILE__)
|
6
|
+
require 'rails/commands'
|
@@ -0,0 +1,95 @@
|
|
1
|
+
require_relative "test_helper"
|
2
|
+
|
3
|
+
class FunkyFormTest < MiniTest::Unit::TestCase
|
4
|
+
parallelize_me!
|
5
|
+
|
6
|
+
def test_values_from_existing_instance
|
7
|
+
job = OpenStruct.new(:to_hash => {:title => "existing title"})
|
8
|
+
|
9
|
+
form = new_funky_form do
|
10
|
+
attribute :title, String
|
11
|
+
end
|
12
|
+
|
13
|
+
assert_equal "existing title", form.new(job).title
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_values_from_existing_instance_when_default_value_is_specified
|
17
|
+
job = OpenStruct.new(:to_hash => {:title => nil})
|
18
|
+
|
19
|
+
form = new_funky_form do
|
20
|
+
attribute :title, String, :default => "developer"
|
21
|
+
end
|
22
|
+
|
23
|
+
assert_equal nil, form.new(job).title
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_values_from_existing_instance_that_responds_to_attributes
|
27
|
+
job = OpenStruct.new(:attributes => {:title => "existing title"})
|
28
|
+
|
29
|
+
form = new_funky_form do
|
30
|
+
attribute :title, String
|
31
|
+
end
|
32
|
+
|
33
|
+
assert_equal "existing title", form.new(job).title
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_default_value
|
37
|
+
form = new_funky_form do
|
38
|
+
attribute :title, String, :default => "developer"
|
39
|
+
end
|
40
|
+
|
41
|
+
assert_equal "developer", form.new.title
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_attributes
|
45
|
+
form = new_funky_form do
|
46
|
+
attribute :title, String
|
47
|
+
attribute :length, Integer
|
48
|
+
end
|
49
|
+
|
50
|
+
f = form.new("title" => "developer", "length" => "112")
|
51
|
+
assert_equal "developer", f.attributes[:title]
|
52
|
+
assert_equal 112, f.attributes[:length]
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_specifing_invalid_attributes
|
56
|
+
form = new_funky_form
|
57
|
+
|
58
|
+
f = form.new(:private => "okou")
|
59
|
+
assert_nil f.attributes[:private]
|
60
|
+
end
|
61
|
+
|
62
|
+
def test_model_with_activemodel_class
|
63
|
+
table_model = Class.new do
|
64
|
+
def self.model_name
|
65
|
+
ActiveModel::Name.new(self, nil, "Table")
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
form_class = new_funky_form do
|
70
|
+
model table_model
|
71
|
+
end
|
72
|
+
|
73
|
+
assert_equal table_model.model_name, form_class.model_name
|
74
|
+
end
|
75
|
+
|
76
|
+
def test_model_with_string
|
77
|
+
form_class = new_funky_form do
|
78
|
+
model "Order"
|
79
|
+
end
|
80
|
+
|
81
|
+
assert_equal "Order", form_class.model_name.to_s
|
82
|
+
end
|
83
|
+
|
84
|
+
def test_validations
|
85
|
+
form = new_funky_form do
|
86
|
+
attribute :title, String
|
87
|
+
validates :title, :presence => true
|
88
|
+
end
|
89
|
+
|
90
|
+
assert form.new(:title => "a title").valid?
|
91
|
+
assert !form.new(:title => nil).valid?, "should be invalid when nil"
|
92
|
+
assert !form.new(:title => "").valid?, "should be invalid when empty"
|
93
|
+
assert !form.new.valid?, "should be invalid when not specified"
|
94
|
+
end
|
95
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require_relative "../integration_test_helper"
|
2
|
+
|
3
|
+
class PostsTest < ActiveSupport::IntegrationCase
|
4
|
+
def test_creating_a_post
|
5
|
+
visit new_post_path
|
6
|
+
|
7
|
+
within "#new_post" do
|
8
|
+
fill_in "Title", :with => "Fruits"
|
9
|
+
fill_in "Body", :with => "Apples and oranges"
|
10
|
+
click_button "Create"
|
11
|
+
end
|
12
|
+
|
13
|
+
assert page.has_content?("Successfully created")
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_creating_a_post_when_validation_errors
|
17
|
+
visit new_post_path
|
18
|
+
|
19
|
+
within "#new_post" do
|
20
|
+
fill_in "Title", :with => "a" * 40
|
21
|
+
fill_in "Body", :with => "Short"
|
22
|
+
click_button "Create"
|
23
|
+
end
|
24
|
+
|
25
|
+
assert page.has_content?("Validation errors")
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_editing_existing_post_title
|
29
|
+
post = Post.create(:title => "Fruits", :body => "Apples and oranges")
|
30
|
+
|
31
|
+
visit edit_post_path(post)
|
32
|
+
|
33
|
+
within "#edit_post_#{post.id}" do
|
34
|
+
assert "Fruits", page.find("#post_title").value
|
35
|
+
fill_in "Title", :with => "My Fruits"
|
36
|
+
|
37
|
+
click_button "Update"
|
38
|
+
end
|
39
|
+
|
40
|
+
assert page.has_content?("Successfully updated")
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_editing_existing_post_title_when_validation_errors
|
44
|
+
post = Post.create(:title => "Fruits", :body => "Apples and oranges")
|
45
|
+
|
46
|
+
visit edit_post_path(post)
|
47
|
+
|
48
|
+
invalid_title = "a" * 40
|
49
|
+
|
50
|
+
within "#edit_post_#{post.id}" do
|
51
|
+
assert "Fruits", page.find("#post_title").value
|
52
|
+
fill_in "Title", :with => invalid_title
|
53
|
+
|
54
|
+
click_button "Update"
|
55
|
+
end
|
56
|
+
|
57
|
+
assert page.has_content?("Validation errors")
|
58
|
+
assert invalid_title, page.find("#post_title").value
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require_relative "test_helper"
|
2
|
+
|
3
|
+
# Configure Rails Envinronment
|
4
|
+
ENV["RAILS_ENV"] = "test"
|
5
|
+
|
6
|
+
require File.expand_path("../dummy/config/environment.rb", __FILE__)
|
7
|
+
require "rails/test_help"
|
8
|
+
|
9
|
+
Rails.backtrace_cleaner.remove_silencers!
|
10
|
+
|
11
|
+
# Configure capybara for integration testing
|
12
|
+
require "capybara/rails"
|
13
|
+
Capybara.default_driver = :rack_test
|
14
|
+
Capybara.default_selector = :css
|
15
|
+
|
16
|
+
class ActiveSupport::IntegrationCase < ActiveSupport::TestCase
|
17
|
+
include Capybara::DSL
|
18
|
+
include Rails.application.routes.url_helpers
|
19
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# Load support files
|
2
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
|
3
|
+
|
4
|
+
require "minitest/autorun"
|
5
|
+
require_relative "../lib/funky_form"
|
6
|
+
|
7
|
+
class MiniTest::Unit::TestCase
|
8
|
+
private
|
9
|
+
|
10
|
+
def new_funky_form(&block)
|
11
|
+
Class.new do
|
12
|
+
include FunkyForm
|
13
|
+
model "Anonymous"
|
14
|
+
instance_eval(&block) if block_given?
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
metadata
ADDED
@@ -0,0 +1,132 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: funky_form
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Indrek Juhkam
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-01-13 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.5.4
|
20
|
+
none: false
|
21
|
+
type: :runtime
|
22
|
+
name: virtus
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ~>
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: 0.5.4
|
28
|
+
none: false
|
29
|
+
prerelease: false
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
requirement: !ruby/object:Gem::Requirement
|
32
|
+
requirements:
|
33
|
+
- - ~>
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: 3.2.0
|
36
|
+
none: false
|
37
|
+
type: :runtime
|
38
|
+
name: activemodel
|
39
|
+
version_requirements: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ~>
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 3.2.0
|
44
|
+
none: false
|
45
|
+
prerelease: false
|
46
|
+
description: ''
|
47
|
+
email:
|
48
|
+
- indrek@urgas.eu
|
49
|
+
executables: []
|
50
|
+
extensions: []
|
51
|
+
extra_rdoc_files: []
|
52
|
+
files:
|
53
|
+
- .gitignore
|
54
|
+
- Gemfile
|
55
|
+
- README.rdoc
|
56
|
+
- Rakefile
|
57
|
+
- funky_form.gemspec
|
58
|
+
- lib/funky_form.rb
|
59
|
+
- lib/funky_form/class_methods.rb
|
60
|
+
- lib/funky_form/instance_methods.rb
|
61
|
+
- lib/funky_form/version.rb
|
62
|
+
- test/dummy/Rakefile
|
63
|
+
- test/dummy/app/controllers/application_controller.rb
|
64
|
+
- test/dummy/app/controllers/posts_controller.rb
|
65
|
+
- test/dummy/app/forms/post_form.rb
|
66
|
+
- test/dummy/app/helpers/application_helper.rb
|
67
|
+
- test/dummy/app/models/post.rb
|
68
|
+
- test/dummy/app/views/layouts/application.html.erb
|
69
|
+
- test/dummy/app/views/orders/new.html.erb
|
70
|
+
- test/dummy/app/views/posts/_form.html.erb
|
71
|
+
- test/dummy/app/views/posts/edit.html.erb
|
72
|
+
- test/dummy/app/views/posts/index.html.erb
|
73
|
+
- test/dummy/app/views/posts/new.html.erb
|
74
|
+
- test/dummy/config.ru
|
75
|
+
- test/dummy/config/application.rb
|
76
|
+
- test/dummy/config/boot.rb
|
77
|
+
- test/dummy/config/database.yml
|
78
|
+
- test/dummy/config/environment.rb
|
79
|
+
- test/dummy/config/environments/development.rb
|
80
|
+
- test/dummy/config/environments/production.rb
|
81
|
+
- test/dummy/config/environments/test.rb
|
82
|
+
- test/dummy/config/initializers/backtrace_silencers.rb
|
83
|
+
- test/dummy/config/initializers/inflections.rb
|
84
|
+
- test/dummy/config/initializers/mime_types.rb
|
85
|
+
- test/dummy/config/initializers/secret_token.rb
|
86
|
+
- test/dummy/config/initializers/session_store.rb
|
87
|
+
- test/dummy/config/locales/en.yml
|
88
|
+
- test/dummy/config/routes.rb
|
89
|
+
- test/dummy/db/development.sqlite3
|
90
|
+
- test/dummy/db/migrate/20120306162814_create_posts.rb
|
91
|
+
- test/dummy/db/schema.rb
|
92
|
+
- test/dummy/public/404.html
|
93
|
+
- test/dummy/public/422.html
|
94
|
+
- test/dummy/public/500.html
|
95
|
+
- test/dummy/public/favicon.ico
|
96
|
+
- test/dummy/public/stylesheets/.gitkeep
|
97
|
+
- test/dummy/script/rails
|
98
|
+
- test/funky_form_test.rb
|
99
|
+
- test/integration/posts_test.rb
|
100
|
+
- test/integration_test_helper.rb
|
101
|
+
- test/test_helper.rb
|
102
|
+
homepage: ''
|
103
|
+
licenses: []
|
104
|
+
post_install_message:
|
105
|
+
rdoc_options: []
|
106
|
+
require_paths:
|
107
|
+
- lib
|
108
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
109
|
+
requirements:
|
110
|
+
- - ! '>='
|
111
|
+
- !ruby/object:Gem::Version
|
112
|
+
hash: -865947392686571946
|
113
|
+
version: '0'
|
114
|
+
segments:
|
115
|
+
- 0
|
116
|
+
none: false
|
117
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
118
|
+
requirements:
|
119
|
+
- - ! '>='
|
120
|
+
- !ruby/object:Gem::Version
|
121
|
+
hash: -865947392686571946
|
122
|
+
version: '0'
|
123
|
+
segments:
|
124
|
+
- 0
|
125
|
+
none: false
|
126
|
+
requirements: []
|
127
|
+
rubyforge_project: funky_form
|
128
|
+
rubygems_version: 1.8.24
|
129
|
+
signing_key:
|
130
|
+
specification_version: 3
|
131
|
+
summary: Simple form objects in ruby
|
132
|
+
test_files: []
|