Permalink
Browse files

adds paperclip support

  • Loading branch information...
1 parent 9a39508 commit 709ab9ff56d9be495dae26c7ca16fc0004df29ac @jeffp committed Sep 10, 2009
View
@@ -40,6 +40,13 @@ require 'spec/rake/spectask'
namespace :spec do
desc "Run all specs"
task :all=>[:macro, :gen, :scaffold, :callback, :callback2, :callback3, :persist, :sandbox, :session]
+ desc "Test the AvatarController"
+ Spec::Rake::SpecTask.new(:avatar) do |t|
+ t.spec_files = FileList['spec/integrations/avatar_spec.rb']
+ t.libs << 'lib' << 'spec' << 'spec/integrations'
+ t.spec_opts = ['--options', 'spec/spec.opts']
+ t.rcov = false
+ end
desc "Test the MacroController"
Spec::Rake::SpecTask.new(:macro) do |t|
t.spec_files = FileList['spec/integrations/macro_spec.rb']
@@ -0,0 +1,9 @@
+require 'wizardly'
+class AvatarSessionController < ApplicationController
+ #tests paperclip
+ act_wizardly_for :user_avatar, :form_data=>:session, :persist_model=>:per_page,
+ :completed=>{:controller=>:main, :action=>:finished},
+ :canceled=>{:controller=>:main, :action=>:canceled}
+
+
+end
@@ -13,8 +13,7 @@ def finish
@wizard = wizard_config
@title = 'Finish'
@description = ''
- h = (self.wizard_form_data||{}).merge(params[:user] || {})
- @user = build_wizard_model(h)
+ _build_wizard_model
if request.post? && callback_performs_action?(:_on_post_finish_form)
raise CallbackError, "render or redirect not allowed in :on_post(:finish) callback", caller
end
@@ -37,7 +36,7 @@ def finish
callback_performs_action?(:_on_finish_form_finish)
complete_wizard unless @do_not_complete
ensure
- self.wizard_form_data = h.merge(@user.attributes) if (@user && !@wizard_completed_flag)
+ _preserve_wizard_model
end
end
@@ -49,8 +48,7 @@ def init
@wizard = wizard_config
@title = 'Init'
@description = ''
- h = (self.wizard_form_data||{}).merge(params[:user] || {})
- @user = build_wizard_model(h)
+ _build_wizard_model
if request.post? && callback_performs_action?(:_on_post_init_form)
raise CallbackError, "render or redirect not allowed in :on_post(:init) callback", caller
end
@@ -75,11 +73,10 @@ def init
complete_wizard unless @do_not_complete
return
end
-
return if callback_performs_action?(:_on_init_form_next)
redirect_to :action=>:second
ensure
- self.wizard_form_data = h.merge(@user.attributes) if (@user && !@wizard_completed_flag)
+ _preserve_wizard_model
end
end
@@ -91,8 +88,7 @@ def second
@wizard = wizard_config
@title = 'Second'
@description = ''
- h = (self.wizard_form_data||{}).merge(params[:user] || {})
- @user = build_wizard_model(h)
+ _build_wizard_model
if request.post? && callback_performs_action?(:_on_post_second_form)
raise CallbackError, "render or redirect not allowed in :on_post(:second) callback", caller
end
@@ -117,11 +113,10 @@ def second
complete_wizard unless @do_not_complete
return
end
-
return if callback_performs_action?(:_on_second_form_next)
redirect_to :action=>:finish
ensure
- self.wizard_form_data = h.merge(@user.attributes) if (@user && !@wizard_completed_flag)
+ _preserve_wizard_model
end
end
@@ -217,18 +212,36 @@ def complete_wizard(redirect = nil)
_on_wizard_finish
redirect_to('/main/finished') unless self.performed?
end
- def build_wizard_model(params)
- if (wizard_config.persist_model_per_page? && (model_id = params['id']))
- begin
- _model = User.find(model_id)
- _model.attributes = params
- return _model
- rescue
+ def _build_wizard_model
+ if self.wizard_config.persist_model_per_page?
+ h = self.wizard_form_data
+ if (h && model_id = h['id'])
+ _model = User.find(model_id)
+ _model.attributes = params[:user]||{}
+ @user = _model
+ return
end
+ @user = User.new(params[:user])
+ else # persist data in session or flash
+ h = (self.wizard_form_data||{}).merge(params[:user] || {})
+ @user = User.new(h)
end
- User.new(params)
end
- hide_action :build_wizard_model, :save_wizard_model!
+ def _preserve_wizard_model
+ return unless (@user && !@wizard_completed_flag)
+ if self.wizard_config.persist_model_per_page?
+ @user.save_without_validation!
+ if request.get?
+ @user.errors.clear
+ else
+ @user.reject_non_validation_group_errors
+ end
+ self.wizard_form_data = {'id'=>@user.id}
+ else
+ self.wizard_form_data = @user.attributes
+ end
+ end
+ hide_action :_build_wizard_model, :_preserve_wizard_model
def initial_referer
session[:generated_irk]
@@ -339,21 +352,21 @@ def self.on_get(*args, &block)
def self.on_errors(*args, &block)
self._define_action_callback_macro('on_errors', '_on_invalid_%s_form', *args, &block)
end
+ def self.on_finish(*args, &block)
+ self._define_action_callback_macro('on_finish', '_on_%s_form_finish', *args, &block)
+ end
def self.on_skip(*args, &block)
self._define_action_callback_macro('on_skip', '_on_%s_form_skip', *args, &block)
end
+ def self.on_next(*args, &block)
+ self._define_action_callback_macro('on_next', '_on_%s_form_next', *args, &block)
+ end
def self.on_back(*args, &block)
self._define_action_callback_macro('on_back', '_on_%s_form_back', *args, &block)
end
def self.on_cancel(*args, &block)
self._define_action_callback_macro('on_cancel', '_on_%s_form_cancel', *args, &block)
end
- def self.on_finish(*args, &block)
- self._define_action_callback_macro('on_finish', '_on_%s_form_finish', *args, &block)
- end
- def self.on_next(*args, &block)
- self._define_action_callback_macro('on_next', '_on_%s_form_next', *args, &block)
- end
def self._define_action_callback_macro(macro_first, macro_last, *args, &block)
return if args.empty?
all_forms = [:init, :second, :finish]
@@ -2,6 +2,7 @@ class MainController < ApplicationController
def index
@links = {
:macro=>'/macro',
+ :avatar_session=>'/avatar_session',
:generated=>'/generated',
:scaffold_test=>'/scaffold_test',
:callbacks=>'/callbacks',
@@ -0,0 +1,30 @@
+module AvatarSessionHelper
+
+ def wizardly_submit
+ @@wizardly_submit ||= {}
+ step_id = "#{controller_name}_#{@step}".to_sym
+ unless @@wizardly_submit[step_id]
+ buttons = @wizard.pages[@step].buttons
+ @@wizardly_submit[step_id] = buttons.inject(StringIO.new) do |io, button|
+ io << submit_tag(button.name, :name=>button.id.to_s)
+ end.string
+ end
+ @@wizardly_submit[step_id]
+ end
+
+ def wizardly_image_submit(asset_dir = nil, opts = {})
+ @@wizardly_image_submit ||={}
+ step_id = "#{controller_name}_#{@step}".to_sym
+ unless @@wizardly_image_submit[step_id]
+ asset_dir = asset_dir ? "#{asset_dir}/".squeeze("/").sub(/^\//,'') : "wizardly/"
+ buttons = @wizard.pages[@step].buttons
+ @@wizardly_image_submit[step_id] = buttons.inject(StringIO.new) do |io, button|
+ opts[:value] = button.name
+ opts[:name] = button.id.to_s
+ io << image_submit_tag("#{asset_dir}#{button.id}.png", opts)
+ end.string
+ end
+ @@wizardly_image_submit[step_id]
+ end
+
+end
View
@@ -0,0 +1,10 @@
+class UserAvatar < User
+
+ has_attached_file :avatar
+ validates_attachment_presence :avatar
+
+ validation_group :init, :fields=>[:first_name, :last_name]
+ validation_group :second, :fields=>[:age, :gender, :programmer, :status, :avatar]
+ validation_group :finish, :fields=>[:username, :password, :password_confirmation]
+
+end
@@ -0,0 +1,31 @@
+<h1>Finish</h1>
+
+
+<%= "<p style=\"color: green\">#{flash[:notice]}<p>" if flash[:notice] %>
+
+<% form_for :user_avatar, :url=>{:action=>:finish} do |f| %>
+ <%= f.error_messages %>
+
+
+ <p>
+ <%= f.label :username %><br />
+ <%= f.text_field :username %>
+ </p>
+
+ <p>
+ <%= f.label :password %><br />
+ <%= f.password_field :password %>
+ </p>
+
+ <p>
+ <%= f.label :password_confirmation %><br />
+ <%= f.password_field :password_confirmation %>
+ </p>
+ <p>
+ <%= wizardly_submit %>
+ </p>
+<% end %>
+
+<script type='text/javascript'>
+ document.getElementById('user_avatar_username').focus()
+</script>
@@ -0,0 +1,26 @@
+<h1>Init</h1>
+
+
+<%= "<p style=\"color: green\">#{flash[:notice]}<p>" if flash[:notice] %>
+
+<% form_for :user_avatar, :url=>{:action=>:init} do |f| %>
+ <%= f.error_messages %>
+
+
+ <p>
+ <%= f.label :first_name %><br />
+ <%= f.text_field :first_name %>
+ </p>
+
+ <p>
+ <%= f.label :last_name %><br />
+ <%= f.text_field :last_name %>
+ </p>
+ <p>
+ <%= wizardly_submit %>
+ </p>
+<% end %>
+
+<script type='text/javascript'>
+ document.getElementById('user_avatar_first_name').focus()
+</script>
@@ -0,0 +1,41 @@
+<h1>Second</h1>
+
+
+<%= "<p style=\"color: green\">#{flash[:notice]}<p>" if flash[:notice] %>
+
+<% form_for :user_avatar, :url=>{:action=>:second}, :html=>{:multipart=>true} do |f| %>
+ <%= f.error_messages %>
+
+
+ <p>
+ <%= f.label :age %><br />
+ <%= f.text_field :age %>
+ </p>
+
+ <p>
+ <%= f.label :gender %><br />
+ <%= f.text_field :gender %>
+ </p>
+
+ <p>
+ <%= f.label :programmer %><br />
+ <%= f.check_box :programmer %>
+ </p>
+
+ <p>
+ <%= f.label :status %><br />
+ <%= f.text_field :status %>
+ </p>
+
+ <p>
+ <%= f.label :avatar %><br />
+ <%= f.file_field :avatar %>
+ </p>
+ <p>
+ <%= wizardly_submit %>
+ </p>
+<% end %>
+
+<script type='text/javascript'>
+ document.getElementById('user_avatar_age').focus()
+</script>
@@ -0,0 +1,15 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
+<head>
+ <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
+ <title>AvatarSessionController: <%= controller.action_name %></title>
+ <%= stylesheet_link_tag 'scaffold' %>
+</head>
+<body>
+
+<%= yield %>
+
+</body>
+</html>
View
@@ -20,6 +20,7 @@
# config.gem "sqlite3-ruby", :lib => "sqlite3"
# config.gem "aws-s3", :lib => "aws/s3"
#config.gem "#{Rails::GemDependency.new("enumerated_attribute").installed? ? "" : "jeffp-"}enumerated_attribute"
+ config.gem "thoughtbot-paperclip", :lib=>"paperclip", :source=>"http://gems.github.com"
# Only load the plugins named here, in the order given (default is alphabetical).
# :all can be used as a placeholder for all plugins not explicitly named
View
Binary file not shown.
@@ -10,6 +10,10 @@ def self.up
t.boolean :programmer
t.string :status
+ t.string :avatar_file_name
+ t.string :avatar_content_type
+ t.integer :avatar_file_size
+
t.timestamps
end
end
View
@@ -30,6 +30,9 @@
t.string "gender"
t.boolean "programmer"
t.string "status"
+ t.string "avatar_file_name"
+ t.string "avatar_content_type"
+ t.integer "avatar_file_size"
t.datetime "created_at"
t.datetime "updated_at"
end
View
Binary file not shown.
View
@@ -77,12 +77,17 @@ def disable_validation_group
# jeffp: delete fields
@current_validation_fields = nil
end
+
+ def reject_non_validation_group_errors
+ return unless validation_group_enabled?
+ self.errors.remove_on(@current_validation_fields)
+ end
# jeffp: optimizer for someone writing custom :validate method -- no need
# to validate fields outside the current validation group note: could also
# use in validation modules to improve performance
def should_validate?(attribute)
- !self.validation_group_enabled? || (@current_validation_fields && @current_validation_fields.include?(attribute.to_sym))
+ !self.validation_group_enabled? || (@current_validation_fields && @current_validation_fields.include?(attribute.to_sym))
end
def validation_group_enabled?
@@ -105,6 +110,12 @@ def add_with_validation_group(attribute,
add_error = @base.respond_to?(:should_validate?) ? @base.should_validate?(attribute.to_sym) : true
add_without_validation_group(attribute, msg, *args, &block) if add_error
end
+
+ def remove_on(attributes)
+ return unless attributes
+ attributes = [attributes] unless attributes.is_a?(Array)
+ @errors.reject!{|k,v| !attributes.include?(k.to_sym)}
+ end
def self.included(base) #:nodoc:
base.class_eval do
@@ -23,7 +23,7 @@ def initialize(controller, opts) #completed_redirect = nil, canceled_redirect =
@include_cancel_button = opts.key?(:cancel) ? opts[:cancel] : true
@guard_entry = opts.key?(:guard) ? opts[:guard] : true
@password_fields = opts[:mask_fields] || opts[:mask_passwords] || [:password, :password_confirmation]
- @persist_model = opts[:persist_model] || :once
+ @persist_model = opts[:persist_model] || :per_page
@form_data = opts[:form_data] || :session
raise(ArgumentError, ":persist_model option must be one of :once or :per_page", caller) unless [:once, :per_page].include?(@persist_model)
raise(ArgumentError, ":form_data option must be one of :sandbox or :session", caller) unless [:sandbox, :session].include?(@form_data)
Oops, something went wrong.

0 comments on commit 709ab9f

Please sign in to comment.