jrhicks-static-scaffolds 0.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.
@@ -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,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
+
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jrhicks-static-scaffolds
3
+ version: !ruby/object:Gem::Version
4
+ version: "0.1"
5
+ platform: ruby
6
+ authors:
7
+ - Jeffrey Hicks
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-06-28 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rails
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "2.3"
24
+ version:
25
+ description: Generate attractive interfaces that are easy to edit.
26
+ email: jrhicks@gmail.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files: []
32
+
33
+ files:
34
+ - README
35
+ - USAGE
36
+ - static_app/static_app_generator.rb
37
+ - static_app/templates/application_helper.rb
38
+ - static_app/templates/layout.html.erb
39
+ - static_app/templates/layout_helper.rb
40
+ - static_app/templates/sorttable.js
41
+ - static_app/templates/static_style.css
42
+ - static_gen_specs/static_gen_specs_generator.rb
43
+ - static_gen_specs/templates/generator_specs.rb
44
+ - static_gen_specs/templates/model_gen_specs.rb
45
+ - static_scaffold/static_scaffold_generator.rb
46
+ has_rdoc: true
47
+ homepage: http://github.com/jrhicks/static-scaffolds
48
+ post_install_message:
49
+ rdoc_options:
50
+ - --main
51
+ - README
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: "0"
59
+ version:
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: "0"
65
+ version:
66
+ requirements: []
67
+
68
+ rubyforge_project:
69
+ rubygems_version: 1.2.0
70
+ signing_key:
71
+ specification_version: 2
72
+ summary: Generate attractive interfaces that are easy to edit.
73
+ test_files: []
74
+