handy-generators 0.0.0 → 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.0
1
+ 0.0.1
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{handy-generators}
8
- s.version = "0.0.0"
8
+ s.version = "0.0.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["jordinl"]
@@ -54,6 +54,18 @@ Gem::Specification.new do |s|
54
54
  "rails_generators/handy_setup/templates/views/new_user.html.erb",
55
55
  "rails_generators/handy_setup/templates/views/show_user.html.erb",
56
56
  "rails_generators/handy_setup/templates/views/signup.html.erb",
57
+ "rails_generators/hmodel/hmodel_generator.rb",
58
+ "rails_generators/hmodel/templates/fixtures.yml",
59
+ "rails_generators/hmodel/templates/migration.rb",
60
+ "rails_generators/hmodel/templates/model.rb",
61
+ "rails_generators/hmodel/templates/unit_test.rb",
62
+ "rails_generators/hscaffold/hscaffold_generator.rb",
63
+ "rails_generators/hscaffold/templates/controller.rb",
64
+ "rails_generators/hscaffold/templates/view__form.html.erb",
65
+ "rails_generators/hscaffold/templates/view_edit.html.erb",
66
+ "rails_generators/hscaffold/templates/view_index.html.erb",
67
+ "rails_generators/hscaffold/templates/view_new.html.erb",
68
+ "rails_generators/hscaffold/templates/view_show.html.erb",
57
69
  "test/helper.rb",
58
70
  "test/test_handy-generators.rb"
59
71
  ]
