lib/model/nested/base.rb in fire-model-0.0.12 vs lib/model/nested/base.rb in fire-model-0.0.14
- old
+ new
@@ -1,18 +1,32 @@
module Fire
class NestedModel < Model
non_shared_cattr_accessor :parent, :nested_options
+ def initialize(hash={}, parent_original={})
+ @parent_original = parent_original
+ super(hash)
+ end
+
+ def sync_parent
+ @parent_original.merge!(saving_data)
+ end
+
def saving_data
return super() if nested_options.parent_values
- self.class.parent.all_path_keys.each_with_object( data.clone) do |k, res|
+ self.class.parent.all_path_keys.each_with_object(data.clone) do |k, res|
res.delete(k)
end
end
+ def save
+ sync_parent
+ super
+ end
+
def nested_options
self.class.nested_options
end
class << self
@@ -46,13 +60,14 @@
path_value_param(self.nested_options.folder ? self.nested_options.folder : default_folder_name)
end
def folder_content(parent)
levels_count = (path_keys || []).count
- objects = self.down_levels(parent.send(folder), levels_count)
- objects.map{|x|
- full_data = x.merge(parent.path_data)
- new(full_data)
+ nested_folder = parent.send(folder) || {}
+ objects = self.down_levels(nested_folder, levels_count)
+ objects.map{|parent_original|
+ full_data = parent_original.clone.merge(parent.path_data)
+ new(full_data, parent_original)
}
end
def query(params={}, &filter_condition)
raise QueryingNotSupportedError.new