Skip to content

Instantly share code, notes, and snippets.

@neaf
Created December 20, 2009 14:21
Show Gist options
  • Select an option

  • Save neaf/260501 to your computer and use it in GitHub Desktop.

Select an option

Save neaf/260501 to your computer and use it in GitHub Desktop.
require "bureaucrat/quickfields"
module DesignDetailsFields
def self.included(base)
base.class_eval do
text :design_description, :label => "Description"
end
end
end
module MarkupDetailsFields
def self.included(base)
base.class_eval do
integer :markup_page_count, :label => "Page/state/overlay count"
end
end
end
module FlashDetailsFields
def self.included(base)
base.class_eval do
type_choices = {
:website => "Website",
:application => "Application",
:game => "Game",
:widget => "Widget"
}
choice :flash_type, type_choices, :label => "Type"
end
end
end
module CMSDetailsFields
def self.included(base)
base.class_eval do
string :cms_plugins, :label => "Wordpress plugins support"
end
end
end
class ProjectForm < Bureaucrat::Forms::Form
extend Bureaucrat::Quickfields
string :name, :max_length => 250
integer :budget
text :description
boolean :include_design, :required => false
boolean :include_markup, :required => false
boolean :include_flash, :required => false
boolean :include_cms, :required => false
include DesignDetailsFields
include MarkupDetailsFields
include FlashDetailsFields
include CMSDetailsFields
def clean
[:design, :markup, :flash, :cms].each do |type|
unless data.has_key?("include_#{type}".to_sym)
cleaned_data.reject! { |key, value| key.match(/^#{type}/) }
errors.reject! { |key, value| key.match(/^#{type}/) }
else
cleaned_data.each do |key, value|
if match = key.to_s.match(/^#{type}_(?!details)(\w+)/)
details = cleaned_data["#{type}_details".to_sym] ||= "#{type.to_s.camelize}Details".constantize.new
details.attributes = { match.captures.first.to_sym => value }
cleaned_data.delete(key)
puts cleaned_data
end
end
end
end
unless data[:include_design] || data[:include_markup] || data[:include_flash] || data[:include_cms]
raise Bureaucrat::Fields::FieldValidationError.new("You have to choose at least one project task.")
end
super
end
def save(user)
project = Project.new(cleaned_data)
project.user = user
project.save
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment