schofield 0.1.5 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. data/.bundle/config +2 -0
  2. data/Gemfile +13 -0
  3. data/Gemfile.lock +28 -0
  4. data/{LICENSE → LICENSE.txt} +1 -1
  5. data/README.rdoc +11 -9
  6. data/Rakefile +28 -24
  7. data/VERSION +1 -1
  8. data/lib/generators/schofield/association.rb +94 -0
  9. data/lib/generators/schofield/attribute.rb +76 -0
  10. data/lib/generators/schofield/attributes.rb +204 -0
  11. data/lib/generators/schofield/level.rb +287 -0
  12. data/lib/generators/schofield/levels.rb +74 -0
  13. data/lib/generators/schofield/responses.rb +46 -0
  14. data/lib/generators/schofield/routes.rb +46 -0
  15. data/lib/generators/schofield/schofield_generator.rb +124 -0
  16. data/lib/generators/templates/controller.erb +25 -0
  17. data/lib/generators/templates/form.erb +31 -0
  18. data/lib/generators/templates/index.erb +5 -0
  19. data/lib/generators/templates/join_controller.erb +15 -0
  20. data/lib/generators/templates/model.erb +109 -0
  21. data/lib/generators/templates/show.erb +21 -0
  22. data/schofield.gemspec +48 -46
  23. data/spec/spec_helper.rb +7 -4
  24. metadata +82 -47
  25. data/.gitignore +0 -21
  26. data/generators/schofield_controller/schofield_controller_generator.rb +0 -157
  27. data/generators/schofield_controller/templates/controller_spec.rb +0 -25
  28. data/generators/schofield_controller/templates/helper_spec.rb +0 -11
  29. data/generators/schofield_controller/templates/index_partial.rb +0 -26
  30. data/generators/schofield_controller/templates/nested/controller.rb +0 -106
  31. data/generators/schofield_controller/templates/nested/edit.rb +0 -5
  32. data/generators/schofield_controller/templates/nested/index.rb +0 -3
  33. data/generators/schofield_controller/templates/nested/new.rb +0 -1
  34. data/generators/schofield_controller/templates/nested/show.rb +0 -12
  35. data/generators/schofield_controller/templates/unnested/controller.rb +0 -94
  36. data/generators/schofield_controller/templates/unnested/edit.rb +0 -5
  37. data/generators/schofield_controller/templates/unnested/index.rb +0 -3
  38. data/generators/schofield_controller/templates/unnested/new.rb +0 -1
  39. data/generators/schofield_controller/templates/unnested/show.rb +0 -12
  40. data/generators/schofield_controller/templates/view_spec.rb +0 -12
  41. data/generators/schofield_form/schofield_form_generator.rb +0 -22
  42. data/generators/schofield_form/templates/view__form.html.haml +0 -11
  43. data/lib/schofield.rb +0 -111
  44. data/lib/schofield/tasks.rb +0 -3
  45. data/lib/schofield/tasks/schofield.rake +0 -7
