mage 0.1.0
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.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +112 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/db/migrate/20160407080915_create_mage_steps.rb +10 -0
- data/lib/mage.rb +76 -0
- data/lib/mage/version.rb +3 -0
- data/mage.gemspec +29 -0
- metadata +102 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 2f5683b046e4441b7bdf7b9634844d3060794f52
|
4
|
+
data.tar.gz: 018f52b8564cb24285328e83a83f3a8b0984bba4
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d5402a3640a1a4ca7104944d938a7f57084c2c430d1cbab31c3cb928b09993f69a13b3e915cb99e64f89f8a99b0bcfeb023fb358bbffc648b8bd265d899e2992
|
7
|
+
data.tar.gz: 0c69e4ae1b9e309a5afc3c62b0c395caa0fc7899a35c06e26b7774ce00cf255e7c6be59b6fd245ef36ac3944335c76de0ecac03b6665315d7cd33f0de85cc3cc
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 effect305
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
# Mage
|
2
|
+
|
3
|
+
Mage can be used for making model creation wizards with step by step validations in Rails.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'mage', git: 'git://github.com/effect305/mage.git'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle install
|
16
|
+
|
17
|
+
After that migrate your database:
|
18
|
+
|
19
|
+
$ rake db:migrate
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
Define steps in your model:
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
class Product < ActiveRecord::Base
|
27
|
+
has_mage_steps :name, :price, :category
|
28
|
+
end
|
29
|
+
```
|
30
|
+
|
31
|
+
Add validations for each step, using mage_step_STEPNAME? syntax:
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
validates :name, presence: true, if: :mage_step_name?
|
35
|
+
validates :price, presence: true, if: :mage_step_price?
|
36
|
+
validates :category, presence: true, if: :mage_step_category?
|
37
|
+
```
|
38
|
+
|
39
|
+
You can use also custom validators or anything you want, just define on which step they should be run.
|
40
|
+
Also bear in mind that each step runs validations of all previous steps. Step increases after
|
41
|
+
successful save of the object.
|
42
|
+
|
43
|
+
After that create steps subdirectory in views directory of your controller.
|
44
|
+
|
45
|
+
$ mkdir app/views/products/steps
|
46
|
+
|
47
|
+
Put there templates for each step, naming them like steps. You can use form_for helper in your templates:
|
48
|
+
|
49
|
+
```ruby
|
50
|
+
<h1>Creating new product, step 1</h1>
|
51
|
+
<%= form_for @product do |f| %>
|
52
|
+
<%= f.label :name %>
|
53
|
+
<%= f.text_field :name %>
|
54
|
+
<%= f.submit %>
|
55
|
+
<% end %>
|
56
|
+
```
|
57
|
+
|
58
|
+
And the main part, it's time to set up your controller! Use render_mage method with your object as argument
|
59
|
+
in each place that you want to render wizard. This method will render necessary template from steps directory
|
60
|
+
according to the state of the object.
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
def new
|
64
|
+
@product = Product.new
|
65
|
+
render_mage(@product)
|
66
|
+
end
|
67
|
+
|
68
|
+
def create
|
69
|
+
@product = Product.new(product_params)
|
70
|
+
@product.save
|
71
|
+
render_mage(@product)
|
72
|
+
end
|
73
|
+
|
74
|
+
def edit
|
75
|
+
@product = Product.find(params[:id])
|
76
|
+
render_mage(@product)
|
77
|
+
end
|
78
|
+
|
79
|
+
def update
|
80
|
+
@product = Product.find(params[:id])
|
81
|
+
@product.update(product_params)
|
82
|
+
render_mage(@product)
|
83
|
+
redirect_to product_path(@product)
|
84
|
+
end
|
85
|
+
```
|
86
|
+
|
87
|
+
Take a look at update action in our example, there's redirect_to method after render_mage. Yes, this code
|
88
|
+
will be executed only if your object is finished wizard, if not - wizard will be rendered, and all the code
|
89
|
+
below render_mage won't be executed, so you don't need to worry about that.
|
90
|
+
|
91
|
+
Also if tou want to show current step in url as a param, you can use show_step option each time you call
|
92
|
+
render_mage method like this:
|
93
|
+
|
94
|
+
```ruby
|
95
|
+
render_mage(@product, show_step: true)
|
96
|
+
```
|
97
|
+
|
98
|
+
## Contributing
|
99
|
+
|
100
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/effect305/mage.
|
101
|
+
|
102
|
+
## ToDo
|
103
|
+
|
104
|
+
I think there's a lot of stuff to do. Here's just examples:
|
105
|
+
* Keep flash messages if showing step
|
106
|
+
* Generator
|
107
|
+
* Tests
|
108
|
+
|
109
|
+
## License
|
110
|
+
|
111
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
112
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "mage"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/lib/mage.rb
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
require 'mage/version'
|
2
|
+
|
3
|
+
module Mage
|
4
|
+
class WizardRendered < Exception
|
5
|
+
end
|
6
|
+
|
7
|
+
class MageStep < ActiveRecord::Base
|
8
|
+
belongs_to :object, polymorphic: true
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
class ActiveRecord::Base
|
13
|
+
def self.has_mage_steps(*steps)
|
14
|
+
@@mage_steps = steps
|
15
|
+
after_save :mage_after_save
|
16
|
+
after_destroy { Mage::MageStep.where(object_id: self.id, object_type: self.model_name.name).try(:take).try(:destroy) }
|
17
|
+
|
18
|
+
define_method :mage_steps do
|
19
|
+
@@mage_steps
|
20
|
+
end
|
21
|
+
|
22
|
+
instance_eval do
|
23
|
+
define_method :mage_step do
|
24
|
+
Mage::MageStep.where(object_id: self.id, object_type: self.model_name.name)
|
25
|
+
.try(:take).try(:step).try(:to_sym) || @@mage_steps.first
|
26
|
+
end
|
27
|
+
|
28
|
+
steps.each_with_index do |step, index|
|
29
|
+
define_method "mage_step_#{step}?" do
|
30
|
+
index <= (@@mage_steps.find_index(mage_step) || @@mage_steps.count)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
define_method :mage_after_save do
|
35
|
+
if mage_step != :done
|
36
|
+
mage_model = Mage::MageStep.where(object_id: id, object_type: model_name.name).try(:take) ||
|
37
|
+
Mage::MageStep.create(object_id: id, object_type: model_name.name, step: mage_step)
|
38
|
+
mage_model.update(step: (@@mage_steps[@@mage_steps.find_index(mage_step) + 1] || :done))
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
private :mage_after_save
|
43
|
+
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
class ActionController::Base
|
49
|
+
rescue_from Mage::WizardRendered, with: :mage_return
|
50
|
+
|
51
|
+
def render_mage(object, options = {})
|
52
|
+
unless object.mage_step == :done
|
53
|
+
if request.method == 'GET'
|
54
|
+
if options[:show_step] && params[:step].blank?
|
55
|
+
mage_redirect(object, options[:show_step])
|
56
|
+
else
|
57
|
+
render "#{params[:controller]}/steps/#{object.mage_step}"
|
58
|
+
end
|
59
|
+
else
|
60
|
+
mage_redirect(object, options[:show_step])
|
61
|
+
end
|
62
|
+
raise Mage::WizardRendered.new
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def mage_redirect(object, show_step = false)
|
67
|
+
path = object.new_record? ? :new_polymorphic_path : :edit_polymorphic_path
|
68
|
+
redirect_to send(path, object, step: (object.mage_step if show_step))
|
69
|
+
end
|
70
|
+
|
71
|
+
def mage_return
|
72
|
+
end
|
73
|
+
|
74
|
+
private :mage_return, :mage_redirect
|
75
|
+
end
|
76
|
+
|
data/lib/mage/version.rb
ADDED
data/mage.gemspec
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'mage/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "mage"
|
8
|
+
spec.version = Mage::VERSION
|
9
|
+
spec.authors = ["Viacheslav Mefodin"]
|
10
|
+
spec.email = ["mefodin.v@gmailcom"]
|
11
|
+
|
12
|
+
spec.summary = %q{Rails wizard model creation}
|
13
|
+
spec.description = %q{This gem allows model to have state, and based on that
|
14
|
+
create wizard with step by step validations}
|
15
|
+
spec.homepage = "https://github.com/effect305/mage"
|
16
|
+
spec.license = "MIT"
|
17
|
+
|
18
|
+
# Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
|
19
|
+
# delete this section to allow pushing this gem to any host.
|
20
|
+
|
21
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
22
|
+
spec.bindir = "exe"
|
23
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
24
|
+
spec.require_paths = ["lib", "db"]
|
25
|
+
|
26
|
+
spec.add_development_dependency "bundler", "~> 1.11"
|
27
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
28
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
29
|
+
end
|
metadata
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mage
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Viacheslav Mefodin
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-04-08 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.11'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.11'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
description: |-
|
56
|
+
This gem allows model to have state, and based on that
|
57
|
+
create wizard with step by step validations
|
58
|
+
email:
|
59
|
+
- mefodin.v@gmailcom
|
60
|
+
executables: []
|
61
|
+
extensions: []
|
62
|
+
extra_rdoc_files: []
|
63
|
+
files:
|
64
|
+
- ".gitignore"
|
65
|
+
- ".rspec"
|
66
|
+
- ".travis.yml"
|
67
|
+
- Gemfile
|
68
|
+
- LICENSE.txt
|
69
|
+
- README.md
|
70
|
+
- Rakefile
|
71
|
+
- bin/console
|
72
|
+
- bin/setup
|
73
|
+
- db/migrate/20160407080915_create_mage_steps.rb
|
74
|
+
- lib/mage.rb
|
75
|
+
- lib/mage/version.rb
|
76
|
+
- mage.gemspec
|
77
|
+
homepage: https://github.com/effect305/mage
|
78
|
+
licenses:
|
79
|
+
- MIT
|
80
|
+
metadata: {}
|
81
|
+
post_install_message:
|
82
|
+
rdoc_options: []
|
83
|
+
require_paths:
|
84
|
+
- lib
|
85
|
+
- db
|
86
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - ">="
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '0'
|
96
|
+
requirements: []
|
97
|
+
rubyforge_project:
|
98
|
+
rubygems_version: 2.4.8
|
99
|
+
signing_key:
|
100
|
+
specification_version: 4
|
101
|
+
summary: Rails wizard model creation
|
102
|
+
test_files: []
|