wizardly 0.1.8.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. data/CHANGELOG.rdoc +33 -0
  2. data/LICENSE +20 -0
  3. data/README.rdoc +645 -0
  4. data/init.rb +1 -0
  5. data/lib/jeffp-wizardly.rb +1 -0
  6. data/lib/validation_group.rb +147 -0
  7. data/lib/wizardly.rb +31 -0
  8. data/lib/wizardly/action_controller.rb +36 -0
  9. data/lib/wizardly/wizard.rb +16 -0
  10. data/lib/wizardly/wizard/button.rb +35 -0
  11. data/lib/wizardly/wizard/configuration.rb +194 -0
  12. data/lib/wizardly/wizard/configuration/methods.rb +422 -0
  13. data/lib/wizardly/wizard/dsl.rb +27 -0
  14. data/lib/wizardly/wizard/page.rb +62 -0
  15. data/lib/wizardly/wizard/text_helpers.rb +16 -0
  16. data/lib/wizardly/wizard/utils.rb +11 -0
  17. data/rails_generators/wizardly_app/USAGE +6 -0
  18. data/rails_generators/wizardly_app/templates/wizardly.rake +37 -0
  19. data/rails_generators/wizardly_app/wizardly_app_generator.rb +41 -0
  20. data/rails_generators/wizardly_controller/USAGE +3 -0
  21. data/rails_generators/wizardly_controller/templates/controller.rb.erb +34 -0
  22. data/rails_generators/wizardly_controller/templates/helper.rb.erb +14 -0
  23. data/rails_generators/wizardly_controller/wizardly_controller_generator.rb +57 -0
  24. data/rails_generators/wizardly_scaffold/USAGE +4 -0
  25. data/rails_generators/wizardly_scaffold/templates/form.html.erb +23 -0
  26. data/rails_generators/wizardly_scaffold/templates/form.html.haml.erb +22 -0
  27. data/rails_generators/wizardly_scaffold/templates/helper.rb.erb +30 -0
  28. data/rails_generators/wizardly_scaffold/templates/images/back.png +0 -0
  29. data/rails_generators/wizardly_scaffold/templates/images/cancel.png +0 -0
  30. data/rails_generators/wizardly_scaffold/templates/images/finish.png +0 -0
  31. data/rails_generators/wizardly_scaffold/templates/images/next.png +0 -0
  32. data/rails_generators/wizardly_scaffold/templates/images/skip.png +0 -0
  33. data/rails_generators/wizardly_scaffold/templates/layout.html.erb +15 -0
  34. data/rails_generators/wizardly_scaffold/templates/layout.html.haml.erb +10 -0
  35. data/rails_generators/wizardly_scaffold/templates/style.css +54 -0
  36. data/rails_generators/wizardly_scaffold/wizardly_scaffold_generator.rb +109 -0
  37. metadata +90 -0
