OSDN Git Service

fix js manifest
[pettanr/pettanr.git] / app / assets / javascripts / manifest / model.js.coffee
1 class Manifest.Model extends ManifestBase.Base\r
2   _attributes = null\r
3   _associations = null\r
4   _list = null\r
5   \r
6   attributes: () ->\r
7     _attributes\r
8   \r
9   associations: () ->\r
10     _associations\r
11   \r
12   list: () ->\r
13     _list\r
14   \r
15   set_default: () ->\r
16     super\r
17     @json['attributes'] ||= {}\r
18     @json['associations'] ||= {}\r
19     @json['list'] ||= {}\r
20     @json['attributes']['id'] = {\r
21       'type': 'number',\r
22       'primary_key': 1,\r
23       'rules': {\r
24         'number': true\r
25       }\r
26     }\r
27     @json['attributes']['created_at'] = {\r
28       'type': 'datetime',\r
29     }\r
30     @json['attributes']['updated_at'] = {\r
31       'type': 'datetime',\r
32     }\r
33   \r
34   init: () ->\r
35     super\r
36     _attributes = ManifestBase.load_name_values this, @json, 'attributes', Manifest.ModelModule.Attribute\r
37     _associations = ManifestBase.load_value this, @json, 'associations', Manifest.ModelModule.Association\r
38     _list = ManifestBase.load_value this, @json, 'list', Manifest.ModelModule.List\r
39   \r
40   model_name: () ->\r
41     _name\r
42   \r
43   classify: () ->\r
44     Manifest.item_name_to_model _name\r
45   \r
46   table_name: () ->\r
47     @classify.table_name\r
48   \r
49   valid_encode_columns: () ->\r
50     r = []\r
51     _.each @attributes, (attribute, attribute_name) ->\r
52       next if not attribute.type == 'text'\r
53       r.push attribute_name\r
54     r\r
55   \r
56   owner_type: () ->\r
57     return 'author' if @attributes['author_id']\r
58     return 'artist' if @attributes['artist_id']\r
59     false\r
60   \r
61   content_model: () ->\r
62     return true if @owner_type\r
63     false\r
64   \r
65   child_model_manifests: () ->\r
66     r = []\r
67     _.each Manifest.items, (peta_manifest, peta_name) ->\r
68       next if not peta_manifest.is_element\r
69       next if not peta_manifest.parent_model_name == @name\r
70       r.push Manifest.models[peta_name]\r
71     r\r
72   \r
73   child_models: () ->\r
74     _.map @child_model_manifests, (child_model_manifest) ->\r
75       child_model_manifest.classify\r
76   \r
77   child_element_names: () ->\r
78     _.map @child_models, (child_model) ->\r
79       _associations.child_element_name(child_model)\r
80   \r
81   child_element_name: (item_name) ->\r
82     _associations.child_element_name(child_model_manifest)\r
83   \r
84 class Manifest.ModelModule\r