From b80bd744ad873f6fc43018bc4bfb90677de167bd Mon Sep 17 00:00:00 2001 From: Stefan Wintermeyer Date: Mon, 17 Dec 2012 12:01:45 +0100 Subject: Start of GS5. --- lib/generators/nifty/layout/USAGE | 25 +++++++ lib/generators/nifty/layout/layout_generator.rb | 29 ++++++++ .../layout/templates/error_messages_helper.rb | 23 ++++++ .../nifty/layout/templates/layout.html.haml | 21 ++++++ .../nifty/layout/templates/layout_helper.rb | 38 ++++++++++ .../nifty/layout/templates/stylesheet.css | 83 ++++++++++++++++++++++ .../nifty/layout/templates/stylesheet.sass | 73 +++++++++++++++++++ 7 files changed, 292 insertions(+) create mode 100644 lib/generators/nifty/layout/USAGE create mode 100644 lib/generators/nifty/layout/layout_generator.rb create mode 100644 lib/generators/nifty/layout/templates/error_messages_helper.rb create mode 100644 lib/generators/nifty/layout/templates/layout.html.haml create mode 100644 lib/generators/nifty/layout/templates/layout_helper.rb create mode 100644 lib/generators/nifty/layout/templates/stylesheet.css create mode 100644 lib/generators/nifty/layout/templates/stylesheet.sass (limited to 'lib/generators/nifty/layout') diff --git a/lib/generators/nifty/layout/USAGE b/lib/generators/nifty/layout/USAGE new file mode 100644 index 0000000..f94af0d --- /dev/null +++ b/lib/generators/nifty/layout/USAGE @@ -0,0 +1,25 @@ +Description: + The nifty_layout generator creates a basic layout, stylesheet and + helper which will give some structure to a starting Rails app. + + The generator takes one argument which will be the name of the + layout and stylesheet files. If no argument is passed then it defaults + to "application". + + The helper module includes some methods which can be called in any + template or partial to set variables to be used in the layout, such as + page title and javascript/stylesheet includes. + +Examples: + rails generate nifty:layout + + Layout: app/views/layouts/application.html.erb + Stylesheet: public/stylesheets/application.css + Helper: app/helpers/layout_helper.rb + + + rails generate nifty:layout admin + + Layout: app/views/layouts/admin.html.erb + Stylesheet: public/stylesheets/admin.css + Helper: app/helpers/layout_helper.rb diff --git a/lib/generators/nifty/layout/layout_generator.rb b/lib/generators/nifty/layout/layout_generator.rb new file mode 100644 index 0000000..fb7f9f4 --- /dev/null +++ b/lib/generators/nifty/layout/layout_generator.rb @@ -0,0 +1,29 @@ +require 'generators/nifty' + +module Nifty + module Generators + class LayoutGenerator < Base + argument :layout_name, :type => :string, :default => 'application', :banner => 'layout_name' + + class_option :haml, :desc => 'Generate HAML for view, and SASS for stylesheet.', :type => :boolean, :default => true + + def create_layout + if options.haml? + template 'layout.html.haml', "app/views/layouts/#{file_name}.html.haml" + copy_file 'stylesheet.sass', "public/stylesheets/sass/#{file_name}.sass" + else + template 'layout.html.erb', "app/views/layouts/#{file_name}.html.erb" + copy_file 'stylesheet.css', "public/stylesheets/#{file_name}.css" + end + copy_file 'layout_helper.rb', 'app/helpers/layout_helper.rb' + copy_file 'error_messages_helper.rb', 'app/helpers/error_messages_helper.rb' + end + + private + + def file_name + layout_name.underscore + end + end + end +end diff --git a/lib/generators/nifty/layout/templates/error_messages_helper.rb b/lib/generators/nifty/layout/templates/error_messages_helper.rb new file mode 100644 index 0000000..8e9c4d3 --- /dev/null +++ b/lib/generators/nifty/layout/templates/error_messages_helper.rb @@ -0,0 +1,23 @@ +module ErrorMessagesHelper + # Render error messages for the given objects. The :message and :header_message options are allowed. + def error_messages_for(*objects) + options = objects.extract_options! + options[:header_message] ||= I18n.t(:"activerecord.errors.header", :default => "Invalid Fields") + options[:message] ||= I18n.t(:"activerecord.errors.message", :default => "Correct the following errors and try again.") + messages = objects.compact.map { |o| o.errors.full_messages }.flatten + unless messages.empty? + content_tag(:div, :class => "error_messages") do + list_items = messages.map { |msg| content_tag(:li, msg.html_safe) } + content_tag(:h2, options[:header_message].html_safe) + content_tag(:p, options[:message].html_safe) + content_tag(:ul, list_items.join.html_safe) + end + end + end + + module FormBuilderAdditions + def error_messages(options = {}) + @template.error_messages_for(@object, options) + end + end +end + +ActionView::Helpers::FormBuilder.send(:include, ErrorMessagesHelper::FormBuilderAdditions) diff --git a/lib/generators/nifty/layout/templates/layout.html.haml b/lib/generators/nifty/layout/templates/layout.html.haml new file mode 100644 index 0000000..24ec1d6 --- /dev/null +++ b/lib/generators/nifty/layout/templates/layout.html.haml @@ -0,0 +1,21 @@ +!!! +%html + + %head + %title + = content_for?(:title) ? yield(:title) : "Untitled" + %meta{"http-equiv"=>"Content-Type", :content=>"text/html; charset=utf-8"}/ + = stylesheet_link_tag "application" + = javascript_include_tag "application" + = csrf_meta_tag + = yield(:head) + + %body + #container + - flash.each do |name, msg| + = content_tag :div, msg, :id => "flash_#{name}" + + - if show_title? + %h1= yield(:title) + + = yield diff --git a/lib/generators/nifty/layout/templates/layout_helper.rb b/lib/generators/nifty/layout/templates/layout_helper.rb new file mode 100644 index 0000000..93011d6 --- /dev/null +++ b/lib/generators/nifty/layout/templates/layout_helper.rb @@ -0,0 +1,38 @@ +# These helper methods can be called in your template to set variables to be used in the layout +# This module should be included in all views globally, +# to do so you may need to add this line to your ApplicationController +# helper :layout +module LayoutHelper + + def title(page_title, show_title = true) + content_for(:title) { strip_tags(page_title.to_s) } + @show_title = show_title + end + + def show_title? + @show_title + end + + def stylesheet(*args) + content_for(:head) { stylesheet_link_tag(*args) } + end + + def javascript(*args) + content_for(:head) { javascript_include_tag(*args) } + end + + def translation_missing?(output) + (output =~ /span/ or output.empty?) + end + + def conditional_hint(translation_key) + output = t(translation_key) + return output unless translation_missing?(output) + false + end + + def conditional_t(translation_key) + output = t(translation_key) + strip_tags(output) + end +end \ No newline at end of file diff --git a/lib/generators/nifty/layout/templates/stylesheet.css b/lib/generators/nifty/layout/templates/stylesheet.css new file mode 100644 index 0000000..448a53f --- /dev/null +++ b/lib/generators/nifty/layout/templates/stylesheet.css @@ -0,0 +1,83 @@ +html, body { + background-color: #4B7399; + font-family: Verdana, Helvetica, Arial; + font-size: 14px; +} + +a img { + border: none; +} + +a { + color: #0000FF; +} + +.clear { + clear: both; + height: 0; + overflow: hidden; +} + +#container { + width: 75%; + margin: 0 auto; + background-color: #FFF; + padding: 20px 40px; + border: solid 1px black; + margin-top: 20px; +} + +#flash_notice, #flash_error, #flash_alert { + padding: 5px 8px; + margin: 10px 0; +} + +#flash_notice { + background-color: #CFC; + border: solid 1px #6C6; +} + +#flash_error, #flash_alert { + background-color: #FCC; + border: solid 1px #C66; +} + +.error_messages { + width: 400px; + border: 2px solid #CF0000; + padding: 0px; + padding-bottom: 12px; + margin-bottom: 20px; + background-color: #f0f0f0; + font-size: 12px; +} + +.error_messages h2 { + text-align: left; + font-weight: bold; + padding: 5px 10px; + font-size: 12px; + margin: 0; + background-color: #c00; + color: #fff; +} + +.error_messages p { + margin: 8px 10px; +} + +.error_messages ul { + margin: 0; +} + +.field_with_errors { + display: inline; +} + +form .field, form .actions { + margin: 10px 0; +} + +form label { + display: block; +} diff --git a/lib/generators/nifty/layout/templates/stylesheet.sass b/lib/generators/nifty/layout/templates/stylesheet.sass new file mode 100644 index 0000000..383bfd3 --- /dev/null +++ b/lib/generators/nifty/layout/templates/stylesheet.sass @@ -0,0 +1,73 @@ +$primary_color: #4b7399 + +body + background-color: $primary_color + font: + family: Verdana, Helvetica, Arial + size: 14px + +a + color: blue + img + border: none + +.clear + clear: both + height: 0 + overflow: hidden + +#container + width: 75% + margin: 0 auto + background: white + padding: 20px 40px + border: solid 1px black + margin-top: 20px + +#flash_notice, +#flash_error, +#flash_alert + padding: 5px 8px + margin: 10px 0 + +#flash_notice + background-color: #ccffcc + border: solid 1px #66cc66 + +#flash_error, +#flash_alert + background-color: #ffcccc + border: solid 1px #cc6666 + +.error_messages + width: 400px + border: 2px solid #cf0000 + padding: 0 + padding-bottom: 12px + margin-bottom: 20px + background-color: #f0f0f0 + font: + size: 12px + h2 + text-align: left + padding: 5px 5px 5px 15px + margin: 0 + font: + weight: bold + size: 12px + background-color: #cc0000 + color: white + p + margin: 8px 10px + ul + margin: 0 + +.field_with_errors + display: inline + +form .field, +form .actions + margin: 10px 0 + +form label + display: block -- cgit v1.2.3