gendalf 0.1 → 0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/CHANGELOG CHANGED
@@ -1 +1,3 @@
1
- v0.1. just a placeholder for the coming gem
1
+ v0.2. First working implementation
2
+
3
+ v0.1. Initial gem release
data/Manifest CHANGED
@@ -1,4 +1,8 @@
1
1
  CHANGELOG
2
- README
3
- Rakefile
4
2
  Manifest
3
+ Rakefile
4
+ gendalf.gemspec
5
+ lib/controllers/wizard_controller.rb
6
+ lib/extensions/routes.rb
7
+ lib/gendalf.rb
8
+ lib/models/wizard_model.rb
data/Rakefile CHANGED
@@ -1,2 +1,11 @@
1
1
  require 'echoe'
2
- Echoe.new('gendalf')
2
+ Echoe.new('gendalf') do |p|
3
+ p.project = 'gendalf'
4
+ p.author = 'Ivan Kasatenko'
5
+ p.email = 'sky.31338@gmail.com'
6
+ p.summary = 'Trivial gem to support wizard application style creation.'
7
+ p.url = 'http://github.com/skywriter/gendalf'
8
+ p.runtime_dependencies = ['activemodel', 'actionpack']
9
+ p.development_dependencies = ['echoe']
10
+ p.retain_gemspec = true
11
+ end
data/gendalf.gemspec CHANGED
@@ -2,29 +2,38 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{gendalf}
5
- s.version = "0.1"
5
+ s.version = "0.2"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
- s.authors = [""]
9
- s.date = %q{2011-02-15}
10
- s.description = %q{}
11
- s.email = %q{}
12
- s.extra_rdoc_files = ["CHANGELOG", "README"]
13
- s.files = ["CHANGELOG", "README", "Rakefile", "Manifest", "gendalf.gemspec"]
14
- s.homepage = %q{}
15
- s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Gendalf", "--main", "README"]
8
+ s.authors = ["Ivan Kasatenko"]
9
+ s.date = %q{2011-02-16}
10
+ s.description = %q{Trivial gem to support wizard application style creation.}
11
+ s.email = %q{sky.31338@gmail.com}
12
+ s.extra_rdoc_files = ["CHANGELOG", "lib/controllers/wizard_controller.rb", "lib/extensions/routes.rb", "lib/gendalf.rb", "lib/models/wizard_model.rb"]
13
+ s.files = ["CHANGELOG", "Manifest", "Rakefile", "gendalf.gemspec", "lib/controllers/wizard_controller.rb", "lib/extensions/routes.rb", "lib/gendalf.rb", "lib/models/wizard_model.rb"]
14
+ s.homepage = %q{http://github.com/skywriter/gendalf}
15
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Gendalf"]
16
16
  s.require_paths = ["lib"]
17
17
  s.rubyforge_project = %q{gendalf}
18
18
  s.rubygems_version = %q{1.3.7}
19
- s.summary = %q{}
19
+ s.summary = %q{Trivial gem to support wizard application style creation.}
20
20
 
21
21
  if s.respond_to? :specification_version then
22
22
  current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
23
23
  s.specification_version = 3
24
24
 
25
25
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
26
+ s.add_runtime_dependency(%q<activemodel>, [">= 0"])
27
+ s.add_runtime_dependency(%q<actionpack>, [">= 0"])
28
+ s.add_development_dependency(%q<echoe>, [">= 0"])
26
29
  else
30
+ s.add_dependency(%q<activemodel>, [">= 0"])
31
+ s.add_dependency(%q<actionpack>, [">= 0"])
32
+ s.add_dependency(%q<echoe>, [">= 0"])
27
33
  end
28
34
  else
35
+ s.add_dependency(%q<activemodel>, [">= 0"])
36
+ s.add_dependency(%q<actionpack>, [">= 0"])
37
+ s.add_dependency(%q<echoe>, [">= 0"])
29
38
  end
30
39
  end
@@ -0,0 +1,97 @@
1
+ module Gendalf
2
+
3
+ class WizardController < ActionController::Base
4
+ protect_from_forgery
5
+
6
+ before_filter :redirect_to_current_step
7
+
8
+ def model
9
+ @@model_name.to_s.camelize.constantize.steps[step_no]
10
+ end
11
+
12
+ def step
13
+ @step = model.new(params[param_name])
14
+ send step_action_name
15
+ render :action => step_action_name
16
+ end
17
+
18
+ def step_submit
19
+ if proceed_custom_or_default_step_submit
20
+ if respond_to?(step_action_name(step_no+1))
21
+ self.current_step = step_no+1
22
+ redirect_to wizard_path(self.current_step)
23
+ end
24
+ else
25
+ render :action => step_action_name
26
+ end
27
+ end
28
+
29
+ class << self
30
+ def set_model_name(model_name)
31
+ @@model_name = model_name
32
+ end
33
+
34
+ def model_name
35
+ @@model_name
36
+ end
37
+ end
38
+
39
+ def default
40
+ redirect_to custom_wizard_path(current_step)
41
+ end
42
+
43
+ protected
44
+
45
+ def wizard_model
46
+ raise "Please, implement 'wizard_model' method in #{self.class.name}"
47
+ end
48
+
49
+ def current_step
50
+ raise "Please, implement 'current_step' method in #{self.class.name}"
51
+ end
52
+
53
+ def current_step=(step)
54
+ raise "Please, implement 'current_step=' method in #{self.class.name}"
55
+ end
56
+
57
+ def wizard_path(step)
58
+ send("#{self.class.name.gsub('Controller', '').underscore}_path", :step => step+1)
59
+ end
60
+
61
+ private
62
+
63
+ def redirect_to_current_step
64
+ redirect_to wizard_path(current_step) unless step_no == current_step
65
+ end
66
+
67
+ def param_name
68
+ model.name.underscore
69
+ end
70
+
71
+ def proceed_custom_or_default_step_submit
72
+ respond_to?("#{step_action_name}_submit") ?
73
+ send("#{step_action_name}_submit") :
74
+ default_step_submit
75
+ end
76
+
77
+ def default_step_submit
78
+ @step = model.new(params[param_name])
79
+ if @step.valid?
80
+ wizard_model.merge_step!(@step)
81
+ true
82
+ else
83
+ flash[:error] = @step.errors.full_messages
84
+ false
85
+ end
86
+ end
87
+
88
+ def step_no
89
+ params[:step].to_i-1
90
+ end
91
+
92
+ def step_action_name(action_step_no=nil)
93
+ "step#{action_step_no || step_no}"
94
+ end
95
+ end
96
+
97
+ end
@@ -0,0 +1,13 @@
1
+ class ActionDispatch::Routing::Mapper
2
+ def wizard(url, controller, total_steps)
3
+ match "#{url}/step:step" => "#{controller.to_s.underscore}#step", :via => :get, :as => controller.to_s.underscore.to_sym
4
+ match "#{url}/step:step" => "#{controller.to_s.underscore}#step_submit", :via => :post, :as => controller.to_s.underscore.to_sym
5
+ match "#{url}" => "#{controller.to_s.underscore}#default", :via => :get
6
+
7
+
8
+ model_prefix = "#{controller.to_s.camelcase}Controller".constantize.model_name.to_s.underscore
9
+ total_steps.times do |idx|
10
+ match "#{url}/step#{idx+1}" => "#{controller.to_s.underscore}#step", :via => :get, :as => "#{model_prefix}_#{idx}_steps"
11
+ end
12
+ end
13
+ end
data/lib/gendalf.rb ADDED
@@ -0,0 +1,5 @@
1
+ $HERE = File.dirname(__FILE__)
2
+
3
+ require "#{$HERE}/controllers/wizard_controller.rb"
4
+ require "#{$HERE}/models/wizard_model.rb"
5
+ require "#{$HERE}/extensions/routes.rb"
@@ -0,0 +1,92 @@
1
+ module Gendalf
2
+
3
+ class WizardModel
4
+ include ActiveModel::Validations
5
+ include ActiveModel::Serialization
6
+
7
+ attr_accessor :step
8
+
9
+ def merge_step!(step_model)
10
+ step_model.class.step_attributes.each do |attrib|
11
+ send("#{attrib}=", step_model.send(attrib))
12
+ end
13
+ end
14
+
15
+ class << self
16
+ # Defines a step
17
+ def define_step(step_no, step_attributes, &validations_block)
18
+ define_attributes step_attributes
19
+
20
+ @steps ||= []
21
+ @steps[step_no] = step_model_class(step_no, step_attributes, &validations_block)
22
+ end
23
+
24
+ def define_null_step(step_no)
25
+ define_step step_no, [] do
26
+ end
27
+ end
28
+
29
+ alias_method :define_final_step, :define_null_step
30
+
31
+ def steps
32
+ @steps
33
+ end
34
+
35
+ private
36
+ # Defines steps attributes.
37
+ def define_attributes(step_attributes)
38
+ [*step_attributes].each do |attrib|
39
+ instance_eval do
40
+ attr_accessor attrib.to_sym
41
+ end
42
+ end
43
+ end
44
+
45
+ # Create a step model class (used for validations and holding intermediate step values)
46
+ def step_model_class(step_no, step_attributes, &validations_block)
47
+ step_model_class = Class.new
48
+
49
+ # make its name accessible to itself
50
+ step_model_class.instance_variable_set :@name, "#{self.name}_#{step_no}_Step"
51
+ # ... and save step attributes for future merging
52
+ step_model_class.instance_variable_set :@step_attributes, [*step_attributes]
53
+
54
+ # make it an proper ActiveModel
55
+ step_model_class.instance_eval do
56
+ include ActiveModel::Validations
57
+ include ActiveModel::Serialization
58
+ include ActiveModel::Conversion
59
+
60
+ def name; @name; end
61
+ def step_attributes; @step_attributes; end
62
+ end
63
+
64
+ step_model_class.class_eval do
65
+ def initialize(attributes = {})
66
+ return unless attributes.kind_of? Hash
67
+ attributes.each do |name, value|
68
+ send("#{name}=", value)
69
+ end
70
+ end
71
+
72
+ def persisted?
73
+ false
74
+ end
75
+ end
76
+
77
+ # define appropriate attributes for the step
78
+ [*step_attributes].each do |attrib|
79
+ step_model_class.instance_eval do
80
+ attr_accessor attrib.to_sym
81
+ end
82
+ end
83
+
84
+ # give user a chance to add the validations
85
+ step_model_class.instance_eval(&validations_block)
86
+
87
+ return step_model_class
88
+ end
89
+ end
90
+ end
91
+
92
+ end
metadata CHANGED
@@ -1,40 +1,87 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gendalf
3
3
  version: !ruby/object:Gem::Version
4
- hash: 9
4
+ hash: 15
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 1
9
- version: "0.1"
8
+ - 2
9
+ version: "0.2"
10
10
  platform: ruby
11
11
  authors:
12
- - ""
12
+ - Ivan Kasatenko
13
13
  autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-02-15 00:00:00 +03:00
17
+ date: 2011-02-16 00:00:00 +03:00
18
18
  default_executable:
19
- dependencies: []
20
-
21
- description: ""
22
- email: ""
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: activemodel
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 0
31
+ version: "0"
32
+ type: :runtime
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: actionpack
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ hash: 3
43
+ segments:
44
+ - 0
45
+ version: "0"
46
+ type: :runtime
47
+ version_requirements: *id002
48
+ - !ruby/object:Gem::Dependency
49
+ name: echoe
50
+ prerelease: false
51
+ requirement: &id003 !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ hash: 3
57
+ segments:
58
+ - 0
59
+ version: "0"
60
+ type: :development
61
+ version_requirements: *id003
62
+ description: Trivial gem to support wizard application style creation.
63
+ email: sky.31338@gmail.com
23
64
  executables: []
24
65
 
25
66
  extensions: []
26
67
 
27
68
  extra_rdoc_files:
28
69
  - CHANGELOG
29
- - README
70
+ - lib/controllers/wizard_controller.rb
71
+ - lib/extensions/routes.rb
72
+ - lib/gendalf.rb
73
+ - lib/models/wizard_model.rb
30
74
  files:
31
75
  - CHANGELOG
32
- - README
33
- - Rakefile
34
76
  - Manifest
77
+ - Rakefile
35
78
  - gendalf.gemspec
79
+ - lib/controllers/wizard_controller.rb
80
+ - lib/extensions/routes.rb
81
+ - lib/gendalf.rb
82
+ - lib/models/wizard_model.rb
36
83
  has_rdoc: true
37
- homepage: ""
84
+ homepage: http://github.com/skywriter/gendalf
38
85
  licenses: []
39
86
 
40
87
  post_install_message:
@@ -43,8 +90,6 @@ rdoc_options:
43
90
  - --inline-source
44
91
  - --title
45
92
  - Gendalf
46
- - --main
47
- - README
48
93
  require_paths:
49
94
  - lib
50
95
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -72,6 +117,6 @@ rubyforge_project: gendalf
72
117
  rubygems_version: 1.3.7
73
118
  signing_key:
74
119
  specification_version: 3
75
- summary: ""
120
+ summary: Trivial gem to support wizard application style creation.
76
121
  test_files: []
77
122
 
data/README DELETED
File without changes