From 3a68aec1a1c6329bfcbf0f87bdc2fbd75cb98feb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sun, 17 Apr 2011 11:44:52 +0200 Subject: Make generators more modular, add hooks and improve test suite. --- railties/lib/rails/generators.rb | 9 +++- .../generators/rails/assets/assets_generator.rb | 48 +++++++++--------- .../rails/scaffold/scaffold_generator.rb | 21 +++++++- .../rails/scaffold/templates/scaffold.css | 56 +++++++++++++++++++++ .../rails/scaffold/templates/scaffold.css.scss | 58 ++++++++++++++++++++++ .../lib/rails/generators/rails/stylesheets/USAGE | 5 -- .../rails/stylesheets/stylesheets_generator.rb | 23 --------- .../rails/stylesheets/templates/scaffold.css | 56 --------------------- .../rails/stylesheets/templates/scaffold.css.scss | 58 ---------------------- 9 files changed, 166 insertions(+), 168 deletions(-) create mode 100644 railties/lib/rails/generators/rails/scaffold/templates/scaffold.css create mode 100644 railties/lib/rails/generators/rails/scaffold/templates/scaffold.css.scss delete mode 100644 railties/lib/rails/generators/rails/stylesheets/USAGE delete mode 100644 railties/lib/rails/generators/rails/stylesheets/stylesheets_generator.rb delete mode 100644 railties/lib/rails/generators/rails/stylesheets/templates/scaffold.css delete mode 100644 railties/lib/rails/generators/rails/stylesheets/templates/scaffold.css.scss (limited to 'railties/lib') diff --git a/railties/lib/rails/generators.rb b/railties/lib/rails/generators.rb index be61b087f9..82171d0832 100644 --- a/railties/lib/rails/generators.rb +++ b/railties/lib/rails/generators.rb @@ -24,9 +24,11 @@ module Rails :rails => { :actions => '-a', :orm => '-o', + :javascript_engine => '-je', :resource_controller => '-c', :scaffold_controller => '-c', :stylesheets => '-y', + :stylesheet_engine => '-se', :template_engine => '-e', :test_framework => '-t' }, @@ -43,15 +45,18 @@ module Rails DEFAULT_OPTIONS = { :rails => { + :assets => true, :force_plural => false, :helper => true, - :assets => true, - :orm => nil, :integration_tool => nil, + :javascripts => true, + :javascript_engine => nil, + :orm => nil, :performance_tool => nil, :resource_controller => :controller, :scaffold_controller => :scaffold_controller, :stylesheets => true, + :stylesheet_engine => nil, :test_framework => nil, :template_engine => :erb }, diff --git a/railties/lib/rails/generators/rails/assets/assets_generator.rb b/railties/lib/rails/generators/rails/assets/assets_generator.rb index 59239d1a03..80beb7abfe 100644 --- a/railties/lib/rails/generators/rails/assets/assets_generator.rb +++ b/railties/lib/rails/generators/rails/assets/assets_generator.rb @@ -1,36 +1,38 @@ module Rails module Generators - # TODO: Add hooks for using other asset pipelines, like Less class AssetsGenerator < NamedBase - def create_asset_files + class_option :javascripts, :type => :boolean, :desc => "Generate javascripts" + class_option :stylesheets, :type => :boolean, :desc => "Generate stylesheets" + + class_option :javascript_engine, :desc => "Engine for javascripts" + class_option :stylesheet_engine, :desc => "Engine for stylesheets" + + def create_javascript_files + return unless options.javascripts? copy_file "javascript.#{javascript_extension}", - File.join('app/assets/javascripts', "#{file_name}.#{javascript_extension}") + File.join('app/assets/javascripts', class_path, "#{asset_name}.#{javascript_extension}") + end + def create_stylesheet_files + return unless options.stylesheets? copy_file "stylesheet.#{stylesheet_extension}", - File.join('app/assets/stylesheets', "#{file_name}.#{stylesheet_extension}") + File.join('app/assets/stylesheets', class_path, "#{asset_name}.#{stylesheet_extension}") end - - private - def javascript_extension - using_coffee? ? "js.coffee" : "js" + + protected + + def asset_name + file_name end - - def using_coffee? - require 'coffee-script' - defined?(CoffeeScript) - rescue LoadError - false + + def javascript_extension + options.javascript_engine.present? ? + "js.#{options.javascript_engine}" : "js" end - + def stylesheet_extension - using_sass? ? "css.scss" : "css" - end - - def using_sass? - require 'sass' - defined?(Sass) - rescue LoadError - false + options.stylesheet_engine.present? ? + "css.#{options.stylesheet_engine}" : "css" end end end diff --git a/railties/lib/rails/generators/rails/scaffold/scaffold_generator.rb b/railties/lib/rails/generators/rails/scaffold/scaffold_generator.rb index 779f933785..6eef0dbe5b 100644 --- a/railties/lib/rails/generators/rails/scaffold/scaffold_generator.rb +++ b/railties/lib/rails/generators/rails/scaffold/scaffold_generator.rb @@ -6,8 +6,27 @@ module Rails remove_hook_for :resource_controller remove_class_option :actions + class_option :stylesheets, :type => :boolean, :desc => "Generate stylesheets" + class_option :stylesheet_engine, :desc => "Engine for stylesheets" + hook_for :scaffold_controller, :required => true - hook_for :stylesheets + + def copy_stylesheets_file + if behavior == :invoke && options.stylesheets? + template "scaffold.#{stylesheet_extension}", "app/assets/stylesheets/scaffold.#{stylesheet_extension}" + end + end + + hook_for :assets do |assets| + invoke assets, [controller_name] + end + + private + + def stylesheet_extension + options.stylesheet_engine.present? ? + "css.#{options.stylesheet_engine}" : "css" + end end end end diff --git a/railties/lib/rails/generators/rails/scaffold/templates/scaffold.css b/railties/lib/rails/generators/rails/scaffold/templates/scaffold.css new file mode 100644 index 0000000000..1ae7000299 --- /dev/null +++ b/railties/lib/rails/generators/rails/scaffold/templates/scaffold.css @@ -0,0 +1,56 @@ +body { background-color: #fff; color: #333; } + +body, p, ol, ul, td { + font-family: verdana, arial, helvetica, sans-serif; + font-size: 13px; + line-height: 18px; +} + +pre { + background-color: #eee; + padding: 10px; + font-size: 11px; +} + +a { color: #000; } +a:visited { color: #666; } +a:hover { color: #fff; background-color:#000; } + +div.field, div.actions { + margin-bottom: 10px; +} + +#notice { + color: green; +} + +.field_with_errors { + padding: 2px; + background-color: red; + display: table; +} + +#error_explanation { + width: 450px; + border: 2px solid red; + padding: 7px; + padding-bottom: 0; + margin-bottom: 20px; + background-color: #f0f0f0; +} + +#error_explanation h2 { + text-align: left; + font-weight: bold; + padding: 5px 5px 5px 15px; + font-size: 12px; + margin: -7px; + margin-bottom: 0px; + background-color: #c00; + color: #fff; +} + +#error_explanation ul li { + font-size: 12px; + list-style: square; +} diff --git a/railties/lib/rails/generators/rails/scaffold/templates/scaffold.css.scss b/railties/lib/rails/generators/rails/scaffold/templates/scaffold.css.scss new file mode 100644 index 0000000000..45116b53f6 --- /dev/null +++ b/railties/lib/rails/generators/rails/scaffold/templates/scaffold.css.scss @@ -0,0 +1,58 @@ +body { background-color: #fff; color: #333; } + +body, p, ol, ul, td { + font-family: verdana, arial, helvetica, sans-serif; + font-size: 13px; + line-height: 18px; +} + +pre { + background-color: #eee; + padding: 10px; + font-size: 11px; +} + +a { + color: #000; + &:visited { color: #666; } + &:hover { color: #fff; background-color:#000; } +} + +div.field, div.actions { + margin-bottom: 10px; +} + +#notice { + color: green; +} + +.field_with_errors { + padding: 2px; + background-color: red; + display: table; +} + +#error_explanation { + width: 450px; + border: 2px solid red; + padding: 7px; + padding-bottom: 0; + margin-bottom: 20px; + background-color: #f0f0f0; + + h2 { + text-align: left; + font-weight: bold; + padding: 5px 5px 5px 15px; + font-size: 12px; + margin: -7px; + margin-bottom: 0px; + background-color: #c00; + color: #fff; + } + + ul li { + font-size: 12px; + list-style: square; + } +} \ No newline at end of file diff --git a/railties/lib/rails/generators/rails/stylesheets/USAGE b/railties/lib/rails/generators/rails/stylesheets/USAGE deleted file mode 100644 index 59e5495d0b..0000000000 --- a/railties/lib/rails/generators/rails/stylesheets/USAGE +++ /dev/null @@ -1,5 +0,0 @@ -Description: - Copies scaffold stylesheets to public/stylesheets/. - -Examples: - `rails generate stylesheets` diff --git a/railties/lib/rails/generators/rails/stylesheets/stylesheets_generator.rb b/railties/lib/rails/generators/rails/stylesheets/stylesheets_generator.rb deleted file mode 100644 index d06db25292..0000000000 --- a/railties/lib/rails/generators/rails/stylesheets/stylesheets_generator.rb +++ /dev/null @@ -1,23 +0,0 @@ -module Rails - module Generators - class StylesheetsGenerator < Base - def copy_stylesheets_file - if behavior == :invoke - template "scaffold.#{stylesheet_extension}", "app/assets/stylesheets/scaffold.#{stylesheet_extension}" - end - end - - private - def stylesheet_extension - using_sass? ? "css.scss" : "css" - end - - def using_sass? - require 'sass' - defined?(Sass) - rescue LoadError - false - end - end - end -end diff --git a/railties/lib/rails/generators/rails/stylesheets/templates/scaffold.css b/railties/lib/rails/generators/rails/stylesheets/templates/scaffold.css deleted file mode 100644 index 1ae7000299..0000000000 --- a/railties/lib/rails/generators/rails/stylesheets/templates/scaffold.css +++ /dev/null @@ -1,56 +0,0 @@ -body { background-color: #fff; color: #333; } - -body, p, ol, ul, td { - font-family: verdana, arial, helvetica, sans-serif; - font-size: 13px; - line-height: 18px; -} - -pre { - background-color: #eee; - padding: 10px; - font-size: 11px; -} - -a { color: #000; } -a:visited { color: #666; } -a:hover { color: #fff; background-color:#000; } - -div.field, div.actions { - margin-bottom: 10px; -} - -#notice { - color: green; -} - -.field_with_errors { - padding: 2px; - background-color: red; - display: table; -} - -#error_explanation { - width: 450px; - border: 2px solid red; - padding: 7px; - padding-bottom: 0; - margin-bottom: 20px; - background-color: #f0f0f0; -} - -#error_explanation h2 { - text-align: left; - font-weight: bold; - padding: 5px 5px 5px 15px; - font-size: 12px; - margin: -7px; - margin-bottom: 0px; - background-color: #c00; - color: #fff; -} - -#error_explanation ul li { - font-size: 12px; - list-style: square; -} diff --git a/railties/lib/rails/generators/rails/stylesheets/templates/scaffold.css.scss b/railties/lib/rails/generators/rails/stylesheets/templates/scaffold.css.scss deleted file mode 100644 index 45116b53f6..0000000000 --- a/railties/lib/rails/generators/rails/stylesheets/templates/scaffold.css.scss +++ /dev/null @@ -1,58 +0,0 @@ -body { background-color: #fff; color: #333; } - -body, p, ol, ul, td { - font-family: verdana, arial, helvetica, sans-serif; - font-size: 13px; - line-height: 18px; -} - -pre { - background-color: #eee; - padding: 10px; - font-size: 11px; -} - -a { - color: #000; - &:visited { color: #666; } - &:hover { color: #fff; background-color:#000; } -} - -div.field, div.actions { - margin-bottom: 10px; -} - -#notice { - color: green; -} - -.field_with_errors { - padding: 2px; - background-color: red; - display: table; -} - -#error_explanation { - width: 450px; - border: 2px solid red; - padding: 7px; - padding-bottom: 0; - margin-bottom: 20px; - background-color: #f0f0f0; - - h2 { - text-align: left; - font-weight: bold; - padding: 5px 5px 5px 15px; - font-size: 12px; - margin: -7px; - margin-bottom: 0px; - background-color: #c00; - color: #fff; - } - - ul li { - font-size: 12px; - list-style: square; - } -} \ No newline at end of file -- cgit v1.2.3