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
@@ -0,0 +1,124 @@
1
+ require 'rails/generators/base'
2
+
3
+ %w( levels level responses association routes attributes attribute ).each do |file|
4
+ require File.join(File.dirname(__FILE__), file)
5
+ end
6
+
7
+
8
+
9
+ module Schofield
10
+
11
+ module Generators
12
+
13
+ class Schofield < Rails::Generators::Base
14
+
15
+ source_root File.expand_path(File.join(File.dirname(__FILE__), '../', 'templates'))
16
+
17
+ class_option :tables_to_ignore, :aliases => '-i', :type => :string, :default => '', :desc => 'Indicates which tables to ignore'
18
+ class_option :ask_questions, :aliases => '-a', :type => :boolean, :desc => 'Indicates whether to ask questions again rather than use recorded answers'
19
+
20
+
21
+
22
+ def generate
23
+ Responses.generator = self
24
+ Responses.re_ask = options[:ask_questions]
25
+ Level.tables_to_ignore = tables_to_ignore
26
+ Levels.models = models
27
+ Responses.save
28
+ Association.reference_parents
29
+ Responses.save
30
+ Association.report
31
+ end
32
+
33
+ def generate_routes
34
+ inject_into_file(File.join(Rails.root, 'config', 'routes.rb'), Routes.generate, :before => /end\s*\Z/)
35
+ end
36
+
37
+ def generate_controllers
38
+ Levels.all.select(&:controllers?).each do |level|
39
+ source = level.join? ? 'join_controller.erb' : 'controller.erb'
40
+ destination = File.join('app/controllers/admin', "#{level.name.tableize}_controller.rb")
41
+ template_with_context(source, Proc.new{level}, destination)
42
+ end
43
+ end
44
+
45
+ def generate_views
46
+ Levels.all.select(&:views?).each do |level|
47
+ template_with_context('form.erb', Proc.new{level}, view_path(level, '_form.haml'))
48
+ create_file(view_path(level, 'new.haml'), %q(= render :partial => 'form', :layout => 'shared/admin/module_layout'))
49
+ template_with_context('show.erb', Proc.new{level}, view_path(level, 'show.haml'))
50
+ if !level.nested?
51
+ template_with_context('index.erb', Proc.new{level}, view_path(level, 'index.haml'))
52
+ end
53
+ end
54
+ end
55
+
56
+ def generate_models
57
+ Levels.all.select(&:models?).each do |level|
58
+ destination = File.join('app/models', "#{level.name}.rb")
59
+ template_with_context('model.erb', Proc.new{level}, destination)
60
+ end
61
+ end
62
+
63
+ def generate_tables
64
+ tables = []
65
+ Levels.all.select(&:tables?).each do |level|
66
+ columns_level = level.subclass? ? level.superclass : level
67
+ columns = columns_level.attributes.select{ |a| columns_level.form_field?(a) }.map(&:to_column)
68
+ columns.shift if level.join?
69
+ if level.nested?
70
+ tables << <<-STRING.gsub(/^ {12}/, '')
71
+ def #{level.name.pluralize}_table
72
+ [
73
+ #{columns.join(",\n ")}
74
+ ]
75
+ end
76
+ STRING
77
+ else
78
+ tables << <<-STRING.gsub(/^ {12}/, '')
79
+ def render_#{level.name.pluralize} collection
80
+ table_renderer(collection).render(
81
+ #{columns.join(",\n ")}
82
+ )
83
+ end
84
+ STRING
85
+ end
86
+ end
87
+ create_file 'app/helpers/admin/table_definitions_helper.rb' do
88
+ "module Admin::TableDefinitionsHelper\n\n#{tables.join("\n\n")}\nend"
89
+ end
90
+ end
91
+
92
+
93
+
94
+ private
95
+
96
+ def tables_to_ignore
97
+ @tables_to_ignore ||= %w( schema_migrations ) + options[:tables_to_ignore].split(',').map{ |e| e.strip.tableize }
98
+ end
99
+
100
+ def view_path level, file_name
101
+ File.join('app/views/admin', level.name.tableize, file_name)
102
+ end
103
+
104
+ def models
105
+ @models ||= tables.map { |t| t.camelize.singularize.constantize }
106
+ end
107
+
108
+ def tables
109
+ ActiveRecord::Base.connection.tables - tables_to_ignore
110
+ end
111
+
112
+ def template_with_context(source, context, destination=nil, config={}, &block)
113
+ destination ||= source
114
+ source = File.expand_path(find_in_source_paths(source.to_s))
115
+ create_file destination, nil, config do
116
+ content = ERB.new(::File.binread(source), nil, '-').result(context)
117
+ content = block.call(content) if block
118
+ content
119
+ end
120
+ end
121
+
122
+ end
123
+ end
124
+ end
@@ -0,0 +1,25 @@
1
+ class Admin::<%= level.name.tableize.camelize %>Controller < Admin::AdminController
2
+
3
+ include InheritResources
4
+ <%- if level.sortable? -%>
5
+ include SortAction
6
+
7
+ <%- end -%>
8
+
9
+ actions :new, :create, :update, :destroy, :show<%= ', :index' unless level.nested? %>
10
+
11
+ <%- if level.nests? -%>
12
+ drill_down_on_create
13
+
14
+ <%- end -%>
15
+ <%- if level.nested? -%>
16
+ <%= level.polymorphic? ? 'polymorphic_' : '' %>belongs_to :<%= level.parent_associations.select(&:nest?).map(&:parent_name).join(', :') %>, :optional => true
17
+
18
+ <%- end -%>
19
+ private
20
+
21
+ def titles
22
+ generate_titles :<%= level.ancestry.join(', :') %>
23
+ end
24
+
25
+ end
@@ -0,0 +1,31 @@
1
+ = better_error_messages_for
2
+
3
+ = semantic_form_for <%= level.nested? ? 'parent? ? [:admin, parent, resource] : ' : '' %>[:admin, resource]<%= level.multipart? ? ', :html => { :multipart => true }' : '' %> do |f|
4
+
5
+ = f.inputs do
6
+
7
+ <%- level.attributes.each do |attribute| -%>
8
+ <%- if level.form_field?(attribute) -%>
9
+ <%- if attribute.attachment? -%>
10
+ = f.input :<%= attribute.name.gsub(/_file_name$/, '') %>, :as => :image
11
+ <%- else -%>
12
+ = f.input :<%= attribute.actual_name %><%= attribute.text? ? %q(, :input_html => { :class => 'medium previewable' }) : '' %>
13
+ <%- end -%>
14
+ <%- end -%>
15
+ <%- end -%>
16
+ <%- level.has_ones.each do |child| -%>
17
+
18
+ = f.semantic_fields_for :<%= child.name %>, resource.<%= child.name %> || resource.build_<%= child.name %> do |<%= child.name %>|
19
+ <%- child.attributes.each do |attribute| -%>
20
+ <%- if child.form_field?(attribute) && attribute.actual_name != level.name -%>
21
+ <%- if attribute.attachment? -%>
22
+ = <%= child.name %>.input :<%= attribute.name.gsub(/_file_name$/, '') %>, :as => :image
23
+ <%- else -%>
24
+ = <%= child.name %>.input :<%= attribute.actual_name %><%= attribute.text? ? %q(, :input_html => { :class => 'medium previewable' }) : '' %>
25
+ <%- end -%>
26
+ <%- end -%>
27
+ <%- end -%>
28
+ = <%= child.name %>.input :nested, :as => :hidden
29
+ <%- end -%>
30
+
31
+ = f.buttons
@@ -0,0 +1,5 @@
1
+ .module
2
+ .add
3
+ %p= link_to('Add new <%= level.name.gsub('_', ' ') %>', new_admin_<%= level.name %>_path)
4
+
5
+ = render_<%= level.name.pluralize %> collection
@@ -0,0 +1,15 @@
1
+ class Admin::<%= level.name.tableize.camelize %>Controller < Admin::AdminController
2
+
3
+ def create
4
+ <%= level.name %> = <%= level.name.camelize %>.new(params[:<%= level.name %>])
5
+ <%= level.name %>.save
6
+ redirect_to_form_referer
7
+ end
8
+
9
+ def destroy
10
+ <%= level.name %> = <%= level.name.camelize %>.find(params[:id])
11
+ <%= level.name %>.destroy
12
+ redirect_to :back
13
+ end
14
+
15
+ end
@@ -0,0 +1,109 @@
1
+ class <%= level.name.camelize %> < <%= level.subclass? ? level.superclass.name.camelize : 'ActiveRecord::Base' %>
2
+
3
+ strip_attributes!
4
+ <%- if level.has_manies? -%>
5
+ prevent_destroy_if_has_children :<%= level.has_manies.map{ |l| l.name.pluralize }.join(', :') %>
6
+ <%- end -%>
7
+ <%- if level.sortable? -%>
8
+ acts_as_list<%=
9
+ if level.polymorphic?
10
+ " :scope => [:#{level.polymorphic_name}_type, :#{level.polymorphic_name}_id]"
11
+ elsif level.nested?
12
+ " :scope => :#{level.parent_associations.find(&:nest?).parent_name}"
13
+ end
14
+ %>
15
+ <%- end -%>
16
+ <%- if level.acts_as_markdowns? -%>
17
+ <%= level.acts_as_markdowns %>
18
+ <%- end -%>
19
+
20
+
21
+ # ATTRIBUTES
22
+ <%- if level.attr_protecteds? -%>
23
+
24
+ <%= level.attr_protecteds %>
25
+ <%- end -%>
26
+ <%- if level.belongs_to_one? -%>
27
+
28
+ attr_accessor :nested
29
+ <%- end -%>
30
+
31
+
32
+ # FILTERS
33
+
34
+
35
+ # ASSOCIATIONS
36
+ <%- if level.parent_associations.find(&:non_polymorphic?) -%>
37
+
38
+ <%- level.parent_associations.select(&:non_polymorphic?).each do |association| -%>
39
+ belongs_to :<%= association.parent_name %>
40
+ <%- end -%>
41
+ <%- end -%>
42
+ <%- if level.parent_associations.find(&:polymorphic?) -%>
43
+ <%- level.parent_associations.select(&:polymorphic?).each do |association| -%>
44
+ belongs_to :<%= association.parent_name %>, :polymorphic => true
45
+ <%- end -%>
46
+ <%- end -%>
47
+ <%- if level.child_associations.find(&:non_polymorphic_one_to_many?) -%>
48
+
49
+ <%- level.child_associations.select(&:non_polymorphic_one_to_many?).map(&:child).each do |child| -%>
50
+ has_many :<%= child.name.pluralize %>, :dependent => :destroy
51
+ <%- if child.join? -%>
52
+ has_many :<%= child.other_parent_name(level.name).pluralize %>, :through => :<%= child.name.pluralize %>
53
+ <%- end -%>
54
+ <%- end -%>
55
+ <%- end -%>
56
+ <%- if level.child_associations.find(&:non_polymorphic_one_to_one?) -%>
57
+
58
+ <%- level.child_associations.select(&:non_polymorphic_one_to_one?).each do |association| -%>
59
+ has_one :<%= association.child_name %>, :dependent => :destroy
60
+ <%- end -%>
61
+ <%- end -%>
62
+ <%- if level.child_associations.find(&:polymorphic_one_to_many?) -%>
63
+
64
+ <%- level.child_associations.select(&:polymorphic_one_to_many?).each do |association| -%>
65
+ has_many :<%= association.child_name.pluralize %>, :as => :<%= association.polymorphic_name %>, :dependent => :destroy
66
+ <%- end -%>
67
+ <%- end -%>
68
+ <%- if level.child_associations.find(&:polymorphic_one_to_one?) -%>
69
+
70
+ <%- level.child_associations.select(&:polymorphic_one_to_one?).each do |association| -%>
71
+ has_one :<%= association.child_name %>, :as => :<%= association.polymorphic_name %>, :dependent => :destroy
72
+ <%- end -%>
73
+ <%- end -%>
74
+
75
+
76
+ # NESTING
77
+ <%- if level.child_associations.find(&:one_to_one?) %>
78
+
79
+ <%- level.child_associations.select(&:one_to_one?).each do |association| -%>
80
+ accepts_nested_attributes_for :<%= association.child_name %>
81
+ <%- end -%>
82
+ <%- end -%>
83
+
84
+
85
+ # ATTACHMENTS
86
+ <%- if level.attachments? -%>
87
+ <%= level.attached_files %>
88
+ <%- end -%>
89
+
90
+
91
+ # VALIDATIONS
92
+ <%- if !level.subclass? && level.validations? -%>
93
+
94
+ <%= level.validations.join("\n ") %>
95
+ <%- end -%>
96
+
97
+
98
+ # SCOPES
99
+
100
+ scope :collection
101
+
102
+
103
+ # METHODS
104
+
105
+ def to_s
106
+ <%= level.to_s_string || (level.join? && level.nested? && level.parent_associations.find(&:nest?).parent_name) || '"##{id}"' %>
107
+ end
108
+
109
+ end
@@ -0,0 +1,21 @@
1
+ = render :partial => 'form', :layout => 'shared/admin/module_layout'
2
+ <%- level.nested_levels.each do |child| -%>
3
+
4
+ <%- if child.join? -%>
5
+ <%- parent_name = level.subclass? ? level.superclass.name : level.name -%>
6
+ <%- other_parent_name = child.other_parent_name(parent_name) -%>
7
+ .module
8
+ .tabletop
9
+ .description
10
+ %h3 <%= other_parent_name.titleize %>
11
+ .add
12
+ - @unassociated_<%= other_parent_name.pluralize %> = [Object]
13
+ - if @unassociated_<%= other_parent_name.pluralize %>.length > 0
14
+ - form_for [:admin, <%= child.name.camelize %>.new], :html => { :class => 'link_form' } do |f|
15
+ %p
16
+ = f.hidden_field :<%= parent_name %>_id, :value => resource.id
17
+ = f.select(:<%= other_parent_name %>_id, @unassociated_<%= other_parent_name.pluralize %>.collect { |c| [c.name, c.id] }, { :prompt => 'Link <%= other_parent_name.gsub('_', ' ') %>' }, {:onchange => "submit()"})
18
+ <%- else -%>
19
+ = render_collection :<%= child.name.pluralize %>
20
+ <%- end -%>
21
+ <%- end -%>
@@ -1,77 +1,79 @@
1
1
  # Generated by jeweler
