easy_form_for 0.0.1.alpha
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +5 -0
- data/Gemfile +7 -0
- data/README +58 -0
- data/Rakefile +5 -0
- data/config/database.yml +22 -0
- data/easy_form_for.gemspec +23 -0
- data/lib/easy_form_for.rb +3 -0
- data/lib/easy_form_for/railtie.rb +8 -0
- data/lib/easy_form_for/version.rb +3 -0
- data/lib/easy_form_for/view_helpers.rb +107 -0
- data/spec/easy_form_for_spec.rb +28 -0
- data/spec/spec_helper.rb +22 -0
- metadata +88 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
TODO:
|
2
|
+
- Create real README
|
3
|
+
- Create Docs
|
4
|
+
- Refactor
|
5
|
+
- Refactor
|
6
|
+
- Refactor
|
7
|
+
- yield block in multiple places from block (possible?)
|
8
|
+
- put tests in this project (right now they are at https://github.com/bookis/easy_form_for_specs)
|
9
|
+
|
10
|
+
Working on a rails form generator. As easy as:
|
11
|
+
|
12
|
+
<%= easy_form_for(@user) %>
|
13
|
+
|
14
|
+
or as advanced as:
|
15
|
+
|
16
|
+
<%= easy_form_for(@user,
|
17
|
+
:only => [:name, (:username if @user.username.blank?), :email, :birthday, :city, :state_id, :zipcode, :country_id, :bio, :diet_id, :me_gender, :you_gender],
|
18
|
+
:field_options => {
|
19
|
+
:email => {:type => "email", :autocapitalize => "off"},
|
20
|
+
:submit => {:disable_with => "Saving…".html_safe, :class => "button white full"},
|
21
|
+
:birthday => {
|
22
|
+
:order => [:month, :day, :year],
|
23
|
+
:start_year => 12.years.ago.year,
|
24
|
+
:end_year => 60.years.ago.year,
|
25
|
+
:include_blank => true},
|
26
|
+
:me_gender => {:label => "My Gender"},
|
27
|
+
:you_gender => {:label => "Desired Gender"}
|
28
|
+
}) do |f| %>
|
29
|
+
|
30
|
+
<%= f.label :label_id, "My Straightedginess" %>
|
31
|
+
<table id="my-label">
|
32
|
+
<% Label.all.each do |label| %>
|
33
|
+
<tr>
|
34
|
+
<td><%= f.radio_button :label_id, label.id, :checked => @user.label == label %></td>
|
35
|
+
<th><%= f.label "label_id_#{label.id}", label.name.titleize %></th>
|
36
|
+
</tr>
|
37
|
+
<% end %>
|
38
|
+
</table>
|
39
|
+
|
40
|
+
<%= f.label :your_labels_attributes, "Desired Straightedginess" %>
|
41
|
+
<table id="desired-labels">
|
42
|
+
<% Label.all.each_with_index do |label, index| %>
|
43
|
+
<%= f.fields_for "your_labels_attributes[#{index}]", :child_index => index do |ff| %>
|
44
|
+
<tr>
|
45
|
+
<% if @user.desired_labels.include? label %>
|
46
|
+
<%# HACK remove find from view, do it in the controller %>
|
47
|
+
<%= ff.hidden_field :id, :value => @user.your_labels.where(:label_id => label.id).first.id, :style => "display:none" %>
|
48
|
+
<td><%= ff.check_box :_destroy, { :checked => true }, 0, 1 %></td>
|
49
|
+
<th><%= ff.label :_destroy, label.name.titleize %></th>
|
50
|
+
<% else %>
|
51
|
+
<td><%= ff.check_box :label_id, {}, label.id %></td>
|
52
|
+
<th><%= ff.label :label_id, label.name.titleize %></th>
|
53
|
+
<% end %>
|
54
|
+
</th>
|
55
|
+
<% end %>
|
56
|
+
<% end %>
|
57
|
+
</table>
|
58
|
+
<% end %>
|
data/Rakefile
ADDED
data/config/database.yml
ADDED
@@ -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,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "easy_form_for/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "easy_form_for"
|
7
|
+
s.version = EasyFormFor::VERSION
|
8
|
+
s.authors = ["Bookis Smuin"]
|
9
|
+
s.email = ["vegan.bookis@gmail.com"]
|
10
|
+
s.homepage = "https://github.com/bookis/Easy-Form-For"
|
11
|
+
s.summary = %q{Easy Rails form generation}
|
12
|
+
s.description = %q{Just use easy_form_for(MyModel) to generate a form for an object}
|
13
|
+
|
14
|
+
s.rubyforge_project = "easy_form_for"
|
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_development_dependency "rspec"
|
22
|
+
s.add_development_dependency "supermodel"
|
23
|
+
end
|
@@ -0,0 +1,107 @@
|
|
1
|
+
module EasyFormFor
|
2
|
+
module ViewHelpers
|
3
|
+
|
4
|
+
attr_accessor :keys, :object
|
5
|
+
EXCLUDE_KEYS = %w(created_at updated_at id)
|
6
|
+
|
7
|
+
def easy_form_for(object, options={}, &block)
|
8
|
+
@object = object
|
9
|
+
@options = options
|
10
|
+
set_keys
|
11
|
+
generate_form(&block).html_safe
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def generate_form(&block)
|
17
|
+
form_class = @options[:classes][:form] if @options[:classes]
|
18
|
+
form_options = {:html => {:class => form_class}} if form_class ||= {:html => {}}
|
19
|
+
form_options[:html].merge! @options[:field_options][:form] if @options[:field_options] && @options[:field_options][:form]
|
20
|
+
form_for(@object, form_options) do |f|
|
21
|
+
@keys.each_with_index do |key, i|
|
22
|
+
concat(yield f) if block_given? && @options[:yield_block] && @options[:yield_block][:before] && (@options[:yield_block][:before] == key.to_sym || (@options[:yield_block][:before] == :start && i == 0))
|
23
|
+
field_type(f, key)
|
24
|
+
concat(yield f) if block_given? && @options[:yield_block] && @options[:yield_block][:after] && (@options[:yield_block][:after] == key.to_sym || (@options[:yield_block][:after] == :end && i + 1 >= @keys.size))
|
25
|
+
end
|
26
|
+
concat(yield f) if block_given? && @options[:yield_block].blank?
|
27
|
+
submit_options = @options[:field_options][:submit] if @options[:field_options] && @options[:field_options][:submit]
|
28
|
+
concat f.submit(:submit, submit_options ||= {})
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def field_type(f, key, args={})
|
33
|
+
if key.match /_id/
|
34
|
+
type = "select"
|
35
|
+
class_name = key.split(/_id/).first
|
36
|
+
attribute = @options[:associations][class_name.to_sym] ||= 'name' if @options[:associations] && @options[:associations][class_name.to_sym]
|
37
|
+
selects = options_for_select(Kernel.const_get(class_name.camelcase).order("#{attribute || 'name'} asc").map{|k| word = k.send(attribute || 'name'); [word.first.upcase == word.first ? word : word.titleize, k.id]})
|
38
|
+
args[:class] = @options[:classes][:fields] if @options[:classes] && @options[:classes][:fields]
|
39
|
+
if @options[:field_options] && @options[:field_options][key.to_sym]
|
40
|
+
if @options[:field_options][key.to_sym][:class]
|
41
|
+
args[:class] += " #{@options[:field_options][key.to_sym][:class]}"
|
42
|
+
@options[:field_options][key.to_sym].delete(:class)
|
43
|
+
end
|
44
|
+
args.merge!(@options[:field_options][key.to_sym])
|
45
|
+
end
|
46
|
+
concat f.label(key.to_sym, class_name.to_s.capitalize)
|
47
|
+
concat f.send(type, key.to_sym, selects, {}, args)
|
48
|
+
else
|
49
|
+
type = case object.class.columns_hash[key].type
|
50
|
+
when :string then 'text_field'
|
51
|
+
when :text then 'text_area'
|
52
|
+
when :date then 'date_select'
|
53
|
+
when :boolean then 'check_box'
|
54
|
+
when :integer then "text_field"
|
55
|
+
else
|
56
|
+
'text_field'
|
57
|
+
end
|
58
|
+
args[:class] = @options[:classes][:fields] if @options[:classes] && @options[:classes][:fields]
|
59
|
+
if @options[:field_options] && @options[:field_options][key.to_sym]
|
60
|
+
if @options[:field_options][key.to_sym][:class]
|
61
|
+
args[:class] += " #{@options[:field_options][key.to_sym][:class]}"
|
62
|
+
@options[:field_options][key.to_sym].delete(:class)
|
63
|
+
end
|
64
|
+
args.merge!(@options[:field_options][key.to_sym])
|
65
|
+
end
|
66
|
+
if args[:hidden] == true
|
67
|
+
type = 'hidden_field'
|
68
|
+
args.delete(:hidden)
|
69
|
+
end
|
70
|
+
label_text = args[:label]
|
71
|
+
concat f.label(key.to_sym, label_text)
|
72
|
+
concat f.send(type, key.to_sym, args)
|
73
|
+
end
|
74
|
+
|
75
|
+
end
|
76
|
+
|
77
|
+
def set_keys
|
78
|
+
@keys = sort_by(@options.has_key?(:only) ? only_keys : except_keys)
|
79
|
+
end
|
80
|
+
|
81
|
+
|
82
|
+
def sort_by(keys)
|
83
|
+
return keys if @options[:sort_by].nil?
|
84
|
+
if @options[:sort_by].instance_of? Array
|
85
|
+
@options[:sort_by].reverse.each { |key| keys.unshift(keys.delete(key.to_s)) }
|
86
|
+
keys
|
87
|
+
else
|
88
|
+
case @options[:sort_by]
|
89
|
+
when :alphabetical then keys.sort
|
90
|
+
when :type then keys.sort_by { |key| @object.class.columns_hash[key].type.to_s }.reverse
|
91
|
+
else
|
92
|
+
keys.sort
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
def only_keys
|
98
|
+
[@options[:only]].flatten.compact.map { |key| key.to_s }
|
99
|
+
end
|
100
|
+
|
101
|
+
def except_keys
|
102
|
+
excluded_keys = (EXCLUDE_KEYS + [@options[:except]]).flatten.compact.map{|k| k.to_s}
|
103
|
+
attrs = @object.attributes.keys.delete_if { |key| excluded_keys.include? key.to_s }
|
104
|
+
end
|
105
|
+
|
106
|
+
end
|
107
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class Country < SuperModel::Base
|
4
|
+
attributes :name, :abbreviation
|
5
|
+
end
|
6
|
+
class Blog < SuperModel::Base
|
7
|
+
attributes :name, :tagline, :theme, :body, :country_id, :published, :published_at
|
8
|
+
end
|
9
|
+
|
10
|
+
describe EasyFormFor do
|
11
|
+
|
12
|
+
before(:each) do
|
13
|
+
Country.create(:name => "new zealand", :abbreviation => "NZ")
|
14
|
+
end
|
15
|
+
|
16
|
+
describe "except" do
|
17
|
+
before(:each) do
|
18
|
+
@result = EasyFormFor::ViewHelpers.new.easy_form_for(Blog.new(:name => "test"), :except => :tagline, :sort_by => [:theme]) do |f|
|
19
|
+
f.label :temp_field
|
20
|
+
f.text_field :temp_field
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should have a name" do
|
25
|
+
@result.should include 'id="blog_name"'
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'easy_form_for'
|
2
|
+
require 'supermodel'
|
3
|
+
require 'active_model'
|
4
|
+
require 'action_controller'
|
5
|
+
require 'action_view'
|
6
|
+
require 'action_view/template'
|
7
|
+
include ActionView::Helpers
|
8
|
+
|
9
|
+
module EasyFormFor
|
10
|
+
class ViewHelpers
|
11
|
+
|
12
|
+
def blog_path(*args)
|
13
|
+
"/blogs"
|
14
|
+
end
|
15
|
+
def output_buffer=(*args)
|
16
|
+
""
|
17
|
+
end
|
18
|
+
def output_buffer(*args)
|
19
|
+
""
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
metadata
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: easy_form_for
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: 6
|
5
|
+
version: 0.0.1.alpha
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Bookis Smuin
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-12-31 00:00:00 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: rspec
|
17
|
+
prerelease: false
|
18
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
19
|
+
none: false
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
type: :development
|
25
|
+
version_requirements: *id001
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: supermodel
|
28
|
+
prerelease: false
|
29
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
30
|
+
none: false
|
31
|
+
requirements:
|
32
|
+
- - ">="
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: "0"
|
35
|
+
type: :development
|
36
|
+
version_requirements: *id002
|
37
|
+
description: Just use easy_form_for(MyModel) to generate a form for an object
|
38
|
+
email:
|
39
|
+
- vegan.bookis@gmail.com
|
40
|
+
executables: []
|
41
|
+
|
42
|
+
extensions: []
|
43
|
+
|
44
|
+
extra_rdoc_files: []
|
45
|
+
|
46
|
+
files:
|
47
|
+
- .gitignore
|
48
|
+
- Gemfile
|
49
|
+
- README
|
50
|
+
- Rakefile
|
51
|
+
- config/database.yml
|
52
|
+
- easy_form_for.gemspec
|
53
|
+
- lib/easy_form_for.rb
|
54
|
+
- lib/easy_form_for/railtie.rb
|
55
|
+
- lib/easy_form_for/version.rb
|
56
|
+
- lib/easy_form_for/view_helpers.rb
|
57
|
+
- spec/easy_form_for_spec.rb
|
58
|
+
- spec/spec_helper.rb
|
59
|
+
homepage: https://github.com/bookis/Easy-Form-For
|
60
|
+
licenses: []
|
61
|
+
|
62
|
+
post_install_message:
|
63
|
+
rdoc_options: []
|
64
|
+
|
65
|
+
require_paths:
|
66
|
+
- lib
|
67
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
68
|
+
none: false
|
69
|
+
requirements:
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: "0"
|
73
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
74
|
+
none: false
|
75
|
+
requirements:
|
76
|
+
- - ">"
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: 1.3.1
|
79
|
+
requirements: []
|
80
|
+
|
81
|
+
rubyforge_project: easy_form_for
|
82
|
+
rubygems_version: 1.8.11
|
83
|
+
signing_key:
|
84
|
+
specification_version: 3
|
85
|
+
summary: Easy Rails form generation
|
86
|
+
test_files:
|
87
|
+
- spec/easy_form_for_spec.rb
|
88
|
+
- spec/spec_helper.rb
|