auto_form 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/auto_form.rb +26 -26
- metadata +12 -1
data/lib/auto_form.rb
CHANGED
@@ -1,31 +1,31 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
1
|
+
module ActionController
|
2
|
+
class Base
|
3
|
+
def self.auto_form(model,method)
|
4
|
+
action_name = "#{method}_#{model.name.underscore}"
|
5
|
+
Rails.logger.info "\n\n\n#{action_name}"
|
6
|
+
define_method(action_name) { |*args|
|
7
|
+
form_erb = "<%= form_for(@params, :as => \"#{method}_args\", :url => { :action => params[:action] }) do |f| %>\
|
8
|
+
<% @params.class.args.keys.each { |a| %>\
|
9
|
+
<%= f.send(@params.class.args[a][:field_type],a) %><br/>\
|
10
|
+
<% } %>\
|
11
|
+
<%= f.submit \"Submit\" %>\
|
12
|
+
<% end %>"
|
13
|
+
args_class = model.const_get(method.to_s.camelize + "Args")
|
14
|
+
if request.post?
|
15
|
+
@params = args_class.new(params["#{method}_args"]) # compliant with ActiveModel API
|
16
|
+
status, message = model.new.approve(@params)
|
17
|
+
if status
|
18
|
+
flash[:notice] = "#{model}##{method} successful"
|
19
|
+
redirect_to '/'
|
20
|
+
else
|
21
|
+
flash.now[:error] = "#{model}##{method} failed: #{message.errors.first.join('-')}"
|
22
|
+
render :layout => true, :inline => form_erb
|
23
|
+
end
|
21
24
|
else
|
22
|
-
|
25
|
+
@params = args_class.new
|
23
26
|
render :layout => true, :inline => form_erb
|
24
27
|
end
|
25
|
-
|
26
|
-
|
27
|
-
render :layout => true, :inline => form_erb
|
28
|
-
end
|
29
|
-
}
|
28
|
+
}
|
29
|
+
end
|
30
30
|
end
|
31
31
|
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: auto_form
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.0.
|
5
|
+
version: 0.0.2
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Nilesh Trivedi
|
@@ -24,6 +24,17 @@ dependencies:
|
|
24
24
|
version: "0"
|
25
25
|
type: :runtime
|
26
26
|
version_requirements: *id001
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: actionpack
|
29
|
+
prerelease: false
|
30
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
31
|
+
none: false
|
32
|
+
requirements:
|
33
|
+
- - ">="
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: 3.1.0
|
36
|
+
type: :runtime
|
37
|
+
version_requirements: *id002
|
27
38
|
description: Use validated_arguments to know metadata about method arguments and the ActiveModel compliant API to build the form
|
28
39
|
email:
|
29
40
|
- github@nileshtrivedi.com
|