ixtlan-generators 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +64 -0
- data/features/generators.feature +6 -0
- data/features/step_definitions/simple_steps.rb +1 -0
- data/lib/generators/ixtlan/base.rb~ +44 -0
- data/lib/generators/ixtlan/base_configuration.rb~ +40 -0
- data/lib/generators/ixtlan/configuration_base.rb~ +44 -0
- data/lib/generators/ixtlan/configuration_model/configuration_generator.rb~ +40 -0
- data/lib/generators/ixtlan/configuration_model/configuration_model_generator.rb~ +10 -0
- data/lib/generators/ixtlan/configuration_model/setup_controller.rb~ +5 -0
- data/lib/generators/ixtlan/configuration_model/setup_generator.rb~ +15 -0
- data/lib/generators/ixtlan/configuration_scaffold/configuration_generator.rb~ +40 -0
- data/lib/generators/ixtlan/configuration_scaffold/configuration_model_generator.rb~ +40 -0
- data/lib/generators/ixtlan/configuration_scaffold/configuration_scaffold_generator.rb~ +13 -0
- data/lib/generators/ixtlan/configuration_scaffold/setup_controller.rb~ +5 -0
- data/lib/generators/ixtlan/configuration_scaffold/setup_generator.rb~ +15 -0
- data/lib/generators/ixtlan/setup/setup_controller.rb~ +5 -0
- data/lib/generators/ixtlan/setup/setup_generator.rb~ +25 -0
- data/lib/generators/ixtlan/setup/templates/error.html.erb~ +1 -0
- data/lib/generators/ixtlan/setup/templates/error_with_session.html.erb~ +1 -0
- data/lib/generators/ixtlan/setup/templates/initializer.rb~ +54 -0
- data/lib/generators/model/model_generator.rb~ +12 -0
- data/lib/generators/rails/active_record/active_record_generator.rb +42 -34
- data/lib/generators/rails/active_record/active_record_generator.rb~ +43 -0
- data/lib/generators/rails/active_record/active_record_generatorr.rb~ +51 -0
- data/lib/generators/rails/active_record/model/model.rb +32 -1
- data/lib/generators/rails/active_record/model/model.rb~ +16 -0
- data/lib/generators/rails/erb/erb_generator.rb~ +32 -0
- data/lib/generators/rails/erb/scaffold/_form.html.erb~ +25 -0
- data/lib/generators/rails/erb/scaffold/index.html.erb~ +47 -0
- data/lib/generators/rails/erb_generator.rb~ +32 -0
- data/lib/generators/rails/scaffold_controller/scaffold_controller/controller.rb +47 -35
- data/lib/generators/rails/scaffold_controller/scaffold_controller/controller.rb~ +148 -0
- data/lib/generators/rails/scaffold_controller/scaffold_controller/singleton_controller.rb +43 -7
- data/lib/generators/rails/scaffold_controller/scaffold_controller/singleton_controller.rb~ +60 -0
- data/lib/generators/scaffold/scaffold_generator.rb~ +31 -0
- data/lib/generators/scaffold_controller/scaffold_controller_generator.rb +3 -0
- data/lib/generators/scaffold_controller/scaffold_controller_generator.rb-~ +48 -0
- data/lib/generators/scaffold_controller/scaffold_controller_generator.rb~ +46 -0
- data/lib/ixtlan/railtie.rb~ +81 -0
- data/lib/ixtlan-generators.rb~ +3 -0
- metadata +43 -10
@@ -0,0 +1,51 @@
|
|
1
|
+
p defined?(ActiveRecord)
|
2
|
+
p !defined?(Rake)
|
3
|
+
if defined?(ActiveRecord) && !defined?(Rake) && false
|
4
|
+
require 'rails/generators/active_record/model/model_generator'
|
5
|
+
|
6
|
+
module ActiveRecord
|
7
|
+
module Generators
|
8
|
+
class ModelGenerator < Base
|
9
|
+
include ::Ixtlan::Generators::Singleton
|
10
|
+
|
11
|
+
argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"
|
12
|
+
|
13
|
+
check_class_collision
|
14
|
+
|
15
|
+
class_option :migration, :type => :boolean
|
16
|
+
class_option :timestamps, :type => :boolean
|
17
|
+
class_option :parent, :type => :string, :desc => "The parent class for the generated model"
|
18
|
+
|
19
|
+
def create_migration_file
|
20
|
+
return unless options[:migration] && options[:parent].nil?
|
21
|
+
migration_template "migration.rb", "db/migrate/create_#{table_name}.rb"
|
22
|
+
end
|
23
|
+
|
24
|
+
def create_model_file
|
25
|
+
template 'model.rb', File.join('app/models', class_path, "#{file_name}.rb")
|
26
|
+
end
|
27
|
+
|
28
|
+
def create_module_file
|
29
|
+
return if class_path.empty?
|
30
|
+
template 'module.rb', File.join('app/models', "#{class_path.join('/')}.rb") if behavior == :invoke
|
31
|
+
end
|
32
|
+
|
33
|
+
hook_for :test_framework
|
34
|
+
|
35
|
+
protected
|
36
|
+
|
37
|
+
def parent_class_name
|
38
|
+
options[:parent] || "ActiveRecord::Base"
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
else
|
46
|
+
module Ixtlan
|
47
|
+
module Dummy
|
48
|
+
end
|
49
|
+
end
|
50
|
+
puts "asd"
|
51
|
+
end
|
@@ -6,11 +6,42 @@ class <%= class_name %> < <%= parent_class_name.classify %>
|
|
6
6
|
<%= attribute.type %> :<%= attribute.name %>
|
7
7
|
<% end -%>
|
8
8
|
<% if options[:modified_by] -%>
|
9
|
-
belongs_to :modified_by, :class_name => "
|
9
|
+
belongs_to :modified_by, :class_name => "User"
|
10
|
+
validates :modified_by_id, :presence => true
|
10
11
|
<% end -%>
|
11
12
|
<% if options[:singleton] -%>
|
12
13
|
def self.instance
|
13
14
|
self.first || self.new
|
14
15
|
end
|
15
16
|
<% end -%>
|
17
|
+
<% unless options[:singleton] -%>
|
18
|
+
|
19
|
+
def self.options
|
20
|
+
{
|
21
|
+
<% except = []
|
22
|
+
except = [:created_at, :updated_at] if options[:timestamps]
|
23
|
+
except << :modified_by_id if options[:modified_by] -%>
|
24
|
+
:except => <%= except.inspect %>
|
25
|
+
}
|
26
|
+
end
|
27
|
+
<% end -%>
|
28
|
+
|
29
|
+
def self.single_options
|
30
|
+
{
|
31
|
+
<% if options[:modified_by] -%>
|
32
|
+
:except => [:modified_by_id],
|
33
|
+
<% end -%>
|
34
|
+
<% if options[:modified_by] || attributes.select {|attr| attr.reference? }.size > 0 -%>
|
35
|
+
:include => {
|
36
|
+
<% if options[:modified_by] -%>
|
37
|
+
:modified_by => {
|
38
|
+
:only => [:id, :login, :name]
|
39
|
+
}<% end -%><% attributes.select {|attr| attr.reference? }.each do |attribute| -%>,
|
40
|
+
:<%= attribute.name %> => {
|
41
|
+
:except => [:created_at, :updated_at, :modified_by_id]
|
42
|
+
}<% end %>
|
43
|
+
}
|
44
|
+
<% end -%>
|
45
|
+
}
|
46
|
+
end
|
16
47
|
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
class <%= class_name %> < <%= parent_class_name.classify %>
|
2
|
+
<% attributes.select {|attr| attr.reference? }.each do |attribute| -%>
|
3
|
+
belongs_to :<%= attribute.name %>
|
4
|
+
<% end -%>
|
5
|
+
<% attributes.select {|attr| [:has_one, :has_many].include?(attr.type) }.each do |attribute| -%>
|
6
|
+
<%= attribute.type %> :<%= attribute.name %>
|
7
|
+
<% end -%>
|
8
|
+
<% if options[:modified_by] -%>
|
9
|
+
belongs_to :modified_by, :class_name => "<%= options[:user_model] %>"
|
10
|
+
<% end -%>
|
11
|
+
<% if options[:singleton] -%>
|
12
|
+
def self.instance
|
13
|
+
self.first || self.new
|
14
|
+
end
|
15
|
+
<% end -%>
|
16
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'rails/generators/erb'
|
2
|
+
require 'rails/generators/resource_helpers'
|
3
|
+
|
4
|
+
module Erb
|
5
|
+
module Generators
|
6
|
+
class ErbGenerator < Base
|
7
|
+
include Rails::Generators::ResourceHelpers
|
8
|
+
|
9
|
+
class_option :optimistic, :type => :boolean, :default => false
|
10
|
+
class_option :timestamps, :type => :boolean, :default => true
|
11
|
+
|
12
|
+
argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"
|
13
|
+
|
14
|
+
def create_root_folder
|
15
|
+
empty_directory File.join("app/views", controller_file_path)
|
16
|
+
end
|
17
|
+
|
18
|
+
def copy_view_files
|
19
|
+
available_views.each do |view|
|
20
|
+
filename = filename_with_extensions(view)
|
21
|
+
template filename, File.join("app/views", controller_file_path, filename)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
protected
|
26
|
+
|
27
|
+
def available_views
|
28
|
+
%w(index edit show new _form)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
<%%= form_for(@<%= singular_table_name %><% if options[:singleton] -%>
|
2
|
+
, :url => <%= singular_table_name %>_path, :html => { :method => :put, :class => "edit_<%= singular_table_name %>", :id => "edit_<%= singular_table_name %>"}<% end -%>
|
3
|
+
) do |f| %>
|
4
|
+
<%% if @<%= singular_table_name %>.errors.any? %>
|
5
|
+
<div id="error_explanation">
|
6
|
+
<h2><%%= pluralize(@<%= singular_table_name %>.errors.count, "error") %> prohibited this <%= singular_table_name %> from being saved:</h2>
|
7
|
+
|
8
|
+
<ul>
|
9
|
+
<%% @<%= singular_table_name %>.errors.full_messages.each do |msg| %>
|
10
|
+
<li><%%= msg %></li>
|
11
|
+
<%% end %>
|
12
|
+
</ul>
|
13
|
+
</div>
|
14
|
+
<%% end %>
|
15
|
+
|
16
|
+
<% for attribute in attributes -%>
|
17
|
+
<div class="field"><!-- begin - <%= attribute.name %> -->
|
18
|
+
<%%= f.label :<%= attribute.name %> %><br />
|
19
|
+
<%%= f.<%= attribute.field_type %> :<%= attribute.name %> %>
|
20
|
+
</div><!-- end - <%= attribute.name %> -->
|
21
|
+
<% end -%>
|
22
|
+
<div class="actions">
|
23
|
+
<%%= f.submit %>
|
24
|
+
</div>
|
25
|
+
<%% end %>
|
@@ -0,0 +1,47 @@
|
|
1
|
+
<h1>Listing <%= plural_table_name %></h1>
|
2
|
+
|
3
|
+
<table>
|
4
|
+
<tr>
|
5
|
+
<% for attribute in attributes -%>
|
6
|
+
<th><%= attribute.human_name %></th>
|
7
|
+
<% end -%>
|
8
|
+
<th></th>
|
9
|
+
<th></th>
|
10
|
+
<th></th>
|
11
|
+
</tr>
|
12
|
+
|
13
|
+
<%% @<%= plural_table_name %>.each do |<%= singular_table_name %>| %>
|
14
|
+
<tr>
|
15
|
+
<% for attribute in attributes -%>
|
16
|
+
<td><%%= <%= singular_table_name %>.<%= attribute.name %> %></td>
|
17
|
+
<% end -%>
|
18
|
+
<% if defined? ::Ixtlan::Guard -%>
|
19
|
+
<%% if allowed?(:<%= table_name %>, :show) %>
|
20
|
+
<% end -%>
|
21
|
+
<td><%%= link_to 'Show', <%= singular_table_name %> %></td>
|
22
|
+
<% if defined? ::Ixtlan::Guard -%>
|
23
|
+
<%% end %>
|
24
|
+
<%% if allowed?(:<%= table_name %>, :update) %>
|
25
|
+
<% end -%>
|
26
|
+
<td><%%= link_to 'Edit', edit_<%= singular_table_name %>_path(<%= singular_table_name %>) %></td>
|
27
|
+
<% if defined? ::Ixtlan::Guard -%>
|
28
|
+
<%% end %>
|
29
|
+
<%% if allowed?(:<%= table_name %>, :destroy) %>
|
30
|
+
<% end -%>
|
31
|
+
<td><%%= link_to 'Destroy', <%= singular_table_name %>, :confirm => 'Are you sure?', :method => :delete %></td>
|
32
|
+
<% if defined? ::Ixtlan::Guard -%>
|
33
|
+
<%% end %>
|
34
|
+
<% end -%>
|
35
|
+
</tr>
|
36
|
+
<%% end %>
|
37
|
+
</table>
|
38
|
+
|
39
|
+
<br />
|
40
|
+
|
41
|
+
<% if defined? ::Ixtlan::Guard -%>
|
42
|
+
<%% if allowed?(:<%= table_name %>, :create) %>
|
43
|
+
<% end -%>
|
44
|
+
<%%= link_to 'New <%= human_name %>', new_<%= singular_table_name %>_path %>
|
45
|
+
<% if defined? ::Ixtlan::Guard -%>
|
46
|
+
<%% end %>
|
47
|
+
<% end -%>
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'rails/generators/erb'
|
2
|
+
require 'rails/generators/resource_helpers'
|
3
|
+
|
4
|
+
module Erb
|
5
|
+
module Generators
|
6
|
+
class ScaffoldGenerator < Base
|
7
|
+
include Rails::Generators::ResourceHelpers
|
8
|
+
|
9
|
+
class_option :optimistic, :type => :boolean, :default => false
|
10
|
+
class_option :timestamps, :type => :boolean, :default => true
|
11
|
+
|
12
|
+
argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"
|
13
|
+
|
14
|
+
def create_root_folder
|
15
|
+
empty_directory File.join("app/views", controller_file_path)
|
16
|
+
end
|
17
|
+
|
18
|
+
def copy_view_files
|
19
|
+
available_views.each do |view|
|
20
|
+
filename = filename_with_extensions(view)
|
21
|
+
template filename, File.join("app/views", controller_file_path, filename)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
protected
|
26
|
+
|
27
|
+
def available_views
|
28
|
+
%w(index edit show new _form)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -1,4 +1,35 @@
|
|
1
1
|
class <%= controller_class_name %>Controller < ApplicationController
|
2
|
+
|
3
|
+
before_filter :cleanup_params
|
4
|
+
|
5
|
+
private
|
6
|
+
|
7
|
+
def cleanup_params
|
8
|
+
# compensate the shortcoming of the incoming json/xml
|
9
|
+
model = params[:<%= singular_table_name %>] || []
|
10
|
+
model.delete :id
|
11
|
+
<% if options[:timestamps] -%>
|
12
|
+
model.delete :created_at
|
13
|
+
<% if options[:optimistic] -%>params[:updated_at] = <% end -%>model.delete :updated_at
|
14
|
+
<% if options[:optimistic] -%>
|
15
|
+
end
|
16
|
+
|
17
|
+
def stale?
|
18
|
+
if @<%= singular_table_name %>.nil?
|
19
|
+
@<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:id]") %>
|
20
|
+
respond_to do |format|
|
21
|
+
format.html { render :action => "edit" }
|
22
|
+
format.xml { render :xml => nil, :status => :conflict }
|
23
|
+
format.json { render :json => nil, :status => :conflict }
|
24
|
+
end
|
25
|
+
true
|
26
|
+
end
|
27
|
+
<% end -%>
|
28
|
+
<% end -%>
|
29
|
+
end
|
30
|
+
|
31
|
+
public
|
32
|
+
|
2
33
|
# GET <%= route_url %>
|
3
34
|
# GET <%= route_url %>.xml
|
4
35
|
# GET <%= route_url %>.json
|
@@ -7,8 +38,8 @@ class <%= controller_class_name %>Controller < ApplicationController
|
|
7
38
|
|
8
39
|
respond_to do |format|
|
9
40
|
format.html # index.html.erb
|
10
|
-
format.xml { render :xml => @<%= plural_table_name
|
11
|
-
format.json { render :json => @<%= plural_table_name
|
41
|
+
format.xml { render :xml => @<%= plural_table_name %>.to_xml(<%= class_name %>.options) }
|
42
|
+
format.json { render :json => @<%= plural_table_name %>.to_json(<%= class_name %>.options) }
|
12
43
|
end
|
13
44
|
end
|
14
45
|
|
@@ -20,8 +51,8 @@ class <%= controller_class_name %>Controller < ApplicationController
|
|
20
51
|
|
21
52
|
respond_to do |format|
|
22
53
|
format.html # show.html.erb
|
23
|
-
format.xml { render :xml => @<%= singular_table_name
|
24
|
-
format.json { render :json => @<%= singular_table_name
|
54
|
+
format.xml { render :xml => @<%= singular_table_name %>.to_xml(<%= class_name %>.single_options) }
|
55
|
+
format.json { render :json => @<%= singular_table_name %>.to_json(<%= class_name %>.single_options) }
|
25
56
|
end
|
26
57
|
end
|
27
58
|
|
@@ -41,14 +72,14 @@ class <%= controller_class_name %>Controller < ApplicationController
|
|
41
72
|
def create
|
42
73
|
@<%= singular_table_name %> = <%= orm_class.build(class_name, "params[:#{singular_table_name}]") %>
|
43
74
|
<% if options[:modified_by] -%>
|
44
|
-
@<%= singular_table_name %>.
|
75
|
+
@<%= singular_table_name %>.modified_by = current_user
|
45
76
|
<% end -%>
|
46
77
|
|
47
78
|
respond_to do |format|
|
48
79
|
if @<%= orm_instance.save %>
|
49
80
|
format.html { redirect_to(@<%= singular_table_name %>, :notice => '<%= human_name %> was successfully created.') }
|
50
|
-
format.xml { render :xml => @<%= singular_table_name
|
51
|
-
format.json { render :json => @<%= singular_table_name
|
81
|
+
format.xml { render :xml => @<%= singular_table_name %>.to_xml(<%= class_name %>.single_options), :status => :created, :location => @<%= singular_table_name %> }
|
82
|
+
format.json { render :json => @<%= singular_table_name %>.to_json(<%= class_name %>.single_options), :status => :created, :location => @<%= singular_table_name %> }
|
52
83
|
else
|
53
84
|
format.html { render :action => "new" }
|
54
85
|
format.xml { render :xml => @<%= orm_instance.errors %>, :status => :unprocessable_entity }
|
@@ -62,31 +93,23 @@ class <%= controller_class_name %>Controller < ApplicationController
|
|
62
93
|
# PUT <%= route_url %>/1.json
|
63
94
|
def update
|
64
95
|
<% if options[:optimistic] && options[:timestamps] -%>
|
65
|
-
@<%= singular_table_name %> = <%= orm_class.find(class_name, "
|
96
|
+
@<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:updated_at], params[:id]").sub(/\.(get|find)/, '.optimistic_\1') %>
|
66
97
|
|
67
|
-
if
|
68
|
-
@<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:id]") %>
|
69
|
-
respond_to do |format|
|
70
|
-
format.html { render :action => "edit" }
|
71
|
-
format.xml { render :xml => nil, :status => :conflict }
|
72
|
-
format.json { render :json => nil, :status => :conflict }
|
73
|
-
end
|
74
|
-
return
|
75
|
-
end
|
98
|
+
return if stale?
|
76
99
|
<% else -%>
|
77
100
|
@<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:id]") %>
|
78
|
-
(params[:<%= singular_table_name %>]||[]).delete(:updated_at)
|
79
101
|
<% end -%>
|
80
|
-
(params[:<%= singular_table_name %>]||[]).delete(:id)
|
81
102
|
<% if options[:modified_by] -%>
|
82
|
-
|
103
|
+
|
104
|
+
params[:<%= singular_table_name %>] ||= {}
|
105
|
+
params[:<%= singular_table_name %>][:modified_by] = current_user
|
83
106
|
<% end -%>
|
84
107
|
|
85
108
|
respond_to do |format|
|
86
109
|
if @<%= orm_instance.update_attributes("params[:#{singular_table_name}]") %>
|
87
110
|
format.html { redirect_to(@<%= singular_table_name %>, :notice => '<%= human_name %> was successfully updated.') }
|
88
|
-
format.xml { render :xml => @<%= singular_table_name
|
89
|
-
format.json { render :json => @<%= singular_table_name
|
111
|
+
format.xml { render :xml => @<%= singular_table_name %>.to_xml(<%= class_name %>.single_options) }
|
112
|
+
format.json { render :json => @<%= singular_table_name %>.to_json(<%= class_name %>.single_options) }
|
90
113
|
else
|
91
114
|
format.html { render :action => "edit" }
|
92
115
|
format.xml { render :xml => @<%= orm_instance.errors %>, :status => :unprocessable_entity }
|
@@ -100,23 +123,12 @@ class <%= controller_class_name %>Controller < ApplicationController
|
|
100
123
|
# DELETE <%= route_url %>/1.json
|
101
124
|
def destroy
|
102
125
|
<% if options[:optimistic] && options[:timestamps] -%>
|
103
|
-
@<%= singular_table_name %> = <%= orm_class.find(class_name, "
|
126
|
+
@<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:updated_at], params[:id]").sub(/\.(get|find)/, '.optimistic_\1') %>
|
104
127
|
|
105
|
-
if
|
106
|
-
@<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:id]") %>
|
107
|
-
respond_to do |format|
|
108
|
-
format.html { render :action => "edit" }
|
109
|
-
format.xml { render :xml => nil, :status => :conflict }
|
110
|
-
format.json { render :json => nil, :status => :conflict }
|
111
|
-
end
|
112
|
-
return
|
113
|
-
end
|
128
|
+
return if stale?
|
114
129
|
<% else -%>
|
115
130
|
@<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:id]") %>
|
116
131
|
<% end -%>
|
117
|
-
<% if options[:modified_by] -%>
|
118
|
-
@<%= singular_table_name %>.current_user = current_user
|
119
|
-
<% end -%>
|
120
132
|
|
121
133
|
@<%= orm_instance.destroy %>
|
122
134
|
|
@@ -0,0 +1,148 @@
|
|
1
|
+
<% field_names = attributes.select {|a| a.type == :belongs_to }.collect{ |a| a.name } -%>
|
2
|
+
<% fields = ":" + field_names.join(", :") -%>
|
3
|
+
class <%= controller_class_name %>Controller < ApplicationController
|
4
|
+
|
5
|
+
before_filter :cleanup_params
|
6
|
+
|
7
|
+
private
|
8
|
+
|
9
|
+
def cleanup_params
|
10
|
+
# compensate the shortcoming of the incoming json/xml
|
11
|
+
model = params[:<%= singular_table_name %>] || []
|
12
|
+
model.delete :id
|
13
|
+
<% if options[:timestamps] -%>
|
14
|
+
model.delete :created_at
|
15
|
+
<% unless options[:optmistic] -%>
|
16
|
+
model.delete :updated_at
|
17
|
+
<% end -%>
|
18
|
+
<% end -%>
|
19
|
+
end
|
20
|
+
|
21
|
+
public
|
22
|
+
|
23
|
+
# GET <%= route_url %>
|
24
|
+
# GET <%= route_url %>.xml
|
25
|
+
# GET <%= route_url %>.json
|
26
|
+
def index
|
27
|
+
@<%= plural_table_name %> = <%= orm_class.all(class_name) %>
|
28
|
+
|
29
|
+
respond_to do |format|
|
30
|
+
format.html # index.html.erb
|
31
|
+
format.xml { render :xml => @<%= plural_table_name %> }
|
32
|
+
format.json { render :json => @<%= plural_table_name %> }
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
# GET <%= route_url %>/1
|
37
|
+
# GET <%= route_url %>/1.xml
|
38
|
+
# GET <%= route_url %>/1.json
|
39
|
+
def show
|
40
|
+
@<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:id]") %>
|
41
|
+
|
42
|
+
respond_to do |format|
|
43
|
+
format.html # show.html.erb
|
44
|
+
format.xml { render :xml => @<%= singular_table_name %> }
|
45
|
+
format.json { render :json => @<%= singular_table_name %><% if fields -%>.to_json(:methods => [<%= fields %>])<% end -%> }
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
# GET <%= route_url %>/new
|
50
|
+
def new
|
51
|
+
@<%= singular_table_name %> = <%= orm_class.build(class_name) %>
|
52
|
+
end
|
53
|
+
|
54
|
+
# GET <%= route_url %>/1/edit
|
55
|
+
def edit
|
56
|
+
@<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:id]") %>
|
57
|
+
end
|
58
|
+
|
59
|
+
# POST <%= route_url %>
|
60
|
+
# POST <%= route_url %>.xml
|
61
|
+
# POST <%= route_url %>.json
|
62
|
+
def create
|
63
|
+
@<%= singular_table_name %> = <%= orm_class.build(class_name, "params[:#{singular_table_name}]") %>
|
64
|
+
<% if options[:modified_by] -%>
|
65
|
+
@<%= singular_table_name %>.current_user = current_user
|
66
|
+
<% end -%>
|
67
|
+
|
68
|
+
respond_to do |format|
|
69
|
+
if @<%= orm_instance.save %>
|
70
|
+
format.html { redirect_to(@<%= singular_table_name %>, :notice => '<%= human_name %> was successfully created.') }
|
71
|
+
format.xml { render :xml => @<%= singular_table_name %>, :status => :created, :location => @<%= singular_table_name %> }
|
72
|
+
format.json { render :json => @<%= singular_table_name %><% if fields -%>.to_json(:methods => [<%= fields %>])<% end -%>, :status => :created, :location => @<%= singular_table_name %> }
|
73
|
+
else
|
74
|
+
format.html { render :action => "new" }
|
75
|
+
format.xml { render :xml => @<%= orm_instance.errors %>, :status => :unprocessable_entity }
|
76
|
+
format.json { render :json => @<%= orm_instance.errors %>, :status => :unprocessable_entity }
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
# PUT <%= route_url %>/1
|
82
|
+
# PUT <%= route_url %>/1.xml
|
83
|
+
# PUT <%= route_url %>/1.json
|
84
|
+
def update
|
85
|
+
<% if options[:optimistic] && options[:timestamps] -%>
|
86
|
+
@<%= singular_table_name %> = <%= orm_class.find(class_name, "(params[:#{singular_table_name}]||[]).delete(:updated_at), params[:id]").sub(/\.(get|find)/, '.optimistic_\1') %>
|
87
|
+
|
88
|
+
if @<%= singular_table_name %>.nil?
|
89
|
+
@<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:id]") %>
|
90
|
+
respond_to do |format|
|
91
|
+
format.html { render :action => "edit" }
|
92
|
+
format.xml { render :xml => nil, :status => :conflict }
|
93
|
+
format.json { render :json => nil, :status => :conflict }
|
94
|
+
end
|
95
|
+
return
|
96
|
+
end
|
97
|
+
<% else -%>
|
98
|
+
@<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:id]") %>
|
99
|
+
<% end -%>
|
100
|
+
<% if options[:modified_by] -%>
|
101
|
+
@<%= singular_table_name %>.current_user = current_user
|
102
|
+
<% end -%>
|
103
|
+
|
104
|
+
respond_to do |format|
|
105
|
+
if @<%= orm_instance.update_attributes("params[:#{singular_table_name}]") %>
|
106
|
+
format.html { redirect_to(@<%= singular_table_name %>, :notice => '<%= human_name %> was successfully updated.') }
|
107
|
+
format.xml { render :xml => @<%= singular_table_name %> }
|
108
|
+
format.json { render :json => @<%= singular_table_name %><% if fields -%>.to_json(:methods => [<%= fields %>])<% end -%> }
|
109
|
+
else
|
110
|
+
format.html { render :action => "edit" }
|
111
|
+
format.xml { render :xml => @<%= orm_instance.errors %>, :status => :unprocessable_entity }
|
112
|
+
format.json { render :json => @<%= orm_instance.errors %>, :status => :unprocessable_entity }
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
# DELETE <%= route_url %>/1
|
118
|
+
# DELETE <%= route_url %>/1.xml
|
119
|
+
# DELETE <%= route_url %>/1.json
|
120
|
+
def destroy
|
121
|
+
<% if options[:optimistic] && options[:timestamps] -%>
|
122
|
+
@<%= singular_table_name %> = <%= orm_class.find(class_name, "(params[:#{singular_table_name}]||[]).delete(:updated_at), params[:id]").sub(/\.(get|find)/, '.optimistic_\1') %>
|
123
|
+
|
124
|
+
if @<%= singular_table_name %>.nil?
|
125
|
+
@<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:id]") %>
|
126
|
+
respond_to do |format|
|
127
|
+
format.html { render :action => "edit" }
|
128
|
+
format.xml { render :xml => nil, :status => :conflict }
|
129
|
+
format.json { render :json => nil, :status => :conflict }
|
130
|
+
end
|
131
|
+
return
|
132
|
+
end
|
133
|
+
<% else -%>
|
134
|
+
@<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:id]") %>
|
135
|
+
<% end -%>
|
136
|
+
<% if options[:modified_by] -%>
|
137
|
+
@<%= singular_table_name %>.current_user = current_user
|
138
|
+
<% end -%>
|
139
|
+
|
140
|
+
@<%= orm_instance.destroy %>
|
141
|
+
|
142
|
+
respond_to do |format|
|
143
|
+
format.html { redirect_to(<%= index_helper %>_url) }
|
144
|
+
format.xml { head :ok }
|
145
|
+
format.json { head :ok }
|
146
|
+
end
|
147
|
+
end
|
148
|
+
end
|
@@ -1,5 +1,34 @@
|
|
1
1
|
class <%= controller_class_name %>Controller < ApplicationController
|
2
2
|
|
3
|
+
before_filter :cleanup_params
|
4
|
+
|
5
|
+
private
|
6
|
+
|
7
|
+
def cleanup_params
|
8
|
+
# compensate the shortcoming of the incoming json/xml
|
9
|
+
model = params[:<%= singular_table_name %>] || []
|
10
|
+
<% if options[:timestamps] -%>
|
11
|
+
model.delete :created_at
|
12
|
+
<% if options[:optimistic] -%>params[:updated_at] = <% end -%>model.delete :updated_at
|
13
|
+
<% if options[:optimistic] -%>
|
14
|
+
end
|
15
|
+
|
16
|
+
def stale?
|
17
|
+
if @<%= singular_table_name %>.nil?
|
18
|
+
@<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:id]") %>
|
19
|
+
respond_to do |format|
|
20
|
+
format.html { render :action => "edit" }
|
21
|
+
format.xml { render :xml => nil, :status => :conflict }
|
22
|
+
format.json { render :json => nil, :status => :conflict }
|
23
|
+
end
|
24
|
+
true
|
25
|
+
end
|
26
|
+
<% end -%>
|
27
|
+
<% end -%>
|
28
|
+
end
|
29
|
+
|
30
|
+
public
|
31
|
+
|
3
32
|
# GET <%= route_url %>
|
4
33
|
# GET <%= route_url %>.xml
|
5
34
|
# GET <%= route_url %>.json
|
@@ -8,8 +37,8 @@ class <%= controller_class_name %>Controller < ApplicationController
|
|
8
37
|
|
9
38
|
respond_to do |format|
|
10
39
|
format.html # show.html.erb
|
11
|
-
format.xml { render :xml => @<%= singular_table_name
|
12
|
-
format.json { render :json => @<%= singular_table_name
|
40
|
+
format.xml { render :xml => @<%= singular_table_name %>.to_xml(<%= class_name %>.single_options) }
|
41
|
+
format.json { render :json => @<%= singular_table_name %>.to_json(<%= class_name %>.single_options) }
|
13
42
|
end
|
14
43
|
end
|
15
44
|
|
@@ -22,17 +51,24 @@ class <%= controller_class_name %>Controller < ApplicationController
|
|
22
51
|
# PUT <%= route_url %>.xml
|
23
52
|
# PUT <%= route_url %>.json
|
24
53
|
def update
|
54
|
+
<% if options[:optimistic] && options[:timestamps] -%>
|
55
|
+
@<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:updated_at], #{class_name}.instance.id").sub(/\.(get|find)/, '.optimistic_\1') %>
|
56
|
+
|
57
|
+
return if stale?
|
58
|
+
<% else -%>
|
25
59
|
@<%= singular_table_name %> = <%= class_name %>.instance
|
26
|
-
<%
|
27
|
-
|
28
|
-
|
60
|
+
<% end -%>
|
61
|
+
<% if options[:modified_by] -%>
|
62
|
+
|
63
|
+
params[:<%= singular_table_name %>] ||= {}
|
64
|
+
params[:<%= singular_table_name %>][:modified_by] = current_user
|
29
65
|
<% end -%>
|
30
66
|
|
31
67
|
respond_to do |format|
|
32
68
|
if @<%= orm_instance.update_attributes("params[:#{singular_table_name}]") %>
|
33
69
|
format.html { redirect_to(<%= singular_table_name %>_path, :notice => '<%= human_name %> was successfully updated.') }
|
34
|
-
format.xml { render :xml => @<%= singular_table_name
|
35
|
-
format.json { render :json => @<%= singular_table_name
|
70
|
+
format.xml { render :xml => @<%= singular_table_name %>.to_xml(<%= class_name %>.single_options) }
|
71
|
+
format.json { render :json => @<%= singular_table_name %>.to_json(<%= class_name %>.single_options) }
|
36
72
|
else
|
37
73
|
format.html { render :action => "edit" }
|
38
74
|
format.xml { render :xml => @<%= orm_instance.errors %>, :status => :unprocessable_entity }
|
@@ -0,0 +1,60 @@
|
|
1
|
+
class <%= controller_class_name %>Controller < ApplicationController
|
2
|
+
|
3
|
+
before_filter :cleanup_params
|
4
|
+
|
5
|
+
private
|
6
|
+
|
7
|
+
def cleanup_params
|
8
|
+
# compensate the shortcoming of the incoming json/xml
|
9
|
+
model = params[:<%= singular_table_name %>] || []
|
10
|
+
<% if options[:timestamps] -%>
|
11
|
+
model.delete :created_at
|
12
|
+
<% unless options[:optmistic] -%>
|
13
|
+
model.delete :updated_at
|
14
|
+
<% end -%>
|
15
|
+
<% end -%>
|
16
|
+
end
|
17
|
+
|
18
|
+
public
|
19
|
+
|
20
|
+
# GET <%= route_url %>
|
21
|
+
# GET <%= route_url %>.xml
|
22
|
+
# GET <%= route_url %>.json
|
23
|
+
def show
|
24
|
+
@<%= singular_table_name %> = <%= class_name %>.instance
|
25
|
+
|
26
|
+
respond_to do |format|
|
27
|
+
format.html # show.html.erb
|
28
|
+
format.xml { render :xml => @<%= singular_table_name %> }
|
29
|
+
format.json { render :json => @<%= singular_table_name %> }
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
# GET <%= route_url %>/edit
|
34
|
+
def edit
|
35
|
+
@<%= singular_table_name %> = <%= class_name %>.instance
|
36
|
+
end
|
37
|
+
|
38
|
+
# PUT <%= route_url %>
|
39
|
+
# PUT <%= route_url %>.xml
|
40
|
+
# PUT <%= route_url %>.json
|
41
|
+
def update
|
42
|
+
@<%= singular_table_name %> = <%= class_name %>.instance
|
43
|
+
<% orm_class.find(class_name)
|
44
|
+
if options[:modified_by] -%>
|
45
|
+
@<%= singular_table_name %>.current_user = current_user
|
46
|
+
<% end -%>
|
47
|
+
|
48
|
+
respond_to do |format|
|
49
|
+
if @<%= orm_instance.update_attributes("params[:#{singular_table_name}]") %>
|
50
|
+
format.html { redirect_to(<%= singular_table_name %>_path, :notice => '<%= human_name %> was successfully updated.') }
|
51
|
+
format.xml { render :xml => @<%= singular_table_name %> }
|
52
|
+
format.json { render :json => @<%= singular_table_name %> }
|
53
|
+
else
|
54
|
+
format.html { render :action => "edit" }
|
55
|
+
format.xml { render :xml => @<%= orm_instance.errors %>, :status => :unprocessable_entity }
|
56
|
+
format.json { render :json => @<%= orm_instance.errors %>, :status => :unprocessable_entity }
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|