@@ -0,0 +1,16 @@
1
+ module Wizardly
2
+ module Wizard
3
+ module TextHelpers
4
+ private
5
+ def underscore_button_name(name)
6
+ name.to_s.strip.squeeze(' ').gsub(/ /, '_').downcase
7
+ end
8
+ def button_name_to_symbol(str)
9
+ underscore_button_name(str).to_sym
10
+ end
11
+ def symbol_to_button_name(sym)
12
+ sym.to_s.gsub(/_/, ' ').squeeze(' ').titleize
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,11 @@
1
+ module Wizardly
2
+ module Wizard
3
+ module Utils
4
+ def self.formatted_redirect(r)
5
+ return(nil) unless r
6
+ r.is_a?(Hash)? r : "'#{r}'"
7
+ end
8
+ end
9
+ end
10
+ end
11
+
@@ -0,0 +1,6 @@
1
+ Prepares a rails application to use wizardly gem
2
+
3
+ Moves the wizardly.rake file in to lib/tasks directory
4
+
5
+
6
+
@@ -0,0 +1,37 @@
1
+
2
+ namespace :wizardly do
3
+ desc "Display wizard configuration details"
4
+ task :config => :environment do
5
+ name = ENV['name']
6
+ return print_usage unless name
7
+ begin
8
+ name = name.strip.camelize
9
+ name += 'Controller' unless name.match('Controller$')
10
+ controller = name.constantize
11
+ begin
12
+ c = controller.wizard_config
13
+ begin
14
+ puts
15
+ puts c.print_config
16
+ rescue Exception=>e
17
+ puts "Problem printing configuration."
18
+ puts "#{e.class.name} -- #{e.message}"
19
+ puts
20
+ end
21
+ rescue Exception=>e
22
+ puts "{#{name}} is not a 'wizardly' controller.\nMake sure 'wizard_for_model' is defined in the controller class."
23
+ puts "#{e.class.name} -- #{e.message}"
24
+ puts
25
+ end
26
+ rescue Exception=>e
27
+ puts "{#{name}} does not reference a controller class."
28
+ puts "#{e.class.name} -- #{e.message}"
29
+ puts
30
+ end
31
+ end
32
+
33
+ def print_usage
34
+ puts "Usage: rake wizardly:config name={controller_name}"
35
+ puts
36
+ end
37
+ end
@@ -0,0 +1,41 @@
1
+ #require 'wizardly'
2
+
3
+ class WizardlyAppGenerator < Rails::Generator::Base
4
+
5
+ def initialize(runtime_args, runtime_options = {})
6
+ super
7
+ end
8
+
9
+ def manifest
10
+ record do |m|
11
+ m.directory "lib/tasks"
12
+
13
+ m.file "wizardly.rake", "lib/tasks/wizardly.rake"
14
+
15
+ end
16
+ end
17
+
18
+ def controller_class_name
19
+ "#{controller_name.camelize}Controller"
20
+ end
21
+ def model_class_name
22
+ "#{model_name.camelize}"
23
+ end
24
+ def action_methods
25
+ @wizard_config.print_page_action_methods
26
+ end
27
+ def callback_methods
28
+ @wizard_config.print_callbacks
29
+ end
30
+ def helper_methods
31
+ @wizard_config.print_helpers
32
+ end
33
+
34
+ protected
35
+ # Override with your own usage banner.
36
+ def banner
37
+ "Usage: #{$0} wizardly_app"
38
+ end
39
+
40
+
41
+ end
@@ -0,0 +1,3 @@
1
+ Generates a wizard controller with all the code displayed
2
+
3
+
@@ -0,0 +1,34 @@
1
+ #
2
+ # <%= controller_class_name %> class generated by wizardly_controller
3
+ #
4
+
5
+ class <%= controller_class_name %> < ApplicationController
6
+ before_filter :guard_entry
7
+
8
+ <%= action_methods %>
9
+
10
+ <%= callback_methods %>
11
+
12
+ <%= helper_methods %>
13
+
14
+ <%= callback_macro_methods %>
15
+
16
+ public
17
+ def wizard_config; self.class.wizard_config; end
18
+ hide_action :wizard_config
19
+
20
+ private
21
+
22
+ def self.wizard_config; @wizard_config; end
23
+ @wizard_config = Wizardly::Wizard::Configuration.create(:<%=controller_name %>, :<%=model_name %>, :allow_skip=>true) do
24
+ <%= "when_completed_redirect_to #{Wizardly::Wizard::Utils.formatted_redirect(completed_redirect)}" if completed_redirect %>
25
+ <%= "when_canceled_redirect_to #{Wizardly::Wizard::Utils.formatted_redirect(canceled_redirect)}" if canceled_redirect %>
26
+
27
+ # other things you can configure
28
+ # change_button(:next).to('Next One')
29
+ # change_button(:back).to('Previous')
30
+ # create_button('Help')
31
+ # set_page(:init).buttons_to :next_one, :previous, :cancel, :help #this removes skip
32
+ end
33
+
34
+ end
@@ -0,0 +1,14 @@
1
+ module <%=controller_name.camelize %>Helper
2
+
3
+ def wizardly_submit
4
+ @@wizardly_submit ||= {}
5
+ unless @@wizardly_submit[@step]
6
+ buttons = @wizard.pages[@step].buttons
7
+ @@wizardly_submit[@step] = buttons.inject(StringIO.new) do |io, button|
8
+ io << submit_tag(button.name)
9
+ end.string
10
+ end
11
+ @@wizardly_submit[@step]
12
+ end
13
+
14
+ end
@@ -0,0 +1,57 @@
1
+ require 'wizardly'
2
+
3
+ class WizardlyControllerGenerator < Rails::Generator::Base
4
+ attr_reader :controller_name, :model_name, :completed_redirect, :canceled_redirect
5
+
6
+ def initialize(runtime_args, runtime_options = {})
7
+ super
8
+ @controller_name = @args[0].sub(/^:/,'')
9
+ @model_name = @args[1].sub(/^:/, '').underscore
10
+ @completed_redirect = @args[2]
11
+ @canceled_redirect = @args[3]
12
+ opts = {}
13
+ opts[:completed] = @completed_redirect if @completed_redirect
14
+ opts[:canceled] = @canceled_redirect if @canceled_redirect
15
+
16
+ @wizard_config = Wizardly::Wizard::Configuration.new(@controller_name, opts)
17
+ @wizard_config.inspect_model!(@model_name.to_sym)
18
+ end
19
+
20
+ def manifest
21
+ record do |m|
22
+ m.directory "app/controllers"
23
+
24
+ m.template "controller.rb.erb", "app/controllers/#{controller_name}_controller.rb"
25
+
26
+ m.template "helper.rb.erb", "app/helpers/#{controller_name}_helper.rb"
27
+
28
+ end
29
+ end
30
+
31
+ def controller_class_name
32
+ "#{controller_name.camelize}Controller"
33
+ end
34
+ def model_class_name
35
+ "#{model_name.camelize}"
36
+ end
37
+ def action_methods
38
+ @wizard_config.print_page_action_methods
39
+ end
40
+ def callback_methods
41
+ @wizard_config.print_callbacks
42
+ end
43
+ def helper_methods
44
+ @wizard_config.print_helpers
45
+ end
46
+ def callback_macro_methods
47
+ @wizard_config.print_callback_macros
48
+ end
49
+
50
+ protected
51
+ # Override with your own usage banner.
52
+ def banner
53
+ "Usage: #{$0} wizardly_controller controller_name model_name [completed_redirect canceled_redirect]"
54
+ end
55
+
56
+
57
+ end
@@ -0,0 +1,4 @@
1
+ Generates a wizard scaffold from a wizard controller using wizard_for_model (or wizard_for)
2
+
3
+
4
+
@@ -0,0 +1,23 @@
1
+ <h1><%= page.title %></h1>
2
+
3
+ <%= "<p>#{page.description}<p>" if !page.description.blank? %>
4
+ <%%= "<p style=\"color: green\">#{flash[:notice]}<p>" if flash[:notice] %>
5
+
6
+ <%% form_for :<%= model_name %>, :url=>{:action=>:<%= page.name %>} do |f| %>
7
+ <%%= f.error_messages %>
8
+
9
+ <% for field in page.fields -%>
10
+ <% first_field_id ||= "#{model_name}_#{field.name}" %>
11
+ <p>
12
+ <%%= f.label :<%= field.name %> %><br />
13
+ <%%= f.<%= field.field_type %> :<%= field.name %> %>
14
+ </p>
15
+ <% end -%>
16
+ <p>
17
+ <%%= <%= submit_tag %> %>
18
+ </p>
19
+ <%% end %>
20
+
21
+ <script type='text/javascript'>
22
+ document.getElementById('<%=first_field_id %>').focus()
23
+ </script>
@@ -0,0 +1,22 @@
1
+ %h1 <%= page.title %>
2
+
3
+ <% unless page.description.blank? %>
4
+ %p <%= page.description %>
5
+ <% end %>
6
+ - if flash[:notice]
7
+ %p{:style=>'color: green'}= flash[:notice]
8
+
9
+ - form_for :<%= model_name %>, :url=>{:action=>:<%= page.name %>} do |f|
10
+ = f.error_messages
11
+
12
+ <% for field in page.fields %>
13
+ <% first_field_id ||= "#{model_name}_#{field.name}" %>
14
+ %p
15
+ = f.label :<%= field.name %>
16
+ %br
17
+ = f.<%= field.field_type %> :<%= field.name %>
18
+ <% end %>
19
+ %p= <%= submit_tag %>
20
+
21
+ %script{:type=>'text/javascript'}
22
+ document.getElementById('<%=first_field_id %>').focus()
@@ -0,0 +1,30 @@
1
+ module <%=controller_name.camelize %>Helper
2
+
3
+ def wizardly_submit
4
+ @@wizardly_submit ||= {}
5
+ step_id = "#{controller_name}_#{@step}".to_sym
6
+ unless @@wizardly_submit[step_id]
7
+ buttons = @wizard.pages[@step].buttons
8
+ @@wizardly_submit[step_id] = buttons.inject(StringIO.new) do |io, button|
9
+ io << submit_tag(button.name, :name=>button.id.to_s)
10
+ end.string
11
+ end
12
+ @@wizardly_submit[step_id]
13
+ end
14
+
15
+ def wizardly_image_submit(asset_dir = nil, opts = {})
16
+ @@wizardly_image_submit ||={}
17
+ step_id = "#{controller_name}_#{@step}".to_sym
18
+ unless @@wizardly_image_submit[step_id]
19
+ asset_dir = asset_dir ? "#{asset_dir}/".squeeze("/").sub(/^\//,'') : "wizardly/"
20
+ buttons = @wizard.pages[@step].buttons
21
+ @@wizardly_image_submit[step_id] = buttons.inject(StringIO.new) do |io, button|
22
+ opts[:value] = button.name
23
+ opts[:name] = button.id.to_s
24
+ io << image_submit_tag("#{asset_dir}#{button.id}.png", opts)
25
+ end.string
26
+ end
27
+ @@wizardly_image_submit[step_id]
28
+ end
29
+
30
+ end
@@ -0,0 +1,15 @@
1
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3
+
4
+ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
5
+ <head>
6
+ <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
7
+ <title><%= controller_class_name %>: <%%= controller.action_name %></title>
8
+ <%%= stylesheet_link_tag 'scaffold' %>
9
+ </head>
10
+ <body>
11
+
12
+ <%%= yield %>
13
+
14
+ </body>
15
+ </html>
@@ -0,0 +1,10 @@
1
+ !!! XML
2
+ !!!
3
+ %html{ :'xml:lang' => "en", :lang => "en" }
4
+ %head
5
+ %title= "<%=controller_class_name %>: " + controller.action_name
6
+ %meta{ :"http-equiv" => "Content-Type", :content => "text/html; charset-utf-8" }
7
+ %link{ :rel => "shortcut icon", :href => "/favicon.ico" }
8
+ = stylesheet_link_tag "scaffold", :media => "screen"
9
+ %body
10
+ = yield
@@ -0,0 +1,54 @@
1
+ body { background-color: #fff; color: #333; }
2
+
3
+ body, p, ol, ul, td {
4
+ font-family: verdana, arial, helvetica, sans-serif;
5
+ font-size: 13px;
6
+ line-height: 18px;
7
+ }
8
+
9
+ pre {
10
+ background-color: #eee;
11
+ padding: 10px;
12
+ font-size: 11px;
13
+ }
14
+
15
+ a { color: #000; }
16
+ a:visited { color: #666; }
17
+ a:hover { color: #fff; background-color:#000; }
18
+
19
+ .fieldWithErrors {
20
+ padding: 2px;
21
+ background-color: red;
22
+ display: table;
23
+ }
24
+
25
+ #errorExplanation {
26
+ width: 400px;
27
+ border: 2px solid red;
28
+ padding: 7px;
29
+ padding-bottom: 12px;
30
+ margin-bottom: 20px;
31
+ background-color: #f0f0f0;
32
+ }
33
+
34
+ #errorExplanation h2 {
35
+ text-align: left;
36
+ font-weight: bold;
37
+ padding: 5px 5px 5px 15px;
38
+ font-size: 12px;
39
+ margin: -7px;
40
+ background-color: #c00;
41
+ color: #fff;
42
+ }
43
+
44
+ #errorExplanation p {
45
+ color: #333;
46
+ margin-bottom: 0;
47
+ padding: 5px;
48
+ }
49
+
50
+ #errorExplanation ul li {
51
+ font-size: 12px;
52
+ list-style: square;
53
+ }
54
+
@@ -0,0 +1,109 @@
1
+ require 'wizardly'
2
+
3
+ class WizardlyScaffoldGenerator < Rails::Generator::Base
4
+ attr_reader :wizard_config, :pages, :submit_tag
5
+ attr_reader :controller_name,
6
+ :controller_class_path,
7
+ :controller_file_path,
8
+ :controller_class_nesting,
9
+ :controller_class_nesting_depth,
10
+ :controller_class_name,
11
+ :controller_underscore_name
12
+ attr_reader :model_name, :view_file_ext
13
+
14
+ alias_method :controller_file_name, :controller_underscore_name
15
+
16
+ def add_options!(opt)
17
+ opt.on('--haml', 'Generate scaffold for haml wizard') { |v| options[:output] = :haml }
18
+ opt.on('--ajax', 'Generate scaffold for ajax wizard') { |v| options[:output] = :ajax }
19
+ opt.on('--underscore', 'Append an underscore to front of each file') { |v| options[:underscore] = true }
20
+ opt.on('--image_submit', 'Use image submit tags in forms') {|v| options[:image_submit] = true }
21
+ end
22
+
23
+
24
+ def initialize(runtime_args, runtime_options = {})
25
+ super
26
+ name = @args[0].sub(/^:/, '').underscore.sub(/_controller$/, '').camelize + 'Controller'
27
+
28
+ base_name, @controller_class_path, @controller_file_path, @controller_class_nesting, @controller_class_nesting_depth = extract_modules(name)
29
+ @controller_class_name_without_nesting = base_name.camelize
30
+ @controller_underscore_name = base_name.underscore
31
+ @controller_name = @controller_underscore_name.sub(/_controller$/, '')
32
+ if @controller_class_nesting.empty?
33
+ @controller_class_name = @controller_class_name_without_nesting
34
+ else
35
+ @controller_class_name = "#{@controller_class_nesting}::#{@controller_class_name_without_nesting}"
36
+ end
37
+
38
+ begin
39
+ @controller_class = @controller_class_name.constantize
40
+ rescue Exception => e
41
+ raise Wizardly::WizardlyScaffoldError, "No controller #{@controller_class_name} found: " + e.message, caller
42
+ end
43
+ begin
44
+ @wizard_config = @controller_class.wizard_config
45
+ rescue Exception => e
46
+ raise Wizardly::WizardlyScaffoldError, "#{@controller_class_name} must contain a valid 'act_wizardly_for' or 'wizard_for_model' macro: " + e.message, caller
47
+ end
48
+
49
+ @pages = @wizard_config.pages
50
+ @model_name = @wizard_config.model
51
+ @submit_tag = options[:image_submit] ? "wizardly_image_submit" : "wizardly_submit"
52
+
53
+ #based on options, default is --html, others --ajax, --haml
54
+ @view_file_ext = ["html.erb", "html.erb"]
55
+ @view_file_ext = ["html.haml.erb", "html.haml"] if options[:output] == :haml
56
+ #ajax
57
+ end
58
+
59
+ def manifest
60
+ record do |m|
61
+ # Helper, views, test and stylesheets directories.
62
+ m.directory(File.join('app/helpers', controller_class_path))
63
+ m.directory(File.join('app/views', controller_class_path, controller_name))
64
+ m.directory(File.join('app/views/layouts', controller_class_path))
65
+ m.directory('public/images/wizardly') if options[:image_submit]
66
+ #m.directory(File.join('test/functional', controller_class_path))
67
+ #m.directory(File.join('public/stylesheets', class_path))
68
+
69
+ underscore = options[:underscore] ? '_' : ''
70
+ pages.each do |id, page|
71
+ m.template(
72
+ "form.#{view_file_ext.first}",
73
+ File.join('app/views', controller_class_path, controller_name, "#{underscore}#{id}.#{view_file_ext.last}"),
74
+ :assigns=>{:id=>id, :page=>page}
75
+ )
76
+ end
77
+
78
+ if options[:image_submit]
79
+ %w(next skip back cancel finish).each do |fn|
80
+ m.file("images/#{fn}.png", "public/images/wizardly/#{fn}.png")
81
+ end
82
+ end
83
+
84
+ m.template("helper.rb.erb", File.join('app/helpers', controller_class_path, "#{controller_name}_helper.rb"))
85
+
86
+ # Layout and stylesheet.
87
+ m.template("layout.#{view_file_ext.first}", File.join('app/views/layouts', controller_class_path, "#{controller_name}.#{view_file_ext.last}"))
88
+ m.template('style.css', 'public/stylesheets/scaffold.css')
89
+
90
+ #m.dependency 'model', [name] + @args, :collision => :skip
91
+ end
92
+ end
93
+
94
+ protected
95
+ def banner
96
+ "Usage: #{$0} wizardly_scaffold controller_name --ajax --haml"
97
+ end
98
+
99
+ def extract_modules(name)
100
+ modules = name.include?('/') ? name.split('/') : name.split('::')
101
+ name = modules.pop
102
+ path = modules.map { |m| m.underscore }
103
+ file_path = (path + [name.underscore]).join('/')
104
+ nesting = modules.map { |m| m.camelize }.join('::')
105
+ [name, path, file_path, nesting, modules.size]
106
+ end
107
+
108
+
109
+ end