@@ -1,106 +0,0 @@
1
- class <%= class_name %>Controller < Admin::AdminController
2
-
3
- before_filter :find_<%= sco_underscored %>, :only => [<%= filter_actions %w( show edit update destroy) %>]
4
- <%- sco_parents.each do |parent| -%>
5
- before_filter :find_<%= parent[:underscored] %>, :except => [<%= filter_actions %w( destroy sort ) %>]
6
- <%- end -%>
7
- before_filter :titles, :except => [<%= filter_actions %w( destroy sort ) %>]
8
-
9
-
10
- <% if actions.include?('index') -%>
11
-
12
- def index
13
- @<%= sco_underscored_plural %> = @<%= sco_parents.first[:underscored] %>.<%= sco_underscored_plural %>
14
- end
15
-
16
- <% end -%>
17
- <% if actions.include?('show') -%>
18
-
19
- def show
20
- end
21
-
22
- <% end -%>
23
- <% if actions.include?('new') -%>
24
-
25
- def new
26
- @<%= sco_underscored %> = @<%= sco_parents.first[:underscored] %>.<%= sco_underscored_plural %>.build
27
- end
28
-
29
- <% end -%>
30
- <% if actions.include?('edit') -%>
31
-
32
- def edit
33
- end
34
-
35
- <% end -%>
36
- <% if actions.include?('create') -%>
37
-
38
- def create
39
- @<%= sco_underscored %> = @<%= sco_parents.first[:underscored] %>.<%= sco_underscored_plural %>.build(params[:<%= sco_underscored %>])
40
- if @<%= sco_underscored %>.save
41
- flash[:notice] = '<%= sco_humanized_uc %> was successfully created.'
42
- redirect_to_form_referer
43
- else
44
- render :action => 'new'
45
- end
46
- end
47
-
48
- <% end -%>
49
- <% if actions.include?('update') -%>
50
-
51
- def update
52
- if @<%= sco_underscored %>.update_attributes(params[:<%= sco_underscored %>])
53
- flash[:notice] = '<%= sco_humanized_uc %> was successfully updated.'
54
- redirect_to_form_referer
55
- else
56
- render :action => 'edit'
57
- end
58
- end
59
-
60
- <% end -%>
61
- <% if actions.include?('destroy') -%>
62
-
63
- def destroy
64
- @<%= sco_underscored %>.destroy
65
- redirect_to :back
66
- end
67
-
68
- <% end -%>
69
- <% if actions.include?('sort') -%>
70
-
71
- def sort
72
- params[:<%= sco_underscored_plural %>].each_with_index do |id, index|
73
- <%= sco_class %>.update_all(['position=?', index+1], ['id=?', id])
74
- end
75
- render :nothing => true
76
- end
77
-
78
- <% end -%>
79
- <% for action in non_restful_actions -%>
80
-
81
- def <%= action %>
82
- end
83
-
84
- <% end -%>
85
-
86
- private
87
- <% sco_parents.each_with_index do |parent, index| -%>
88
- def find_<%= parent[:underscored] %>
89
- @<%= parent[:underscored] %> = params[:<%= parent[:underscored] %>_id].present? ? <%= parent[:class] %>.find(params[:<%= parent[:underscored] %>_id]) : @<%= index == 0 ? sco_underscored : sco_parents[index-1][:underscored] %>.<%= parent[:underscored] %>
90
- end
91
-
92
- <% end -%>
93
- def find_<%= sco_underscored %>
94
- @<%= sco_underscored %> = <%= sco_class %>.find(params[:id])
95
- end
96
-
97
- def titles
98
- <%- sco_parents.reverse.each_with_index do |parent, index| -%>
99
- add_breadcrumb '<%= parent[:titleized_plural] %>', admin<%= index > 0 ? "_#{sco_parents.reverse[index-1][:underscored]}" : '' %>_<%= parent[:underscored_plural] %>_path<%= index > 0 ? "(@#{sco_parents.reverse[index-1][:underscored]})" : '' %>
100
- add_breadcrumb @<%= parent[:underscored] %>
101
- <%- end -%>
102
- add_breadcrumb '<%= sco_titleized_plural %>'
103
- add_breadcrumb @<%= sco_underscored %>
104
- end
105
-
106
- end
@@ -1,5 +0,0 @@
1
- <%- if actions.include?('show') -%>
2
- - toggle_show_edit 'Show', :admin, @<%= sco_underscored %>
3
- <%- end -%>
4
-
5
- = render :partial => 'form', :object => @<%= sco_underscored %>
@@ -1,3 +0,0 @@
1
- %p= link_to('Add new <%= sco_humanized %>', new_admin_<%= sco_parent.first[:underscored] %>_<%= sco_underscored %>_path)
2
-
3
- = render :partial => 'shared/<%= sco_underscored_plural %>', :object => @<%= sco_underscored_plural %>
@@ -1 +0,0 @@
1
- = render :partial => 'form', :object => @<%= sco_underscored %>
@@ -1,12 +0,0 @@
1
- <%- if actions.include?('edit') -%>
2
- - toggle_show_edit 'Edit', :admin, @<%= sco_underscored %>
3
- <%- end -%>
4
- <% sco_children.each do |child| -%>
5
-
6
-
7
- %h5 <%= child[:titleized_plural] %>
8
-
9
- %p= link_to('Add new <%= child[:humanized] %>', new_admin_<%= sco_underscored %>_<%= child[:underscored] %>_path(@<%= sco_underscored %>))
10
-
11
- = render :partial => 'shared/<%= child[:underscored_plural] %>', :object => @<%= sco_underscored %>.<%= child[:underscored_plural] %>
12
- <% end -%>
@@ -1,94 +0,0 @@
1
- class <%= class_name %>Controller < Admin::AdminController
2
-
3
- before_filter :find_<%= sco_underscored %>, :only => [<%= filter_actions %w( show edit update destroy) %>]
4
- before_filter :titles, :except => [<%= filter_actions %w( destroy sort ) %>]
5
-
6
-
7
- <% if actions.include?('index') -%>
8
-
9
- def index
10
- @<%= sco_underscored_plural %> = <%= sco_class %>.all
11
- end
12
-
13
- <% end -%>
14
- <% if actions.include?('show') -%>
15
-
16
- def show
17
- end
18
-
19
- <% end -%>
20
- <% if actions.include?('new') -%>
21
-
22
- def new
23
- @<%= sco_underscored %> = <%= sco_class %>.new
24
- end
25
-
26
- <% end -%>
27
- <% if actions.include?('edit') -%>
28
-
29
- def edit
30
- end
31
-
32
- <% end -%>
33
- <% if actions.include?('create') -%>
34
-
35
- def create
36
- @<%= sco_underscored %> = <%= sco_class %>.new(params[:<%= sco_underscored %>])
37
- if @<%= sco_underscored %>.save
38
- flash[:notice] = '<%= sco_humanized_uc %> was successfully created.'
39
- redirect_to_form_referer
40
- else
41
- render :action => 'new'
42
- end
43
- end
44
-
45
- <% end -%>
46
- <% if actions.include?('update') -%>
47
-
48
- def update
49
- if @<%= sco_underscored %>.update_attributes(params[:<%= sco_underscored %>])
50
- flash[:notice] = '<%= sco_humanized_uc %> was successfully updated.'
51
- redirect_to_form_referer
52
- else
53
- render :action => 'edit'
54
- end
55
- end
56
-
57
- <% end -%>
58
- <% if actions.include?('destroy') -%>
59
-
60
- def destroy
61
- @<%= sco_underscored %>.destroy
62
- redirect_to :back
63
- end
64
-
65
- <% end -%>
66
- <% if actions.include?('sort') -%>
67
-
68
- def sort
69
- params[:<%= sco_underscored_plural %>].each_with_index do |id, index|
70
- <%= sco_class %>.update_all(['position=?', index+1], ['id=?', id])
71
- end
72
- render :nothing => true
73
- end
74
-
75
- <% end -%>
76
- <% for action in non_restful_actions -%>
77
-
78
- def <%= action %>
79
- end
80
-
81
- <% end -%>
82
-
83
- private
84
-
85
- def find_<%= sco_underscored %>
86
- @<%= sco_underscored %> = <%= sco_class %>.find(params[:id])
87
- end
88
-
89
- def titles
90
- add_breadcrumb '<%= sco_titleized_plural %>', admin_<%= sco_underscored_plural %>_path
91
- add_breadcrumb @<%= sco_underscored %>
92
- end
93
-
94
- end
@@ -1,5 +0,0 @@
1
- <%- if actions.include?('show') -%>
2
- - toggle_show_edit 'Show', :admin, @<%= sco_underscored %>
3
- <%- end -%>
4
-
5
- = render :partial => 'form', :object => @<%= sco_underscored %>
@@ -1,3 +0,0 @@
1
- %p= link_to('Add new <%= sco_humanized %>', new_admin_<%= sco_underscored %>_path)
2
-
3
- = render :partial => 'shared/<%= sco_underscored_plural %>', :object => @<%= sco_underscored_plural %>
@@ -1 +0,0 @@
1
- = render :partial => 'form', :object => @<%= sco_underscored %>
@@ -1,12 +0,0 @@
1
- <%- if actions.include?('edit') -%>
2
- - toggle_show_edit 'Edit', :admin, @<%= sco_underscored %>
3
- <%- end -%>
4
- <% sco_children.each do |child| -%>
5
-
6
-
7
- %h5 <%= child[:titleized_plural] %>
8
-
9
- %p= link_to('Add new <%= child[:humanized] %>', new_admin_<%= sco_underscored %>_<%= child[:underscored] %>_path(@<%= sco_underscored %>))
10
-
11
- = render :partial => 'shared/<%= child[:underscored_plural] %>', :object => @<%= sco_underscored %>.<%= child[:underscored_plural] %>
12
- <% end -%>
@@ -1,12 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe "/<%= class_name.underscore %>/<%= action %>" do
4
- before(:each) do
5
- render '<%= class_name.underscore %>/<%= action %>'
6
- end
7
-
8
- #Delete this example and add some real ones or delete this file
9
- it "should tell you where to find the file" do
10
- response.should have_tag('p', %r[Find me in app/views/<%= class_name.underscore %>/<%= action %>])
11
- end
12
- end
@@ -1,22 +0,0 @@
1
- class SchofieldFormGenerator < FormGenerator
2
-
3
- protected
4
-
5
- def add_options!(opt)
6
- super
7
- opt.on('--parents PARENTS_PATH',
8
- "Specify the parents for the specified model.") do |v|
9
- options[:parents] = v if v.present?
10
- end
11
- end
12
-
13
- def form_model type='new'
14
- array = [':admin']
15
- if type == 'new' && options[:parents].present?
16
- array << "@#{options[:parents].first}"
17
- end
18
- array << "@#{singular_name.singularize}"
19
- "[#{array.join(', ')}]"
20
- end
21
-
22
- end
@@ -1,11 +0,0 @@
1
- = error_messages_for '<%= singular_name.singularize %>', :object => object
2
-
3
- - semantic_form_for @<%= singular_name.singularize %>.new_record? ? <%= form_model('new') %> : <%= form_model('edit') %>, :html => { :multipart => true } do |f|
4
-
5
- - f.inputs do
6
-
7
- <% attributes.each do |attribute| -%>
8
- = f.input :<%= attribute.name %>, :label => '<%= attribute.name.humanize %>'
9
- <% end -%>
10
-
11
- = f.buttons
@@ -1,111 +0,0 @@
1
- module Schofield
2
-
3
- class RouteInfo
4
-
5
- def initialize route
6
- @route = route
7
- @requirements = @route.requirements
8
- end
9
-
10
- def use?
11
- admin_namespace? && @requirements.any? && @route.segments.length > 3
12
- end
13
-
14
- def controller_name
15
- use? ? @requirements[:controller].gsub(/^admin\//, '') : nil
16
- end
17
-
18
- def controller_path
19
- "admin/#{controller_name}"
20
- end
21
-
22
- def model_name
23
- controller_name.singularize
24
- end
25
-
26
- def action_name
27
- use? ? @requirements[:action] : nil
28
- end
29
-
30
- def parent
31
- return unless use?
32
- segment = @route.segments[3].to_s.singularize
33
- segment == controller_name.singularize ? nil : segment
34
- end
35
-
36
- private
37
- def admin_namespace?
38
- @admin_namespace.nil? ? @admin_namespace = @route.segments[1].to_s == 'admin' : @admin_namespace
39
- end
40
-
41
- end
42
-
43
-
44
- class ControllerInfo
45
-
46
- attr_reader :parent, :controller_name, :controller_path, :model_name
47
- attr_accessor :actions, :children, :parents
48
-
49
- def initialize route_info
50
- @controller_name = route_info.controller_name
51
- @controller_path = route_info.controller_path
52
- @model_name = route_info.model_name
53
- @parent = route_info.parent
54
- @actions = []
55
- @children = []
56
- @parents = []
57
- end
58
-
59
- end
60
-
61
-
62
- class Generator
63
-
64
- def initialize
65
- @controller_infos = {}
66
- end
67
-
68
- def process_routes
69
- ActionController::Routing::Routes.routes.each { |route| process_route(route) }
70
- end
71
-
72
- def process_route route
73
- route_info = RouteInfo.new(route)
74
- return unless route_info.use?
75
- @controller_infos[route_info.model_name] ||= ControllerInfo.new(route_info)
76
- @controller_infos[route_info.model_name].actions << route_info.action_name
77
- end
78
-
79
- def set_children
80
- @controller_infos.each do |index, info|
81
- @controller_infos[info.parent].children << index unless info.parent.nil?
82
- end
83
- end
84
-
85
- def set_parents
86
- @controller_infos.each do |index, info|
87
- parent_models = []
88
- parent_model = info.parent
89
- while parent_model
90
- parent_models << parent_model
91
- parent_model = @controller_infos[parent_model].parent
92
- end
93
- info.parents = parent_models
94
- end
95
- end
96
-
97
-
98
- def generate
99
- process_routes
100
- set_children
101
- set_parents
102
- require 'rails_generator'
103
- require 'rails_generator/scripts/generate'
104
- @controller_infos.each do |index, info|
105
- Rails::Generator::Scripts::Generate.new.run(['schofield_controller', info.controller_path, *info.actions ], :parents => info.parents, :children => info.children)
106
- Rails::Generator::Scripts::Generate.new.run(['schofield_form', info.model_name], :partial => true, :haml => true, :controller => info.controller_path, :parents => info.parents)
107
- end
108
- end
109
-
110
- end
111
- end
@@ -1,3 +0,0 @@
1
- Dir[File.join(File.dirname(__FILE__), 'tasks', '*.rake')].each do |f|
2
- load f
3
- end
@@ -1,7 +0,0 @@
1
- namespace :schofield do
2
- desc "Generate controllers and views based on routes"
3
- task :generate => :environment do
4
- @generator = Schofield::Generator.new
5
- @generator.generate
6
- end
7
- end