magic_form 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,17 @@
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.5.1"
12
+ gem "rcov", ">= 0"
13
+ end
14
+
15
+ gem "ruby-debug19"
16
+ gem 'rails'
17
+ gem 'sqlite3'
data/Gemfile.lock ADDED
@@ -0,0 +1,102 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ abstract (1.0.0)
5
+ actionmailer (3.0.3)
6
+ actionpack (= 3.0.3)
7
+ mail (~> 2.2.9)
8
+ actionpack (3.0.3)
9
+ activemodel (= 3.0.3)
10
+ activesupport (= 3.0.3)
11
+ builder (~> 2.1.2)
12
+ erubis (~> 2.6.6)
13
+ i18n (~> 0.4)
14
+ rack (~> 1.2.1)
15
+ rack-mount (~> 0.6.13)
16
+ rack-test (~> 0.5.6)
17
+ tzinfo (~> 0.3.23)
18
+ activemodel (3.0.3)
19
+ activesupport (= 3.0.3)
20
+ builder (~> 2.1.2)
21
+ i18n (~> 0.4)
22
+ activerecord (3.0.3)
23
+ activemodel (= 3.0.3)
24
+ activesupport (= 3.0.3)
25
+ arel (~> 2.0.2)
26
+ tzinfo (~> 0.3.23)
27
+ activeresource (3.0.3)
28
+ activemodel (= 3.0.3)
29
+ activesupport (= 3.0.3)
30
+ activesupport (3.0.3)
31
+ archive-tar-minitar (0.5.2)
32
+ arel (2.0.5)
33
+ builder (2.1.2)
34
+ columnize (0.3.2)
35
+ erubis (2.6.6)
36
+ abstract (>= 1.0.0)
37
+ ffi (1.0.0)
38
+ rake (>= 0.8.7)
39
+ git (1.2.5)
40
+ i18n (0.5.0)
41
+ jeweler (1.5.1)
42
+ bundler (~> 1.0.0)
43
+ git (>= 1.2.5)
44
+ rake
45
+ linecache19 (0.5.11)
46
+ ruby_core_source (>= 0.1.4)
47
+ mail (2.2.11)
48
+ activesupport (>= 2.3.6)
49
+ i18n (~> 0.5.0)
50
+ mime-types (~> 1.16)
51
+ treetop (~> 1.4.8)
52
+ mime-types (1.16)
53
+ polyglot (0.3.1)
54
+ rack (1.2.1)
55
+ rack-mount (0.6.13)
56
+ rack (>= 1.0.0)
57
+ rack-test (0.5.6)
58
+ rack (>= 1.0)
59
+ rails (3.0.3)
60
+ actionmailer (= 3.0.3)
61
+ actionpack (= 3.0.3)
62
+ activerecord (= 3.0.3)
63
+ activeresource (= 3.0.3)
64
+ activesupport (= 3.0.3)
65
+ bundler (~> 1.0)
66
+ railties (= 3.0.3)
67
+ railties (3.0.3)
68
+ actionpack (= 3.0.3)
69
+ activesupport (= 3.0.3)
70
+ rake (>= 0.8.7)
71
+ thor (~> 0.14.4)
72
+ rake (0.8.7)
73
+ rcov (0.9.9)
74
+ ruby-debug-base19 (0.11.24)
75
+ columnize (>= 0.3.1)
76
+ linecache19 (>= 0.5.11)
77
+ ruby_core_source (>= 0.1.4)
78
+ ruby-debug19 (0.11.6)
79
+ columnize (>= 0.3.1)
80
+ linecache19 (>= 0.5.11)
81
+ ruby-debug-base19 (>= 0.11.19)
82
+ ruby_core_source (0.1.4)
83
+ archive-tar-minitar (>= 0.5.2)
84
+ shoulda (2.11.3)
85
+ sqlite3 (0.1.1)
86
+ ffi (>= 0.6.3)
87
+ thor (0.14.6)
88
+ treetop (1.4.9)
89
+ polyglot (>= 0.3.1)
90
+ tzinfo (0.3.23)
91
+
92
+ PLATFORMS
93
+ ruby
94
+
95
+ DEPENDENCIES
96
+ bundler (~> 1.0.0)
97
+ jeweler (~> 1.5.1)
98
+ rails
99
+ rcov
100
+ ruby-debug19
101
+ shoulda
102
+ sqlite3
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Luis Galaviz
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.markdown ADDED
@@ -0,0 +1,46 @@
1
+ # magic_form
2
+
3
+ The laziest way to implement a *Form*. This gem extends Rails 3 ActionView::Helpers.
4
+
5
+ ## Installation
6
+
7
+ ### Rails 3
8
+
9
+ Include it on your Gemfile:
10
+
11
+ gem 'magic_form'
12
+
13
+ And install it
14
+
15
+ bundle install
16
+
17
+ ## Usage
18
+
19
+ You have to call in your view to the helper *magic_form*. Example:
20
+
21
+ = raw magic_form(@user)
22
+
23
+ Also, you can define your own attribute layer message. Example:
24
+
25
+ = raw magic_form(@user, :name => "User name: ", :submit => "Save")
26
+
27
+ ## To Do
28
+
29
+ * Implement nested resources compatibility.
30
+ * Implement a *do* cycle.
31
+
32
+ ## Contributing to magic_form
33
+
34
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
35
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
36
+ * Fork the project
37
+ * Start a feature/bugfix branch
38
+ * Commit and push until you are happy with your contribution
39
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
40
+ * 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.
41
+
42
+ ## Copyright
43
+
44
+ Copyright (c) 2010 Luis Galaviz. See LICENSE.txt for
45
+ further details.
46
+
data/Rakefile ADDED
@@ -0,0 +1,53 @@
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 'rake'
11
+
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |gem|
14
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
15
+ gem.name = "magic_form"
16
+ gem.homepage = "http://github.com/MGalv/magic_form"
17
+ gem.license = "MIT"
18
+ gem.summary = %Q{Generate a form using a specific resource}
19
+ gem.description = %Q{Generate a form using a specific resource}
20
+ gem.email = "luis.galaviz@crowdint.com"
21
+ gem.authors = ["Luis Galaviz"]
22
+ # Include your dependencies below. Runtime dependencies are required when using your gem,
23
+ # and development dependencies are only needed for development (ie running rake tasks, tests, etc)
24
+ # gem.add_runtime_dependency 'jabber4r', '> 0.1'
25
+ # gem.add_development_dependency 'rspec', '> 1.2.3'
26
+ end
27
+ Jeweler::RubygemsDotOrgTasks.new
28
+
29
+ require 'rake/testtask'
30
+ Rake::TestTask.new(:test) do |test|
31
+ test.libs << 'lib' << 'test'
32
+ test.pattern = 'test/**/test_*.rb'
33
+ test.verbose = true
34
+ end
35
+
36
+ require 'rcov/rcovtask'
37
+ Rcov::RcovTask.new do |test|
38
+ test.libs << 'test'
39
+ test.pattern = 'test/**/test_*.rb'
40
+ test.verbose = true
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 = "magic_form #{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/lib/magic_form.rb ADDED
@@ -0,0 +1,41 @@
1
+ require 'action_view/context'
2
+
3
+ module ActionView
4
+ module Helpers
5
+ module MagicFormHelper
6
+
7
+ extend ActiveSupport::Concern
8
+ include FormHelper
9
+
10
+ def magic_form(*resources)
11
+ form_content = ""
12
+ form_eval = []
13
+ out = ""
14
+ options = resources.extract_options!
15
+ resource = resources.last
16
+ resource.attribute_names.each do |attribute|
17
+ unless attribute == "created_at" || attribute == "updated_at"
18
+ form_eval << "f.label '#{options[attribute.to_sym] || attribute.to_sym}'"
19
+ form_eval << "f.text_field :#{attribute}"
20
+ end
21
+ end
22
+
23
+ form_eval << "f.submit '#{options[:submit] || 'submit' }'"
24
+ out = form_for resource do |f|
25
+ form_eval.each do |c|
26
+ form_content << "#{eval(c)}"
27
+ end
28
+ form_content
29
+ end
30
+ out
31
+ end
32
+ end
33
+
34
+ end
35
+
36
+ ActionView::Helpers.autoload :MagicFormHelper
37
+ ActionView::Helpers.send(:include, Helpers::MagicFormHelper)
38
+
39
+ end
40
+
41
+
data/test/helper.rb ADDED
@@ -0,0 +1,41 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'bundler'
4
+ require 'sqlite3'
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
+
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
15
+
16
+ ENV["RAILS_ENV"] = "test"
17
+
18
+ require 'shoulda'
19
+ require 'rails/all'
20
+ require 'rails/test_help'
21
+ require 'magic_form'
22
+ require 'action_view/testing/resolvers'
23
+
24
+ module MagicForm
25
+ class Application < Rails::Application; end
26
+ end
27
+
28
+ MagicForm::Application.routes.draw do
29
+ match '/:controller(/:action(/:id))'
30
+ resource :products
31
+ end
32
+
33
+ ActionController::Base.send :include, MagicForm::Application.routes.url_helpers
34
+ ProductsController = Class.new(ActionController::Base)
35
+
36
+ ::ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
37
+ ::ActiveRecord::Schema.define(:version => 1) do
38
+ create_table :products do |t|
39
+ t.column :name, :string
40
+ end
41
+ end
@@ -0,0 +1,41 @@
1
+ require 'helper'
2
+
3
+ class Product < ActiveRecord::Base
4
+ end
5
+
6
+ class MagicFormTest < ActionView::TestCase
7
+ tests ActionView::Helpers::MagicFormHelper
8
+
9
+ def setup
10
+ @product = Product.new
11
+ end
12
+
13
+ def teardown
14
+ @product = nil
15
+ end
16
+
17
+ context "having a product create a form with the Product attributes" do
18
+
19
+ should "with no options" do
20
+ assert_equal magic_form(@product),
21
+ (form_for @product do |f|
22
+ form_content = ""
23
+ form_content << (f.label :name)
24
+ form_content << (f.text_field :name)
25
+ form_content << (f.submit 'submit')
26
+ form_content
27
+ end)
28
+ end
29
+
30
+ should "with options" do
31
+ assert_equal magic_form(@product, :name => "Product name: "),
32
+ (form_for @product do |f|
33
+ form_content = ""
34
+ form_content << (f.label "Product name: ")
35
+ form_content << (f.text_field :name)
36
+ form_content << (f.submit 'submit')
37
+ form_content
38
+ end)
39
+ end
40
+ end
41
+ end
metadata ADDED
@@ -0,0 +1,170 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: magic_form
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Luis Galaviz
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-12-01 00:00:00 -06:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: ruby-debug19
22
+ requirement: &id001 !ruby/object:Gem::Requirement
23
+ none: false
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ version: "0"
30
+ type: :runtime
31
+ prerelease: false
32
+ version_requirements: *id001
33
+ - !ruby/object:Gem::Dependency
34
+ name: rails
35
+ requirement: &id002 !ruby/object:Gem::Requirement
36
+ none: false
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ segments:
41
+ - 0
42
+ version: "0"
43
+ type: :runtime
44
+ prerelease: false
45
+ version_requirements: *id002
46
+ - !ruby/object:Gem::Dependency
47
+ name: sqlite3
48
+ requirement: &id003 !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ segments:
54
+ - 0
55
+ version: "0"
56
+ type: :runtime
57
+ prerelease: false
58
+ version_requirements: *id003
59
+ - !ruby/object:Gem::Dependency
60
+ name: shoulda
61
+ requirement: &id004 !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ segments:
67
+ - 0
68
+ version: "0"
69
+ type: :development
70
+ prerelease: false
71
+ version_requirements: *id004
72
+ - !ruby/object:Gem::Dependency
73
+ name: bundler
74
+ requirement: &id005 !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ~>
78
+ - !ruby/object:Gem::Version
79
+ segments:
80
+ - 1
81
+ - 0
82
+ - 0
83
+ version: 1.0.0
84
+ type: :development
85
+ prerelease: false
86
+ version_requirements: *id005
87
+ - !ruby/object:Gem::Dependency
88
+ name: jeweler
89
+ requirement: &id006 !ruby/object:Gem::Requirement
90
+ none: false
91
+ requirements:
92
+ - - ~>
93
+ - !ruby/object:Gem::Version
94
+ segments:
95
+ - 1
96
+ - 5
97
+ - 1
98
+ version: 1.5.1
99
+ type: :development
100
+ prerelease: false
101
+ version_requirements: *id006
102
+ - !ruby/object:Gem::Dependency
103
+ name: rcov
104
+ requirement: &id007 !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ segments:
110
+ - 0
111
+ version: "0"
112
+ type: :development
113
+ prerelease: false
114
+ version_requirements: *id007
115
+ description: Generate a form using a specific resource
116
+ email: luis.galaviz@crowdint.com
117
+ executables: []
118
+
119
+ extensions: []
120
+
121
+ extra_rdoc_files:
122
+ - LICENSE.txt
123
+ - README.markdown
124
+ files:
125
+ - .document
126
+ - Gemfile
127
+ - Gemfile.lock
128
+ - LICENSE.txt
129
+ - README.markdown
130
+ - Rakefile
131
+ - VERSION
132
+ - lib/magic_form.rb
133
+ - test/helper.rb
134
+ - test/test_magic_form.rb
135
+ has_rdoc: true
136
+ homepage: http://github.com/MGalv/magic_form
137
+ licenses:
138
+ - MIT
139
+ post_install_message:
140
+ rdoc_options: []
141
+
142
+ require_paths:
143
+ - lib
144
+ required_ruby_version: !ruby/object:Gem::Requirement
145
+ none: false
146
+ requirements:
147
+ - - ">="
148
+ - !ruby/object:Gem::Version
149
+ hash: -1016163088646830549
150
+ segments:
151
+ - 0
152
+ version: "0"
153
+ required_rubygems_version: !ruby/object:Gem::Requirement
154
+ none: false
155
+ requirements:
156
+ - - ">="
157
+ - !ruby/object:Gem::Version
158
+ segments:
159
+ - 0
160
+ version: "0"
161
+ requirements: []
162
+
163
+ rubyforge_project:
164
+ rubygems_version: 1.3.7
165
+ signing_key:
166
+ specification_version: 3
167
+ summary: Generate a form using a specific resource
168
+ test_files:
169
+ - test/helper.rb
170
+ - test/test_magic_form.rb