module FormInput::StepMethods
Localize few step methods.
Additional methods used for multi-step form processing.
Public Class Methods
Initialize new instance.
# File lib/form_input/steps.rb, line 31 def initialize( *args ) super self.seen = last_step( seen, step ) self.step ||= steps.first self.next ||= step self.last ||= step if correct_step? self.step = self.next self.seen = last_step( seen, previous_step( step ) ) end self.last = last_step( step, last ) end
Public Instance Methods
Test if given/current step is accessible.
# File lib/form_input/steps.rb, line 317 def accessible_step?( step = self.step ) not inaccessible_step?( step ) end
Get already accessible steps, including the last accessed step.
# File lib/form_input/steps.rb, line 307 def accessible_steps steps - inaccessible_steps end
Test if given/current step shall be displayed as incorrect.
# File lib/form_input/steps.rb, line 357 def bad_step?( step = self.step ) incomplete_step?( step ) end
Get steps which shall be displayed as incorrect.
# File lib/form_input/steps.rb, line 347 def bad_steps steps.select{ |step| bad_step?( step ) } end
Test if given/current step is one of the complete steps.
# File lib/form_input/steps.rb, line 332 def complete_step?( step = self.step ) finished_step?( step ) and correct_step?( step ) end
Get correct finished steps. Includes finished steps without parameters.
# File lib/form_input/steps.rb, line 322 def complete_steps finished_steps.select{ |step| correct_step?( step ) } end
Test if the current/given step has all data filled in correctly. Considered true for steps without parameters.
# File lib/form_input/steps.rb, line 250 def correct_step?( step = self.step ) valid?( step_params( step ) ) end
Get steps which have all data filled in correctly. Excludes steps without parameters.
# File lib/form_input/steps.rb, line 235 def correct_steps filter_steps{ |params| valid?( params ) } end
Get the parameters relevant for the current step.
# File lib/form_input/steps.rb, line 58 def current_params tagged_params( step ) end
Test if given/current step has all parameters disabled. Considered false for steps without parameters.
# File lib/form_input/steps.rb, line 276 def disabled_step?( step = self.step ) not enabled_step?( step ) end
Get steps with all parameters disabled. Excludes steps without parameters.
# File lib/form_input/steps.rb, line 265 def disabled_steps filter_steps{ |params| params.all?{ |p| p.disabled? } } end
Test if given/current step has some parameters enabled. Considered true for steps without parameters.
# File lib/form_input/steps.rb, line 270 def enabled_step?( step = self.step ) params = step_params( step ) params.empty? or params.any?{ |p| p.enabled? } end
Get steps with some parameters enabled. Excludes steps without parameters.
# File lib/form_input/steps.rb, line 260 def enabled_steps filter_steps{ |params| params.any?{ |p| p.enabled? } } end
Test if the current/given step has no parameters defined.
# File lib/form_input/steps.rb, line 166 def extra_step?( step = self.step ) step_params( step ).empty? end
Get steps with no parameters defined.
# File lib/form_input/steps.rb, line 176 def extra_steps steps.select{ |step| extra_step?( step ) } end
Test if given/current step has some data filled in. Considered true for steps without parameters.
# File lib/form_input/steps.rb, line 224 def filled_step?( step = self.step ) params = step_params( step ) params.empty? or params.any?{ |p| p.filled? } end
Get steps which have some data filled in. Excludes steps without parameters.
# File lib/form_input/steps.rb, line 214 def filled_steps filter_steps{ |params| params.any?{ |p| p.filled? } } end
Filter steps by testing their corresponding parameters with given block. Excludes steps without parameters.
# File lib/form_input/steps.rb, line 186 def filter_steps steps.select do |step| params = step_params( step ) yield params unless params.empty? end end
Test if given/current step was visited or skipped over before.
# File lib/form_input/steps.rb, line 297 def finished_step?( step = self.step ) not unfinished_step?( step ) end
Get finished steps, those we have visited or skipped over before.
# File lib/form_input/steps.rb, line 286 def finished_steps steps - unfinished_steps end
Get first step, or first step among given list of steps, if any.
# File lib/form_input/steps.rb, line 106 def first_step( *args ) if args.empty? steps.first else args.flatten.compact.min_by{ |x| step_index( x ) } end end
Test if given/current step is the first step.
# File lib/form_input/steps.rb, line 124 def first_step?( step = self.step ) step == first_step end
Get hash mapping defined steps to their names. Note that this is never localized. See step_names
instead.
# File lib/form_input/steps.rb, line 69 def form_steps self.class.form_steps end
Test if given/current step shall be displayed as correct.
# File lib/form_input/steps.rb, line 352 def good_step?( step = self.step ) complete_step?( step ) and filled_step?( step ) and regular_step?( step ) end
Get steps which shall be displayed as correct.
# File lib/form_input/steps.rb, line 342 def good_steps steps.select{ |step| good_step?( step ) } end
Test if given/current step is inaccessible.
# File lib/form_input/steps.rb, line 312 def inaccessible_step?( step = self.step ) step_index( step ) > step_index( last ) end
Get yet inaccessible steps, excluding the last accessed step.
# File lib/form_input/steps.rb, line 302 def inaccessible_steps next_steps( last ) end
Test if given/current step is one of the incomplete steps.
# File lib/form_input/steps.rb, line 337 def incomplete_step?( step = self.step ) finished_step?( step ) and incorrect_step?( step ) end
Get incorrect finished steps. Excludes steps without parameters.
# File lib/form_input/steps.rb, line 327 def incomplete_steps finished_steps.select{ |step| incorrect_step?( step ) } end
Get first step with invalid data, or nil if there is none.
# File lib/form_input/steps.rb, line 245 def incorrect_step incorrect_steps.first end
Test if the current/given step has some invalid data filled in. Considered false for steps without parameters.
# File lib/form_input/steps.rb, line 255 def incorrect_step?( step = self.step ) invalid?( step_params( step ) ) end
Get steps which have some invalid data filled in. Excludes steps without parameters.
# File lib/form_input/steps.rb, line 240 def incorrect_steps filter_steps{ |params| invalid?( params ) } end
Get last step, or last step among given list of steps, if any.
# File lib/form_input/steps.rb, line 115 def last_step( *args ) if args.empty? steps.last else args.flatten.compact.max_by{ |x| step_index( x ) } end end
Test if given/current step is the last step.
# File lib/form_input/steps.rb, line 129 def last_step?( step = self.step ) step == last_step end
Get the next step, or nil if there is none.
# File lib/form_input/steps.rb, line 146 def next_step( step = self.step ) next_steps( step ).first end
Get name of the next step, if any.
# File lib/form_input/steps.rb, line 156 def next_step_name step_name( next_step ) end
Get steps after given/current step.
# File lib/form_input/steps.rb, line 140 def next_steps( step = self.step ) index = steps.index( step ) || -1 steps[ index + 1 .. -1 ] end
Test if given/current step has no required parameters. Considered true for steps without parameters.
# File lib/form_input/steps.rb, line 209 def optional_step?( step = self.step ) not required_step?( step ) end
Get steps which have no required parameters. Excludes steps without parameters.
# File lib/form_input/steps.rb, line 199 def optional_steps filter_steps{ |params| params.none?{ |p| p.required? } } end
Get the parameters irrelevant for the current step.
# File lib/form_input/steps.rb, line 63 def other_params untagged_params( step ) end
Get the previous step, or nil if there is none.
# File lib/form_input/steps.rb, line 151 def previous_step( step = self.step ) previous_steps( step ).last end
Get name of the previous step, if any.
# File lib/form_input/steps.rb, line 161 def previous_step_name step_name( previous_step ) end
Get steps before given/current step.
# File lib/form_input/steps.rb, line 134 def previous_steps( step = self.step ) index = steps.index( step ) || 0 steps.first( index ) end
Test if the current/given step has some parameters defined.
# File lib/form_input/steps.rb, line 171 def regular_step?( step = self.step ) not extra_step?( step ) end
Get steps with some parameters defined.
# File lib/form_input/steps.rb, line 181 def regular_steps steps.select{ |step| regular_step?( step ) } end
Test if given/current has some required parameters. Considered false for steps without parameters.
# File lib/form_input/steps.rb, line 204 def required_step?( step = self.step ) step_params( step ).any?{ |p| p.required? } end
Get steps which have required parameters. Excludes steps without parameters.
# File lib/form_input/steps.rb, line 194 def required_steps filter_steps{ |params| params.any?{ |p| p.required? } } end
Test if current step is after given step.
# File lib/form_input/steps.rb, line 101 def step_after?( step ) step_index > step_index( step ) end
Test if current step is before given step.
# File lib/form_input/steps.rb, line 96 def step_before?( step ) step_index < step_index( step ) end
Get index of given/current step among all steps.
# File lib/form_input/steps.rb, line 91 def step_index( step = self.step ) steps.index( step ) or fail( ArgumentError, "invalid step name #{step}" ) end
Get name of current or given step, if any.
# File lib/form_input/r18n.rb, line 91 def step_name( step = self.step ) name = raw_step_name( step ) name = ( ft.steps[ step ] | name ).to_s if r18n and name name end
Get hash of steps along with their names.
# File lib/form_input/r18n.rb, line 98 def step_names hash = raw_step_names hash = Hash[ hash.map{ |k,v| [ k, ( ft.steps[ k ] | v ).to_s ] } ] if r18n hash end
Get parameters relevant for given step.
# File lib/form_input/steps.rb, line 52 def step_params( step ) fail( ArgumentError, "invalid step name #{step}" ) unless form_steps.key?( step ) tagged_params( step ) end
Get allowed form steps as list of symbols.
# File lib/form_input/steps.rb, line 74 def steps form_steps.keys end
Test if given/current step has no data filled in. Considered false for steps without parameters.
# File lib/form_input/steps.rb, line 230 def unfilled_step?( step = self.step ) not filled_step?( step ) end
Get steps which have no data filled in. Excludes steps without parameters.
# File lib/form_input/steps.rb, line 219 def unfilled_steps filter_steps{ |params| params.none?{ |p| p.filled? } } end
Test if given/current step was not yet visited or was visited for the first time.
# File lib/form_input/steps.rb, line 291 def unfinished_step?( step = self.step ) index = seen ? step_index( seen ) : -1 step_index( step ) > index end
Get unfinished steps, those we have not yet visited or visited for the first time.
# File lib/form_input/steps.rb, line 281 def unfinished_steps next_steps( seen ) end
Make all steps instantly available. Returns self for chaining.
# File lib/form_input/steps.rb, line 46 def unlock_steps self.last = self.seen = steps.last self end