Skip to content

Commit

Permalink
Moved all code into sane places in lib and split nicely into differen…
Browse files Browse the repository at this point in the history
…t classes / modules. Forms won't work yet, but single pages will again
  • Loading branch information
linsen committed Sep 12, 2012
1 parent 1e11135 commit a555051
Show file tree
Hide file tree
Showing 40 changed files with 601 additions and 229 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ test/dummy/db/*.sqlite3
test/dummy/log/*.log
test/dummy/tmp/
test/dummy/.sass-cache
*.gem
*.DS_Store
2 changes: 1 addition & 1 deletion Gemfile.lock
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
Expand Down
202 changes: 0 additions & 202 deletions app/controllers/mxit_controller.rb

This file was deleted.

125 changes: 125 additions & 0 deletions app/controllers/mxit_rails/form_controller.rb
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
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
module MxitHelper
module MxitRailsHelper

def mxit_path route
MxitRails::Router.url route
end

def mxit_link route, label
output = "<a href=\"/#{route.to_s}\">#{label}</a>".html_safe
path = mxit_path route
output = "<a href=\"#{path}\">#{label}</a>".html_safe
end

def mxit_style *names
Expand Down
21 changes: 11 additions & 10 deletions app/views/layouts/mxit.html.erb
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
<!DOCTYPE html>
<html>
<head>
<title><%= @title %></title>
<title><%= @_mxit.title %></title>
</head>
<body>

<h1><%= @title %></h1>
<% if @nav_link %>
<p><%= mxit_link @nav_target, @nav_link.to_s.capitalize %></p>
<h1><%= @_mxit.title %></h1>
<% if @_mxit.nav_link %>
<p><%= mxit_link @_mxit.nav_target, @_mxit.nav_link.to_s.capitalize %></p>
<% end %>
<%= yield %>
<% if @input %>
<p><b>&gt; <%= @input_label %></b></p>
<form method="POST" action="/<%= @route %>">
<input type="text" name="<%= @input %>" />
<% if @_mxit.input %>
<p><b>&gt; <%= @_mxit.input_label %></b></p>
<form method="POST" action="<%= @_mxit.url %>">
<input type="text" name="<%= @_mxit.input %>" />
<input type="submit" name="submit" value="Proceed" />
</form>
<% elsif @proceed %>
<p><b>&gt; <%= mxit_link @proceed, @proceed_label.to_s.capitalize %></b></p>

<% elsif @_mxit.proceed %>
<p><b>&gt; <%= mxit_link @_mxit.proceed, @_mxit.proceed_label.to_s.capitalize %></b></p>
<% end %>

</body>
Expand Down
2 changes: 2 additions & 0 deletions app/views/mxit_rails/error.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<p><%= @_mxit_error_message %></p>

Loading

0 comments on commit a555051

Please sign in to comment.