-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved all code into sane places in lib and split nicely into differen…
…t classes / modules. Forms won't work yet, but single pages will again
- Loading branch information
Showing
40 changed files
with
601 additions
and
229 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,5 @@ test/dummy/db/*.sqlite3 | |
test/dummy/log/*.log | ||
test/dummy/tmp/ | ||
test/dummy/.sass-cache | ||
*.gem | ||
*.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
PATH | ||
remote: . | ||
specs: | ||
mxit-rails (0.0.1) | ||
mxit-rails (0.0.2) | ||
rails (~> 3.2.3) | ||
|
||
GEM | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
module MxitRails | ||
class FormController < Controller | ||
|
||
# DSL handling | ||
#============== | ||
def init_template | ||
# self.class.variables.each do |key, value| | ||
# instance_variable_set("@#{key}", value) | ||
# end | ||
@inputs = inputs | ||
super | ||
end | ||
|
||
# getter method for class' steps and inputs | ||
def steps | ||
self.class.steps | ||
end | ||
def inputs | ||
if @inputs.nil? | ||
@inputs = {} | ||
self.class.inputs.each do |input| | ||
@inputs[input] = params.include?(input) ? params[input] : nil | ||
end | ||
end | ||
@inputs | ||
end | ||
|
||
# Class methods - used for defining macros | ||
#========================================== | ||
class << self | ||
attr_reader :steps | ||
attr_reader :inputs | ||
|
||
# Create a show method that calls the block with the appropriate scope. Allows it to be declared without a def in the controller | ||
def step_executor step, method, &block | ||
# TODO: Don't use a closure? | ||
define_method method do | ||
instance_eval &block | ||
end | ||
end | ||
|
||
def step name, &block | ||
name = name.to_sym | ||
@steps ||= [] | ||
@steps << name | ||
@scope = name | ||
instance_eval &block | ||
@scope = nil | ||
end | ||
|
||
def input input_name, input_label | ||
@inputs ||= [] | ||
@inputs << input_name | ||
super input_name, input_label | ||
end | ||
end | ||
|
||
def current_step | ||
if @current_step.nil? | ||
@current_step = steps.first | ||
if params.include? :step | ||
@current_step = params[:step].to_sym | ||
end | ||
end | ||
@current_step | ||
end | ||
def step_index | ||
steps.each_with_index do |cur, i| | ||
if cur == current_step | ||
return i | ||
end | ||
end | ||
return nil | ||
end | ||
def first_step? | ||
step_index == 0 | ||
end | ||
def last_step? | ||
step_index == steps.length - 1 | ||
end | ||
def next_step | ||
if last_step? | ||
return @proceed | ||
end | ||
steps[step_index + 1] | ||
end | ||
def previous_step | ||
if first_step? | ||
return nil | ||
end | ||
steps[step_index - 1] | ||
end | ||
|
||
def index | ||
if params.include?(:submit) | ||
init_scope previous_step | ||
@form_action = mxit_path(@route, current_step) | ||
# Validate the newest input | ||
if params.include? @input.to_sym | ||
validate! params[@input.to_sym], previous_step | ||
end | ||
instance_variable_set("@#{@input}", params[@input.to_sym]) | ||
submit(previous_step) | ||
|
||
# Validate the whole form if it is complete | ||
# validate! params[@input.to_sym], current_step | ||
# instance_variable_set("@#{@input}", params[@input.to_sym]) | ||
# submit() | ||
end | ||
|
||
init_scope current_step | ||
@form_action = mxit_path(@route, next_step) | ||
|
||
show() | ||
|
||
# Update the back link if there is one | ||
# if @nav_link == :back | ||
# @nav_target = previous_step | ||
# end | ||
|
||
render_template "/#{Rails.application.config.mxit_template_root}/#{route}/#{current_step}" | ||
end | ||
|
||
end | ||
end |
9 changes: 7 additions & 2 deletions
9
app/helpers/mxit_helper.rb → app/helpers/mxit_rails_helper.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<p><%= @_mxit_error_message %></p> | ||
|
Oops, something went wrong.