class Peta.ElementNestableContent extends Peta.Content element_items: (element_model) -> n = @my_class().my_manifest().associations.child_element_name(element_model) @get(n) elements_items: () -> r = _.map @my_class().child_models(), (element_model) => @element_items(element_model) _.compact(_.flatten(r)) boosts: (level) -> super(level) _.each @elements_items(), (item) => item.boosts(level) if item replaced_attributes: (options = {}) -> attributes = {} _.each @attributes, (attribute, name) => replaced = if _.isArray(attribute) # has many association model = @my_class().my_manifest().associations.has_many[name].model() _.map attribute, (attr) => child = if options.hold @replace_and_hold_element(model, attr) else @replace_element(model, attr) child.attributes = child.replaced_attributes(options) child else if _.isObject(attribute) # has one association model = @my_class().my_manifest().associations.has_one[name].model() child = if options.hold @replace_and_hold_element(model, attribute) else @replace_element(model, attribute) child.attributes = child.replaced_attributes(options) child else attribute attributes[name] = replaced attributes replace_element: (model, attr) -> empty_child = new model(attr) cached_child = Pettanr.cache.restore(empty_child) item = if cached_child cached_child else Pettanr.cache.store(empty_child) item.attributes = attr item replace_and_hold_element: (model, attr) -> empty_child = new model(attr) cached_child = Pettanr.cache.restore(empty_child) item = if cached_child cached_child else Pettanr.cache.store(empty_child) holden = item.hold() holden.attributes = attr holden hold: () -> super() fix: () -> super() @fix_elements() release: () -> super() @release_elements() release_elements: () -> _.each @attributes, (elements, name) => if _.isArray(elements) # has many association model = @my_class().my_manifest().associations.has_many[name].model() _.each elements, (element) => element.release() else if _.isObject(elements) # has one association elements.release() fix_elements: () -> return _.each @attributes, (elements, name) => if _.isArray(elements) # has many association model = @my_class().my_manifest().associations.has_many[name].model() _.each elements, (element) => if @has('_destroy') element.release() # destroy element by editor else element.fix() else if _.isObject(elements) # has one association if @has('_destroy') element.release() else element.fix()