@@ -0,0 +1,45 @@
1
+ class HmodelGenerator < Rails::Generator::NamedBase
2
+ default_options :skip_timestamps => false, :skip_migration => false, :skip_fixture => false
3
+
4
+ def manifest
5
+ record do |m|
6
+ # Check for class naming collisions.
7
+ m.class_collisions class_name, "#{class_name}Test"
8
+
9
+ # Model, test, and fixture directories.
10
+ m.directory File.join('app/models', class_path)
11
+
12
+ # Model class, unit test, and fixtures.
13
+ m.template 'model.rb', File.join('app/models', class_path, "#{file_name}.rb")
14
+
15
+ migration_file_path = file_path.gsub(/\//, '_')
16
+ migration_name = class_name
17
+ if ActiveRecord::Base.pluralize_table_names
18
+ migration_name = migration_name.pluralize
19
+ migration_file_path = migration_file_path.pluralize
20
+ end
21
+
22
+ unless options[:skip_migration]
23
+ m.migration_template 'migration.rb', 'db/migrate', :assigns => {
24
+ :migration_name => "Create#{migration_name.gsub(/::/, '')}"
25
+ }, :migration_file_name => "create_#{migration_file_path}"
26
+ end
27
+ end
28
+ end
29
+
30
+ protected
31
+ def banner
32
+ "Usage: #{$0} #{spec.name} ModelName [field:type, field:type]"
33
+ end
34
+
35
+ def add_options!(opt)
36
+ opt.separator ''
37
+ opt.separator 'Options:'
38
+ opt.on("--skip-timestamps",
39
+ "Don't add timestamps to the migration file for this model") { |v| options[:skip_timestamps] = v }
40
+ opt.on("--skip-migration",
41
+ "Don't generate a migration file for this model") { |v| options[:skip_migration] = v }
42
+ opt.on("--skip-fixture",
43
+ "Don't generation a fixture file for this model") { |v| options[:skip_fixture] = v}
44
+ end
45
+ 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,5 @@
1
+ class <%= class_name %> < ActiveRecord::Base
2
+ <% attributes.select(&:reference?).each do |attribute| -%>
3
+ belongs_to :<%= attribute.name %>
4
+ <% end -%>
5
+ end
@@ -0,0 +1,8 @@
1
+ require 'test_helper'
2
+
3
+ class <%= class_name %>Test < ActiveSupport::TestCase
4
+ # Replace this with your real tests.
5
+ test "the truth" do
6
+ assert true
7
+ end
8
+ end
@@ -0,0 +1,120 @@
1
+ class HscaffoldGenerator < 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
+
16
+ def initialize(runtime_args, runtime_options = {})
17
+ super
18
+
19
+ if @name == @name.pluralize && !options[:force_plural]
20
+ logger.warning "Plural version of the model detected, using singularized version. Override with --force-plural."
21
+ @name = @name.singularize
22
+ assign_names!(@name)
23
+ end
24
+
25
+ @controller_name = @name.pluralize
26
+
27
+ base_name, @controller_class_path, @controller_file_path, @controller_class_nesting, @controller_class_nesting_depth = extract_modules(@controller_name)
28
+ @controller_class_name_without_nesting, @controller_underscore_name, @controller_plural_name = inflect_names(base_name)
29
+ @controller_singular_name=base_name.singularize
30
+ if @controller_class_nesting.empty?
31
+ @controller_class_name = @controller_class_name_without_nesting
32
+ else
33
+ @controller_class_name = "#{@controller_class_nesting}::#{@controller_class_name_without_nesting}"
34
+ end
35
+
36
+ end
37
+
38
+ def manifest
39
+ record do |m|
40
+ # Check for class naming collisions.
41
+ m.class_collisions("#{controller_class_name}Controller", "#{controller_class_name}Helper")
42
+ m.class_collisions(class_name)
43
+
44
+ # Controller, helper, views, test and stylesheets directories.
45
+ m.directory(File.join('app/controllers', controller_class_path))
46
+ m.directory(File.join('app/views', controller_class_path, controller_file_name))
47
+
48
+ for action in scaffold_views
49
+ m.template(
50
+ "view_#{action}.html.erb",
51
+ File.join('app/views', controller_class_path, controller_file_name, "#{action}.html.erb")
52
+ )
53
+ end
54
+
55
+ # Layout and stylesheet.
56
+ m.template(
57
+ 'controller.rb', File.join('app/controllers', controller_class_path, "#{controller_file_name}_controller.rb")
58
+ )
59
+
60
+ m.route_resources controller_file_name
61
+
62
+ m.dependency 'hmodel', [singular_name] + @args, :collision => :skip
63
+ end
64
+ end
65
+
66
+ protected
67
+ # Override with your own usage banner.
68
+ def banner
69
+ "Usage: #{$0} scaffold ModelName [field:type, field:type]"
70
+ end
71
+
72
+ def add_options!(opt)
73
+ opt.separator ''
74
+ opt.separator 'Options:'
75
+ opt.on("--skip-timestamps",
76
+ "Don't add timestamps to the migration file for this model") { |v| options[:skip_timestamps] = v }
77
+ opt.on("--skip-migration",
78
+ "Don't generate a migration file for this model") { |v| options[:skip_migration] = v }
79
+ opt.on("--force-plural",
80
+ "Forces the generation of a plural ModelName") { |v| options[:force_plural] = v }
81
+ end
82
+
83
+ def scaffold_views
84
+ %w[ index show new edit _form ]
85
+ end
86
+
87
+ def model_name
88
+ class_name.demodulize
89
+ end
90
+
91
+ def nested_to_class
92
+ references = @args.select{|x| x =~ /references/}
93
+ references.empty? ? nil : references.first.split(":")[0]
94
+ end
95
+
96
+ def nested_object
97
+ "[@#{nested_to_class}, @#{file_name}]" if nested_to_class
98
+ end
99
+
100
+ def index_path
101
+ prefix_path.length > 0 ? "[#{(prefix_path + [":#{plural_name}"]).join(", ")}]" : "#{plural_name}_url"
102
+ end
103
+
104
+ def prefix_path
105
+ controller_class_nesting.split("::").map(&:underscore).map{|x| ":"+x} + (nested_to_class ? ["@#{nested_to_class}"] : [])
106
+ end
107
+
108
+ def object_path(object)
109
+ prefix_path.length > 0 ? "[#{(prefix_path + ["#{object}"]).join(", ")}]" : "#{object}"
110
+ end
111
+
112
+ def edit_path(object)
113
+ prefix_path.length > 0 ? "[#{([":edit"] + prefix_path + ["#{object}"]).join(", ")}]" : "edit_#{file_name}_url(#{object})"
114
+ end
115
+
116
+ def new_path
117
+ prefix_path.length > 0 ? "[#{([":new"] + prefix_path + [":#{file_name}"]).join(", ")}]" : "new_#{file_name}_url"
118
+ end
119
+
120
+ end
@@ -0,0 +1,64 @@
1
+ class <%= controller_class_name %>Controller < ApplicationController
2
+
3
+ <% if nested_to_class %>before_filter :find_<%= nested_to_class %><% end %>
4
+
5
+ # GET /<%= plural_name %>
6
+ def index
7
+ @<%= plural_name %> = <%= nested_to_class ? "@#{nested_to_class}.#{ plural_name}" : "#{model_name}.all" %>
8
+ end
9
+
10
+ # GET /<%= plural_name %>/1
11
+ def show
12
+ @<%= file_name %> = <%= model_name %>.find(params[:id])
13
+ end
14
+
15
+ # GET /<%= plural_name %>/new
16
+ def new
17
+ @<%= file_name %> = <%= nested_to_class ? "@#{nested_to_class}.#{plural_name}.new" : "#{ model_name}.new" %>
18
+ end
19
+
20
+ # GET /<%= plural_name %>/1/edit
21
+ def edit
22
+ @<%= file_name %> = <%= model_name %>.find(params[:id])
23
+ end
24
+
25
+ # POST /<%= plural_name %>
26
+ def create
27
+ @<%= file_name %> = <%= nested_to_class ? "@#{nested_to_class}.#{plural_name}.build" : "#{model_name}.new" %>(params[:<%= file_name %>])
28
+
29
+ if @<%= file_name %>.save
30
+ flash[:notice] = '<%= model_name %> was successfully created.'
31
+ redirect_to <%= object_path("@#{file_name}") %>
32
+ else
33
+ render :action => "new"
34
+ end
35
+ end
36
+
37
+ # PUT /<%= plural_name %>/1
38
+ def update
39
+ @<%= file_name %> = <%= model_name %>.find(params[:id])
40
+
41
+ if @<%= file_name %>.update_attributes(params[:<%= file_name %>])
42
+ flash[:notice] = '<%= model_name %> was successfully updated.'
43
+ redirect_to <%= object_path("@#{file_name}") %>
44
+ else
45
+ render :action => "edit"
46
+ end
47
+ end
48
+
49
+ # DELETE /<%= plural_name %>/1
50
+ def destroy
51
+ @<%= file_name %> = <%= model_name %>.find(params[:id])
52
+ @<%= file_name %>.destroy
53
+
54
+ redirect_to <%= index_path %>
55
+ end
56
+
57
+ <% if nested_to_class %>
58
+ protected
59
+
60
+ def find_<%= nested_to_class %>
61
+ @<%= nested_to_class %> = <%= nested_to_class.camelcase %>.find(params[:<%= nested_to_class %>_id])
62
+ end
63
+ <% end %>
64
+ end
@@ -0,0 +1,10 @@
1
+ <%%= f.error_messages %>
2
+
3
+ <%- for attribute in attributes -%>
4
+ <% if attribute.name != nested_to_class %>
5
+ <p>
6
+ <%%= f.label :<%= attribute.name %> %><br />
7
+ <%%= f.<%= attribute.field_type %> :<%= attribute.name %> %>
8
+ </p>
9
+ <% end %>
10
+ <%- end -%>
@@ -0,0 +1,9 @@
1
+ <h1>Editing <%= singular_name %></h1>
2
+
3
+ <%% form_for <%= object_path("@#{singular_name}") %> do |f| %>
4
+ <%%= render :partial => "form", :locals => { :f => f } %>
5
+ <p><%%= f.submit 'Update' %></p>
6
+ <%% end %>
7
+
8
+ <%%= link_to 'Show', <%= object_path("@#{singular_name}") %> %> |
9
+ <%%= link_to 'Back', <%= index_path %> %>
@@ -0,0 +1,24 @@
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
+ <%% @<%= plural_name %>.each do |<%= singular_name %>| %>
11
+ <tr>
12
+ <% for attribute in attributes -%>
13
+ <td><%%=h <%= singular_name %>.<%= attribute.name %> %></td>
14
+ <% end -%>
15
+ <td><%%= link_to 'Show', <%= object_path("#{singular_name}") %> %></td>
16
+ <td><%%= link_to 'Edit', <%= edit_path("#{singular_name}") %> %></td>
17
+ <td><%%= link_to 'Destroy', <%= object_path("#{singular_name}") %>, :confirm => 'Are you sure?', :method => :delete %></td>
18
+ </tr>
19
+ <%% end %>
20
+ </table>
21
+
22
+ <br />
23
+
24
+ <%%= link_to 'New <%= singular_name %>', <%= new_path %> %>
@@ -0,0 +1,8 @@
1
+ <h1>New <%= singular_name %></h1>
2
+
3
+ <%% form_for <%= object_path("@#{singular_name}") %> do |f| %>
4
+ <%%= render :partial => "form", :locals => { :f => f } %>
5
+ <p><%%= f.submit 'Create' %></p>
6
+ <%% end %>
7
+
8
+ <%%= link_to 'Back', <%= index_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_path("@#{singular_name}") %> %> |
10
+ <%%= link_to 'Back', <%= index_path %> %>
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: handy-generators
3
3
  version: !ruby/object:Gem::Version
4
- hash: 31
4
+ hash: 29
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 0
10
- version: 0.0.0
9
+ - 1
10
+ version: 0.0.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - jordinl
@@ -93,6 +93,18 @@ files:
93
93
  - rails_generators/handy_setup/templates/views/new_user.html.erb
94
94
  - rails_generators/handy_setup/templates/views/show_user.html.erb
95
95
  - rails_generators/handy_setup/templates/views/signup.html.erb
96
+ - rails_generators/hmodel/hmodel_generator.rb
97
+ - rails_generators/hmodel/templates/fixtures.yml
98
+ - rails_generators/hmodel/templates/migration.rb
99
+ - rails_generators/hmodel/templates/model.rb
100
+ - rails_generators/hmodel/templates/unit_test.rb
101
+ - rails_generators/hscaffold/hscaffold_generator.rb
102
+ - rails_generators/hscaffold/templates/controller.rb
103
+ - rails_generators/hscaffold/templates/view__form.html.erb
104
+ - rails_generators/hscaffold/templates/view_edit.html.erb
105
+ - rails_generators/hscaffold/templates/view_index.html.erb
106
+ - rails_generators/hscaffold/templates/view_new.html.erb
107
+ - rails_generators/hscaffold/templates/view_show.html.erb
96
108
  - test/helper.rb
97
109
  - test/test_handy-generators.rb
98
110
  has_rdoc: true