OSDN Git Service

Merge branch 'v06' of git.sourceforge.jp:/gitroot/pettanr/pettanr into v06
[pettanr/pettanr.git] / app / assets / javascripts / peta / element_nestable_content.js.coffee
1 class Peta.ElementNestableContent extends Peta.Content\r
2   element_items: (element_model) ->\r
3     n = @my_class().my_manifest().associations.child_element_name(element_model)\r
4     @get(n)\r
5   \r
6   elements_items: () ->\r
7     r = _.map @my_class().child_models(), (element_model) =>\r
8       @element_items(element_model)\r
9     _.compact(_.flatten(r))\r
10   \r
11   boosts: (level) ->\r
12     super(level)\r
13     _.each @elements_items(), (item) =>\r
14       item.boosts(level) if item\r
15   \r
16   replaced_attributes: () ->\r
17     attributes = {}\r
18     _.each @attributes, (attribute, name) =>\r
19       replaced = if _.isArray(attribute)\r
20         # has many association\r
21         model = @my_class().my_manifest().associations.has_many[name].model()\r
22         _.map attribute, (attr) =>\r
23           child = @replace_element(model, attr)\r
24           child.attributes = child.replaced_attributes()\r
25           child\r
26       else if _.isObject(attribute)\r
27         # has one association\r
28         model = @my_class().my_manifest().associations.has_one[name].model()\r
29         child = @replace_element(model, attribute)\r
30         child.attributes = child.replaced_attributes()\r
31         child\r
32       else\r
33         attribute\r
34       attributes[name] = replaced\r
35     attributes\r
36   \r
37   replace_element: (model, attr) ->\r
38     empty_child = new model(attr)\r
39     cached_child = Pettanr.cache.restore(empty_child)\r
40     Pettanr.cache.store(empty_child) if !cached_child\r
41     cached_child || empty_child\r
42   \r