jfs-generators 0.1.1 → 0.2.0

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.
Files changed (43) hide show
  1. data/README.md +131 -10
  2. data/rails_generators/jfs_960/USAGE +10 -0
  3. data/rails_generators/jfs_960/jfs_960_generator.rb +23 -0
  4. data/rails_generators/jfs_960/templates/960.css +1 -0
  5. data/rails_generators/jfs_960/templates/reset.css +1 -0
  6. data/rails_generators/jfs_960/templates/text.css +1 -0
  7. data/rails_generators/jfs_config/USAGE +23 -0
  8. data/rails_generators/jfs_config/jfs_config_generator.rb +35 -0
  9. data/rails_generators/jfs_config/templates/config.yml +8 -0
  10. data/rails_generators/jfs_config/templates/load_config.rb +2 -0
  11. data/rails_generators/jfs_jquery/USAGE +10 -0
  12. data/rails_generators/jfs_jquery/jfs_jquery_generator.rb +30 -0
  13. data/rails_generators/jfs_jquery/templates/jquery-ui.min.js +9 -0
  14. data/rails_generators/jfs_jquery/templates/jquery.min.js +19 -0
  15. data/rails_generators/jfs_layout/USAGE +30 -0
  16. data/rails_generators/jfs_layout/jfs_layout_generator.rb +36 -0
  17. data/rails_generators/jfs_layout/templates/en.yml +3 -0
  18. data/rails_generators/jfs_layout/templates/helper.rb +10 -0
  19. data/rails_generators/jfs_layout/templates/layout.html.erb +31 -0
  20. data/rails_generators/jfs_layout/templates/stylesheet.css +73 -0
  21. data/rails_generators/jfs_model/USAGE +6 -5
  22. data/rails_generators/jfs_model/jfs_model_generator.rb +3 -3
  23. data/rails_generators/jfs_model/templates/model.rb +5 -3
  24. data/rails_generators/jfs_scaffold/USAGE +33 -0
  25. data/rails_generators/jfs_scaffold/jfs_scaffold_generator.rb +104 -0
  26. data/rails_generators/jfs_scaffold/templates/actions/create.rb +25 -0
  27. data/rails_generators/jfs_scaffold/templates/actions/destroy.rb +16 -0
  28. data/rails_generators/jfs_scaffold/templates/actions/edit.rb +14 -0
  29. data/rails_generators/jfs_scaffold/templates/actions/index.rb +14 -0
  30. data/rails_generators/jfs_scaffold/templates/actions/new.rb +14 -0
  31. data/rails_generators/jfs_scaffold/templates/actions/show.rb +14 -0
  32. data/rails_generators/jfs_scaffold/templates/actions/update.rb +25 -0
  33. data/rails_generators/jfs_scaffold/templates/controller.rb +3 -0
  34. data/rails_generators/jfs_scaffold/templates/en.yml +31 -0
  35. data/rails_generators/jfs_scaffold/templates/helper.rb +2 -0
  36. data/rails_generators/jfs_scaffold/templates/migration.rb +14 -0
  37. data/rails_generators/jfs_scaffold/templates/model.rb +12 -0
  38. data/rails_generators/jfs_scaffold/templates/views/_fields.html.erb +7 -0
  39. data/rails_generators/jfs_scaffold/templates/views/edit.html.erb +15 -0
  40. data/rails_generators/jfs_scaffold/templates/views/index.html.erb +36 -0
  41. data/rails_generators/jfs_scaffold/templates/views/new.html.erb +8 -0
  42. data/rails_generators/jfs_scaffold/templates/views/show.html.erb +29 -0
  43. metadata +40 -2
