lib/embedded_associations.rb in embedded_associations-0.0.4 vs lib/embedded_associations.rb in embedded_associations-4.0.0
- old
+ new
@@ -9,29 +9,20 @@
class_attribute :embedded_associations
def self.embedded_association(definition)
unless embedded_associations
self.embedded_associations = Definitions.new
- before_filter :handle_embedded_associations, only: [:update, :create, :destroy]
end
self.embedded_associations = embedded_associations.add_definition(definition)
end
end
end
- def handle_embedded_associations
- Processor.new(embedded_associations, self).run
+ def handle_embedded_associations(resource, params)
+ Processor.new(embedded_associations, self, resource, params).run
end
- def root_resource
- resource
- end
-
- def root_resource_name
- resource_name
- end
-
def filter_attributes(name, attrs, action)
attrs
end
# Simple callbacks for now, eventually should use a filter system
@@ -65,19 +56,23 @@
class Processor
attr_reader :definitions
attr_reader :controller
+ attr_reader :resource
+ attr_reader :params
- def initialize(definitions, controller)
+ def initialize(definitions, controller, resource, params)
@definitions = definitions
@controller = controller
+ @params = params
+ @resource = resource
end
def run
definitions.each do |definition|
- handle_resource(definition, controller.root_resource, controller.params[controller.root_resource_name])
+ handle_resource(definition, resource, params)
end
end
private
@@ -92,11 +87,11 @@
end
definition.each do |name, child_definition|
reflection = parent.class.reflect_on_association(name)
attrs = parent_params && parent_params.delete(name.to_s)
-
+
if reflection.collection?
attrs ||= []
handle_plural_resource parent, name, attrs, child_definition
else
handle_singular_resource parent, name, attrs, child_definition
@@ -113,11 +108,10 @@
run_before_destroy_callbacks(r)
r.mark_for_destruction
end
attr_array.each do |attrs|
- attrs = ActionController::Parameters.new(attrs)
if id = attrs['id']
# can't use current_assoc.find(id), see http://stackoverflow.com/questions/11605120/autosave-ignored-on-has-many-relation-what-am-i-missing
r = current_assoc.find{|r| r.id == id.to_i}
attrs = controller.send(:filter_attributes, r.class.name, attrs, :update)
handle_resource(child_definition, r, attrs) if child_definition
@@ -133,11 +127,10 @@
end
end
def handle_singular_resource(parent, name, attrs, child_definition)
current_assoc = parent.send(name)
- attrs = ActionController::Parameters.new(attrs)
-
+
if r = current_assoc
if attrs
attrs = controller.send(:filter_attributes, r.class.name, attrs, :update)
handle_resource(child_definition, r, attrs) if child_definition
r.assign_attributes(attrs)