stepper 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/Gemfile +15 -0
- data/Gemfile.lock +144 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +84 -0
- data/Rakefile +53 -0
- data/VERSION +1 -0
- data/app/views/stepper/_fields.html.erb +17 -0
- data/config/locales/stepper.yml +6 -0
- data/lib/stepper.rb +3 -0
- data/lib/stepper/controllers/controller_additions.rb +75 -0
- data/lib/stepper/controllers/controller_resource.rb +58 -0
- data/lib/stepper/engine.rb +4 -0
- data/lib/stepper/exceptions.rb +4 -0
- data/lib/stepper/helper/action_view_additions.rb +14 -0
- data/lib/stepper/models/active_record_additions.rb +96 -0
- data/lib/stepper/railtie.rb +26 -0
- data/stepper.gemspec +136 -0
- data/test/controllers/controller_additions_test.rb +16 -0
- data/test/controllers/controller_resource_test.rb +49 -0
- data/test/controllers/controller_test.rb +138 -0
- data/test/helper.rb +20 -0
- data/test/helpers/helper_test.rb +49 -0
- data/test/integration/steps_test.rb +39 -0
- data/test/models/assigns_test.rb +15 -0
- data/test/models/instance_test.rb +65 -0
- data/test/models/models_test.rb +26 -0
- data/test/models/validation_test.rb +36 -0
- data/test/rails_app/Rakefile +7 -0
- data/test/rails_app/app/controllers/application_controller.rb +3 -0
- data/test/rails_app/app/controllers/companies_controller.rb +11 -0
- data/test/rails_app/app/helpers/application_helper.rb +2 -0
- data/test/rails_app/app/mailers/.gitkeep +0 -0
- data/test/rails_app/app/models/.gitkeep +0 -0
- data/test/rails_app/app/models/company.rb +18 -0
- data/test/rails_app/app/models/users.rb +3 -0
- data/test/rails_app/app/views/companies/_step1_step.html.erb +2 -0
- data/test/rails_app/app/views/companies/_step2_step.html.erb +2 -0
- data/test/rails_app/app/views/companies/_step3_step.html.erb +2 -0
- data/test/rails_app/app/views/companies/index.html.erb +1 -0
- data/test/rails_app/app/views/companies/new.html.erb +14 -0
- data/test/rails_app/app/views/companies/show.html.erb +1 -0
- data/test/rails_app/app/views/layouts/application.html.erb +14 -0
- data/test/rails_app/config.ru +4 -0
- data/test/rails_app/config/application.rb +43 -0
- data/test/rails_app/config/boot.rb +6 -0
- data/test/rails_app/config/database.yml +13 -0
- data/test/rails_app/config/environment.rb +5 -0
- data/test/rails_app/config/environments/development.rb +30 -0
- data/test/rails_app/config/environments/production.rb +60 -0
- data/test/rails_app/config/environments/test.rb +42 -0
- data/test/rails_app/config/initializers/backtrace_silencers.rb +7 -0
- data/test/rails_app/config/initializers/inflections.rb +10 -0
- data/test/rails_app/config/initializers/mime_types.rb +5 -0
- data/test/rails_app/config/initializers/secret_token.rb +7 -0
- data/test/rails_app/config/initializers/session_store.rb +8 -0
- data/test/rails_app/config/initializers/wrap_parameters.rb +14 -0
- data/test/rails_app/config/locales/en.yml +10 -0
- data/test/rails_app/config/routes.rb +3 -0
- data/test/rails_app/db/migrate/20110928102949_create_tables.rb +25 -0
- data/test/rails_app/db/schema.rb +20 -0
- data/test/rails_app/lib/assets/.gitkeep +0 -0
- data/test/rails_app/lib/tasks/.gitkeep +0 -0
- data/test/rails_app/public/404.html +26 -0
- data/test/rails_app/public/422.html +26 -0
- data/test/rails_app/public/500.html +26 -0
- data/test/rails_app/public/favicon.ico +0 -0
- data/test/rails_app/public/index.html +241 -0
- data/test/rails_app/public/robots.txt +5 -0
- data/test/rails_app/script/rails +6 -0
- metadata +232 -0
data/.document
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
gem "rails", "~> 3.1.0"
|
3
|
+
|
4
|
+
group :development do
|
5
|
+
gem "ruby-debug19", :require => 'ruby-debug'
|
6
|
+
gem "sqlite3"
|
7
|
+
gem "shoulda", ">= 0"
|
8
|
+
gem "bundler", "~> 1.0.0"
|
9
|
+
gem "jeweler", "~> 1.6.4"
|
10
|
+
gem "rcov", ">= 0"
|
11
|
+
gem "mocha"
|
12
|
+
gem "capybara"
|
13
|
+
gem "launchy"
|
14
|
+
end
|
15
|
+
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,144 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
actionmailer (3.1.0)
|
5
|
+
actionpack (= 3.1.0)
|
6
|
+
mail (~> 2.3.0)
|
7
|
+
actionpack (3.1.0)
|
8
|
+
activemodel (= 3.1.0)
|
9
|
+
activesupport (= 3.1.0)
|
10
|
+
builder (~> 3.0.0)
|
11
|
+
erubis (~> 2.7.0)
|
12
|
+
i18n (~> 0.6)
|
13
|
+
rack (~> 1.3.2)
|
14
|
+
rack-cache (~> 1.0.3)
|
15
|
+
rack-mount (~> 0.8.2)
|
16
|
+
rack-test (~> 0.6.1)
|
17
|
+
sprockets (~> 2.0.0)
|
18
|
+
activemodel (3.1.0)
|
19
|
+
activesupport (= 3.1.0)
|
20
|
+
bcrypt-ruby (~> 3.0.0)
|
21
|
+
builder (~> 3.0.0)
|
22
|
+
i18n (~> 0.6)
|
23
|
+
activerecord (3.1.0)
|
24
|
+
activemodel (= 3.1.0)
|
25
|
+
activesupport (= 3.1.0)
|
26
|
+
arel (~> 2.2.1)
|
27
|
+
tzinfo (~> 0.3.29)
|
28
|
+
activeresource (3.1.0)
|
29
|
+
activemodel (= 3.1.0)
|
30
|
+
activesupport (= 3.1.0)
|
31
|
+
activesupport (3.1.0)
|
32
|
+
multi_json (~> 1.0)
|
33
|
+
addressable (2.2.6)
|
34
|
+
archive-tar-minitar (0.5.2)
|
35
|
+
arel (2.2.1)
|
36
|
+
bcrypt-ruby (3.0.1)
|
37
|
+
builder (3.0.0)
|
38
|
+
capybara (1.1.1)
|
39
|
+
mime-types (>= 1.16)
|
40
|
+
nokogiri (>= 1.3.3)
|
41
|
+
rack (>= 1.0.0)
|
42
|
+
rack-test (>= 0.5.4)
|
43
|
+
selenium-webdriver (~> 2.0)
|
44
|
+
xpath (~> 0.1.4)
|
45
|
+
childprocess (0.2.2)
|
46
|
+
ffi (~> 1.0.6)
|
47
|
+
columnize (0.3.4)
|
48
|
+
erubis (2.7.0)
|
49
|
+
ffi (1.0.9)
|
50
|
+
git (1.2.5)
|
51
|
+
hike (1.2.1)
|
52
|
+
i18n (0.6.0)
|
53
|
+
jeweler (1.6.4)
|
54
|
+
bundler (~> 1.0)
|
55
|
+
git (>= 1.2.5)
|
56
|
+
rake
|
57
|
+
json_pure (1.6.1)
|
58
|
+
launchy (2.0.5)
|
59
|
+
addressable (~> 2.2.6)
|
60
|
+
linecache19 (0.5.12)
|
61
|
+
ruby_core_source (>= 0.1.4)
|
62
|
+
mail (2.3.0)
|
63
|
+
i18n (>= 0.4.0)
|
64
|
+
mime-types (~> 1.16)
|
65
|
+
treetop (~> 1.4.8)
|
66
|
+
metaclass (0.0.1)
|
67
|
+
mime-types (1.16)
|
68
|
+
mocha (0.10.0)
|
69
|
+
metaclass (~> 0.0.1)
|
70
|
+
multi_json (1.0.3)
|
71
|
+
nokogiri (1.5.0)
|
72
|
+
polyglot (0.3.2)
|
73
|
+
rack (1.3.3)
|
74
|
+
rack-cache (1.0.3)
|
75
|
+
rack (>= 0.4)
|
76
|
+
rack-mount (0.8.3)
|
77
|
+
rack (>= 1.0.0)
|
78
|
+
rack-ssl (1.3.2)
|
79
|
+
rack
|
80
|
+
rack-test (0.6.1)
|
81
|
+
rack (>= 1.0)
|
82
|
+
rails (3.1.0)
|
83
|
+
actionmailer (= 3.1.0)
|
84
|
+
actionpack (= 3.1.0)
|
85
|
+
activerecord (= 3.1.0)
|
86
|
+
activeresource (= 3.1.0)
|
87
|
+
activesupport (= 3.1.0)
|
88
|
+
bundler (~> 1.0)
|
89
|
+
railties (= 3.1.0)
|
90
|
+
railties (3.1.0)
|
91
|
+
actionpack (= 3.1.0)
|
92
|
+
activesupport (= 3.1.0)
|
93
|
+
rack-ssl (~> 1.3.2)
|
94
|
+
rake (>= 0.8.7)
|
95
|
+
rdoc (~> 3.4)
|
96
|
+
thor (~> 0.14.6)
|
97
|
+
rake (0.9.2)
|
98
|
+
rcov (0.9.10)
|
99
|
+
rdoc (3.9.4)
|
100
|
+
ruby-debug-base19 (0.11.25)
|
101
|
+
columnize (>= 0.3.1)
|
102
|
+
linecache19 (>= 0.5.11)
|
103
|
+
ruby_core_source (>= 0.1.4)
|
104
|
+
ruby-debug19 (0.11.6)
|
105
|
+
columnize (>= 0.3.1)
|
106
|
+
linecache19 (>= 0.5.11)
|
107
|
+
ruby-debug-base19 (>= 0.11.19)
|
108
|
+
ruby_core_source (0.1.5)
|
109
|
+
archive-tar-minitar (>= 0.5.2)
|
110
|
+
rubyzip (0.9.4)
|
111
|
+
selenium-webdriver (2.7.0)
|
112
|
+
childprocess (>= 0.2.1)
|
113
|
+
ffi (>= 1.0.7)
|
114
|
+
json_pure
|
115
|
+
rubyzip
|
116
|
+
shoulda (2.11.3)
|
117
|
+
sprockets (2.0.0)
|
118
|
+
hike (~> 1.2)
|
119
|
+
rack (~> 1.0)
|
120
|
+
tilt (!= 1.3.0, ~> 1.1)
|
121
|
+
sqlite3 (1.3.4)
|
122
|
+
thor (0.14.6)
|
123
|
+
tilt (1.3.3)
|
124
|
+
treetop (1.4.10)
|
125
|
+
polyglot
|
126
|
+
polyglot (>= 0.3.1)
|
127
|
+
tzinfo (0.3.29)
|
128
|
+
xpath (0.1.4)
|
129
|
+
nokogiri (~> 1.3)
|
130
|
+
|
131
|
+
PLATFORMS
|
132
|
+
ruby
|
133
|
+
|
134
|
+
DEPENDENCIES
|
135
|
+
bundler (~> 1.0.0)
|
136
|
+
capybara
|
137
|
+
jeweler (~> 1.6.4)
|
138
|
+
launchy
|
139
|
+
mocha
|
140
|
+
rails (~> 3.1.0)
|
141
|
+
rcov
|
142
|
+
ruby-debug19
|
143
|
+
shoulda
|
144
|
+
sqlite3
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Anton Versal
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
= Stepper
|
2
|
+
|
3
|
+
Stepper is multistep form (wizard) solution for Rails 3.
|
4
|
+
Stepper allows you to split up your large form into series of pages that users can navigate through to complete the form and save it state.
|
5
|
+
== Installation
|
6
|
+
You can use it with Rails 3:
|
7
|
+
|
8
|
+
gem install stepper
|
9
|
+
== Getting Started
|
10
|
+
=== Configuring model
|
11
|
+
Create migration for model:
|
12
|
+
|
13
|
+
add_column :companies, :current_step, :string
|
14
|
+
add_index :companies, :current_step
|
15
|
+
|
16
|
+
Setup names of steps that you want to have:
|
17
|
+
|
18
|
+
has_steps :steps => %w{name city kind}
|
19
|
+
|
20
|
+
Setup validation for each step if necessary, method should have name like <tt>validation_#{step_name}</tt>:
|
21
|
+
|
22
|
+
def validate_name
|
23
|
+
self.validates_presence_of :name
|
24
|
+
end
|
25
|
+
|
26
|
+
def validate_city
|
27
|
+
self.validates_presence_of :city
|
28
|
+
end
|
29
|
+
|
30
|
+
def validate_kind
|
31
|
+
self.validates_presence_of :kind
|
32
|
+
end
|
33
|
+
|
34
|
+
Now Your model ready for multistep form!
|
35
|
+
=== Configuring controller
|
36
|
+
|
37
|
+
Stepper use +update+, +create+ and +new+ action.
|
38
|
+
|
39
|
+
For controller you need just add +has_steps+ method:
|
40
|
+
class CompaniesController < ApplicationController
|
41
|
+
has_steps
|
42
|
+
end
|
43
|
+
|
44
|
+
And you should have +show+ action because stepper redirects to it when last step finished. It will be configurable in a feature.
|
45
|
+
=== Configuring view
|
46
|
+
|
47
|
+
Add stepper helper method into the form in view that rendered by new action:
|
48
|
+
|
49
|
+
<%= form_for(@company) do |f| %>
|
50
|
+
<%= stepper f %>
|
51
|
+
<% end %>
|
52
|
+
|
53
|
+
+stepper+ helper renders partial according to the current step of form. Partials should have name like <tt>#{step_name}_step</tt>:
|
54
|
+
|
55
|
+
+name_step.html.erb+
|
56
|
+
|
57
|
+
<%= f.label :name %>
|
58
|
+
<%= f.text_field :name %>
|
59
|
+
|
60
|
+
+city_step.html.erb+
|
61
|
+
|
62
|
+
<%= f.label :city %>
|
63
|
+
<%= f.text_field :city %>
|
64
|
+
|
65
|
+
+kind_step.html.erb+
|
66
|
+
|
67
|
+
<%= f.label :kind %>
|
68
|
+
<%= f.text_field :kind %>
|
69
|
+
|
70
|
+
== Contributing to stepper
|
71
|
+
|
72
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
73
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
74
|
+
* Fork the project
|
75
|
+
* Start a feature/bugfix branch
|
76
|
+
* Commit and push until you are happy with your contribution
|
77
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
78
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
79
|
+
|
80
|
+
== Copyright
|
81
|
+
|
82
|
+
Copyright (c) 2011 Anton Versal. See LICENSE.txt for
|
83
|
+
further details.
|
84
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
|
14
|
+
require 'jeweler'
|
15
|
+
Jeweler::Tasks.new do |gem|
|
16
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
17
|
+
gem.name = "stepper"
|
18
|
+
gem.homepage = "http://github.com/antonversal/stepper"
|
19
|
+
gem.license = "MIT"
|
20
|
+
gem.summary = %Q{Stepper is multistep form (wizard) solution for Rails 3.}
|
21
|
+
gem.description = %Q{Stepper is multistep form (wizard) solution for Rails 3. Stepper allows you to split up your large form into series of pages that users can navigate through to complete the form and save it state.}
|
22
|
+
gem.email = "ant.ver@gmail.com"
|
23
|
+
gem.authors = ["Anton Versal"]
|
24
|
+
# dependencies defined in Gemfile
|
25
|
+
end
|
26
|
+
Jeweler::RubygemsDotOrgTasks.new
|
27
|
+
|
28
|
+
require 'rake/testtask'
|
29
|
+
Rake::TestTask.new(:test) do |test|
|
30
|
+
test.libs << 'lib' << 'test'
|
31
|
+
test.pattern = 'test/**/*_test.rb'
|
32
|
+
test.verbose = true
|
33
|
+
end
|
34
|
+
|
35
|
+
require 'rcov/rcovtask'
|
36
|
+
Rcov::RcovTask.new do |test|
|
37
|
+
test.libs << 'test'
|
38
|
+
test.pattern = 'test/**/test_*.rb'
|
39
|
+
test.verbose = true
|
40
|
+
test.rcov_opts << '--exclude "gems/*"'
|
41
|
+
end
|
42
|
+
|
43
|
+
task :default => :test
|
44
|
+
|
45
|
+
require 'rake/rdoctask'
|
46
|
+
Rake::RDocTask.new do |rdoc|
|
47
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
48
|
+
|
49
|
+
rdoc.rdoc_dir = 'rdoc'
|
50
|
+
rdoc.title = "stepper #{version}"
|
51
|
+
rdoc.rdoc_files.include('README*')
|
52
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
53
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
@@ -0,0 +1,17 @@
|
|
1
|
+
<div class="stepper">
|
2
|
+
<%= f.hidden_field current_step_column, :value => resource.next_step %>
|
3
|
+
<%= render "#{resource.next_step}_step", :f => f %>
|
4
|
+
|
5
|
+
<ul class="actions">
|
6
|
+
<li class="first save"><%= f.submit :value => t('stepper.save').html_safe %></li>
|
7
|
+
<% unless resource.first_step?(resource.next_step) %>
|
8
|
+
<li class="previous_step"><%= f.submit :value => t('stepper.previous_step').html_safe %></li>
|
9
|
+
<% end %>
|
10
|
+
<% unless resource.last_step?(resource.next_step) %>
|
11
|
+
<li class="last next_step"><%= f.submit :value =>t('stepper.next_step').html_safe %></li>
|
12
|
+
<% else %>
|
13
|
+
<li class="last finish"><%= f.submit :value =>t('stepper.finish').html_safe %></li>
|
14
|
+
<% end %>
|
15
|
+
</ul>
|
16
|
+
</div>
|
17
|
+
|
data/lib/stepper.rb
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
module Stepper
|
2
|
+
module ControllerAdditions
|
3
|
+
|
4
|
+
def self.included(base)
|
5
|
+
base.extend ClassMethods
|
6
|
+
end
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
def has_steps(*args)
|
10
|
+
include InstanceMethods
|
11
|
+
stepper_resource_class.add_before_filter(self, *args)
|
12
|
+
end
|
13
|
+
|
14
|
+
def stepper_resource_class
|
15
|
+
ControllerResource
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
|
20
|
+
module InstanceMethods
|
21
|
+
|
22
|
+
def create
|
23
|
+
respond_to do |format|
|
24
|
+
if @_stepper_resource_instance.save
|
25
|
+
format.html { redirect_steps }
|
26
|
+
else
|
27
|
+
@_stepper_resource_instance.previous_step!
|
28
|
+
format.html { render :action => "new" }
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def update
|
34
|
+
|
35
|
+
@_stepper_resource_instance.previous_step!.previous_step! if params[:commit] == t('stepper.previous_step').html_safe
|
36
|
+
|
37
|
+
@_stepper_resource_instance.previous_step! if params[:commit] == t('stepper.save').html_safe
|
38
|
+
|
39
|
+
respond_to do |format|
|
40
|
+
if @_stepper_resource_instance.save
|
41
|
+
format.html { redirect_steps }
|
42
|
+
else
|
43
|
+
@_stepper_resource_instance.previous_step!
|
44
|
+
format.html { render :action => "new" }
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def new
|
50
|
+
end
|
51
|
+
|
52
|
+
protected
|
53
|
+
|
54
|
+
def redirect_steps
|
55
|
+
if params[:commit] == t('stepper.save').html_safe
|
56
|
+
redirect_to url_for(sanitized_params.merge(:action => "index"))
|
57
|
+
elsif params[:commit] == t('stepper.previous_step').html_safe and params[:action] == "update"
|
58
|
+
redirect_to url_for(sanitized_params.merge(:action => "new", :id => @_stepper_resource_instance.id))
|
59
|
+
elsif params[:commit] == t('stepper.next_step').html_safe
|
60
|
+
redirect_to url_for(sanitized_params.merge(:action => "new", :id => @_stepper_resource_instance.id)), :notice => "Step #{@_stepper_resource_instance.stepper_current_step.humanize} was successfully created."
|
61
|
+
elsif params[:commit] == t('stepper.finish').html_safe
|
62
|
+
redirect_to url_for(sanitized_params.merge(:action => "show", :id => @_stepper_resource_instance.id))
|
63
|
+
else
|
64
|
+
raise Stepper::StepperException.new("Unknown commit: #{params[:commit]}")
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def sanitized_params
|
69
|
+
params.except(@_stepper_name, :commit, :id)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
74
|
+
|
75
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
module Stepper
|
2
|
+
class ControllerResource
|
3
|
+
|
4
|
+
def self.add_before_filter(controller_class, *args)
|
5
|
+
resource_name = args.first
|
6
|
+
controller_class.send(:before_filter, :only => [:create, :update, :new]) do |controller|
|
7
|
+
controller_resource = controller.class.stepper_resource_class.new(controller, resource_name)
|
8
|
+
controller.instance_variable_set :@_stepper_resource_instance, controller_resource.load_resource
|
9
|
+
controller.instance_variable_set :@_stepper_name, controller_resource.name
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def initialize(controller, *args)
|
14
|
+
@controller = controller
|
15
|
+
@params = controller.params
|
16
|
+
@name = args.first
|
17
|
+
end
|
18
|
+
|
19
|
+
def load_resource
|
20
|
+
self.resource_instance ||= load_resource_instance
|
21
|
+
end
|
22
|
+
|
23
|
+
def load_resource_instance
|
24
|
+
if @params[:action] == 'create'
|
25
|
+
resource = resource_class.new(@params[name] || {})
|
26
|
+
else
|
27
|
+
resource = unless @params[:id].blank?
|
28
|
+
resource_class.find(@params[:id])
|
29
|
+
else
|
30
|
+
resource_class.new
|
31
|
+
end
|
32
|
+
resource.attributes = @params[name] unless @params[name].blank?
|
33
|
+
end
|
34
|
+
resource
|
35
|
+
end
|
36
|
+
|
37
|
+
def resource_class
|
38
|
+
name.camelize.constantize
|
39
|
+
end
|
40
|
+
|
41
|
+
def name_from_controller
|
42
|
+
@params[:controller].sub("Controller", "").underscore.split('/').last.singularize
|
43
|
+
end
|
44
|
+
|
45
|
+
def name
|
46
|
+
@name || name_from_controller
|
47
|
+
end
|
48
|
+
|
49
|
+
def resource_instance=(object)
|
50
|
+
@controller.instance_variable_set "@#{name}", object
|
51
|
+
end
|
52
|
+
|
53
|
+
def resource_instance
|
54
|
+
@controller.instance_variable_get "@#{name}"
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
end
|