@@ -0,0 +1,31 @@
1
+ <?xml version="1.0" encoding="utf-8"?>
2
+
3
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
4
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
5
+
6
+ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
7
+ <head>
8
+ <meta http-equiv="content-type" content="text/html; charset=utf-8" />
9
+
10
+ <title><%%=h (yield(:title) || t('layout.untitled'))%></title>
11
+
12
+ <%%= stylesheet_link_tag '<%= file_name %>' %>
13
+
14
+ <%%= javascript_include_tag '<%= file_name %>' %>
15
+ </head>
16
+ <body>
17
+ <div id="container">
18
+ <%%- unless flash.empty? -%>
19
+ <div id="notification" class="section">
20
+ <%%- flash.each do |name, message| -%>
21
+ <%%= content_tag :div, message, :id => "flash_#{name}", :class => 'flash' %>
22
+ <%%- end -%>
23
+ </div>
24
+ <%%- end -%>
25
+ <div id="content" class="section">
26
+ <%%= yield %>
27
+ </div>
28
+ </div>
29
+ </div>
30
+ </body>
31
+ </html>
@@ -0,0 +1,73 @@
1
+ body {
2
+ background-color: #333;
3
+ color: #CCC;
4
+ font-family:Verdana,Geneva,sans-serif;
5
+ font-size: 0.8em;
6
+ }
7
+ a { color: #FFF; }
8
+ h1, h2, h3, h4, h5, h6
9
+ {
10
+ font-family:Tahoma,Arial,Helvetica,sans-serif;
11
+ font-variant: small-caps;
12
+ letter-spacing: 0.1em;
13
+ }
14
+ img { border: none; }
15
+ input
16
+ {
17
+ border: solid #CCC 1px;
18
+ padding: 0.2em 0.4em;
19
+ }
20
+ input[type=text], input[type=password], textarea { width: 50%; }
21
+ textarea { height: 10em; }
22
+ td { padding: 0.2em 0.4em; }
23
+ th { text-align: left; }
24
+
25
+ #container {
26
+ margin: auto;
27
+ width: 960px;
28
+ }
29
+ .section { margin: 2em 0em; }
30
+
31
+ .flash {
32
+ margin: 1em 0;
33
+ padding: 0.5em 4em;
34
+ }
35
+ #flash_notice {
36
+ background-color: #CFC;
37
+ border: solid #060 1px;
38
+ color: #060;
39
+ }
40
+ #flash_error {
41
+ background-color: #FCC;
42
+ border: solid #600 1px;
43
+ color: #600;
44
+ }
45
+
46
+ #content {
47
+ background-color: #FFF;
48
+ border: solid #000 1px;
49
+ color: #333;
50
+ padding: 2em 4em;
51
+ }
52
+ #content a { color: #000; }
53
+ #content h1:first-child { margin-top: 0em; }
54
+ #content input { border: solid #333 1px; }
55
+
56
+ #errorExplanation {
57
+ background-color: #FCC;
58
+ border: solid #600 1px;
59
+ color: #600;
60
+ padding: 0.5em 4em;
61
+ }
62
+ #errorExplanation h2 {
63
+ font-size: 1em;
64
+ margin-top: 0em;
65
+ }
66
+ #errorExplanation ul { margin: 0em; }
67
+
68
+ .fieldWithErrors { display: inline; }
69
+ .fieldWithErrors input { background-color: #FCC; }
70
+ .fieldWithErrors label {
71
+ color: #600;
72
+ font-weight: bold;
73
+ }
@@ -1,6 +1,7 @@
1
1
  Description:
2
- Stubs out a new model. Pass the model name, either CamelCased or
3
- under_scored, and an optional list of attribute pairs as arguments.
2
+ Stubs out a new model, including `attr_accessible` statement for attributes.
3
+ Pass the model name, either CamelCased or under_scored, and an optional list
4
+ of attribute pairs as arguments.
4
5
 
5
6
  Attribute pairs are column_name:sql_type arguments specifying the
6
7
  model's attributes. Timestamps are added by default, so you don't have to
@@ -11,10 +12,10 @@ Description:
11
12
  Examples:
12
13
  `./script/generate jfs_model Account`
13
14
 
14
- creates an Account model and migration:
15
+ Creates an Account model and migration:
15
16
  Model: app/models/account.rb
16
- Migration: db/migrate/XXX_add_accounts.rb
17
+ Migration: db/migrate/XXX_create_accounts.rb
17
18
 
18
19
  `./script/generate model Article title:string content:text`
19
20
 
20
- creates an Article model with a string title and text body,.
21
+ Creates an Article model with a string title and text body,.
@@ -39,12 +39,12 @@ class JfsModelGenerator < Rails::Generator::Base
39
39
  plural_name.camelize
40
40
  end
41
41
 
42
- def gen_attr_accessor(attribute)
42
+ def gen_attr_accessible(attribute)
43
43
  case attribute.type
44
44
  when :belongs_to
45
- "attr_accessor :#{attribute.name}_ids"
45
+ "attr_accessible :#{attribute.name}_ids"
46
46
  else
47
- "attr_accessor :#{attribute.name}"
47
+ "attr_accessible :#{attribute.name}"
48
48
  end
49
49
  end
50
50
 
@@ -1,10 +1,12 @@
1
1
  class <%= class_name %> < ActiveRecord::Base
2
+
2
3
  <%- attributes.each do |attribute| -%>
3
- <%- if attribute.type.eql? :belongs_to -%>
4
- belongs_to :<%= attribute.name %>
4
+ <%- if attribute.type.to_sym.eql? :belongs_to -%>
5
+ <%= "belongs_to :#{attribute.name}" %>
5
6
  <%- end -%>
6
7
  <%- end -%>
7
8
  <%- attributes.each do |attribute| -%>
8
- <%= gen_attr_accessor attribute %>
9
+ <%= gen_attr_accessible attribute %>
9
10
  <%- end -%>
11
+
10
12
  end
@@ -0,0 +1,33 @@
1
+ Description:
2
+ Scaffolds an entire resource, from model and migration to translation ready
3
+ controller and views. The resource is ready to use as a starting point for
4
+ your RESTful, resource-oriented application.
5
+
6
+ Pass the model name, either CamelCased or under_scored, as the first
7
+ argument, and an optional list of attribute pairs.
8
+
9
+ Attribute pairs are column_name:sql_type arguments specifying the model's
10
+ attributes. Timestamps are added by default, so you don't have to specify
11
+ them by hand as 'created_at:datetime updated_at:datetime'.
12
+
13
+ For example, `jfs_scaffold article title:string content:text` gives you a
14
+ model with those two attributes, a controller that handles the
15
+ create/show/update/destroy, forms to create and edit your posts, and an
16
+ index that lists them all, as well as a map.resources :posts declaration in
17
+ config/routes.rb.
18
+
19
+ Examples:
20
+ `./script/generate scaffold article title:string body:text published:boolean`
21
+
22
+ Creates a model, migration, helper, controller, views and translation
23
+ file:
24
+ View: app/views/article/index.html.erb
25
+ View: app/views/article/new.html.erb
26
+ View: app/views/article/show.html.erb
27
+ View: app/views/article/edit.html.erb
28
+ View: app/views/article/_fields.html.erb
29
+ Translation: config/locales/en.article.yml
30
+ Controller: app/controllers/articles_controller.rb
31
+ Helper: app/helpers/articles_helper.rb
32
+ Model: app/models/article.rb
33
+ Migration: db/migrate/XXX_create_articles.rb
@@ -0,0 +1,104 @@
1
+ class JfsScaffoldGenerator < Rails::Generator::Base
2
+
3
+ attr_accessor :name, :attributes, :controller_actions
4
+
5
+ def initialize(runtime_args, runtime_options = {})
6
+ super
7
+ usage if @args.empty?
8
+
9
+ @name = @args.first
10
+ @controller_actions = %w[index new create show edit update destroy]
11
+ @attributes = []
12
+
13
+ @args[1..-1].each do |arg|
14
+ @attributes << Rails::Generator::GeneratedAttribute.new(*arg.split(":"))
15
+ end
16
+ @attributes.uniq!
17
+ end
18
+
19
+ def manifest
20
+ record do |m|
21
+ # generate views
22
+ m.directory "app/views/#{plural_name}"
23
+ controller_actions.each do |action|
24
+ if File.exist? source_path("views/#{action}.html.erb")
25
+ m.template "views/#{action}.html.erb", "app/views/#{plural_name}/#{action}.html.erb"
26
+ end
27
+ end
28
+ m.template "views/_fields.html.erb", "app/views/#{plural_name}/_fields.html.erb"
29
+ m.directory 'config/locales'
30
+ m.template 'en.yml', "config/locales/en.#{plural_name}.yml"
31
+
32
+ # generate controller
33
+ m.directory 'app/controllers'
34
+ m.template 'controller.rb', "app/controllers/#{plural_name}_controller.rb"
35
+ m.directory 'app/helpers'
36
+ m.template 'helper.rb', "app/helpers/#{plural_name}_helper.rb"
37
+ m.route_resources plural_name
38
+
39
+ m.dependency 'jfs_model', [@name] + (@attributes.map { |attribute| "#{attribute.name}:#{attribute.type}" }), :collision => :skip
40
+ end
41
+ end
42
+
43
+ def class_name
44
+ singular_name.camelize
45
+ end
46
+
47
+ def plural_class_name
48
+ plural_name.camelize
49
+ end
50
+
51
+ def plural_name
52
+ name.underscore.pluralize
53
+ end
54
+
55
+ def singular_name
56
+ name.underscore.singularize
57
+ end
58
+
59
+ def is_action_included?(name)
60
+ controller_actions.include? name.to_s
61
+ end
62
+
63
+ def gen_attr_accessible(attribute)
64
+ case attribute.type.to_sym
65
+ when :belongs_to
66
+ "attr_accessible :#{attribute.name}_id"
67
+ else
68
+ "attr_accessible :#{attribute.name}"
69
+ end
70
+ end
71
+
72
+ def gen_controller_methods
73
+ controller_actions.map do |action|
74
+ if File.exists? source_path("actions/#{action}.rb")
75
+ read_template("actions/#{action}.rb")
76
+ else
77
+ "\n def #{action}\n end\n"
78
+ end
79
+ end.join('')
80
+ end
81
+
82
+ protected
83
+
84
+ def read_template(relative_path)
85
+ ERB.new(File.read(source_path(relative_path)), nil, '-').result(binding)
86
+ end
87
+
88
+ def add_options!(opt)
89
+ opt.separator ''
90
+ opt.separator 'Options:'
91
+ opt.on("--no-xml", "Don't generate xml output for controller actions.") { |v| options[:no_xml] = v }
92
+ opt.on("--no-json", "Don't generate json output for controller actions.") { |v| options[:no_json] = v }
93
+ end
94
+
95
+ def banner
96
+ <<-END
97
+ Creates a controller, model and views given the model name, actions, and
98
+ attributes.
99
+
100
+ USAGE: #{$0} #{spec.name} ModelName [controller_actions and model:attributes] [options]
101
+ END
102
+ end
103
+
104
+ end
@@ -0,0 +1,25 @@
1
+
2
+ def create
3
+ @<%= singular_name %> = <%= class_name %>.new(params[:<%= singular_name %>])
4
+
5
+ respond_to do |format|
6
+ if @<%= singular_name %>.save
7
+ flash[:notice] = t('<%= plural_name %>.create.flash')
8
+ format.html { redirect_to <%= "#{plural_name}_url" %> }
9
+ <%- unless options[:no_xml] -%>
10
+ format.xml { render :xml => @<%= singular_name %>, :status => :created, :location => @<%= singular_name %> }
11
+ <%- end -%>
12
+ <%- unless options[:no_json] -%>
13
+ format.json { render :json => @<%= singular_name %>, :status => :created, :location => @<%= singular_name %> }
14
+ <%- end -%>
15
+ else
16
+ format.html { render :action => "new" }
17
+ <%- unless options[:no_xml] -%>
18
+ format.xml { render :xml => @<%= singular_name %>.errors, :status => :unprocessable_entity }
19
+ <%- end -%>
20
+ <%- unless options[:no_json] -%>
21
+ format.json { render :json => @<%= singular_name %>.errors, :status => :unprocessable_entity }
22
+ <%- end -%>
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,16 @@
1
+
2
+ def destroy
3
+ @<%= singular_name %> = <%= class_name %>.find_by_id(params[:id])
4
+
5
+ @<%= singular_name %>.destroy
6
+ respond_to do |format|
7
+ flash[:notice] = t('<%= plural_name %>.destroy.flash')
8
+ format.html { redirect_to <%= plural_name %>_url }
9
+ <%- unless options[:no_xml] -%>
10
+ format.xml { head :ok }
11
+ <%- end -%>
12
+ <%- unless options[:no_json] -%>
13
+ format.json { head :ok }
14
+ <%- end -%>
15
+ end
16
+ end
@@ -0,0 +1,14 @@
1
+
2
+ def edit
3
+ @<%= singular_name %> = <%= class_name %>.find_by_id(params[:id])
4
+
5
+ respond_to do |format|
6
+ format.html # edit.html.erb
7
+ <%- unless options[:no_xml] -%>
8
+ format.xml { render :xml => @<%= singular_name %>.to_xml(:except => [:id, :created_at, :updated_at]) }
9
+ <%- end -%>
10
+ <%- unless options[:no_json] -%>
11
+ format.json { render :json => @<%= singular_name %>.to_json(:except => [:id, :created_at, :updated_at]) }
12
+ <%- end -%>
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+
2
+ def index
3
+ @<%= plural_name %> = <%= class_name %>.all
4
+
5
+ respond_to do |format|
6
+ format.html # index.html.erb
7
+ <%- unless options[:no_xml] -%>
8
+ format.xml { render :xml => @<%= plural_name %> }
9
+ <%- end -%>
10
+ <%- unless options[:no_json] -%>
11
+ format.json { render :json => @<%= plural_name %> }
12
+ <%- end -%>
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+
2
+ def new
3
+ @<%= singular_name %> = <%= class_name %>.new
4
+
5
+ respond_to do |format|
6
+ format.html # new.html.erb
7
+ <%- unless options[:no_xml] -%>
8
+ format.xml { render :xml => @<%= singular_name %>.to_xml(:except => [:id, :created_at, :updated_at]) }
9
+ <%- end -%>
10
+ <%- unless options[:no_json] -%>
11
+ format.json { render :json => @<%= singular_name %>.to_json(:except => [:id, :created_at, :updated_at]) }
12
+ <%- end -%>
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+
2
+ def show
3
+ @<%= singular_name %> = <%= class_name %>.find_by_id(params[:id])
4
+
5
+ respond_to do |format|
6
+ format.html # show.html.erb
7
+ <%- unless options[:no_xml] -%>
8
+ format.xml { render :xml => @<%= singular_name %> }
9
+ <%- end -%>
10
+ <%- unless options[:no_json] -%>
11
+ format.json { render :json => @<%= singular_name %> }
12
+ <%- end -%>
13
+ end
14
+ end
@@ -0,0 +1,25 @@
1
+
2
+ def update
3
+ @<%= singular_name %> = <%= class_name %>.find_by_id(params[:id])
4
+
5
+ respond_to do |format|
6
+ if @<%= singular_name %>.update_attributes(params[:<%= singular_name %>])
7
+ flash[:notice] = t('<%= plural_name %>.update.flash')
8
+ format.html { redirect_to <%= "#{plural_name}_url" %> }
9
+ <%- unless options[:no_xml] -%>
10
+ format.xml { head :ok }
11
+ <%- end -%>
12
+ <%- unless options[:no_json] -%>
13
+ format.json { head :ok }
14
+ <%- end -%>
15
+ else
16
+ format.html { render :action => "edit" }
17
+ <%- unless options[:no_xml] -%>
18
+ format.xml { render :xml => @<%= singular_name %>.errors, :status => :unprocessable_entity }
19
+ <%- end -%>
20
+ <%- unless options[:no_json] -%>
21
+ format.json { render :json => @<%= singular_name %>.errors, :status => :unprocessable_entity }
22
+ <%- end -%>
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,3 @@
1
+ class <%= plural_class_name %>Controller < ApplicationController
2
+ <%= gen_controller_methods %>
3
+ end