ingoweiss_generators 0.0.3

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,41 @@
1
+ module ScopeHelpers
2
+
3
+ def scope
4
+ options[:scope]
5
+ end
6
+
7
+ def scoped?
8
+ scope.any?
9
+ end
10
+
11
+ def unscoped?
12
+ !scoped?
13
+ end
14
+
15
+ def singleton?
16
+ options[:singleton]
17
+ end
18
+
19
+ # Example: 'post_comment_' for post_comment_approval_path
20
+ def scope_prefix
21
+ scope.collect{|s| s.singularize + '_'}.join
22
+ end
23
+
24
+ # Examples: 'post_comments', 'post_comment_approval'
25
+ def scoped_controller_plural_name
26
+ scope_prefix + (options[:singleton] ? singular_name : plural_name)
27
+ end
28
+
29
+ # Example: '@post, @comment, approval'
30
+ def instance_variable_scope(variable=nil)
31
+ instance_variables = scope.collect{|s| '@' + s.singularize}
32
+ instance_variables << variable if variable
33
+ instance_variables.join(', ')
34
+ end
35
+
36
+ # Examples: 'PostComments', 'PostCommentApproval'
37
+ def scoped_controller_class_name
38
+ scoped_controller_plural_name.camelize
39
+ end
40
+
41
+ end
@@ -0,0 +1,45 @@
1
+ require 'rails/generators/resource_helpers'
2
+ require 'rails/generators/migration'
3
+ require File.join(File.dirname(__FILE__), '../helpers/scope_helpers')
4
+
5
+ module Ingoweiss
6
+ class ScaffoldGenerator < Rails::Generators::NamedBase
7
+ include Rails::Generators::ResourceHelpers
8
+ include Rails::Generators::Migration
9
+ include ScopeHelpers
10
+
11
+ argument :attributes, :type => :array, :required => false, :banner => 'field:type field:type'
12
+ class_option :scope, :type => :array, :default => [], :banner => 'grand_parent parent', :desc => 'Indicate parent resource(s) if nested'
13
+ class_option :singleton, :type => :boolean, :default => false, :desc => 'Is this a singleton resource?'
14
+ class_option :skip_route, :type => :boolean, :default => false, :desc => 'Do not add route to config/routes.rb'
15
+
16
+ hook_for :scaffold_controller
17
+
18
+ def self.source_root
19
+ @source_root ||= File.expand_path('../../templates', __FILE__)
20
+ end
21
+
22
+ def add_routes
23
+ return if options[:skip_route]
24
+ route "resource#{'s' unless options[:singleton]} :#{options[:singleton] ? singular_name : plural_name}"
25
+ end
26
+
27
+ def generate_model
28
+ arguments = [singular_name] + attributes.collect{ |a| [a.name, a.type].join(':') }
29
+ arguments << "#{scope.last.singularize}_id:integer" if scope.any?
30
+ invoke :model, arguments
31
+ end
32
+
33
+ def inject_associations
34
+ return if scope.empty?
35
+ parent = scope.last.singularize
36
+ inject_into_file("app/models/#{parent}.rb", :after => /< ActiveRecord::Base\n/) do
37
+ options[:singleton] ? " has_one :#{singular_name}\n" : " has_many :#{plural_name}\n"
38
+ end
39
+ inject_into_file("app/models/#{singular_name}.rb", :after => /< ActiveRecord::Base\n/) do
40
+ " belongs_to :#{parent}\n"
41
+ end
42
+ end
43
+
44
+ end
45
+ end
@@ -0,0 +1,114 @@
1
+ require File.expand_path('../../helpers/scope_helpers', __FILE__)
2
+
3
+ module Ingoweiss
4
+ class ScaffoldControllerGenerator < Rails::Generators::NamedBase
5
+
6
+ include ScopeHelpers
7
+ argument :attributes, :type => :array, :required => false #only needed to determine whether resource HAS attributes or not. Maybe use boolean option --attributes instead?
8
+ class_option :scope, :type => :array, :default => [], :banner => 'grand_parent parent', :desc => 'Indicate parent resource(s) if nested'
9
+ class_option :singleton, :type => :boolean, :default => false, :desc => 'Is this a singleton resource?'
10
+
11
+ def self.source_root
12
+ @source_root ||= File.expand_path('../templates', __FILE__)
13
+ end
14
+
15
+ def generate_controller
16
+ template 'controller.erb', "app/controllers/#{scoped_controller_plural_name}_controller.rb"
17
+ end
18
+
19
+ def generate_views
20
+ invoke 'ingoweiss:scaffold_views'
21
+ end
22
+
23
+ private
24
+
25
+ # Examples: 'post_comments', 'post_comment_approval'
26
+ def scoped_controller_plural_name
27
+ scope_prefix + (options.singleton? ? singular_name : plural_name)
28
+ end
29
+
30
+ # Examples: 'PostComments', 'PostCommentApproval'
31
+ def scoped_controller_class_name
32
+ scoped_controller_plural_name.camelize
33
+ end
34
+
35
+ def controller_retrieve_scope
36
+ lines = []
37
+ options.scope.each_with_index do |scope_item, index|
38
+ if index == 0
39
+ lines << "@#{scope_item.singularize} = #{scope_item.singularize.classify}.find(params[:#{scope_item.singularize}_id])"
40
+ else
41
+ previous_scope_item = scope[index-1]
42
+ if scope_item.pluralize == scope_item
43
+ lines << "@#{scope_item.singularize} = @#{previous_scope_item.singularize}.#{scope_item.pluralize}.find(params[:#{scope_item.singularize}_id])"
44
+ else
45
+ lines << "@#{scope_item.singularize} = @#{previous_scope_item.singularize}.#{scope_item.singularize}"
46
+ end
47
+ end
48
+ end
49
+ lines
50
+ end
51
+
52
+ def controller_retrieve_resource
53
+ if options.scope.any?
54
+ if options[:singleton]
55
+ "@#{name.singularize} = @#{scope.last.singularize}.#{name.singularize}"
56
+ else
57
+ "@#{name.singularize} = @#{scope.last.singularize}.#{name.pluralize}.find(params[:id])"
58
+ end
59
+ else
60
+ "@#{name.singularize} = #{name.singularize.classify}.find(params[:id])"
61
+ end
62
+ end
63
+
64
+ def controller_retrieve_collection
65
+ if options.scope.any?
66
+ "@#{name.pluralize} = @#{scope.last.singularize}.#{name.pluralize}"
67
+ else
68
+ "@#{name.pluralize} = #{name.singularize.classify}.all"
69
+ end
70
+ end
71
+
72
+ def controller_build_resource
73
+ if options.scope.any?
74
+ if options[:singleton]
75
+ "@#{name.singularize} = @#{scope.last.singularize}.build_#{name.singularize}" + (attributes.any? ? "(params[:#{name.singularize}])" : '')
76
+ else
77
+ "@#{name.singularize} = @#{scope.last.singularize}.#{name.pluralize}.build" + (attributes.any? ? "(params[:#{name.singularize}])" : '')
78
+ end
79
+ else
80
+ "@#{name.singularize} = #{name.singularize.classify}.new" + (attributes.any? ? "(params[:#{name.singularize}])" : '')
81
+ end
82
+ end
83
+
84
+ def controller_create_resource
85
+ if options.scope.any?
86
+ if options[:singleton]
87
+ "@#{name.singularize} = @#{scope.last.singularize}.create_#{name.singularize}" + (attributes.any? ? "(params[:#{name.singularize}])" : '')
88
+ else
89
+ "@#{name.singularize} = @#{scope.last.singularize}.#{name.pluralize}.create" + (attributes.any? ? "(params[:#{name.singularize}])" : '')
90
+ end
91
+
92
+ else
93
+ "@#{name.singularize} = #{name.singularize.classify}.create" + (attributes.any? ? "(params[:#{name.singularize}])" : '')
94
+ end
95
+ end
96
+
97
+ def controller_destroy_resource
98
+ "@#{name.singularize}.destroy"
99
+ end
100
+
101
+ def controller_update_resource
102
+ "@#{name.singularize}.update_attributes" + (attributes.any? ? "(params[:#{name.singularize}])" : '')
103
+ end
104
+
105
+ def controller_respond_with_collection
106
+ "respond_with #{(options.scope.collect{|scope_item| '@' + scope_item.singularize} + ['@' + name.pluralize]).join(', ')}"
107
+ end
108
+
109
+ def controller_respond_with_resource
110
+ "respond_with #{(options.scope.collect{|scope_item| '@' + scope_item.singularize} + ['@' + name.singularize]).join(', ')}"
111
+ end
112
+
113
+ end
114
+ end
@@ -0,0 +1,58 @@
1
+ class <%= scoped_controller_class_name %>Controller < ActionController::Base
2
+
3
+ <%- if scoped? -%>
4
+ before_filter :scope
5
+ <%- end -%>
6
+ respond_to :html, :xml, :json
7
+ layout 'scaffold'
8
+
9
+ <%- unless options[:singleton] -%>
10
+ def index
11
+ <%= controller_retrieve_collection %>
12
+ <%= controller_respond_with_collection %>
13
+ end
14
+
15
+ <%- end -%>
16
+ def show
17
+ <%= controller_retrieve_resource %>
18
+ <%= controller_respond_with_resource %>
19
+ end
20
+
21
+ def new
22
+ <%= controller_build_resource %>
23
+ <%= controller_respond_with_resource %>
24
+ end
25
+
26
+ def edit
27
+ <%= controller_retrieve_resource %>
28
+ <%= controller_respond_with_resource %>
29
+ end
30
+
31
+ def create
32
+ <%= controller_create_resource %>
33
+ <%= controller_respond_with_resource %>
34
+ end
35
+
36
+ def update
37
+ <%= controller_retrieve_resource %>
38
+ <%= controller_update_resource %>
39
+ <%= controller_respond_with_resource %>
40
+ end
41
+
42
+ def destroy
43
+ <%= controller_retrieve_resource %>
44
+ <%= controller_destroy_resource %>
45
+ <%= controller_respond_with_resource %>
46
+ end
47
+
48
+ <%- if scoped? -%>
49
+ private
50
+
51
+ def scope
52
+ <%- controller_retrieve_scope.each do |line| -%>
53
+ <%= line %>
54
+ <%- end -%>
55
+ end
56
+
57
+ <%- end -%>
58
+ end
@@ -0,0 +1,36 @@
1
+ require File.expand_path('../../helpers/scope_helpers', __FILE__)
2
+ require 'rails/generators/resource_helpers'
3
+
4
+ module Ingoweiss
5
+ class ScaffoldViewsGenerator < Rails::Generators::NamedBase
6
+
7
+ include Rails::Generators::ResourceHelpers
8
+ include ScopeHelpers
9
+
10
+ argument :attributes, :type => :array, :required => false, :banner => 'field:type field:type'
11
+ class_option :scope, :type => :array, :default => [], :banner => 'grand_parent parent', :desc => 'Indicate parent resource(s) if nested'
12
+ class_option :singleton, :type => :boolean, :default => false, :desc => 'Is this a singleton resource?'
13
+
14
+ def self.source_root
15
+ @source_root ||= File.expand_path('../templates', __FILE__)
16
+ end
17
+
18
+ def generate_views
19
+ template 'index.html.erb', "app/views/#{scoped_controller_plural_name}/index.html.erb" unless options[:singleton]
20
+ template '_entry.html.erb', "app/views/#{scoped_controller_plural_name}/_#{singular_name}.html.erb"
21
+ template 'new.html.erb', "app/views/#{scoped_controller_plural_name}/new.html.erb"
22
+ template 'edit.html.erb', "app/views/#{scoped_controller_plural_name}/edit.html.erb"
23
+ template '_fields.html.erb', "app/views/#{scoped_controller_plural_name}/_fields.html.erb"
24
+ template 'show.html.erb', "app/views/#{scoped_controller_plural_name}/show.html.erb"
25
+ template 'layout.html.erb', "app/views/layouts/scaffold.html.erb" unless File.exists?(File.join(destination_root, 'app/views/layouts/scaffold.html.erb'))
26
+ invoke :stylesheets
27
+ end
28
+
29
+ def inject_link_to_children_index
30
+ return if singleton? || unscoped?
31
+ parent_resource_view_folder = (scope[0..-2].collect(&:singularize) << scope.last).join('_')
32
+ append_file "app/views/#{parent_resource_view_folder}/show.html.erb", "<%= link_to 'Show #{plural_name}', #{scope_prefix}#{plural_name}_path(#{instance_variable_scope}) %>\n"
33
+ end
34
+
35
+ end
36
+ end
@@ -0,0 +1,8 @@
1
+ <tr>
2
+ <% for attribute in attributes -%>
3
+ <td><%%= <%= singular_name %>.<%= attribute.name %> %></td>
4
+ <% end -%>
5
+ <td><%%= link_to 'Show', <%= scope_prefix + singular_name %>_path(<%= instance_variable_scope(singular_name) %>) %></td>
6
+ <td><%%= link_to 'Edit', edit_<%= scope_prefix + singular_name %>_path(<%= instance_variable_scope(singular_name) %>) %></td>
7
+ <td><%%= link_to 'Destroy', <%= scope_prefix + singular_name %>_path(<%= instance_variable_scope(singular_name) %>), :confirm => 'Are you sure?', :method => :delete %></td>
8
+ </tr>
@@ -0,0 +1,6 @@
1
+ <% for attribute in attributes -%>
2
+ <div class="field">
3
+ <%%= f.label :<%= attribute.name %> %><br />
4
+ <%%= f.<%= attribute.field_type %> :<%= attribute.name %> %>
5
+ </div>
6
+ <% end -%>
@@ -0,0 +1,6 @@
1
+ <h1>Editing <%= singular_name %></h1>
2
+
3
+ <%%= render 'form' %>
4
+
5
+ <%%= link_to 'Show', @<%= singular_name %> %> |
6
+ <%%= link_to 'Back', <%= plural_name %>_path %>
@@ -0,0 +1,15 @@
1
+ <h1>Listing <%= plural_name %></h1>
2
+
3
+ <table>
4
+ <tr>
5
+ <% for attribute in attributes -%>
6
+ <th><%= attribute.human_name %></th>
7
+ <% end -%>
8
+ <th></th>
9
+ <th></th>
10
+ <th></th>
11
+ </tr>
12
+ <%%= render :partial => '<%= singular_name %>', :collection => @<%= plural_name %> %>
13
+ </table>
14
+
15
+ <p><%%= link_to 'New <%= singular_name %>', new_<%= scope_prefix + singular_name %>_path(<%= instance_variable_scope %>) %></p>
@@ -0,0 +1,16 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title><%= controller_class_name %>: <%%= controller.action_name %></title>
5
+ <%%= stylesheet_link_tag 'scaffold' %>
6
+ <%%= javascript_include_tag :defaults %>
7
+ <%%= csrf_meta_tag %>
8
+ </head>
9
+ <body>
10
+
11
+ <p class="notice"><%%= notice %></p>
12
+
13
+ <%%= yield %>
14
+
15
+ </body>
16
+ </html>
@@ -0,0 +1,10 @@
1
+ <h1>New <%= singular_name %></h1>
2
+
3
+ <%%= form_for :<%= singular_name %>, :url => <%= scope_prefix + (singleton? ? singular_name : plural_name) %>_path(<%= instance_variable_scope %>) do |f| %>
4
+ <%%= render :partial => 'fields', :locals => {:f => f} %>
5
+ <%%= f.submit %>
6
+ <%% end %>
7
+ <%- unless options[:singleton] -%>
8
+
9
+ <%%= link_to 'Back', <%= scope_prefix + plural_name %>_path(<%= instance_variable_scope %>) %>
10
+ <%- end -%>
@@ -0,0 +1,12 @@
1
+ <% for attribute in attributes -%>
2
+ <p>
3
+ <b><%= attribute.human_name %>:</b>
4
+ <%%= @<%= singular_name %>.<%= attribute.name %> %>
5
+ </p>
6
+
7
+ <% end -%>
8
+
9
+ <%%= link_to 'Edit', edit_<%= scope_prefix + singular_name %>_path(<%= instance_variable_scope('@' + singular_name) %>) %>
10
+ <%- unless options[:singleton] -%>
11
+ <%%= link_to 'Back', <%= scope_prefix + plural_name %>_path(<%= instance_variable_scope %>) %>
12
+ <%- end -%>
@@ -0,0 +1,11 @@
1
+ class <%= migration_class_name %> < ActiveRecord::Migration
2
+ def self.up<% attributes.each do |attribute| %>
3
+ <%= migration_action %>_column :<%= table_name %>, :<%= attribute.name %><% if migration_action == 'add' %>, :<%= attribute.type %><% end -%>
4
+ <%- end %>
5
+ end
6
+
7
+ def self.down<% attributes.reverse.each do |attribute| %>
8
+ <%= migration_action == 'add' ? 'remove' : 'add' %>_column :<%= table_name %>, :<%= attribute.name %><% if migration_action == 'remove' %>, :<%= attribute.type %><% end -%>
9
+ <%- end %>
10
+ end
11
+ end
@@ -0,0 +1,5 @@
1
+ module IngoweissGenerators
2
+ class Railtie < Rails::Railtie
3
+ # config.generators.scaffold_controller :ingoweiss
4
+ end
5
+ end
@@ -0,0 +1,3 @@
1
+ module IngoweissGenerators
2
+ require 'ingoweiss_generators/railtie' if defined?(Rails)
3
+ end
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ingoweiss_generators
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 3
9
+ version: 0.0.3
10
+ platform: ruby
11
+ authors:
12
+ - Ingo Weiss
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-05-09 00:00:00 +02:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rails
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ~>
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 3
29
+ - 0
30
+ - 0
31
+ - beta1
32
+ version: 3.0.0.beta1
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ description: |
36
+ A collection of generators reflecting my personal preferences
37
+
38
+ email: ingo@ingoweiss.com
39
+ executables: []
40
+
41
+ extensions: []
42
+
43
+ extra_rdoc_files: []
44
+
45
+ files:
46
+ - lib/generators/ingoweiss/helpers/scope_helpers.rb
47
+ - lib/generators/ingoweiss/scaffold/scaffold_generator.rb
48
+ - lib/generators/ingoweiss/scaffold_controller/scaffold_controller_generator.rb
49
+ - lib/generators/ingoweiss/scaffold_controller/templates/controller.erb
50
+ - lib/generators/ingoweiss/scaffold_views/scaffold_views_generator.rb
51
+ - lib/generators/ingoweiss/scaffold_views/templates/_entry.html.erb
52
+ - lib/generators/ingoweiss/scaffold_views/templates/_fields.html.erb
53
+ - lib/generators/ingoweiss/scaffold_views/templates/edit.html.erb
54
+ - lib/generators/ingoweiss/scaffold_views/templates/index.html.erb
55
+ - lib/generators/ingoweiss/scaffold_views/templates/layout.html.erb
56
+ - lib/generators/ingoweiss/scaffold_views/templates/new.html.erb
57
+ - lib/generators/ingoweiss/scaffold_views/templates/show.html.erb
58
+ - lib/generators/ingoweiss/templates/migration.erb
59
+ - lib/ingoweiss_generators/railtie.rb
60
+ - lib/ingoweiss_generators.rb
61
+ has_rdoc: true
62
+ homepage:
63
+ licenses: []
64
+
65
+ post_install_message:
66
+ rdoc_options: []
67
+
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ segments:
75
+ - 0
76
+ version: "0"
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ segments:
82
+ - 0
83
+ version: "0"
84
+ requirements: []
85
+
86
+ rubyforge_project:
87
+ rubygems_version: 1.3.6
88
+ signing_key:
89
+ specification_version: 3
90
+ summary: A collection of generators
91
+ test_files: []
92
+