Permalink
Browse files

mo betta teh mount. tests for mount/upload.

  • Loading branch information...
1 parent 359e416 commit 5fbf1b9648537e73491d734804428ec0e074a939 @ahoward committed Apr 8, 2012
View
@@ -107,7 +107,7 @@ def Conducer.new(*args, &block)
def init(*args, &block)
controllers, args = args.partition{|arg| arg.is_a?(ActionController::Base)}
actions, args = args.partition{|arg| arg.is_a?(Action)}
- models, args = args.partition{|arg| arg.respond_to?(:new_record?) }
+ models, args = args.partition{|arg| arg.respond_to?(:persisted?) }
hashes, args = args.partition{|arg| arg.is_a?(Hash)}
@name = self.class.model_name.singular.sub(/_+$/, '')
@@ -146,6 +146,8 @@ def initialize(*args, &block)
def default_initialize(*args, &block)
update_models()
+ update_mounted()
+
initialize_for_action(*args, &block)
update_attributes(params)
@@ -167,6 +169,13 @@ def update_models(*models)
end
end
+ def update_mounted(list = [])
+ list = self.class.mounted if list.empty?
+ list.each do |args, block|
+ mount(*args, &block)
+ end
+ end
+
def model_key_for(model)
case model
when String
@@ -218,27 +227,17 @@ def update_attributes(*args, &block)
params = Map.new(params)
- (@setting ||= []).push(params)
- recursion_depth = @setting.size - 1
-
- begin
- Dao.tree_walk(params) do |key, value|
- unless recursion_depth > 0
- if respond_to?(:_update_attributes) and send(:_update_attributes, key => value)
- throw(:tree_walk, :next_sibling)
- end
+ @attributes.set(params)
- if((handler = @attributes.get(key)).respond_to?(:_update_attributes))
- handler._update_attributes(:value => value)
- throw(:tree_walk, :next_sibling)
- end
- end
+ deepest_first = mounted.sort_by{|mnt| mnt._key.size}.reverse
- @attributes.set(key, value)
- end
- ensure
- @setting.pop
+ deepest_first.each do |mnt|
+ value = @attributes.get(mnt._key)
+ mnt._set(value)
+ @attributes.set(mnt._key => mnt)
end
+
+ @attributes
end
def attributes=(attributes)
@@ -348,6 +347,10 @@ def model?(object)
##
#
def save
+ default_save
+ end
+
+ def default_save
return false unless valid?
if @model
@@ -359,9 +362,14 @@ def save
attributes.delete(key)
end
+ mounted.each do |mnt|
+ attributes.set(mnt._key, mnt._value)
+ end
+
@model.update_attributes(attributes)
if @model.save
+ mounted.each{|mnt| mnt._clear}
return true
else
errors.relay(@model.errors)
@@ -393,7 +401,22 @@ def destroy!
## misc
#
def mount(object, *args, &block)
- object.mount(self, *args, &block)
+ mounted = object.mount(self, *args, &block)
+ ensure
+ Dao.ensure_interface!(mounted, :_set, :_key, :_value, :_clear)
+ self.mounted.push(mounted)
+ end
+
+ def mounted
+ @mounted ||= []
+ end
+
+ def self.mount(*args, &block)
+ mounted.push([args, block])
+ end
+
+ def self.mounted
+ @mounted ||= []
end
def key_for(key)
@@ -1,46 +1,9 @@
# -*- encoding : utf-8 -*-
module DaoHelper
- def render_dao(result, *args, &block)
- if result.status =~ 200 or result.status == 420
- @result = result unless defined?(@result)
- render(*args, &block)
- else
- result.error!
- end
- end
-
- def dao(path, *args, &block)
- options = args.extract_options!.to_options!
-
- mode = options[:mode]
-
- if mode.blank?
- mode =
- case request.method
- when "GET"
- :read
- when "PUT", "POST", "DELETE"
- :write
- else
- :read
- end
- end
-
- @dao = api.send(mode, path, params)
- @dao.route = request.fullpath
- #@dao.mode = mode
-
- #unless options[:error!] == false
- @dao.error! unless @dao.status.ok?
- #end
-
- block ? block.call(@dao) : @dao
- end
-
def dao_form_for(*args, &block)
options = args.extract_options!.to_options!
- model = args.flatten.select{|arg| arg.respond_to?(:new_record?)}.last
+ model = args.flatten.detect{|arg| arg.respond_to?(:persisted?)}
if model
first = args.shift
@@ -51,46 +14,6 @@ def dao_form_for(*args, &block)
options.clear
- url ||= url_for(first)
-
- if model.persisted?
- method ||= :put
- else
- method ||= :post
- end
-
- options[:url] = url
- options[:html] = html.dup.merge(:method => method)
-
- args.push(model)
- args.push(options)
-
- form_for(*args) do
- block.call(model.form)
- end
- else
- args.push(request.fullpath) if args.empty?
- args.push(dao_form_attrs(options))
- form_tag(*args, &block)
- end
- end
-
- def dao_form_for(*args, &block)
- options = args.extract_options!.to_options!
-
- model = args.flatten.select{|arg| arg.respond_to?(:new_record?)}.last
-
- if model
- first = args.shift
- url = args.shift || options.delete(:url)
-
- method = options.delete(:method)
- html = dao_form_attrs(options)
-
- options.clear
-
- #url ||= url_for(first)
-
if model.persisted?
method ||= :put
else
@@ -138,5 +61,42 @@ def dao_form_attrs(*args)
options[:enctype] ||= "multipart/form-data"
options
end
+
+ def render_dao(result, *args, &block)
+ if result.status =~ 200 or result.status == 420
+ @result = result unless defined?(@result)
+ render(*args, &block)
+ else
+ result.error!
+ end
+ end
+
+ def dao(path, *args, &block)
+ options = args.extract_options!.to_options!
+
+ mode = options[:mode]
+
+ if mode.blank?
+ mode =
+ case request.method
+ when "GET"
+ :read
+ when "PUT", "POST", "DELETE"
+ :write
+ else
+ :read
+ end
+ end
+
+ @dao = api.send(mode, path, params)
+ @dao.route = request.fullpath
+ #@dao.mode = mode
+
+ #unless options[:error!] == false
+ @dao.error! unless @dao.status.ok?
+ #end
+
+ block ? block.call(@dao) : @dao
+ end
end
ApplicationController.send(:include, DaoHelper)
View
@@ -227,4 +227,10 @@ def tree_walk(node, *path, &block)
end
end
abort 'no suitable uuid generation library detected' unless method_defined?(:uuid)
+
+ def ensure_interface!(object, *interface)
+ interface.flatten.compact.each do |method|
+ raise(NotImplementedError, "#{ object.class.name }##{ method }") unless object.respond_to?(method)
+ end
+ end
end
View
@@ -138,7 +138,8 @@ def rewind(io, &block)
end
end
- alias_method :mount, :new
+ alias_method('mount', 'new')
+
attr_accessor :placeholder
end
@@ -176,18 +177,26 @@ def initialize(conducer, *args, &block)
@io = nil
end
- def _update_attributes(attributes = {})
- attributes.to_options!
-
- value = attributes[:value]
-
+ def _set(value)
if value.respond_to?(:read) or value.is_a?(IO)
process_currently_uploaded(value)
else
process_previously_uploaded(value)
end
end
+ def _key
+ key
+ end
+
+ def _value
+ path
+ end
+
+ def _clear
+ clear!
+ end
+
def process_currently_uploaded(value)
Upload.tmpdir do |tmp|
original_basename =
@@ -172,12 +172,6 @@ def has(key)
alias_method 'has?', 'has'
- def run_validations!(*args)
- run_validations(*args)
- ensure
- validated!
- end
-
def validations_search_path
@validations_search_path ||= (
if mixin?
@@ -221,8 +215,13 @@ def run_validations(list = validations_list)
end
errors
+ ensure
+ validated!(true)
end
+ alias_method 'run_validations!', 'run_validations'
+ alias_method 'validate', 'run_validations'
+
def _run_validations(errors, list)
Array(list).each do |validations|
validations.each do |keys, chain|
@@ -280,10 +279,6 @@ def validated!(boolean = true)
@validated = !!boolean
end
- def validate
- run_validations
- end
-
def validate!
raise Error.new("#{ object.class.name } is invalid!") unless valid?
object
@@ -306,7 +301,7 @@ def valid?(*args)
true
else
options = Map.options_for!(args)
- validate #if(options[:validate] or !validated?)
+ run_validations
errors.empty? and status.ok?
end
end
Oops, something went wrong.

0 comments on commit 5fbf1b9

Please sign in to comment.