formgen 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/Gemfile +13 -0
- data/Gemfile.lock +20 -0
- data/LICENSE.txt +20 -0
- data/README +40 -0
- data/README.rdoc +58 -0
- data/Rakefile +53 -0
- data/VERSION +1 -0
- data/formgen.gemspec +69 -0
- data/lib/formgen.rb +0 -0
- data/lib/generators/skizmo/form/USAGE +23 -0
- data/lib/generators/skizmo/form/form_generator.rb +99 -0
- data/lib/generators/skizmo/form/templates/_form.html.erb +37 -0
- data/lib/generators/skizmo/form/templates/_nested_fields.html.erb +11 -0
- data/lib/generators/skizmo/form/templates/edit.html.erb +3 -0
- data/lib/generators/skizmo/form/templates/helper.rb +13 -0
- data/lib/generators/skizmo/form/templates/javascript.js +10 -0
- data/lib/generators/skizmo/form/templates/new.html.erb +3 -0
- data/test/helper.rb +18 -0
- data/test/test_formgen.rb +7 -0
- metadata +147 -0
data/.document
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
# Add dependencies required to use your gem here.
|
3
|
+
# Example:
|
4
|
+
# gem "activesupport", ">= 2.3.5"
|
5
|
+
|
6
|
+
# Add dependencies to develop your gem here.
|
7
|
+
# Include everything needed to run rake, tests, features, etc.
|
8
|
+
group :development do
|
9
|
+
gem "shoulda", ">= 0"
|
10
|
+
gem "bundler", "~> 1.0.0"
|
11
|
+
gem "jeweler", "~> 1.6.4"
|
12
|
+
gem "rcov", ">= 0"
|
13
|
+
end
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
git (1.2.5)
|
5
|
+
jeweler (1.6.4)
|
6
|
+
bundler (~> 1.0)
|
7
|
+
git (>= 1.2.5)
|
8
|
+
rake
|
9
|
+
rake (0.9.2.2)
|
10
|
+
rcov (0.9.11)
|
11
|
+
shoulda (2.11.3)
|
12
|
+
|
13
|
+
PLATFORMS
|
14
|
+
ruby
|
15
|
+
|
16
|
+
DEPENDENCIES
|
17
|
+
bundler (~> 1.0.0)
|
18
|
+
jeweler (~> 1.6.4)
|
19
|
+
rcov
|
20
|
+
shoulda
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Charlie
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
rails g skizmo:form --help
|
2
|
+
Usage:
|
3
|
+
rails generate skizmo:form NAME [options]
|
4
|
+
|
5
|
+
Options:
|
6
|
+
[--javascript] # Indicates when to generate javascript
|
7
|
+
# Default: true
|
8
|
+
[--helpers] # Indicates when to generate helpers
|
9
|
+
# Default: true
|
10
|
+
|
11
|
+
Runtime options:
|
12
|
+
-p, [--pretend] # Run but do not make any changes
|
13
|
+
-s, [--skip] # Skip files that already exist
|
14
|
+
-q, [--quiet] # Supress status output
|
15
|
+
-f, [--force] # Overwrite files that already exist
|
16
|
+
|
17
|
+
Description:
|
18
|
+
Generates a form per usual but with fields_for for the has_many and belongs_to associations.
|
19
|
+
|
20
|
+
Example:
|
21
|
+
rails generate skizmo:form Foo
|
22
|
+
|
23
|
+
With Foo.rb (model like this):
|
24
|
+
class Foo < ActiveRecord::Base
|
25
|
+
has_many :bars
|
26
|
+
belongs_to :baz
|
27
|
+
|
28
|
+
accepts_nested_attributes_for :bars
|
29
|
+
accepts_nested_attributes_for :baz
|
30
|
+
end
|
31
|
+
|
32
|
+
This will create:
|
33
|
+
public/javascripts/jquery.add_remove_links.js
|
34
|
+
app/helpers/add_remove_links_helper.rb
|
35
|
+
app/views/foos/new.html.erb
|
36
|
+
app/views/foos/edit.html.erb
|
37
|
+
app/views/foos/_form.html.erb
|
38
|
+
app/views/foos/_bar_fields.html.erb
|
39
|
+
app/views/foos/_baz_fields.html.erb
|
40
|
+
|
data/README.rdoc
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
= formgen
|
2
|
+
|
3
|
+
rails g skizmo:form --help
|
4
|
+
=== Usage:
|
5
|
+
rails generate skizmo:form NAME [options]
|
6
|
+
|
7
|
+
=== Options:
|
8
|
+
[--javascript] # Indicates when to generate javascript
|
9
|
+
# Default: true
|
10
|
+
[--helpers] # Indicates when to generate helpers
|
11
|
+
# Default: true
|
12
|
+
|
13
|
+
=== Runtime options:
|
14
|
+
-p, [--pretend] # Run but do not make any changes
|
15
|
+
-s, [--skip] # Skip files that already exist
|
16
|
+
-q, [--quiet] # Supress status output
|
17
|
+
-f, [--force] # Overwrite files that already exist
|
18
|
+
|
19
|
+
=== Description:
|
20
|
+
Generates a form per usual but with fields_for for the has_many and belongs_to associations.
|
21
|
+
|
22
|
+
=== Example:
|
23
|
+
rails generate skizmo:form Foo
|
24
|
+
|
25
|
+
With Foo.rb (model like this):
|
26
|
+
class Foo < ActiveRecord::Base
|
27
|
+
has_many :bars
|
28
|
+
belongs_to :baz
|
29
|
+
|
30
|
+
accepts_nested_attributes_for :bars
|
31
|
+
accepts_nested_attributes_for :baz
|
32
|
+
end
|
33
|
+
|
34
|
+
This will create:
|
35
|
+
public/javascripts/jquery.add_remove_links.js
|
36
|
+
app/helpers/add_remove_links_helper.rb
|
37
|
+
app/views/foos/new.html.erb
|
38
|
+
app/views/foos/edit.html.erb
|
39
|
+
app/views/foos/_form.html.erb
|
40
|
+
app/views/foos/_bar_fields.html.erb
|
41
|
+
app/views/foos/_baz_fields.html.erb
|
42
|
+
|
43
|
+
|
44
|
+
== Contributing to formgen
|
45
|
+
|
46
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
47
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
48
|
+
* Fork the project
|
49
|
+
* Start a feature/bugfix branch
|
50
|
+
* Commit and push until you are happy with your contribution
|
51
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
52
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
53
|
+
|
54
|
+
== Copyright
|
55
|
+
|
56
|
+
Copyright (c) 2011 Pullmonkey. See LICENSE.txt for
|
57
|
+
further details.
|
58
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
|
14
|
+
require 'jeweler'
|
15
|
+
Jeweler::Tasks.new do |gem|
|
16
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
17
|
+
gem.name = "formgen"
|
18
|
+
gem.homepage = "http://github.com/pullmonkey/formgen"
|
19
|
+
gem.license = "MIT"
|
20
|
+
gem.summary = %Q{Ruby on Rails form generator based on has_many and belongs_to associations}
|
21
|
+
gem.description = %Q{Ruby on Rails form generator based on has_many and belongs_to associations. Includes the helpers and javascript for adding and removing fields_for form attributes.}
|
22
|
+
gem.email = "info@skizmo.com"
|
23
|
+
gem.authors = ["Pullmonkey"]
|
24
|
+
# dependencies defined in Gemfile
|
25
|
+
end
|
26
|
+
Jeweler::RubygemsDotOrgTasks.new
|
27
|
+
|
28
|
+
require 'rake/testtask'
|
29
|
+
Rake::TestTask.new(:test) do |test|
|
30
|
+
test.libs << 'lib' << 'test'
|
31
|
+
test.pattern = 'test/**/test_*.rb'
|
32
|
+
test.verbose = true
|
33
|
+
end
|
34
|
+
|
35
|
+
require 'rcov/rcovtask'
|
36
|
+
Rcov::RcovTask.new do |test|
|
37
|
+
test.libs << 'test'
|
38
|
+
test.pattern = 'test/**/test_*.rb'
|
39
|
+
test.verbose = true
|
40
|
+
test.rcov_opts << '--exclude "gems/*"'
|
41
|
+
end
|
42
|
+
|
43
|
+
task :default => :test
|
44
|
+
|
45
|
+
require 'rake/rdoctask'
|
46
|
+
Rake::RDocTask.new do |rdoc|
|
47
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
48
|
+
|
49
|
+
rdoc.rdoc_dir = 'rdoc'
|
50
|
+
rdoc.title = "formgen #{version}"
|
51
|
+
rdoc.rdoc_files.include('README*')
|
52
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
53
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
data/formgen.gemspec
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{formgen}
|
8
|
+
s.version = "0.1.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Pullmonkey"]
|
12
|
+
s.date = %q{2011-11-15}
|
13
|
+
s.description = %q{Ruby on Rails form generator based on has_many and belongs_to associations. Includes the helpers and javascript for adding and removing fields_for form attributes.}
|
14
|
+
s.email = %q{info@skizmo.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE.txt",
|
17
|
+
"README",
|
18
|
+
"README.rdoc"
|
19
|
+
]
|
20
|
+
s.files = [
|
21
|
+
".document",
|
22
|
+
"Gemfile",
|
23
|
+
"Gemfile.lock",
|
24
|
+
"LICENSE.txt",
|
25
|
+
"README",
|
26
|
+
"README.rdoc",
|
27
|
+
"Rakefile",
|
28
|
+
"VERSION",
|
29
|
+
"formgen.gemspec",
|
30
|
+
"lib/formgen.rb",
|
31
|
+
"lib/generators/skizmo/form/USAGE",
|
32
|
+
"lib/generators/skizmo/form/form_generator.rb",
|
33
|
+
"lib/generators/skizmo/form/templates/_form.html.erb",
|
34
|
+
"lib/generators/skizmo/form/templates/_nested_fields.html.erb",
|
35
|
+
"lib/generators/skizmo/form/templates/edit.html.erb",
|
36
|
+
"lib/generators/skizmo/form/templates/helper.rb",
|
37
|
+
"lib/generators/skizmo/form/templates/javascript.js",
|
38
|
+
"lib/generators/skizmo/form/templates/new.html.erb",
|
39
|
+
"test/helper.rb",
|
40
|
+
"test/test_formgen.rb"
|
41
|
+
]
|
42
|
+
s.homepage = %q{http://github.com/pullmonkey/formgen}
|
43
|
+
s.licenses = ["MIT"]
|
44
|
+
s.require_paths = ["lib"]
|
45
|
+
s.rubygems_version = %q{1.4.2}
|
46
|
+
s.summary = %q{Ruby on Rails form generator based on has_many and belongs_to associations}
|
47
|
+
|
48
|
+
if s.respond_to? :specification_version then
|
49
|
+
s.specification_version = 3
|
50
|
+
|
51
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
52
|
+
s.add_development_dependency(%q<shoulda>, [">= 0"])
|
53
|
+
s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
|
54
|
+
s.add_development_dependency(%q<jeweler>, ["~> 1.6.4"])
|
55
|
+
s.add_development_dependency(%q<rcov>, [">= 0"])
|
56
|
+
else
|
57
|
+
s.add_dependency(%q<shoulda>, [">= 0"])
|
58
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
59
|
+
s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
|
60
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
61
|
+
end
|
62
|
+
else
|
63
|
+
s.add_dependency(%q<shoulda>, [">= 0"])
|
64
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
65
|
+
s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
|
66
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
data/lib/formgen.rb
ADDED
File without changes
|
@@ -0,0 +1,23 @@
|
|
1
|
+
Description:
|
2
|
+
Generates a form per usual but with fields_for for the has_many and belongs_to associations.
|
3
|
+
|
4
|
+
Example:
|
5
|
+
rails generate skizmo:form Foo
|
6
|
+
|
7
|
+
With Foo.rb (model like this):
|
8
|
+
class Foo < ActiveRecord::Base
|
9
|
+
has_many :bars
|
10
|
+
belongs_to :baz
|
11
|
+
|
12
|
+
accepts_nested_attributes_for :bars
|
13
|
+
accepts_nested_attributes_for :baz
|
14
|
+
end
|
15
|
+
|
16
|
+
This will create:
|
17
|
+
public/javascripts/jquery.add_remove_links.js
|
18
|
+
app/helpers/add_remove_links_helper.rb
|
19
|
+
app/views/foos/new.html.erb
|
20
|
+
app/views/foos/edit.html.erb
|
21
|
+
app/views/foos/_form.html.erb
|
22
|
+
app/views/foos/_bar_fields.html.erb
|
23
|
+
app/views/foos/_baz_fields.html.erb
|
@@ -0,0 +1,99 @@
|
|
1
|
+
module Skizmo
|
2
|
+
module Generators
|
3
|
+
class FormGenerator < Rails::Generators::NamedBase
|
4
|
+
source_root File.expand_path('../templates', __FILE__)
|
5
|
+
argument :name, :type => :string
|
6
|
+
class_option :javascript, :type => :boolean, :default => true,
|
7
|
+
:description => "Include javascript to add and remove associated attributes if there are has_many associations"
|
8
|
+
class_option :helpers, :type => :boolean, :default => true,
|
9
|
+
:description => "Update helpers to add link_to_add and link_to_remove methods if there are has_many associations"
|
10
|
+
|
11
|
+
def generate_form
|
12
|
+
unless nested_has_many_classes.empty?
|
13
|
+
copy_file "javascript.js", "public/javascripts/jquery.add_remove_links.js" if options.javascript?
|
14
|
+
copy_file "helper.rb", "app/helpers/add_remove_links_helper.rb" if options.helpers?
|
15
|
+
end
|
16
|
+
template "new.html.erb", "app/views/#{file_name.pluralize}/new.html.erb"
|
17
|
+
template "edit.html.erb", "app/views/#{file_name.pluralize}/edit.html.erb"
|
18
|
+
template "_form.html.erb", "app/views/#{file_name.pluralize}/_form.html.erb"
|
19
|
+
nested_classes_with_attributes.each do |hash_with_kls_and_attrs|
|
20
|
+
@kls = hash_with_kls_and_attrs[:kls] # class object
|
21
|
+
@sym = hash_with_kls_and_attrs[:sym] # passed to the fields_for
|
22
|
+
@attrs = hash_with_kls_and_attrs[:attrs] # attrs that are worthy for this association
|
23
|
+
@assoc = hash_with_kls_and_attrs[:assoc] # so we know whether to use the add and remove links or not
|
24
|
+
template "_nested_fields.html.erb", "app/views/#{file_name.pluralize}/_#{@sym.to_s.singularize}_fields.html.erb"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def file_name
|
31
|
+
name.underscore
|
32
|
+
end
|
33
|
+
|
34
|
+
def cls
|
35
|
+
name.classify.constantize
|
36
|
+
end
|
37
|
+
|
38
|
+
def attributes_worth_using_in_the_form(options={})
|
39
|
+
kls = options[:class] || cls
|
40
|
+
rejects = ["created_at","updated_at","created_on","updated_on","id",/_id$/]
|
41
|
+
kls.column_names.reject do |x|
|
42
|
+
rejects.include?(x) || !rejects.select{|y| y.is_a?(Regexp) and x =~ y}.empty?
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def attr_form_method(kls, attr)
|
47
|
+
case kls.columns.find{|c| c.name == attr}.try(:type)
|
48
|
+
when :string, :integer, :float, :decimal, :datetime, :timestamp, :time, :date
|
49
|
+
# using text fields for the date selectors b/c most people will use a js calendar
|
50
|
+
return :text_field
|
51
|
+
when :text
|
52
|
+
return :text_area
|
53
|
+
when :boolean
|
54
|
+
return :check_box
|
55
|
+
else
|
56
|
+
return :text_field
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def class_reflections
|
61
|
+
cls.reflections
|
62
|
+
end
|
63
|
+
|
64
|
+
# TODO only get the classes that have "nested attributes"
|
65
|
+
|
66
|
+
def nested_has_many_classes
|
67
|
+
has_many_through_associations = class_reflections.select{|k,v| v.macro == :has_many and v.options.has_key?(:through)}
|
68
|
+
through_assocs = has_many_through_associations.map{|x| x.last.options[:through]}
|
69
|
+
# we don't care about the associations that primarily exist to assist the has_many :through
|
70
|
+
has_many_associations = class_reflections.select{|k,v| v.macro == :has_many and
|
71
|
+
not v.options.has_key?(:through) and
|
72
|
+
not through_assocs.include?(k)}
|
73
|
+
# these are the keys of the reflections we care about
|
74
|
+
(has_many_through_associations | has_many_associations).map(&:first)
|
75
|
+
end
|
76
|
+
|
77
|
+
def nested_belongs_to_classes
|
78
|
+
class_reflections.select{|k,v| v.macro == :belongs_to}.map(&:first)
|
79
|
+
end
|
80
|
+
|
81
|
+
def nested_classes
|
82
|
+
{:belongs_to => nested_belongs_to_classes, :has_many => nested_has_many_classes}
|
83
|
+
end
|
84
|
+
|
85
|
+
def nested_classes_with_attributes
|
86
|
+
to_return = []
|
87
|
+
nested_classes.each do |assoc,ncs|
|
88
|
+
ncs.each do |nc|
|
89
|
+
kls = nc.to_s.classify.constantize
|
90
|
+
attrs = attributes_worth_using_in_the_form(:class => kls)
|
91
|
+
to_return << {:sym => nc, :kls => kls, :attrs => attrs, :assoc => assoc}
|
92
|
+
end
|
93
|
+
end
|
94
|
+
to_return
|
95
|
+
end
|
96
|
+
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
<%%= form_for @<%= cls.to_s.underscore %> do |f| %>
|
2
|
+
<%% if @<%= cls.to_s.underscore %>.errors.any? %>
|
3
|
+
<div id="errorExplanation">
|
4
|
+
<h2><%%= pluralize(@<%= cls.to_s.underscore %>.errors.count, "error") %> prohibited this <%= cls.to_s.underscore.gsub("_"," ") %> from being saved:</h2>
|
5
|
+
<ul>
|
6
|
+
<%% @<%= cls.to_s.underscore %>.errors.full_messages.each do |msg| %>
|
7
|
+
<li><%%= msg %></li>
|
8
|
+
<%% end %>
|
9
|
+
</ul>
|
10
|
+
</div>
|
11
|
+
<%% end %>
|
12
|
+
<% attributes_worth_using_in_the_form.each do |att| -%>
|
13
|
+
<div class="field">
|
14
|
+
<%%= f.label :<%= att %> %>
|
15
|
+
<%%= f.<%= attr_form_method(cls, att) %> :<%= att %> %>
|
16
|
+
</div>
|
17
|
+
<% end -%>
|
18
|
+
<% nested_belongs_to_classes.each do |cls_name| -%>
|
19
|
+
<%%= f.fields_for :<%= cls_name %> do |nf| %>
|
20
|
+
<%%= render '<%= cls_name.to_s.singularize.underscore %>_fields', :f => nf %>
|
21
|
+
<%% end %>
|
22
|
+
<% end -%>
|
23
|
+
<% nested_has_many_classes.each do |cls_name| -%>
|
24
|
+
<%%= f.fields_for :<%= cls_name %> do |nf| %>
|
25
|
+
<%%= render '<%= cls_name.to_s.singularize.underscore %>_fields', :f => nf %>
|
26
|
+
<%% end %>
|
27
|
+
<% if options.javascript? -%>
|
28
|
+
<p><%%= link_to_add_fields "Add <%= cls_name.to_s.singularize.gsub("_"," ") %>", f, :<%= cls_name %> %></p>
|
29
|
+
<% end -%>
|
30
|
+
<% end -%>
|
31
|
+
<%%= f.submit "Save" %>
|
32
|
+
<%% end %>
|
33
|
+
<% if options.javascript? -%>
|
34
|
+
<%%= content_for :head do %>
|
35
|
+
<%%= javascript_include_tag 'jquery.add_remove_links.js' %>
|
36
|
+
<%% end %>
|
37
|
+
<% end -%>
|
@@ -0,0 +1,11 @@
|
|
1
|
+
<div class="fields">
|
2
|
+
<% @attrs.each do |att| -%>
|
3
|
+
<div class="field">
|
4
|
+
<%%= f.label :<%= att %> %>
|
5
|
+
<%%= f.<%= attr_form_method(@kls, att) %> :<%= att %> %>
|
6
|
+
</div>
|
7
|
+
<% end -%>
|
8
|
+
<% if @assoc == :has_many and options.javascript? -%>
|
9
|
+
<%%= link_to_remove_fields "remove", f %>
|
10
|
+
<% end -%>
|
11
|
+
</div>
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module AddRemoveLinksHelper
|
2
|
+
def link_to_remove_fields(name, f)
|
3
|
+
f.hidden_field(:_destroy) + link_to_function(name, "remove_fields(this)")
|
4
|
+
end
|
5
|
+
|
6
|
+
def link_to_add_fields(name, f, association)
|
7
|
+
new_object = f.object.class.reflect_on_association(association).klass.new
|
8
|
+
fields = f.fields_for(association, new_object, :child_index => "new_#{association}") do |builder|
|
9
|
+
render(association.to_s.singularize + "_fields", :f => builder)
|
10
|
+
end
|
11
|
+
link_to_function(name, "add_fields(this, \"#{association}\", \"#{escape_javascript(fields)}\")")
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
function remove_fields(link) {
|
2
|
+
jQuery(link).prev("input[type=hidden]").val("1");
|
3
|
+
jQuery(link).closest(".fields").hide();
|
4
|
+
}
|
5
|
+
|
6
|
+
function add_fields(link, association, content) {
|
7
|
+
var new_id = new Date().getTime();
|
8
|
+
var regexp = new RegExp("new_" + association, "g");
|
9
|
+
jQuery(link).parent().before(content.replace(regexp, new_id));
|
10
|
+
}
|
data/test/helper.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
begin
|
4
|
+
Bundler.setup(:default, :development)
|
5
|
+
rescue Bundler::BundlerError => e
|
6
|
+
$stderr.puts e.message
|
7
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
8
|
+
exit e.status_code
|
9
|
+
end
|
10
|
+
require 'test/unit'
|
11
|
+
require 'shoulda'
|
12
|
+
|
13
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
14
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
15
|
+
require 'formgen'
|
16
|
+
|
17
|
+
class Test::Unit::TestCase
|
18
|
+
end
|
metadata
ADDED
@@ -0,0 +1,147 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: formgen
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Pullmonkey
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-11-15 00:00:00 -07:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
type: :development
|
23
|
+
version_requirements: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
hash: 3
|
29
|
+
segments:
|
30
|
+
- 0
|
31
|
+
version: "0"
|
32
|
+
requirement: *id001
|
33
|
+
prerelease: false
|
34
|
+
name: shoulda
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
type: :development
|
37
|
+
version_requirements: &id002 !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
39
|
+
requirements:
|
40
|
+
- - ~>
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
hash: 23
|
43
|
+
segments:
|
44
|
+
- 1
|
45
|
+
- 0
|
46
|
+
- 0
|
47
|
+
version: 1.0.0
|
48
|
+
requirement: *id002
|
49
|
+
prerelease: false
|
50
|
+
name: bundler
|
51
|
+
- !ruby/object:Gem::Dependency
|
52
|
+
type: :development
|
53
|
+
version_requirements: &id003 !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
55
|
+
requirements:
|
56
|
+
- - ~>
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
hash: 7
|
59
|
+
segments:
|
60
|
+
- 1
|
61
|
+
- 6
|
62
|
+
- 4
|
63
|
+
version: 1.6.4
|
64
|
+
requirement: *id003
|
65
|
+
prerelease: false
|
66
|
+
name: jeweler
|
67
|
+
- !ruby/object:Gem::Dependency
|
68
|
+
type: :development
|
69
|
+
version_requirements: &id004 !ruby/object:Gem::Requirement
|
70
|
+
none: false
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
hash: 3
|
75
|
+
segments:
|
76
|
+
- 0
|
77
|
+
version: "0"
|
78
|
+
requirement: *id004
|
79
|
+
prerelease: false
|
80
|
+
name: rcov
|
81
|
+
description: Ruby on Rails form generator based on has_many and belongs_to associations. Includes the helpers and javascript for adding and removing fields_for form attributes.
|
82
|
+
email: info@skizmo.com
|
83
|
+
executables: []
|
84
|
+
|
85
|
+
extensions: []
|
86
|
+
|
87
|
+
extra_rdoc_files:
|
88
|
+
- LICENSE.txt
|
89
|
+
- README
|
90
|
+
- README.rdoc
|
91
|
+
files:
|
92
|
+
- .document
|
93
|
+
- Gemfile
|
94
|
+
- Gemfile.lock
|
95
|
+
- LICENSE.txt
|
96
|
+
- README
|
97
|
+
- README.rdoc
|
98
|
+
- Rakefile
|
99
|
+
- VERSION
|
100
|
+
- formgen.gemspec
|
101
|
+
- lib/formgen.rb
|
102
|
+
- lib/generators/skizmo/form/USAGE
|
103
|
+
- lib/generators/skizmo/form/form_generator.rb
|
104
|
+
- lib/generators/skizmo/form/templates/_form.html.erb
|
105
|
+
- lib/generators/skizmo/form/templates/_nested_fields.html.erb
|
106
|
+
- lib/generators/skizmo/form/templates/edit.html.erb
|
107
|
+
- lib/generators/skizmo/form/templates/helper.rb
|
108
|
+
- lib/generators/skizmo/form/templates/javascript.js
|
109
|
+
- lib/generators/skizmo/form/templates/new.html.erb
|
110
|
+
- test/helper.rb
|
111
|
+
- test/test_formgen.rb
|
112
|
+
has_rdoc: true
|
113
|
+
homepage: http://github.com/pullmonkey/formgen
|
114
|
+
licenses:
|
115
|
+
- MIT
|
116
|
+
post_install_message:
|
117
|
+
rdoc_options: []
|
118
|
+
|
119
|
+
require_paths:
|
120
|
+
- lib
|
121
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
122
|
+
none: false
|
123
|
+
requirements:
|
124
|
+
- - ">="
|
125
|
+
- !ruby/object:Gem::Version
|
126
|
+
hash: 3
|
127
|
+
segments:
|
128
|
+
- 0
|
129
|
+
version: "0"
|
130
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
131
|
+
none: false
|
132
|
+
requirements:
|
133
|
+
- - ">="
|
134
|
+
- !ruby/object:Gem::Version
|
135
|
+
hash: 3
|
136
|
+
segments:
|
137
|
+
- 0
|
138
|
+
version: "0"
|
139
|
+
requirements: []
|
140
|
+
|
141
|
+
rubyforge_project:
|
142
|
+
rubygems_version: 1.4.2
|
143
|
+
signing_key:
|
144
|
+
specification_version: 3
|
145
|
+
summary: Ruby on Rails form generator based on has_many and belongs_to associations
|
146
|
+
test_files: []
|
147
|
+
|