urubatan-mydry_generator 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README +25 -0
- data/generators/drymodel/drymodel_generator.rb +13 -0
- data/generators/drymodel/templates/fixtures.yml +19 -0
- data/generators/drymodel/templates/migration.rb +16 -0
- data/generators/drymodel/templates/model.rb +15 -0
- data/generators/drymodel/templates/unit_test.rb +27 -0
- data/generators/dryscaffold/dryscaffold_generator.rb +95 -0
- data/generators/dryscaffold/templates/controller.rb +94 -0
- data/generators/dryscaffold/templates/functional_test.rb +45 -0
- data/generators/dryscaffold/templates/helper.rb +2 -0
- data/generators/dryscaffold/templates/layout.html.erb +17 -0
- data/generators/dryscaffold/templates/style.css +74 -0
- data/generators/dryscaffold/templates/view_edit.html.erb +25 -0
- data/generators/dryscaffold/templates/view_index.html.erb +25 -0
- data/generators/dryscaffold/templates/view_new.html.erb +25 -0
- data/generators/dryscaffold/templates/view_show.html.erb +10 -0
- data/lib/mydry.rb +42 -0
- data/test/mydry_test.rb +8 -0
- metadata +94 -0
data/README
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
Mydry
|
|
2
|
+
=====
|
|
3
|
+
|
|
4
|
+
This plugin emulates some of the old "scaffold" functionality, but using RESTFul controllers, will_paginate for pagination, partials for forms and enforces the model first approach.
|
|
5
|
+
|
|
6
|
+
The basic workflow is this:
|
|
7
|
+
1 - create a migration and define your initial table
|
|
8
|
+
2 - ./script/generate drymodel model_name
|
|
9
|
+
here the model file generated for you, will have all the validations and belongs defined in the database table
|
|
10
|
+
you can add more validations, configure relationships, code your model
|
|
11
|
+
3 - ./script/generate dryscaffold model_name
|
|
12
|
+
here the scaffold will import the model definition (currently it is working with the database, but the next release will import from the model), and generate all the boilerplate code for you.
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
The origin of this plugin was the need to define a validation in the migration (Eg.: :null => false), in the model again (Eg.: validates_presence_of :column), then again in the view, ...
|
|
16
|
+
The way this generators work I define one thing once and it is generated in the other places for me.
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
Example
|
|
20
|
+
=======
|
|
21
|
+
|
|
22
|
+
Example goes here.
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
Copyright (c) 2008 Rodrigo Urubatan Ferreira Jardim, released under the MIT license
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
require 'mydry'
|
|
2
|
+
|
|
3
|
+
class DrymodelGenerator < Mydry::GeneratorBase
|
|
4
|
+
def manifest
|
|
5
|
+
load_meta_from_db
|
|
6
|
+
|
|
7
|
+
record do |m|
|
|
8
|
+
m.template 'model.rb', "app/models/#{file_name}.rb"
|
|
9
|
+
m.template 'fixtures.yml', "test/fixtures/#{file_name.pluralize}.yml"
|
|
10
|
+
m.template 'unit_test.rb', "test/unit/#{file_name}_test.rb"
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
end
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
|
|
2
|
+
|
|
3
|
+
<% unless attributes.empty? -%>
|
|
4
|
+
one:
|
|
5
|
+
<% for attribute in attributes -%>
|
|
6
|
+
<%= attribute.name %>: <%= attribute.default %>
|
|
7
|
+
<% end -%>
|
|
8
|
+
|
|
9
|
+
two:
|
|
10
|
+
<% for attribute in attributes -%>
|
|
11
|
+
<%= attribute.name %>: <%= attribute.default %>
|
|
12
|
+
<% end -%>
|
|
13
|
+
<% else -%>
|
|
14
|
+
# one:
|
|
15
|
+
# column: value
|
|
16
|
+
#
|
|
17
|
+
# two:
|
|
18
|
+
# column: value
|
|
19
|
+
<% end -%>
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
class <%= migration_name %> < ActiveRecord::Migration
|
|
2
|
+
def self.up
|
|
3
|
+
create_table :<%= table_name %> do |t|
|
|
4
|
+
<% for attribute in attributes -%>
|
|
5
|
+
t.<%= attribute.type %> :<%= attribute.name %>
|
|
6
|
+
<% end -%>
|
|
7
|
+
<% unless options[:skip_timestamps] %>
|
|
8
|
+
t.timestamps
|
|
9
|
+
<% end -%>
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def self.down
|
|
14
|
+
drop_table :<%= table_name %>
|
|
15
|
+
end
|
|
16
|
+
end
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
class <%= class_name %> < ActiveRecord::Base
|
|
2
|
+
#Relationships
|
|
3
|
+
<% belongs.each do |b| %>belongs_to :<%= b %>
|
|
4
|
+
<% end %>
|
|
5
|
+
#Validations
|
|
6
|
+
<%- notnulls.each do |nn| %>validates_presence_of :<%= nn %>
|
|
7
|
+
<% end -%>
|
|
8
|
+
<%- numerics.each do |num| %>validates_numericality_of :<%= num %>, :allow_nil => true
|
|
9
|
+
<% end -%>
|
|
10
|
+
<%- lengths.each do |c,s| %>validates_length_of :<%= c %>, :within => 0..<%= s %>, :allow_nil => true
|
|
11
|
+
<% end -%>
|
|
12
|
+
<%- belongs.each do |b| %>validates_associated :<%= b %>, :allow_nil => true
|
|
13
|
+
<% end -%>
|
|
14
|
+
|
|
15
|
+
end
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
require File.dirname(__FILE__) + '<%= '/..' * class_nesting_depth %>/../test_helper'
|
|
2
|
+
|
|
3
|
+
class <%= class_name %>Test < ActiveSupport::TestCase
|
|
4
|
+
# Replace this with your real tests.
|
|
5
|
+
def test_truth
|
|
6
|
+
assert true
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
<%- notnulls.each do |nn| %>
|
|
10
|
+
def test_<%= nn %>_not_null
|
|
11
|
+
assert false
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
<% end -%>
|
|
15
|
+
<%- lengths.each do |nn| %>
|
|
16
|
+
def test_<%= nn[0] %>_length
|
|
17
|
+
assert false
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
<% end -%>
|
|
21
|
+
<%- numerics.each do |nn| %>
|
|
22
|
+
def test_<%= nn %>_numeric_only
|
|
23
|
+
assert false
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
<% end -%>
|
|
27
|
+
end
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
require 'mydry'
|
|
2
|
+
|
|
3
|
+
class DryscaffoldGenerator < Mydry::GeneratorBase
|
|
4
|
+
default_options :skip_timestamps => false, :skip_migration => false
|
|
5
|
+
|
|
6
|
+
attr_reader :controller_name,
|
|
7
|
+
:controller_class_path,
|
|
8
|
+
:controller_file_path,
|
|
9
|
+
:controller_class_nesting,
|
|
10
|
+
:controller_class_nesting_depth,
|
|
11
|
+
:controller_class_name,
|
|
12
|
+
:controller_underscore_name,
|
|
13
|
+
:controller_singular_name,
|
|
14
|
+
:controller_plural_name
|
|
15
|
+
alias_method :controller_file_name, :controller_underscore_name
|
|
16
|
+
alias_method :controller_table_name, :controller_plural_name
|
|
17
|
+
|
|
18
|
+
def initialize(runtime_args, runtime_options = {})
|
|
19
|
+
super
|
|
20
|
+
|
|
21
|
+
@controller_name = @name.pluralize
|
|
22
|
+
|
|
23
|
+
base_name, @controller_class_path, @controller_file_path, @controller_class_nesting, @controller_class_nesting_depth = extract_modules(@controller_name)
|
|
24
|
+
@controller_class_name_without_nesting, @controller_underscore_name, @controller_plural_name = inflect_names(base_name)
|
|
25
|
+
@controller_singular_name=base_name.singularize
|
|
26
|
+
if @controller_class_nesting.empty?
|
|
27
|
+
@controller_class_name = @controller_class_name_without_nesting
|
|
28
|
+
else
|
|
29
|
+
@controller_class_name = "#{@controller_class_nesting}::#{@controller_class_name_without_nesting}"
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def manifest
|
|
34
|
+
load_meta_from_db
|
|
35
|
+
record do |m|
|
|
36
|
+
# Check for class naming collisions.
|
|
37
|
+
m.class_collisions(controller_class_path, "#{controller_class_name}Controller", "#{controller_class_name}Helper")
|
|
38
|
+
m.class_collisions(class_path, "#{class_name}")
|
|
39
|
+
|
|
40
|
+
# Controller, helper, views, and test directories.
|
|
41
|
+
m.directory(File.join('app/models', class_path))
|
|
42
|
+
m.directory(File.join('app/controllers', controller_class_path))
|
|
43
|
+
m.directory(File.join('app/helpers', controller_class_path))
|
|
44
|
+
m.directory(File.join('app/views', controller_class_path, controller_file_name))
|
|
45
|
+
m.directory(File.join('app/views/layouts', controller_class_path))
|
|
46
|
+
m.directory(File.join('test/functional', controller_class_path))
|
|
47
|
+
m.directory(File.join('test/unit', class_path))
|
|
48
|
+
|
|
49
|
+
for action in scaffold_views
|
|
50
|
+
m.template(
|
|
51
|
+
"view_#{action}.html.erb",
|
|
52
|
+
File.join('app/views', controller_class_path, controller_file_name, "#{action}.html.erb")
|
|
53
|
+
)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# Layout and stylesheet.
|
|
57
|
+
m.template('layout.html.erb', File.join('app/views/layouts', controller_class_path, "#{controller_file_name}.html.erb"))
|
|
58
|
+
m.template('style.css', 'public/stylesheets/scaffold.css')
|
|
59
|
+
|
|
60
|
+
m.dependency 'drymodel', [name] + @args, :collision => :skip
|
|
61
|
+
|
|
62
|
+
m.template(
|
|
63
|
+
'controller.rb', File.join('app/controllers', controller_class_path, "#{controller_file_name}_controller.rb")
|
|
64
|
+
)
|
|
65
|
+
|
|
66
|
+
m.template('functional_test.rb', File.join('test/functional', controller_class_path, "#{controller_file_name}_controller_test.rb"))
|
|
67
|
+
m.template('helper.rb', File.join('app/helpers', controller_class_path, "#{controller_file_name}_helper.rb"))
|
|
68
|
+
|
|
69
|
+
m.route_resources controller_file_name
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
protected
|
|
74
|
+
# Override with your own usage banner.
|
|
75
|
+
def banner
|
|
76
|
+
"Usage: #{$0} scaffold ModelName [field:type, field:type]"
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def add_options!(opt)
|
|
80
|
+
opt.separator ''
|
|
81
|
+
opt.separator 'Options:'
|
|
82
|
+
opt.on("--skip-timestamps",
|
|
83
|
+
"Don't add timestamps to the migration file for this model") { |v| options[:skip_timestamps] = v }
|
|
84
|
+
opt.on("--skip-migration",
|
|
85
|
+
"Don't generate a migration file for this model") { |v| options[:skip_migration] = v }
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def scaffold_views
|
|
89
|
+
%w[ index show new edit ]
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def model_name
|
|
93
|
+
class_name.demodulize
|
|
94
|
+
end
|
|
95
|
+
end
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
class <%= controller_class_name %>Controller < ApplicationController
|
|
2
|
+
# GET /<%= table_name %>
|
|
3
|
+
# GET /<%= table_name %>.xml
|
|
4
|
+
def index
|
|
5
|
+
@<%= table_name %> = <%= class_name %>.paginate :page => params[:page]
|
|
6
|
+
|
|
7
|
+
respond_to do |format|
|
|
8
|
+
format.html # index.html.erb
|
|
9
|
+
format.xml { render :xml => @<%= table_name %> }
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
# GET /<%= table_name %>/1
|
|
14
|
+
# GET /<%= table_name %>/1.xml
|
|
15
|
+
def show
|
|
16
|
+
@<%= file_name %> = <%= class_name %>.find(params[:id])
|
|
17
|
+
|
|
18
|
+
respond_to do |format|
|
|
19
|
+
format.html # show.html.erb
|
|
20
|
+
format.xml { render :xml => @<%= file_name %> }
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# GET /<%= table_name %>/new
|
|
25
|
+
# GET /<%= table_name %>/new.xml
|
|
26
|
+
def new
|
|
27
|
+
@<%= file_name %> = <%= class_name %>.new
|
|
28
|
+
load_relations
|
|
29
|
+
|
|
30
|
+
respond_to do |format|
|
|
31
|
+
format.html # new.html.erb
|
|
32
|
+
format.xml { render :xml => @<%= file_name %> }
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# GET /<%= table_name %>/1/edit
|
|
37
|
+
def edit
|
|
38
|
+
@<%= file_name %> = <%= class_name %>.find(params[:id])
|
|
39
|
+
load_relations
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# POST /<%= table_name %>
|
|
43
|
+
# POST /<%= table_name %>.xml
|
|
44
|
+
def create
|
|
45
|
+
@<%= file_name %> = <%= class_name %>.new(params[:<%= file_name %>])
|
|
46
|
+
|
|
47
|
+
respond_to do |format|
|
|
48
|
+
if @<%= file_name %>.save
|
|
49
|
+
flash[:notice] = '<%= class_name %> was successfully created.'
|
|
50
|
+
format.html { redirect_to(@<%= file_name %>) }
|
|
51
|
+
format.xml { render :xml => @<%= file_name %>, :status => :created, :location => @<%= file_name %> }
|
|
52
|
+
else
|
|
53
|
+
format.html { render :action => "new" }
|
|
54
|
+
format.xml { render :xml => @<%= file_name %>.errors, :status => :unprocessable_entity }
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# PUT /<%= table_name %>/1
|
|
60
|
+
# PUT /<%= table_name %>/1.xml
|
|
61
|
+
def update
|
|
62
|
+
@<%= file_name %> = <%= class_name %>.find(params[:id])
|
|
63
|
+
|
|
64
|
+
respond_to do |format|
|
|
65
|
+
if @<%= file_name %>.update_attributes(params[:<%= file_name %>])
|
|
66
|
+
flash[:notice] = '<%= class_name %> was successfully updated.'
|
|
67
|
+
format.html { redirect_to(@<%= file_name %>) }
|
|
68
|
+
format.xml { head :ok }
|
|
69
|
+
else
|
|
70
|
+
format.html { render :action => "edit" }
|
|
71
|
+
format.xml { render :xml => @<%= file_name %>.errors, :status => :unprocessable_entity }
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# DELETE /<%= table_name %>/1
|
|
77
|
+
# DELETE /<%= table_name %>/1.xml
|
|
78
|
+
def destroy
|
|
79
|
+
@<%= file_name %> = <%= class_name %>.find(params[:id])
|
|
80
|
+
@<%= file_name %>.destroy
|
|
81
|
+
|
|
82
|
+
respond_to do |format|
|
|
83
|
+
format.html { redirect_to(<%= table_name %>_url) }
|
|
84
|
+
format.xml { head :ok }
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
private
|
|
89
|
+
def load_relations
|
|
90
|
+
<% belongs.each do |b| %>
|
|
91
|
+
@<%= b.pluralize %> = <%= b.camelcase %>.find(:all)
|
|
92
|
+
<% end %>
|
|
93
|
+
end
|
|
94
|
+
end
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
require File.dirname(__FILE__) + '<%= '/..' * class_nesting_depth %>/../test_helper'
|
|
2
|
+
|
|
3
|
+
class <%= controller_class_name %>ControllerTest < ActionController::TestCase
|
|
4
|
+
def test_should_get_index
|
|
5
|
+
get :index
|
|
6
|
+
assert_response :success
|
|
7
|
+
assert_not_nil assigns(:<%= table_name %>)
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def test_should_get_new
|
|
11
|
+
get :new
|
|
12
|
+
assert_response :success
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def test_should_create_<%= file_name %>
|
|
16
|
+
assert_difference('<%= class_name %>.count') do
|
|
17
|
+
post :create, :<%= file_name %> => { }
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
assert_redirected_to <%= file_name %>_path(assigns(:<%= file_name %>))
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def test_should_show_<%= file_name %>
|
|
24
|
+
get :show, :id => <%= table_name %>(:one).id
|
|
25
|
+
assert_response :success
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def test_should_get_edit
|
|
29
|
+
get :edit, :id => <%= table_name %>(:one).id
|
|
30
|
+
assert_response :success
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def test_should_update_<%= file_name %>
|
|
34
|
+
put :update, :id => <%= table_name %>(:one).id, :<%= file_name %> => { }
|
|
35
|
+
assert_redirected_to <%= file_name %>_path(assigns(:<%= file_name %>))
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def test_should_destroy_<%= file_name %>
|
|
39
|
+
assert_difference('<%= class_name %>.count', -1) do
|
|
40
|
+
delete :destroy, :id => <%= table_name %>(:one).id
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
assert_redirected_to <%= table_name %>_path
|
|
44
|
+
end
|
|
45
|
+
end
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
|
2
|
+
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
|
3
|
+
|
|
4
|
+
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
|
5
|
+
<head>
|
|
6
|
+
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
|
|
7
|
+
<title><%= controller_class_name %>: <%%= controller.action_name %></title>
|
|
8
|
+
<%%= stylesheet_link_tag 'scaffold' %>
|
|
9
|
+
</head>
|
|
10
|
+
<body>
|
|
11
|
+
|
|
12
|
+
<p style="color: green"><%%= flash[:notice] %></p>
|
|
13
|
+
|
|
14
|
+
<%%= yield %>
|
|
15
|
+
|
|
16
|
+
</body>
|
|
17
|
+
</html>
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
body { background-color: #fff; color: #333; }
|
|
2
|
+
|
|
3
|
+
body, p, ol, ul, td {
|
|
4
|
+
font-family: verdana, arial, helvetica, sans-serif;
|
|
5
|
+
font-size: 13px;
|
|
6
|
+
line-height: 18px;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
pre {
|
|
10
|
+
background-color: #eee;
|
|
11
|
+
padding: 10px;
|
|
12
|
+
font-size: 11px;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
a { color: #000; }
|
|
16
|
+
a:visited { color: #666; }
|
|
17
|
+
a:hover { color: #fff; background-color:#000; }
|
|
18
|
+
|
|
19
|
+
.fieldWithErrors {
|
|
20
|
+
padding: 2px;
|
|
21
|
+
background-color: red;
|
|
22
|
+
display: table;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
#errorExplanation {
|
|
26
|
+
width: 400px;
|
|
27
|
+
border: 2px solid red;
|
|
28
|
+
padding: 7px;
|
|
29
|
+
padding-bottom: 12px;
|
|
30
|
+
margin-bottom: 20px;
|
|
31
|
+
background-color: #f0f0f0;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
#errorExplanation h2 {
|
|
35
|
+
text-align: left;
|
|
36
|
+
font-weight: bold;
|
|
37
|
+
padding: 5px 5px 5px 15px;
|
|
38
|
+
font-size: 12px;
|
|
39
|
+
margin: -7px;
|
|
40
|
+
background-color: #c00;
|
|
41
|
+
color: #fff;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
#errorExplanation p {
|
|
45
|
+
color: #333;
|
|
46
|
+
margin-bottom: 0;
|
|
47
|
+
padding: 5px;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
#errorExplanation ul li {
|
|
51
|
+
font-size: 12px;
|
|
52
|
+
list-style: square;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
div.uploadStatus {
|
|
56
|
+
margin: 5px;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
div.progressBar {
|
|
60
|
+
margin: 5px;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
div.progressBar div.border {
|
|
64
|
+
background-color: #fff;
|
|
65
|
+
border: 1px solid gray;
|
|
66
|
+
width: 100%;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
div.progressBar div.background {
|
|
70
|
+
background-color: #333;
|
|
71
|
+
height: 18px;
|
|
72
|
+
width: 0%;
|
|
73
|
+
}
|
|
74
|
+
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
<h1>Editing <%= singular_name %></h1>
|
|
2
|
+
|
|
3
|
+
<%%= error_messages_for :<%= singular_name %> %>
|
|
4
|
+
|
|
5
|
+
<%% form_for(@<%= singular_name %>) do |f| %>
|
|
6
|
+
<% for attribute in attributes -%>
|
|
7
|
+
<p>
|
|
8
|
+
<%%= f.label :<%= attribute.name %> %><br />
|
|
9
|
+
<%%= f.<%= attribute.field_type %> :<%= attribute.name %> %>
|
|
10
|
+
</p>
|
|
11
|
+
|
|
12
|
+
<% end -%>
|
|
13
|
+
<% for rel in belongs -%>
|
|
14
|
+
<p>
|
|
15
|
+
<%%= f.label :<%= rel %>_id %><br/>
|
|
16
|
+
<%%= f.collection_select :<%= rel %>_id, @<%= rel.pluralize %>, 'id', 'id' %>
|
|
17
|
+
</p>
|
|
18
|
+
<% end -%>
|
|
19
|
+
<p>
|
|
20
|
+
<%%= f.submit "Update" %>
|
|
21
|
+
</p>
|
|
22
|
+
<%% end %>
|
|
23
|
+
|
|
24
|
+
<%%= link_to 'Show', @<%= singular_name %> %> |
|
|
25
|
+
<%%= link_to 'Back', <%= plural_name %>_path %>
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
<h1>Listing <%= plural_name %></h1>
|
|
2
|
+
|
|
3
|
+
<table>
|
|
4
|
+
<tr>
|
|
5
|
+
<% for attribute in attributes -%>
|
|
6
|
+
<th><%= attribute.column.human_name %></th>
|
|
7
|
+
<% end -%>
|
|
8
|
+
</tr>
|
|
9
|
+
|
|
10
|
+
<%% for <%= singular_name %> in @<%= plural_name %> %>
|
|
11
|
+
<tr>
|
|
12
|
+
<% for attribute in attributes -%>
|
|
13
|
+
<td><%%=h <%= singular_name %>.<%= attribute.name %> %></td>
|
|
14
|
+
<% end -%>
|
|
15
|
+
<td><%%= link_to 'Show', <%= singular_name %> %></td>
|
|
16
|
+
<td><%%= link_to 'Edit', edit_<%= singular_name %>_path(<%= singular_name %>) %></td>
|
|
17
|
+
<td><%%= link_to 'Destroy', <%= singular_name %>, :confirm => 'Are you sure?', :method => :delete %></td>
|
|
18
|
+
</tr>
|
|
19
|
+
<%% end %>
|
|
20
|
+
</table>
|
|
21
|
+
<%%= will_paginate @<%= plural_name %> %>
|
|
22
|
+
|
|
23
|
+
<br />
|
|
24
|
+
|
|
25
|
+
<%%= link_to 'New <%= singular_name %>', new_<%= singular_name %>_path %>
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
<h1>New <%= singular_name %></h1>
|
|
2
|
+
|
|
3
|
+
<%%= error_messages_for :<%= singular_name %> %>
|
|
4
|
+
|
|
5
|
+
<%% form_for(@<%= singular_name %>) do |f| %>
|
|
6
|
+
<% for attribute in attributes -%>
|
|
7
|
+
<p>
|
|
8
|
+
<%%= f.label :<%= attribute.name %> %><br />
|
|
9
|
+
<%%= f.<%= attribute.field_type %> :<%= attribute.name %> %>
|
|
10
|
+
</p>
|
|
11
|
+
|
|
12
|
+
<% end -%>
|
|
13
|
+
<% for rel in belongs -%>
|
|
14
|
+
<p>
|
|
15
|
+
<%%= f.label :<%= rel %>_id %><br/>
|
|
16
|
+
<%%= f.collection_select :<%= rel %>_id, @<%= rel.pluralize %>, 'id', 'id' %>
|
|
17
|
+
</p>
|
|
18
|
+
<% end -%>
|
|
19
|
+
|
|
20
|
+
<p>
|
|
21
|
+
<%%= f.submit "Create" %>
|
|
22
|
+
</p>
|
|
23
|
+
<%% end %>
|
|
24
|
+
|
|
25
|
+
<%%= link_to 'Back', <%= plural_name %>_path %>
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
<% for attribute in attributes -%>
|
|
2
|
+
<p>
|
|
3
|
+
<b><%= attribute.column.human_name %>:</b>
|
|
4
|
+
<%%=h @<%= singular_name %>.<%= attribute.name %> %>
|
|
5
|
+
</p>
|
|
6
|
+
|
|
7
|
+
<% end -%>
|
|
8
|
+
|
|
9
|
+
<%%= link_to 'Edit', edit_<%= singular_name %>_path(@<%= singular_name %>) %> |
|
|
10
|
+
<%%= link_to 'Back', <%= plural_name %>_path %>
|
data/lib/mydry.rb
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
module Mydry
|
|
2
|
+
class GeneratorBase < Rails::Generator::NamedBase
|
|
3
|
+
attr_accessor :notnulls, :lengths, :numerics, :belongs
|
|
4
|
+
|
|
5
|
+
def load_meta_from_db
|
|
6
|
+
con = ActiveRecord::Base.connection
|
|
7
|
+
cols = con.columns table_name
|
|
8
|
+
@notnulls = []
|
|
9
|
+
@lengths = {}
|
|
10
|
+
@numerics = []
|
|
11
|
+
@attributes = []
|
|
12
|
+
@belongs = []
|
|
13
|
+
cols.each do |c|
|
|
14
|
+
@notnulls << c.name if !c.null && not_id(c)
|
|
15
|
+
@lengths[c.name] = c.limit if c.limit && not_id(c) && !is_number(c)
|
|
16
|
+
@numerics << c.name if not_id(c) && !is_number(c)
|
|
17
|
+
@belongs << c.name.gsub(/(.*)_id$/, '\1') if is_ref(c)
|
|
18
|
+
attrib = Rails::Generator::GeneratedAttribute.new(c.name,c.type)
|
|
19
|
+
attrib.column = c
|
|
20
|
+
attrib.default = c.default
|
|
21
|
+
@attributes << attrib if not_id(c)
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
private
|
|
25
|
+
def not_id(c)
|
|
26
|
+
!is_ref(c) && c.name != 'id'
|
|
27
|
+
end
|
|
28
|
+
def is_number(c)
|
|
29
|
+
[:integer, :numeric, :decimal, :float].index(c.type)
|
|
30
|
+
end
|
|
31
|
+
def is_ref(c)
|
|
32
|
+
c.name =~ /_id$/
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
module Rails
|
|
37
|
+
module Generator
|
|
38
|
+
class GeneratedAttribute
|
|
39
|
+
attr_accessor :default
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
data/test/mydry_test.rb
ADDED
metadata
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: urubatan-mydry_generator
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.2.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Rodrigo Urubatan Ferreira Jardim
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
|
|
12
|
+
date: 2008-04-26 00:00:00 -07:00
|
|
13
|
+
default_executable:
|
|
14
|
+
dependencies:
|
|
15
|
+
- !ruby/object:Gem::Dependency
|
|
16
|
+
name: rails
|
|
17
|
+
version_requirement:
|
|
18
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
19
|
+
requirements:
|
|
20
|
+
- - ">"
|
|
21
|
+
- !ruby/object:Gem::Version
|
|
22
|
+
version: 2.0.2
|
|
23
|
+
version:
|
|
24
|
+
description: mydry is a rails generator that enables you to work in a drier way, using the information you already provided in the migration to create your models, for example all the non nulls, uniques, numerics and relations defined in the database are already defined in the model with the corresponding validation.
|
|
25
|
+
email: rodrigo@urubatan.com.br
|
|
26
|
+
executables: []
|
|
27
|
+
|
|
28
|
+
extensions: []
|
|
29
|
+
|
|
30
|
+
extra_rdoc_files:
|
|
31
|
+
- README
|
|
32
|
+
files:
|
|
33
|
+
- generators/drymodel/drymodel_generator.rb
|
|
34
|
+
- generators/drymodel/templates/fixtures.yml
|
|
35
|
+
- generators/drymodel/templates/migration.rb
|
|
36
|
+
- generators/drymodel/templates/model.rb
|
|
37
|
+
- generators/drymodel/templates/unit_test.rb
|
|
38
|
+
- generators/dryscaffold/dryscaffold_generator.rb
|
|
39
|
+
- generators/dryscaffold/templates/controller.rb
|
|
40
|
+
- generators/dryscaffold/templates/functional_test.rb
|
|
41
|
+
- generators/dryscaffold/templates/helper.rb
|
|
42
|
+
- generators/dryscaffold/templates/layout.html.erb
|
|
43
|
+
- generators/dryscaffold/templates/style.css
|
|
44
|
+
- generators/dryscaffold/templates/view_edit.html.erb
|
|
45
|
+
- generators/dryscaffold/templates/view_index.html.erb
|
|
46
|
+
- generators/dryscaffold/templates/view_new.html.erb
|
|
47
|
+
- generators/dryscaffold/templates/view_show.html.erb
|
|
48
|
+
- generators/drymodel/drymodel_generator.rb
|
|
49
|
+
- generators/drymodel/templates/fixtures.yml
|
|
50
|
+
- generators/drymodel/templates/migration.rb
|
|
51
|
+
- generators/drymodel/templates/model.rb
|
|
52
|
+
- generators/drymodel/templates/unit_test.rb
|
|
53
|
+
- generators/dryscaffold/dryscaffold_generator.rb
|
|
54
|
+
- generators/dryscaffold/templates/controller.rb
|
|
55
|
+
- generators/dryscaffold/templates/functional_test.rb
|
|
56
|
+
- generators/dryscaffold/templates/helper.rb
|
|
57
|
+
- generators/dryscaffold/templates/layout.html.erb
|
|
58
|
+
- generators/dryscaffold/templates/style.css
|
|
59
|
+
- generators/dryscaffold/templates/view_edit.html.erb
|
|
60
|
+
- generators/dryscaffold/templates/view_index.html.erb
|
|
61
|
+
- generators/dryscaffold/templates/view_new.html.erb
|
|
62
|
+
- generators/dryscaffold/templates/view_show.html.erb
|
|
63
|
+
- lib/mydry.rb
|
|
64
|
+
- README
|
|
65
|
+
- test/mydry_test.rb
|
|
66
|
+
has_rdoc: true
|
|
67
|
+
homepage: http://github.com/urubatan/mydry
|
|
68
|
+
post_install_message:
|
|
69
|
+
rdoc_options:
|
|
70
|
+
- --main
|
|
71
|
+
- README
|
|
72
|
+
require_paths:
|
|
73
|
+
- lib
|
|
74
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
75
|
+
requirements:
|
|
76
|
+
- - ">="
|
|
77
|
+
- !ruby/object:Gem::Version
|
|
78
|
+
version: "0"
|
|
79
|
+
version:
|
|
80
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
81
|
+
requirements:
|
|
82
|
+
- - ">="
|
|
83
|
+
- !ruby/object:Gem::Version
|
|
84
|
+
version: "0"
|
|
85
|
+
version:
|
|
86
|
+
requirements: []
|
|
87
|
+
|
|
88
|
+
rubyforge_project:
|
|
89
|
+
rubygems_version: 1.0.1
|
|
90
|
+
signing_key:
|
|
91
|
+
specification_version: 2
|
|
92
|
+
summary: An easy way to generate models for your application based on the information you already provided in the migrations
|
|
93
|
+
test_files:
|
|
94
|
+
- test/mydry_test.rb
|