schofield 0.1.1 → 0.1.2
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.
- data/VERSION +1 -1
- data/generators/schofield_controller/schofield_controller_generator.rb +23 -16
- data/generators/schofield_controller/templates/nested/controller.rb +14 -9
- data/generators/schofield_controller/templates/nested/index.rb +1 -1
- data/generators/schofield_form/schofield_form_generator.rb +9 -5
- data/generators/schofield_form/templates/view__form.html.haml +1 -1
- data/lib/schofield.rb +18 -3
- data/schofield.gemspec +1 -1
- metadata +2 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.2
|
@@ -13,7 +13,7 @@ class SchofieldControllerGenerator < Rails::Generator::NamedBase
|
|
13
13
|
def manifest
|
14
14
|
record do |m|
|
15
15
|
|
16
|
-
template_directory =
|
16
|
+
template_directory = has_parents? ? 'nested' : 'unnested'
|
17
17
|
|
18
18
|
sco_class = class_name.gsub('Admin::', '').singularize
|
19
19
|
sco_underscored = sco_class.underscore
|
@@ -22,13 +22,23 @@ class SchofieldControllerGenerator < Rails::Generator::NamedBase
|
|
22
22
|
sco_titleized_plural = sco_underscored_plural.titleize
|
23
23
|
sco_humanized = sco_humanized_uc.downcase
|
24
24
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
25
|
+
sco_parents = []
|
26
|
+
if has_parents?
|
27
|
+
options[:parents].each do |parent|
|
28
|
+
sco_parent_class = has_parents? ? parent.classify : ''
|
29
|
+
sco_parent_underscored = sco_parent_class.underscore.downcase
|
30
|
+
sco_parent_underscored_plural = sco_parent_underscored.pluralize
|
31
|
+
sco_parent_titleized_plural = sco_parent_underscored.titleize.pluralize
|
32
|
+
sco_parents << {
|
33
|
+
:class => sco_parent_class,
|
34
|
+
:underscored => sco_parent_underscored,
|
35
|
+
:underscored_plural => sco_parent_underscored_plural,
|
36
|
+
:titleized_plural => sco_parent_titleized_plural,
|
37
|
+
}
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
30
41
|
sco_children = []
|
31
|
-
|
32
42
|
if has_children?
|
33
43
|
options[:children].each do |child|
|
34
44
|
sco_child_class = has_children? ? child.classify : ''
|
@@ -50,10 +60,7 @@ class SchofieldControllerGenerator < Rails::Generator::NamedBase
|
|
50
60
|
:sco_underscored_plural => sco_underscored_plural,
|
51
61
|
:sco_class => sco_class,
|
52
62
|
:sco_humanized_uc => sco_humanized_uc,
|
53
|
-
:
|
54
|
-
:sco_parent_underscored_plural => sco_parent_underscored_plural,
|
55
|
-
:sco_parent_titleized_plural => sco_parent_titleized_plural,
|
56
|
-
:sco_parent_class => sco_parent_class,
|
63
|
+
:sco_parents => sco_parents,
|
57
64
|
:sco_titleized_plural => sco_titleized_plural,
|
58
65
|
:sco_humanized => sco_humanized,
|
59
66
|
:non_restful_actions => non_restful_actions,
|
@@ -97,7 +104,7 @@ class SchofieldControllerGenerator < Rails::Generator::NamedBase
|
|
97
104
|
:assigns => assigns if actions_with_view.include?(action)
|
98
105
|
end
|
99
106
|
|
100
|
-
if actions.include?('index') ||
|
107
|
+
if actions.include?('index') || has_parents?
|
101
108
|
m.template 'index_partial.rb',
|
102
109
|
File.join('app/views/shared', "_#{sco_underscored_plural}.haml"),
|
103
110
|
:assigns => assigns
|
@@ -127,12 +134,12 @@ class SchofieldControllerGenerator < Rails::Generator::NamedBase
|
|
127
134
|
def add_options!(opt)
|
128
135
|
opt.separator ''
|
129
136
|
opt.separator 'Options:'
|
130
|
-
opt.on('--
|
137
|
+
opt.on('--parents PARENTS', 'Specify the parent models') { |v| options[:parents] = v if v.present? }
|
131
138
|
opt.on('--children CHILDREN', 'Specify the children models') { |v| options[:children] = v if v.present? }
|
132
139
|
end
|
133
140
|
|
134
|
-
def
|
135
|
-
options[:
|
141
|
+
def has_parents?
|
142
|
+
options[:parents].present?
|
136
143
|
end
|
137
144
|
|
138
145
|
def has_children?
|
@@ -144,7 +151,7 @@ class SchofieldControllerGenerator < Rails::Generator::NamedBase
|
|
144
151
|
end
|
145
152
|
|
146
153
|
def banner
|
147
|
-
"Usage: #{$0} schofield_controller ControllerName [--
|
154
|
+
"Usage: #{$0} schofield_controller ControllerName [--parents PARENTS --children CHILDREN]"
|
148
155
|
end
|
149
156
|
|
150
157
|
end
|
@@ -1,14 +1,16 @@
|
|
1
1
|
class <%= class_name %>Controller < Admin::AdminController
|
2
2
|
|
3
3
|
before_filter :find_<%= sco_underscored %>, :only => [<%= filter_actions %w( show edit update destroy) %>]
|
4
|
-
|
4
|
+
<%- sco_parents.each do |parent| -%>
|
5
|
+
before_filter :find_<%= parent[:underscored] %>, :except => [<%= filter_actions %w( destroy sort ) %>]
|
6
|
+
<%- end -%>
|
5
7
|
before_filter :titles, :except => [<%= filter_actions %w( destroy sort ) %>]
|
6
8
|
|
7
9
|
|
8
10
|
<% if actions.include?('index') -%>
|
9
11
|
|
10
12
|
def index
|
11
|
-
@<%= sco_underscored_plural %> = @<%=
|
13
|
+
@<%= sco_underscored_plural %> = @<%= sco_parents.last[:underscored] %>.<%= sco_underscored_plural %>
|
12
14
|
end
|
13
15
|
|
14
16
|
<% end -%>
|
@@ -21,7 +23,7 @@ class <%= class_name %>Controller < Admin::AdminController
|
|
21
23
|
<% if actions.include?('new') -%>
|
22
24
|
|
23
25
|
def new
|
24
|
-
@<%= sco_underscored %> = @<%=
|
26
|
+
@<%= sco_underscored %> = @<%= sco_parents.last[:underscored] %>.<%= sco_underscored_plural %>.build
|
25
27
|
end
|
26
28
|
|
27
29
|
<% end -%>
|
@@ -34,7 +36,7 @@ class <%= class_name %>Controller < Admin::AdminController
|
|
34
36
|
<% if actions.include?('create') -%>
|
35
37
|
|
36
38
|
def create
|
37
|
-
@<%= sco_underscored %> = @<%=
|
39
|
+
@<%= sco_underscored %> = @<%= sco_parents.last[:underscored] %>.<%= sco_underscored_plural %>.build(params[:<%= sco_underscored %>])
|
38
40
|
if @<%= sco_underscored %>.save
|
39
41
|
flash[:notice] = '<%= sco_humanized_uc %> was successfully created.'
|
40
42
|
redirect_to_form_referer
|
@@ -82,18 +84,21 @@ class <%= class_name %>Controller < Admin::AdminController
|
|
82
84
|
<% end -%>
|
83
85
|
|
84
86
|
private
|
85
|
-
|
86
|
-
def find_<%=
|
87
|
-
@<%=
|
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] %>
|
88
90
|
end
|
89
91
|
|
92
|
+
<% end -%>
|
90
93
|
def find_<%= sco_underscored %>
|
91
94
|
@<%= sco_underscored %> = <%= sco_class %>.find(params[:id])
|
92
95
|
end
|
93
96
|
|
94
97
|
def titles
|
95
|
-
|
96
|
-
add_breadcrumb
|
98
|
+
<%- sco_parents.reverse_each do |parent| -%>
|
99
|
+
add_breadcrumb '<%= parent[:titleized_plural] %>', admin_<%= parent[:underscored_plural] %>_path
|
100
|
+
add_breadcrumb @<%= parent[:underscored] %>
|
101
|
+
<%- end -%>
|
97
102
|
add_breadcrumb '<%= sco_titleized_plural %>'
|
98
103
|
add_breadcrumb @<%= sco_underscored %>
|
99
104
|
end
|
@@ -1,3 +1,3 @@
|
|
1
|
-
%p= link_to('Add new <%= sco_humanized %>', new_admin_<%=
|
1
|
+
%p= link_to('Add new <%= sco_humanized %>', new_admin_<%= sco_parent.first[:underscored] %>_<%= sco_underscored %>_path)
|
2
2
|
|
3
3
|
= render :partial => 'shared/<%= sco_underscored_plural %>', :object => @<%= sco_underscored_plural %>
|
@@ -4,15 +4,19 @@ class SchofieldFormGenerator < FormGenerator
|
|
4
4
|
|
5
5
|
def add_options!(opt)
|
6
6
|
super
|
7
|
-
opt.on('--
|
8
|
-
"Specify the
|
9
|
-
options[:
|
7
|
+
opt.on('--parents PARENTS_PATH',
|
8
|
+
"Specify the parents for the specified model.") do |v|
|
9
|
+
options[:parents] = v if v.present?
|
10
10
|
end
|
11
11
|
end
|
12
12
|
|
13
|
-
def form_model
|
13
|
+
def form_model type='new'
|
14
14
|
array = [':admin']
|
15
|
-
|
15
|
+
if type == 'new' && options[:parents].present?
|
16
|
+
options[:parents].each do |parent|
|
17
|
+
array << "@#{parent}"
|
18
|
+
end
|
19
|
+
end
|
16
20
|
array << "@#{singular_name.singularize}"
|
17
21
|
"[#{array.join(', ')}]"
|
18
22
|
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
= error_messages_for '<%= singular_name.singularize %>', :object => object
|
2
2
|
|
3
|
-
- semantic_form_for <%= form_model %>, :html => { :multipart => true } do |f|
|
3
|
+
- semantic_form_for @<%= singular_name.singularize %>.new_record? ? <%= form_model('new') %> : <%= form_model('edit') %>, :html => { :multipart => true } do |f|
|
4
4
|
|
5
5
|
- f.inputs do
|
6
6
|
|
data/lib/schofield.rb
CHANGED
@@ -44,7 +44,7 @@ module Schofield
|
|
44
44
|
class ControllerInfo
|
45
45
|
|
46
46
|
attr_reader :parent, :controller_name, :controller_path, :model_name
|
47
|
-
attr_accessor :actions, :children
|
47
|
+
attr_accessor :actions, :children, :parents
|
48
48
|
|
49
49
|
def initialize route_info
|
50
50
|
@controller_name = route_info.controller_name
|
@@ -53,6 +53,7 @@ module Schofield
|
|
53
53
|
@parent = route_info.parent
|
54
54
|
@actions = []
|
55
55
|
@children = []
|
56
|
+
@parents = []
|
56
57
|
end
|
57
58
|
|
58
59
|
end
|
@@ -80,15 +81,29 @@ module Schofield
|
|
80
81
|
@controller_infos[info.parent].children << index unless info.parent.nil?
|
81
82
|
end
|
82
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
|
+
|
83
97
|
|
84
98
|
def generate
|
85
99
|
process_routes
|
86
100
|
set_children
|
101
|
+
set_parents
|
87
102
|
require 'rails_generator'
|
88
103
|
require 'rails_generator/scripts/generate'
|
89
104
|
@controller_infos.each do |index, info|
|
90
|
-
Rails::Generator::Scripts::Generate.new.run(['schofield_controller', info.controller_path, *info.actions ], :
|
91
|
-
Rails::Generator::Scripts::Generate.new.run(['schofield_form', info.model_name], :partial => true, :haml => true, :controller => info.controller_path, :
|
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)
|
92
107
|
end
|
93
108
|
end
|
94
109
|
|
data/schofield.gemspec
CHANGED