form_journey 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b5d63c4a9037f39e322a214c36318874c03ea65e
4
+ data.tar.gz: f99720d342be944bad852614907162ccea6e39b4
5
+ SHA512:
6
+ metadata.gz: 404e37955b49b888895130eb3dbf7535480fac454289004a9db923b63d449f786bd287c5600fb9d8d8a3538c77a065d1904b45cc96a5d52a0912114f5e453f92
7
+ data.tar.gz: 91ef6436d06ddb5ab21e180e5102489752bf7f48d33090390d1a32676c3b29f9d742550d8a297f2697724dae5c5ccfed1044747bdb5e910e3e3f8c98dff1ecff
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2014 YOURNAME
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/Rakefile ADDED
@@ -0,0 +1,17 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'FormJourney'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,7 @@
1
+ module FormJourney
2
+ end
3
+
4
+ require_relative './form_journey/routes'
5
+ require_relative './form_journey/controller'
6
+ require_relative './form_journey/parameters'
7
+ require_relative './form_journey/uses_single_model'
@@ -0,0 +1,131 @@
1
+ module FormJourney
2
+ module Controller
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ class_attribute :_steps
7
+ self._steps = []
8
+
9
+ before_action :before_step_action
10
+ helper_method :step_path
11
+ helper_method :current_step_path
12
+ helper_method :next_step_path
13
+ helper_method :previous_step_path
14
+ helper_method :current_step_number
15
+ helper_method :total_steps_number
16
+
17
+ [:current_step, :previous_step, :next_step].each do |key|
18
+ class_eval do
19
+ define_method "#{key}_path".to_sym do
20
+ step_path(self.send(key))
21
+ end
22
+ end
23
+ end
24
+ end
25
+
26
+ def default_step
27
+ redirect_to steps.any? ? step_path(steps.first) : '/'
28
+ end
29
+
30
+ def steps
31
+ instance_steps
32
+ end
33
+
34
+ def instance_steps
35
+ @instance_steps ||= self.class._steps.dup
36
+ end
37
+
38
+ def update_steps(*new_steps)
39
+ @instance_steps = new_steps
40
+ end
41
+
42
+ def add_step(new_step, before: nil)
43
+ if before
44
+ index = instance_steps.index(before)
45
+ return nil unless index
46
+ instance_steps.insert(index, new_step)
47
+ else
48
+ instance_steps << new_step
49
+ end
50
+ end
51
+
52
+ def remove_step(step_name)
53
+ instance_steps.delete(step_name)
54
+ end
55
+
56
+ def step_path(step)
57
+ url_for(controller: params[:controller], action: step)
58
+ end
59
+
60
+ def current_step
61
+ steps.include?(params[:action].to_sym) and params[:action].to_sym or steps.first
62
+ end
63
+
64
+ def next_step
65
+ steps[current_step_index + 1] || steps.last
66
+ end
67
+
68
+ def previous_step
69
+ steps[current_step_index - 1] || steps.first
70
+ end
71
+
72
+ def current_step_number
73
+ current_step_index + 1
74
+ end
75
+
76
+ def total_steps_number
77
+ steps.count
78
+ end
79
+
80
+ def when_post
81
+ return yield if request.post?
82
+ end
83
+
84
+ def when_patch
85
+ return yield if request.patch?
86
+ end
87
+
88
+ def when_post_or_patch
89
+ return yield if request.patch? || request.post?
90
+ end
91
+
92
+ def when_delete
93
+ return yield if request.delete?
94
+ end
95
+
96
+ def when_get
97
+ return yield if request.get?
98
+ end
99
+
100
+ def journey_params
101
+ @journey_params ||= FormJourney::Parameters.new(params, journey_session)
102
+ end
103
+
104
+ private
105
+
106
+ def previous_steps
107
+ Array(steps.clone).tap do |steps|
108
+ steps.slice!(current_step_index + 1, steps.length)
109
+ end
110
+ end
111
+
112
+ def current_step_index
113
+ steps.index(current_step)
114
+ end
115
+
116
+ def before_step_action
117
+ method = "before_#{current_step}"
118
+ return self.send(method) if self.respond_to?(method, true)
119
+ end
120
+
121
+ def journey_session
122
+ (session["#{params[:controller]}_journey_session".to_sym] ||= {})
123
+ end
124
+
125
+ module ClassMethods
126
+ def steps(*steps)
127
+ self._steps.concat(steps)
128
+ end
129
+ end
130
+ end
131
+ end
@@ -0,0 +1,80 @@
1
+ module FormJourney
2
+ class Parameters < HashWithIndifferentAccess
3
+ def initialize(attributes = nil, session)
4
+ @session = (session || {})
5
+ @session.deep_merge!(attributes.deep_symbolize_keys) if attributes
6
+ super(@session)
7
+ end
8
+
9
+ def clear!
10
+ replace({})
11
+ @session.clear
12
+ end
13
+
14
+ def []=(key, value)
15
+ super
16
+ update_session
17
+ end
18
+
19
+ def get(*keys)
20
+ params = to_hash
21
+ while !keys.empty?
22
+ return nil if params.nil?
23
+ params = params[keys.shift.to_s]
24
+ end
25
+ params
26
+ end
27
+
28
+ def del(*keys)
29
+ parent_param = self
30
+ while !keys.empty?
31
+ return nil if parent_param.nil?
32
+
33
+ if keys.length == 1
34
+ del_param = parent_param.try(:delete, keys.shift)
35
+ update_session
36
+ return del_param
37
+ end
38
+ parent_param = parent_param[keys.shift]
39
+ end
40
+ end
41
+
42
+ def set(*keys, value:)
43
+ parent_param = self
44
+ while !keys.empty?
45
+ return nil if parent_param.nil?
46
+
47
+ if keys.length == 1
48
+ parent_param[keys.shift] = value
49
+ update_session
50
+ return self
51
+ end
52
+ parent_param = parent_param[keys.shift]
53
+ end
54
+ end
55
+
56
+ def add_to_array(*path, value:, unique: false)
57
+ array = Array(get(*path))
58
+ array << value
59
+ array.uniq! if unique
60
+ set(*path, value: array)
61
+ end
62
+
63
+ def remove_from_array(*path, value:)
64
+ array = Array(get(*path))
65
+ array.select! { |v| v != value }
66
+ set(*path, value: array)
67
+ end
68
+
69
+ def require(key)
70
+ ActionController::Parameters.new(to_hash).require(key)
71
+ end
72
+
73
+ private
74
+
75
+ def update_session
76
+ @session.clear
77
+ @session.merge!(deep_symbolize_keys)
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,14 @@
1
+ require 'action_dispatch'
2
+
3
+ module ActionDispatch::Routing
4
+ class Mapper
5
+ def mount_journey(path, controller)
6
+ resource "#{path}", only: [] do
7
+ get "/", controller: controller, action: :default_step
8
+ get "/:id/edit", controller: controller, action: 'edit', as: 'edit'
9
+ yield if block_given?
10
+ match "/:action", controller: controller, action: /[a-z0-9_]+/, via: [:get, :post, :patch, :delete], as: 'step'
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,89 @@
1
+ module FormJourney
2
+ module UsesSingleModel
3
+ extend ActiveSupport::Concern
4
+ included do
5
+ class_attribute :_model_class
6
+ class_attribute :_model_scope
7
+ class_attribute :_params_method
8
+ end
9
+
10
+ def edit
11
+ journey_params.clear!
12
+ journey_params.set(:_model_object_id, value: params[:id])
13
+ redirect_to step_path(steps.first)
14
+ end
15
+
16
+ def editing?
17
+ model_object_id.present?
18
+ end
19
+
20
+ def model_object_id
21
+ journey_params.get(:_model_object_id)
22
+ end
23
+
24
+ private
25
+
26
+ def model_object
27
+ @model_object ||= begin
28
+ if editing?
29
+ scoped_class.find(model_object_id).tap do |obj|
30
+ obj.assign_attributes(model_params)
31
+ end
32
+ else
33
+ scoped_class.new(model_params)
34
+ end
35
+ end
36
+ end
37
+
38
+ def scoped_class
39
+ model_class = self.class._model_class
40
+ scope = self.class._model_scope
41
+ return model_class unless scope
42
+ if scope.respond_to?(:call)
43
+ instance_exec(model_class, &scope)
44
+ else
45
+ messages = Array(scope)
46
+ messages.reduce(model_class) do |chained_scope, message|
47
+ chained_scope.send(message.to_sym)
48
+ end
49
+ end
50
+ end
51
+
52
+ def model_params
53
+ if self.class._params_method.respond_to?(:call)
54
+ self.class._params_method.call
55
+ else
56
+ self.send(self.class._params_method.to_sym)
57
+ end
58
+ end
59
+
60
+ module ClassMethods
61
+ def params_method(params_method)
62
+ self._params_method = params_method
63
+ end
64
+
65
+ def model_scope(*scope)
66
+ if scope.size == 1
67
+ self._model_scope = scope.first
68
+ else
69
+ self._model_scope = scope
70
+ end
71
+ end
72
+
73
+ def model_class(clasz)
74
+ clasz = clasz.is_a?(String) ? self.const_get(clasz) : clasz
75
+ self._model_class = clasz
76
+ hyphenated_class_name = clasz.to_s.gsub(/::/, '')
77
+ .gsub(/(?<=[^\b])([A-Z])/, '_\1')
78
+ .downcase
79
+ class_eval do
80
+ define_method(hyphenated_class_name.to_sym) do
81
+ model_object
82
+ end
83
+ end
84
+ self.send(:helper_method, hyphenated_class_name.to_sym)
85
+ end
86
+ end
87
+ end
88
+ end
89
+
@@ -0,0 +1,3 @@
1
+ module FormJourney
2
+ VERSION = "0.3.0"
3
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :form_journey do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: form_journey
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.0
5
+ platform: ruby
6
+ authors:
7
+ - Driftrock
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-05-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 4.1.6
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 4.1.6
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: simplecov
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Create multi page form using Rails
70
+ email:
71
+ - dev@driftrock.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - MIT-LICENSE
77
+ - Rakefile
78
+ - lib/form_journey.rb
79
+ - lib/form_journey/controller.rb
80
+ - lib/form_journey/parameters.rb
81
+ - lib/form_journey/routes.rb
82
+ - lib/form_journey/uses_single_model.rb
83
+ - lib/form_journey/version.rb
84
+ - lib/tasks/form_journey_tasks.rake
85
+ homepage: http://www.driftrock.com
86
+ licenses:
87
+ - MIT
88
+ metadata: {}
89
+ post_install_message:
90
+ rdoc_options: []
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ requirements: []
104
+ rubyforge_project:
105
+ rubygems_version: 2.2.2
106
+ signing_key:
107
+ specification_version: 4
108
+ summary: Rails form journey
109
+ test_files: []