jrhicks-static-generators 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (34) hide show
  1. data/CHANGELOG +3 -0
  2. data/LICENSE +21 -0
  3. data/Manifest +32 -0
  4. data/README.md +27 -0
  5. data/Rakefile +15 -0
  6. data/TODO +4 -0
  7. data/USAGE +17 -0
  8. data/lib/static_generators.rb +3 -0
  9. data/rails_generators/static_app/USAGE +0 -0
  10. data/rails_generators/static_app/static_app_generator.rb +26 -0
  11. data/rails_generators/static_app/templates/application_helper.rb +11 -0
  12. data/rails_generators/static_app/templates/layout.html.erb +0 -0
  13. data/rails_generators/static_app/templates/layout_helper.rb +9 -0
  14. data/rails_generators/static_app/templates/sorttable.js +568 -0
  15. data/rails_generators/static_app/templates/static_style.css +90 -0
  16. data/rails_generators/static_gen_specs/USAGE +0 -0
  17. data/rails_generators/static_gen_specs/static_gen_specs_generator.rb +343 -0
  18. data/rails_generators/static_gen_specs/templates/generator_specs.rb +60 -0
  19. data/rails_generators/static_gen_specs/templates/model_gen_specs.rb +145 -0
  20. data/rails_generators/static_scaffold/USAGE +29 -0
  21. data/rails_generators/static_scaffold/static_scaffold_generator.rb +150 -0
  22. data/rails_generators/static_scaffold/templates/controller.rb +85 -0
  23. data/rails_generators/static_scaffold/templates/functional_test.rb +45 -0
  24. data/rails_generators/static_scaffold/templates/helper.rb +2 -0
  25. data/rails_generators/static_scaffold/templates/helper_test.rb +4 -0
  26. data/rails_generators/static_scaffold/templates/layout.html.erb +18 -0
  27. data/rails_generators/static_scaffold/templates/model.rb +109 -0
  28. data/rails_generators/static_scaffold/templates/view_edit.html.erb +19 -0
  29. data/rails_generators/static_scaffold/templates/view_index.html.erb +26 -0
  30. data/rails_generators/static_scaffold/templates/view_new.html.erb +18 -0
  31. data/rails_generators/static_scaffold/templates/view_show.html.erb +13 -0
  32. data/static-generators.gemspec +31 -0
  33. data/tasks/deployment.rake +9 -0
  34. metadata +95 -0
