schofield 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.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 = has_parent? ? 'nested' : 'unnested'
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
- sco_parent_class = has_parent? ? options[:parent].classify : ''
26
- sco_parent_underscored = sco_parent_class.underscore.downcase
27
- sco_parent_underscored_plural = sco_parent_underscored.pluralize
28
- sco_parent_titleized_plural = sco_parent_underscored.titleize.pluralize
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
- :sco_parent_underscored => sco_parent_underscored,
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') || has_parent?
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('--parent PARENT', 'Specify the parent model') { |v| options[:parent] = v if v.present? }
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 has_parent?
135
- options[:parent].present?
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 [--parent PARENT]"
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
- before_filter :find_<%= sco_parent_underscored %>, :only => [<%= filter_actions %w( index new create) %>]
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 %> = @<%= sco_parent_underscored %>.<%= 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 %> = @<%= sco_parent_underscored %>.<%= sco_underscored_plural %>.build
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 %> = @<%= sco_parent_underscored %>.<%= sco_underscored_plural %>.build(params[:<%= 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_<%= sco_parent_underscored %>
87
- @<%= sco_parent_underscored %> = <%= sco_parent_class %>.find(params[:<%= sco_parent_underscored %>_id])
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
- add_breadcrumb '<%= sco_parent_titleized_plural %>', admin_<%= sco_parent_underscored_plural %>_path
96
- add_breadcrumb @<%= sco_parent_underscored %>
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_<%= sco_parent_underscored %>_<%= sco_underscored %>_path)
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('--parent PARENT_PATH',
8
- "Specify the parent for the specified model.") do |v|
9
- options[:parent] = v if v.present?
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
- array << "@#{options[:parent]}" unless options[:parent].nil?
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 ], :parent => info.parent, :children => info.children)
91
- Rails::Generator::Scripts::Generate.new.run(['schofield_form', info.model_name], :partial => true, :haml => true, :controller => info.controller_path, :parent => info.parent)
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
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{schofield}
8
- s.version = "0.1.1"
8
+ s.version = "0.1.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Marc Tauber"]
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 1
9
- version: 0.1.1
8
+ - 2
9
+ version: 0.1.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Marc Tauber