2
2
  # DO NOT EDIT THIS FILE DIRECTLY
3
- # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
4
  # -*- encoding: utf-8 -*-
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{schofield}
8
- s.version = "0.1.5"
8
+ s.version = "0.2.0"
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"]
12
- s.date = %q{2010-05-27}
13
- s.description = %q{Create views and controllers from routes defined in admin namespace. Very, very basic and very specific to my needs.'}
12
+ s.date = %q{2010-12-14}
13
+ s.description = %q{longer description of your gem}
14
14
  s.email = %q{marc@marctauber.com}
15
15
  s.extra_rdoc_files = [
16
- "LICENSE",
17
- "README.rdoc"
16
+ "LICENSE.txt",
17
+ "README.rdoc"
18
18
  ]
19
19
  s.files = [
20
+ ".bundle/config",
20
21
  ".document",
21
- ".gitignore",
22
- "LICENSE",
23
- "README.rdoc",
24
- "Rakefile",
25
- "VERSION",
26
- "generators/schofield_controller/schofield_controller_generator.rb",
27
- "generators/schofield_controller/templates/controller_spec.rb",
28
- "generators/schofield_controller/templates/helper_spec.rb",
29
- "generators/schofield_controller/templates/index_partial.rb",
30
- "generators/schofield_controller/templates/nested/controller.rb",
31
- "generators/schofield_controller/templates/nested/edit.rb",
32
- "generators/schofield_controller/templates/nested/index.rb",
33
- "generators/schofield_controller/templates/nested/new.rb",
34
- "generators/schofield_controller/templates/nested/show.rb",
35
- "generators/schofield_controller/templates/unnested/controller.rb",
36
- "generators/schofield_controller/templates/unnested/edit.rb",
37
- "generators/schofield_controller/templates/unnested/index.rb",
38
- "generators/schofield_controller/templates/unnested/new.rb",
39
- "generators/schofield_controller/templates/unnested/show.rb",
40
- "generators/schofield_controller/templates/view_spec.rb",
41
- "generators/schofield_form/schofield_form_generator.rb",
42
- "generators/schofield_form/templates/view__form.html.haml",
43
- "lib/schofield.rb",
44
- "lib/schofield/tasks.rb",
45
- "lib/schofield/tasks/schofield.rake",
46
- "schofield.gemspec",
47
- "spec/schofield_spec.rb",
48
- "spec/spec.opts",
49
- "spec/spec_helper.rb"
22
+ "Gemfile",
23
+ "Gemfile.lock",
24
+ "LICENSE.txt",
25
+ "README.rdoc",
26
+ "Rakefile",
27
+ "VERSION",
28
+ "lib/generators/schofield/association.rb",
29
+ "lib/generators/schofield/attribute.rb",
30
+ "lib/generators/schofield/attributes.rb",
31
+ "lib/generators/schofield/level.rb",
32
+ "lib/generators/schofield/levels.rb",
33
+ "lib/generators/schofield/responses.rb",
34
+ "lib/generators/schofield/routes.rb",
35
+ "lib/generators/schofield/schofield_generator.rb",
36
+ "lib/generators/templates/controller.erb",
37
+ "lib/generators/templates/form.erb",
38
+ "lib/generators/templates/index.erb",
39
+ "lib/generators/templates/join_controller.erb",
40
+ "lib/generators/templates/model.erb",
41
+ "lib/generators/templates/show.erb",
42
+ "schofield.gemspec",
43
+ "spec/schofield_spec.rb",
44
+ "spec/spec.opts",
45
+ "spec/spec_helper.rb"
50
46
  ]
51
47
  s.homepage = %q{http://github.com/sauberia/schofield}
52
- s.rdoc_options = ["--charset=UTF-8"]
48
+ s.licenses = ["MIT"]
53
49
  s.require_paths = ["lib"]
54
- s.rubygems_version = %q{1.3.6}
55
- s.summary = %q{Creates views and controller from defined routes and respective models}
50
+ s.rubygems_version = %q{1.3.7}
51
+ s.summary = %q{one-line summary of your gem}
56
52
  s.test_files = [
57
53
  "spec/schofield_spec.rb",
58
- "spec/spec_helper.rb"
54
+ "spec/spec_helper.rb"
59
55
  ]
60
56
 
61
57
  if s.respond_to? :specification_version then
62
58
  current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
63
59
  s.specification_version = 3
64
60
 
65
- if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
66
- s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
67
- s.add_development_dependency(%q<formtastic>, [">= 0.9.8"])
61
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
62
+ s.add_development_dependency(%q<rspec>, ["~> 2.1.0"])
63
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
64
+ s.add_development_dependency(%q<jeweler>, ["~> 1.5.1"])
65
+ s.add_development_dependency(%q<rcov>, [">= 0"])
68
66
  else
69
- s.add_dependency(%q<rspec>, [">= 1.2.9"])
70
- s.add_dependency(%q<formtastic>, [">= 0.9.8"])
67
+ s.add_dependency(%q<rspec>, ["~> 2.1.0"])
68
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
69
+ s.add_dependency(%q<jeweler>, ["~> 1.5.1"])
70
+ s.add_dependency(%q<rcov>, [">= 0"])
71
71
  end
72
72
  else
73
- s.add_dependency(%q<rspec>, [">= 1.2.9"])
74
- s.add_dependency(%q<formtastic>, [">= 0.9.8"])
73
+ s.add_dependency(%q<rspec>, ["~> 2.1.0"])
74
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
75
+ s.add_dependency(%q<jeweler>, ["~> 1.5.1"])
76
+ s.add_dependency(%q<rcov>, [">= 0"])
75
77
  end
76
78
  end
77
79