@@ -0,0 +1,145 @@
1
+ require File.join(RAILS_ROOT,"static_scaffold","generator_specs.rb")
2
+
3
+ class <%=class_name%>GenSpecs < GeneratorSpecs
4
+ attr_accessor :model_name, :class_name, :table_name, :primary_key, :singular_name, :plural_name,
5
+ :plural_label, :singular_label, :plural_title, :singular_title,
6
+ :authentication_method, :authorization_method,
7
+ :order_preference_columns, :order_preference,
8
+ :public_root_path, :private_root_path, :will_paginate
9
+
10
+ def initialize()
11
+ @model_name = <%=model_name.inspect%>
12
+ @table_name = <%=table_name.inspect%>
13
+ @primary_key = "id"
14
+ @plural_name = <%=model_name.underscore.downcase.pluralize.inspect%>
15
+ @singular_name = <%=model_name.underscore.downcase.singularize.inspect%>
16
+ @plural_label = <%=model_name.pluralize.humanize.inspect%>
17
+ @singular_label = <%=model_name.humanize.inspect%>
18
+ @plural_title = <%=model_name.pluralize.titleize.inspect%>
19
+ @singular_title = <%=model_name.titleize.inspect%>
20
+
21
+ # Security
22
+ @authentication_method = :AuthLogic # :none
23
+ @authorization_method = :static_authorization # :ACL9, :none
24
+
25
+ # Order
26
+ @order_preference_columns = <%=guess_ordered_columns.inspect%>
27
+ @order_preference = "ASC"
28
+
29
+ # File Columns
30
+ @public_root_path = 'File.join(<%=RAILS_ROOT.inspect%>,"public")'
31
+ @private_root_path = 'File.join("filestore","private_files","<%=File.basename(RAILS_ROOT)%>")'
32
+
33
+ # Pagination
34
+ @will_paginate = true # false
35
+ end
36
+
37
+ def short_name_columns
38
+ # Specify the columns that constitute the "name" for example ["first_name", "last_name"]
39
+ <%=guess_short_name.inspect%>
40
+ end
41
+
42
+ def table_view_columns
43
+ [
44
+ <%for c in column_names -%>
45
+ <%if guess_list_columns.include?(c) -%>
46
+ <%=c.to_sym.inspect%>,
47
+ <%else -%>
48
+ # <%=c.to_sym.inspect%>,
49
+ <%end -%>
50
+ <%end -%>
51
+ ]
52
+ end
53
+
54
+
55
+ def column_specs
56
+ {
57
+ <%justifier = CodeJustifier.new(columns) -%>
58
+ <%justifier.add_parameter {|o| ":#{o.name} => "} -%>
59
+ <%justifier.add_parameter {|o| "{:label=>#{label(o.name).inspect}, "} -%>
60
+ <%justifier.add_parameter {|o| ":type=>#{guess_type(o.name).inspect}, "} -%>
61
+ <%justifier.add_parameter {|o| ":access=>:private, "} -%>
62
+ <%justifier.add_parameter {|o| ":units=>#{units(o.name).inspect}, "} -%>
63
+ <%justifier.add_parameter {|o| ":rows=>#{estimate_rows(o.name).inspect}, "} -%>
64
+ <%justifier.add_parameter {|o| ":cols=>#{estimate_cols(o.name).inspect}, "} -%>
65
+ <%justifier.add_parameter {|o| ":required=>#{(not o.null).inspect}, "} -%>
66
+ <%justifier.add_parameter {|o| ":align=>#{guess_alignment(o.name).inspect}, "} -%>
67
+ <%justifier.add_parameter {|o| ":decimals=>#{guess_decimals(o.name).inspect}"} -%>
68
+ <%for o in columns -%>
69
+ <%=justifier.render(o)%>},
70
+ <%end -%>
71
+ }
72
+ end
73
+
74
+ def file_columns
75
+ column_specs.select {|k,v| v[:type]==:file or v[:type]==:photo}
76
+ end
77
+
78
+ def belongs_to
79
+ [
80
+ <%justifier = CodeJustifier.new(belongs_to_columns) -%>
81
+ <%justifier.add_parameter {|o| ":name=>#{o.name.slice(0,o.name.length-3).inspect}, "} -%>
82
+ <%justifier.add_parameter {|o| ":model=>#{o.name.slice(0,o.name.length-3).camelize.inspect}, "} -%>
83
+ <%justifier.add_parameter {|o| ":key=>#{o.name.inspect}, "} -%>
84
+ <%for o in belongs_to_columns -%>
85
+ {<%=justifier.render(o)%>},
86
+ <%end -%>
87
+ ]
88
+ end
89
+
90
+ def ascendant
91
+ <%justifier = CodeJustifier.new(belongs_to_columns) -%>
92
+ <%justifier.add_parameter {|o| ":name=>#{o.name.slice(0,o.name.length-3).inspect}, "} -%>
93
+ <%justifier.add_parameter {|o| ":model=>#{o.name.slice(0,o.name.length-3).camelize.inspect}, "} -%>
94
+ <%justifier.add_parameter {|o| ":key=>#{o.name.inspect}, "} -%>
95
+ <%for o in belongs_to_columns -%>
96
+ # {<%=justifier.render(o)%>}
97
+ <%end -%>
98
+ end
99
+
100
+ def has_many
101
+ [
102
+ <%justifier = CodeJustifier.new(has_many_columns) -%>
103
+ <%justifier.add_parameter {|o| ":name=>#{o[:table].pluralize.inspect}, "} -%>
104
+ <%justifier.add_parameter {|o| ":model=>#{o[:table].singularize.camelize.inspect}, "} -%>
105
+ <%justifier.add_parameter {|o| ":key=>#{o[:column].inspect}, "} -%>
106
+ <%for o in has_many_columns -%>
107
+ {<%=justifier.render(o)%>},
108
+ <%end -%>
109
+ ]
110
+ end
111
+
112
+ def desendants
113
+ [
114
+ <%justifier = CodeJustifier.new(has_many_columns) -%>
115
+ <%justifier.add_parameter {|o| ":name=>#{o[:table].pluralize.inspect}, "} -%>
116
+ <%justifier.add_parameter {|o| ":model=>#{o[:table].singularize.camelize.inspect}, "} -%>
117
+ <%justifier.add_parameter {|o| ":key=>#{o[:column].inspect}, "} -%>
118
+ <%for o in has_many_columns -%>
119
+ # {<%=justifier.render(o)%>},
120
+ <%end -%>
121
+ ]
122
+ end
123
+
124
+
125
+
126
+ # These methods are inherited from generator_specs.rb
127
+
128
+ # model_name - <%=model_name.inspect%>
129
+ # model_file_name - <%=model_name.underscore.inspect%>
130
+ # class_name - <%=class_name.inspect%>
131
+ # controller_class_name - <%="#{model_name.pluralize}Controller".inspect%>
132
+ # controller_file_name - <%="#{model_name.pluralize}Controller".underscore.inspect%>
133
+ # view_folder_name - <%=model_name.underscore.inspect%>
134
+
135
+
136
+ <%keys=[:name, :sql_type, :precision, :type, :default, :limit, :null, :scale] -%>
137
+ # <%=keys.map { |k| k.to_s.ljust( max_length(columns,k)+2) }.join("")%>
138
+ # <%=keys.map { |k| "-"*( max_length(columns,k)) +" "}.join("") %>
139
+ <%for c in columns -%>
140
+ # <%=keys.map {|k| c.send(k.to_s).inspect.ljust( max_length(columns,k)+2)}.join("")%>
141
+ <%end -%>
142
+
143
+
144
+
145
+ end
@@ -0,0 +1,29 @@
1
+ Description:
2
+ Scaffolds an entire resource, from model and migration to controller and
3
+ views, along with a full test suite. The resource is ready to use as a
4
+ starting point for your RESTful, resource-oriented application.
5
+
6
+ Pass the name of the model (in singular form), either CamelCased or
7
+ under_scored, as the first argument, and an optional list of attribute
8
+ pairs.
9
+
10
+ Attribute pairs are column_name:sql_type arguments specifying the
11
+ model's attributes. Timestamps are added by default, so you don't have to
12
+ specify them by hand as 'created_at:datetime updated_at:datetime'.
13
+
14
+ You don't have to think up every attribute up front, but it helps to
15
+ sketch out a few so you can start working with the resource immediately.
16
+
17
+ For example, 'scaffold post title:string body:text published:boolean'
18
+ gives you a model with those three attributes, a controller that handles
19
+ the create/show/update/destroy, forms to create and edit your posts, and
20
+ an index that lists them all, as well as a map.resources :posts
21
+ declaration in config/routes.rb.
22
+
23
+ If you want to remove all the generated files, run
24
+ 'script/destroy scaffold ModelName'.
25
+
26
+ Examples:
27
+ `./script/generate scaffold post`
28
+ `./script/generate scaffold post title:string body:text published:boolean`
29
+ `./script/generate scaffold purchase order_id:integer amount:decimal`
@@ -0,0 +1,150 @@
1
+ class StaticScaffoldGenerator < Rails::Generator::NamedBase
2
+ default_options :skip_timestamps => false, :skip_migration => false, :force_plural => false
3
+
4
+ attr_reader :controller_name,
5
+ :controller_class_path,
6
+ :controller_file_path,
7
+ :controller_class_nesting,
8
+ :controller_class_nesting_depth,
9
+ :controller_class_name,
10
+ :controller_underscore_name,
11
+ :controller_singular_name,
12
+ :controller_plural_name
13
+ alias_method :controller_file_name, :controller_underscore_name
14
+ alias_method :controller_table_name, :controller_plural_name
15
+ attr_accessor :count
16
+
17
+ # The Scaffold Generator is configured with a ruby class
18
+ spec_files = Dir.glob(File.join(RAILS_ROOT,"static_scaffold","*.rb"))
19
+ for f in spec_files
20
+ load f
21
+ end
22
+
23
+ def initialize(runtime_args, runtime_options = {})
24
+ super
25
+
26
+ if @name == @name.pluralize && !options[:force_plural]
27
+ logger.warning "Plural version of the model detected, using singularized version. Override with --force-plural."
28
+ @name = @name.singularize
29
+ end
30
+ @count=0
31
+ @controller_name = @name.pluralize
32
+
33
+ base_name, @controller_class_path, @controller_file_path, @controller_class_nesting, @controller_class_nesting_depth = extract_modules(@controller_name)
34
+ @controller_class_name_without_nesting, @controller_underscore_name, @controller_plural_name = inflect_names(base_name)
35
+ @controller_singular_name=base_name.singularize
36
+ if @controller_class_nesting.empty?
37
+ @controller_class_name = @controller_class_name_without_nesting
38
+ else
39
+ @controller_class_name = "#{@controller_class_nesting}::#{@controller_class_name_without_nesting}"
40
+ end
41
+ end
42
+
43
+ def next_count
44
+ @count=@count+1
45
+ return @count
46
+ end
47
+
48
+ def manifest
49
+ record do |m|
50
+ # Check for class naming collisions.
51
+ m.class_collisions("#{controller_class_name}Controller", "#{controller_class_name}Helper")
52
+ m.class_collisions(class_name)
53
+
54
+ # Controller, helper, views, test and stylesheets directories.
55
+ m.directory(File.join('app/models', class_path))
56
+ m.directory(File.join('app/controllers', controller_class_path))
57
+ m.directory(File.join('app/helpers', controller_class_path))
58
+ m.directory(File.join('app/views', controller_class_path, controller_file_name))
59
+ m.directory(File.join('app/views/layouts', controller_class_path))
60
+ m.directory(File.join('test/functional', controller_class_path))
61
+ m.directory(File.join('test/unit', class_path))
62
+ m.directory(File.join('test/unit/helpers', class_path))
63
+ m.directory(File.join('public/stylesheets', class_path))
64
+
65
+ # Model
66
+ m.template(
67
+ "model.rb",
68
+ File.join('app/models',"#{@controller_singular_name.downcase}.rb"))
69
+
70
+ for action in scaffold_views
71
+ m.template(
72
+ "view_#{action}.html.erb",
73
+ File.join('app/views', controller_class_path, controller_file_name, "#{action}.html.erb")
74
+ )
75
+ end
76
+
77
+ # Layout and stylesheet.
78
+ m.template('layout.html.erb', File.join('app/views/layouts', controller_class_path, "#{controller_file_name}.html.erb"))
79
+
80
+ m.template(
81
+ 'controller.rb', File.join('app/controllers', controller_class_path, "#{controller_file_name}_controller.rb")
82
+ )
83
+
84
+ m.template('functional_test.rb', File.join('test/functional', controller_class_path, "#{controller_file_name}_controller_test.rb"))
85
+ m.template('helper.rb', File.join('app/helpers', controller_class_path, "#{controller_file_name}_helper.rb"))
86
+ m.template('helper_test.rb', File.join('test/unit/helpers', controller_class_path, "#{controller_file_name}_helper_test.rb"))
87
+
88
+ m.route_resources controller_file_name
89
+
90
+ #m.dependency 'model', [name] + @args, :collision => :skip
91
+ end
92
+ end
93
+
94
+ protected
95
+ # Override with your own usage banner.
96
+ def banner
97
+ "Usage: #{$0} static_scaffold ModelName"
98
+ end
99
+
100
+ def add_options!(opt)
101
+ opt.separator ''
102
+ opt.separator 'Options:'
103
+ opt.on("--skip-timestamps",
104
+ "Don't add timestamps to the migration file for this model") { |v| options[:skip_timestamps] = v }
105
+ opt.on("--skip-migration",
106
+ "Don't generate a migration file for this model") { |v| options[:skip_migration] = v }
107
+ opt.on("--force-plural",
108
+ "Forces the generation of a plural ModelName") { |v| options[:force_plural] = v }
109
+ end
110
+
111
+ def scaffold_views
112
+ %w[ index show new edit ]
113
+ end
114
+
115
+ def model_name
116
+ class_name.demodulize
117
+ end
118
+
119
+ # generator specs
120
+ def gen_spec(mname=nil)
121
+ mname = model_name if not mname
122
+ gen_specs_mname = "#{mname}GenSpecs"
123
+ Object::const_get(gen_specs_mname).new()
124
+ end
125
+
126
+ class CodeJustifier
127
+ # This class is designed to generate readable code, but is a hypocrate
128
+
129
+ attr_accessor :objects, :parameters
130
+
131
+ def initialize(objects)
132
+ self.objects = objects
133
+ self.parameters = []
134
+ end
135
+
136
+ def add_parameter(&p)
137
+ self.parameters << p
138
+ end
139
+
140
+ def render(object,delimiter)
141
+ self.parameters.map {|p| p.call(o).ljust(max_length(p))+1}.join(delimeter)
142
+ end
143
+
144
+ def max_length(parameter)
145
+ self.objects.map {|o| parameter.call(o).to_s.length }.max
146
+ end
147
+ end
148
+ end
149
+
150
+
@@ -0,0 +1,85 @@
1
+ class <%= controller_class_name %>Controller < ApplicationController
2
+ # GET /<%= table_name %>
3
+ # GET /<%= table_name %>.xml
4
+ def index
5
+ @<%= table_name %> = <%= class_name %>.all
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
+
29
+ respond_to do |format|
30
+ format.html # new.html.erb
31
+ format.xml { render :xml => @<%= file_name %> }
32
+ end
33
+ end
34
+
35
+ # GET /<%= table_name %>/1/edit
36
+ def edit
37
+ @<%= file_name %> = <%= class_name %>.find(params[:id])
38
+ end
39
+
40
+ # POST /<%= table_name %>
41
+ # POST /<%= table_name %>.xml
42
+ def create
43
+ @<%= file_name %> = <%= class_name %>.new(params[:<%= file_name %>])
44
+
45
+ respond_to do |format|
46
+ if @<%= file_name %>.save
47
+ flash[:notice] = '<%= class_name %> was successfully created.'
48
+ format.html { redirect_to(@<%= file_name %>) }
49
+ format.xml { render :xml => @<%= file_name %>, :status => :created, :location => @<%= file_name %> }
50
+ else
51
+ format.html { render :action => "new" }
52
+ format.xml { render :xml => @<%= file_name %>.errors, :status => :unprocessable_entity }
53
+ end
54
+ end
55
+ end
56
+
57
+ # PUT /<%= table_name %>/1
58
+ # PUT /<%= table_name %>/1.xml
59
+ def update
60
+ @<%= file_name %> = <%= class_name %>.find(params[:id])
61
+
62
+ respond_to do |format|
63
+ if @<%= file_name %>.update_attributes(params[:<%= file_name %>])
64
+ flash[:notice] = '<%= class_name %> was successfully updated.'
65
+ format.html { redirect_to(@<%= file_name %>) }
66
+ format.xml { head :ok }
67
+ else
68
+ format.html { render :action => "edit" }
69
+ format.xml { render :xml => @<%= file_name %>.errors, :status => :unprocessable_entity }
70
+ end
71
+ end
72
+ end
73
+
74
+ # DELETE /<%= table_name %>/1
75
+ # DELETE /<%= table_name %>/1.xml
76
+ def destroy
77
+ @<%= file_name %> = <%= class_name %>.find(params[:id])
78
+ @<%= file_name %>.destroy
79
+
80
+ respond_to do |format|
81
+ format.html { redirect_to(<%= table_name %>_url) }
82
+ format.xml { head :ok }
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,45 @@
1
+ require 'test_helper'
2
+
3
+ class <%= controller_class_name %>ControllerTest < ActionController::TestCase
4
+ test "should get index" do
5
+ get :index
6
+ assert_response :success
7
+ assert_not_nil assigns(:<%= table_name %>)
8
+ end
9
+
10
+ test "should get new" do
11
+ get :new
12
+ assert_response :success
13
+ end
14
+
15
+ test "should create <%= file_name %>" do
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
+ test "should show <%= file_name %>" do
24
+ get :show, :id => <%= table_name %>(:one).to_param
25
+ assert_response :success
26
+ end
27
+
28
+ test "should get edit" do
29
+ get :edit, :id => <%= table_name %>(:one).to_param
30
+ assert_response :success
31
+ end
32
+
33
+ test "should update <%= file_name %>" do
34
+ put :update, :id => <%= table_name %>(:one).to_param, :<%= file_name %> => { }
35
+ assert_redirected_to <%= file_name %>_path(assigns(:<%= file_name %>))
36
+ end
37
+
38
+ test "should destroy <%= file_name %>" do
39
+ assert_difference('<%= class_name %>.count', -1) do
40
+ delete :destroy, :id => <%= table_name %>(:one).to_param
41
+ end
42
+
43
+ assert_redirected_to <%= table_name %>_path
44
+ end
45
+ end