spontaneous 0.2.0.alpha7 → 0.2.0.beta1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (247) hide show
  1. data/Gemfile +10 -4
  2. data/Readme.markdown +1 -1
  3. data/application/css/definitions.css.scss +5 -0
  4. data/application/css/dialogue.css.scss +62 -0
  5. data/application/js/content.js +1 -1
  6. data/application/js/dom.js +1 -1
  7. data/application/js/event_source.js +3 -0
  8. data/application/js/{field_types/date_field.js → field/date.js} +2 -2
  9. data/application/js/{field_types/file_field.js → field/file.js} +2 -2
  10. data/application/js/{field_types/image_field.js → field/image.js} +54 -20
  11. data/application/js/{field_types/long_string_field.js → field/long_string.js} +2 -2
  12. data/application/js/{field_types/markdown_field.js → field/markdown.js} +2 -2
  13. data/application/js/{field_types/select_field.js → field/select.js} +2 -2
  14. data/application/js/{field_types/string_field.js → field/string.js} +21 -7
  15. data/application/js/{field_types/webvideo_field.js → field/webvideo.js} +2 -2
  16. data/application/js/field.js +2 -2
  17. data/application/js/publish.js +99 -19
  18. data/application/js/spontaneous.js +8 -8
  19. data/application/js/top_bar.js +6 -4
  20. data/db/migrations/20130109125023_add_page_publish_lock.rb +17 -0
  21. data/db/migrations/20130111161934_convert_bcrypt_passwords.rb +22 -0
  22. data/db/migrations/20130114120000_create_revision_tables.rb +106 -0
  23. data/db/migrations/20130116220423_add_index_to_archive.rb +9 -0
  24. data/lib/spontaneous/box.rb +53 -18
  25. data/lib/spontaneous/box_style.rb +2 -3
  26. data/lib/spontaneous/change.rb +39 -13
  27. data/lib/spontaneous/cli/fields.rb +29 -0
  28. data/lib/spontaneous/cli/init.rb +2 -2
  29. data/lib/spontaneous/cli/migrate.rb +0 -1
  30. data/lib/spontaneous/cli/server.rb +14 -10
  31. data/lib/spontaneous/cli/site.rb +20 -9
  32. data/lib/spontaneous/cli.rb +8 -6
  33. data/lib/spontaneous/collections/box_set.rb +11 -0
  34. data/lib/spontaneous/collections/field_set.rb +24 -1
  35. data/lib/spontaneous/concern.rb +37 -0
  36. data/lib/spontaneous/config.rb +3 -4
  37. data/lib/spontaneous/crypt/version.rb +130 -0
  38. data/lib/spontaneous/crypt.rb +84 -0
  39. data/lib/spontaneous/data_mapper/content_model/associations.rb +199 -0
  40. data/lib/spontaneous/data_mapper/content_model/column_accessors.rb +52 -0
  41. data/lib/spontaneous/data_mapper/content_model/instance_hooks.rb +34 -0
  42. data/lib/spontaneous/data_mapper/content_model/serialization.rb +54 -0
  43. data/lib/spontaneous/data_mapper/content_model/timestamps.rb +39 -0
  44. data/lib/spontaneous/data_mapper/content_model.rb +343 -0
  45. data/lib/spontaneous/data_mapper/content_table.rb +103 -0
  46. data/lib/spontaneous/data_mapper/dataset.rb +194 -0
  47. data/lib/spontaneous/data_mapper/scope.rb +195 -0
  48. data/lib/spontaneous/data_mapper.rb +161 -0
  49. data/lib/spontaneous/facet.rb +2 -2
  50. data/lib/spontaneous/field/base.rb +418 -0
  51. data/lib/spontaneous/field/date.rb +54 -0
  52. data/lib/spontaneous/{field_version.rb → field/field_version.rb} +1 -1
  53. data/lib/spontaneous/field/file.rb +100 -0
  54. data/lib/spontaneous/{field_types/image_field.rb → field/image.rb} +33 -33
  55. data/lib/spontaneous/{field_types/location_field.rb → field/location.rb} +2 -2
  56. data/lib/spontaneous/{field_types/long_string_field.rb → field/long_string.rb} +3 -3
  57. data/lib/spontaneous/field/markdown.rb +36 -0
  58. data/lib/spontaneous/{field_types/select_field.rb → field/select.rb} +4 -5
  59. data/lib/spontaneous/field/string.rb +17 -0
  60. data/lib/spontaneous/field/update.rb +156 -0
  61. data/lib/spontaneous/field/webvideo.rb +310 -0
  62. data/lib/spontaneous/field.rb +80 -0
  63. data/lib/spontaneous/generators/site/Gemfile.tt +2 -2
  64. data/lib/spontaneous/generators/site/config/environments/development.rb.tt +1 -1
  65. data/lib/spontaneous/generators/site/config/environments/production.rb.tt +1 -1
  66. data/lib/spontaneous/generators/site/lib/content.rb.tt +6 -0
  67. data/lib/spontaneous/generators/site/schema/box.rb.tt +3 -2
  68. data/lib/spontaneous/generators/site/schema/page.rb.tt +3 -1
  69. data/lib/spontaneous/generators/site/schema/piece.rb.tt +3 -1
  70. data/lib/spontaneous/generators/site/templates/layouts/standard.html.cut.tt +3 -1
  71. data/lib/spontaneous/generators/site.rb +4 -3
  72. data/lib/spontaneous/image_size.rb +8 -1
  73. data/lib/spontaneous/layout.rb +5 -1
  74. data/lib/spontaneous/loader.rb +2 -5
  75. data/lib/spontaneous/media/file.rb +11 -2
  76. data/lib/spontaneous/media/temp_file.rb +23 -0
  77. data/lib/spontaneous/media.rb +20 -39
  78. data/lib/spontaneous/{plugins → model/box}/allowed_types.rb +38 -17
  79. data/lib/spontaneous/model/box.rb +18 -0
  80. data/lib/spontaneous/{plugins → model/core}/aliases.rb +10 -14
  81. data/lib/spontaneous/{plugins → model/core}/boxes.rb +2 -2
  82. data/lib/spontaneous/{plugins → model/core}/content_groups.rb +2 -2
  83. data/lib/spontaneous/model/core/editor_class.rb +4 -0
  84. data/lib/spontaneous/{plugins → model/core}/entries.rb +19 -7
  85. data/lib/spontaneous/{plugins → model/core}/entry.rb +3 -3
  86. data/lib/spontaneous/{plugins → model/core}/fields.rb +38 -5
  87. data/lib/spontaneous/{plugins → model/core}/instance_code.rb +2 -2
  88. data/lib/spontaneous/{plugins → model/core}/media.rb +2 -12
  89. data/lib/spontaneous/{plugins → model/core}/modifications.rb +7 -6
  90. data/lib/spontaneous/model/core/page_search.rb +36 -0
  91. data/lib/spontaneous/{plugins → model/core}/permissions.rb +4 -4
  92. data/lib/spontaneous/{plugins → model/core}/prototypes.rb +4 -4
  93. data/lib/spontaneous/{plugins → model/core}/publishing.rb +93 -115
  94. data/lib/spontaneous/{plugins → model/core}/render.rb +2 -2
  95. data/lib/spontaneous/{plugins → model/core}/schema_hierarchy.rb +7 -11
  96. data/lib/spontaneous/model/core/schema_id.rb +65 -0
  97. data/lib/spontaneous/{plugins → model/core}/schema_title.rb +2 -2
  98. data/lib/spontaneous/{plugins → model/core}/serialisation.rb +2 -2
  99. data/lib/spontaneous/{plugins → model/core}/styles.rb +2 -2
  100. data/lib/spontaneous/{plugins → model/core}/supertype.rb +2 -2
  101. data/lib/spontaneous/{plugins → model/core}/visibility.rb +7 -48
  102. data/lib/spontaneous/model/core.rb +143 -0
  103. data/lib/spontaneous/{plugins → model/page}/controllers.rb +3 -3
  104. data/lib/spontaneous/{plugins → model}/page/formats.rb +2 -2
  105. data/lib/spontaneous/{plugins → model/page}/layouts.rb +2 -2
  106. data/lib/spontaneous/model/page/locks.rb +14 -0
  107. data/lib/spontaneous/{plugins → model/page}/page_tree.rb +3 -3
  108. data/lib/spontaneous/{plugins → model/page}/paths.rb +30 -12
  109. data/lib/spontaneous/{plugins → model}/page/request.rb +2 -2
  110. data/lib/spontaneous/{plugins → model/page}/site_map.rb +2 -2
  111. data/lib/spontaneous/model/page/site_timestamps.rb +44 -0
  112. data/lib/spontaneous/{page.rb → model/page.rb} +49 -28
  113. data/lib/spontaneous/{piece.rb → model/piece.rb} +7 -6
  114. data/lib/spontaneous/model.rb +97 -0
  115. data/lib/spontaneous/output/context.rb +1 -1
  116. data/lib/spontaneous/output/format.rb +4 -0
  117. data/lib/spontaneous/output/template/renderer.rb +2 -2
  118. data/lib/spontaneous/output.rb +2 -2
  119. data/lib/spontaneous/page_lock.rb +62 -0
  120. data/lib/spontaneous/page_piece.rb +1 -1
  121. data/lib/spontaneous/permissions/access_key.rb +9 -4
  122. data/lib/spontaneous/permissions/user.rb +19 -9
  123. data/lib/spontaneous/permissions.rb +2 -5
  124. data/lib/spontaneous/plugins/application/facets.rb +1 -2
  125. data/lib/spontaneous/plugins/application/features.rb +1 -1
  126. data/lib/spontaneous/plugins/application/paths.rb +1 -1
  127. data/lib/spontaneous/plugins/application/render.rb +1 -1
  128. data/lib/spontaneous/plugins/application/serialisation.rb +1 -1
  129. data/lib/spontaneous/plugins/application/state.rb +30 -1
  130. data/lib/spontaneous/plugins/application/system.rb +12 -12
  131. data/lib/spontaneous/prototypes/box_prototype.rb +1 -1
  132. data/lib/spontaneous/prototypes/field_prototype.rb +3 -6
  133. data/lib/spontaneous/prototypes/style_prototype.rb +1 -1
  134. data/lib/spontaneous/publishing/immediate.rb +77 -49
  135. data/lib/spontaneous/publishing/revision.rb +355 -0
  136. data/lib/spontaneous/publishing/simultaneous.rb +10 -49
  137. data/lib/spontaneous/publishing.rb +1 -0
  138. data/lib/spontaneous/rack/around_back.rb +1 -1
  139. data/lib/spontaneous/rack/around_front.rb +2 -4
  140. data/lib/spontaneous/rack/around_preview.rb +1 -1
  141. data/lib/spontaneous/rack/back.rb +80 -63
  142. data/lib/spontaneous/rack/cacheable_file.rb +2 -2
  143. data/lib/spontaneous/rack/cookie_authentication.rb +1 -1
  144. data/lib/spontaneous/rack/front.rb +1 -1
  145. data/lib/spontaneous/rack/helpers.rb +8 -9
  146. data/lib/spontaneous/{page_controller.rb → rack/page_controller.rb} +1 -1
  147. data/lib/spontaneous/rack/public.rb +3 -3
  148. data/lib/spontaneous/rack.rb +15 -15
  149. data/lib/spontaneous/schema/uid.rb +4 -1
  150. data/lib/spontaneous/schema.rb +57 -24
  151. data/lib/spontaneous/search/database.rb +12 -1
  152. data/lib/spontaneous/search/index.rb +34 -6
  153. data/lib/spontaneous/search/results.rb +1 -1
  154. data/lib/spontaneous/server.rb +3 -3
  155. data/lib/spontaneous/simultaneous.rb +53 -0
  156. data/lib/spontaneous/{plugins/site → site}/features.rb +2 -2
  157. data/lib/spontaneous/{plugins/site → site}/helpers.rb +2 -3
  158. data/lib/spontaneous/{plugins/site → site}/hooks.rb +2 -2
  159. data/lib/spontaneous/{plugins/site → site}/instance.rb +4 -6
  160. data/lib/spontaneous/{plugins/site → site}/level.rb +2 -2
  161. data/lib/spontaneous/{plugins/site → site}/map.rb +4 -4
  162. data/lib/spontaneous/{plugins/site → site}/paths.rb +2 -2
  163. data/lib/spontaneous/site/publishing.rb +89 -0
  164. data/lib/spontaneous/{plugins/site → site}/schema.rb +4 -4
  165. data/lib/spontaneous/{plugins/site → site}/search.rb +2 -2
  166. data/lib/spontaneous/{plugins/site → site}/selectors.rb +15 -7
  167. data/lib/spontaneous/{plugins/site → site}/state.rb +2 -2
  168. data/lib/spontaneous/{plugins/site → site}/storage.rb +2 -2
  169. data/lib/spontaneous/{plugins/site → site}/url.rb +2 -2
  170. data/lib/spontaneous/site.rb +31 -14
  171. data/lib/spontaneous/state.rb +5 -6
  172. data/lib/spontaneous/style.rb +3 -2
  173. data/lib/spontaneous/utils/database/mysql_dumper.rb +13 -0
  174. data/lib/spontaneous/utils/database/postgres_dumper.rb +5 -0
  175. data/lib/spontaneous/version.rb +1 -1
  176. data/lib/spontaneous.rb +34 -89
  177. data/spontaneous.gemspec +112 -114
  178. data/test/experimental/test_crypt.rb +158 -0
  179. data/test/experimental/test_features.rb +3 -3
  180. data/test/fixtures/example_application/config/environments/development.rb +1 -1
  181. data/test/fixtures/example_application/lib/content.rb +5 -0
  182. data/test/fixtures/example_application/schema/page.rb +2 -1
  183. data/test/fixtures/example_application/schema/piece.rb +3 -2
  184. data/test/fixtures/serialisation/class_hash.yaml.erb +5 -5
  185. data/test/fixtures/serialisation/root_hash.yaml.erb +8 -0
  186. data/test/functional/test_application.rb +12 -1
  187. data/test/functional/test_back.rb +80 -48
  188. data/test/functional/test_front.rb +39 -46
  189. data/test/functional/test_user_manager.rb +3 -9
  190. data/test/javascript/test_markdown.rb +2 -2
  191. data/test/test_helper.rb +78 -23
  192. data/test/unit/test_alias.rb +21 -15
  193. data/test/unit/test_asset_bundler.rb +3 -3
  194. data/test/unit/test_assets.rb +2 -2
  195. data/test/unit/test_async.rb +7 -6
  196. data/test/unit/test_authentication.rb +43 -37
  197. data/test/unit/test_boxes.rb +46 -21
  198. data/test/unit/test_changesets.rb +65 -20
  199. data/test/unit/test_config.rb +9 -9
  200. data/test/unit/test_content.rb +50 -51
  201. data/test/unit/test_content_inheritance.rb +6 -20
  202. data/test/unit/test_datamapper.rb +1330 -0
  203. data/test/unit/test_datamapper_content.rb +214 -0
  204. data/test/unit/test_fields.rb +543 -54
  205. data/test/unit/test_formats.rb +2 -3
  206. data/test/unit/test_generators.rb +6 -6
  207. data/test/unit/test_helpers.rb +1 -1
  208. data/test/unit/test_image_size.rb +10 -5
  209. data/test/unit/test_images.rb +17 -18
  210. data/test/unit/test_layouts.rb +18 -3
  211. data/test/unit/test_media.rb +74 -49
  212. data/test/unit/test_modifications.rb +43 -43
  213. data/test/unit/test_page.rb +7 -10
  214. data/test/unit/test_permissions.rb +3 -10
  215. data/test/unit/test_piece.rb +5 -6
  216. data/test/unit/test_plugins.rb +7 -14
  217. data/test/unit/test_prototypes.rb +3 -3
  218. data/test/unit/test_publishing.rb +49 -27
  219. data/test/unit/test_render.rb +46 -15
  220. data/test/unit/test_revisions.rb +124 -127
  221. data/test/unit/test_schema.rb +53 -58
  222. data/test/unit/test_search.rb +64 -16
  223. data/test/unit/test_serialisation.rb +4 -11
  224. data/test/unit/test_site.rb +11 -12
  225. data/test/unit/test_structure.rb +2 -5
  226. data/test/unit/test_styles.rb +22 -24
  227. data/test/unit/test_type_hierarchy.rb +7 -5
  228. data/test/unit/test_visibility.rb +79 -55
  229. metadata +128 -102
  230. data/lib/sequel/plugins/content_table_inheritance.rb +0 -203
  231. data/lib/sequel/plugins/scoped_table_name.rb +0 -54
  232. data/lib/spontaneous/content.rb +0 -129
  233. data/lib/spontaneous/field_types/date_field.rb +0 -56
  234. data/lib/spontaneous/field_types/field.rb +0 -302
  235. data/lib/spontaneous/field_types/file_field.rb +0 -68
  236. data/lib/spontaneous/field_types/markdown_field.rb +0 -38
  237. data/lib/spontaneous/field_types/string_field.rb +0 -19
  238. data/lib/spontaneous/field_types/webvideo_field.rb +0 -313
  239. data/lib/spontaneous/field_types.rb +0 -38
  240. data/lib/spontaneous/generators/site/lib/site.rb.tt +0 -4
  241. data/lib/spontaneous/plugins/field/editor_class.rb +0 -13
  242. data/lib/spontaneous/plugins/page/site_timestamps.rb +0 -28
  243. data/lib/spontaneous/plugins/page_search.rb +0 -63
  244. data/lib/spontaneous/plugins/schema_id.rb +0 -68
  245. data/lib/spontaneous/plugins/site/publishing.rb +0 -75
  246. data/lib/spontaneous/rack/fiber_pool.rb +0 -26
  247. data/test/unit/test_table_scoping.rb +0 -80
@@ -0,0 +1,343 @@
1
+ require 'spontaneous/data_mapper/content_model/column_accessors'
2
+ require 'spontaneous/data_mapper/content_model/serialization'
3
+ require 'spontaneous/data_mapper/content_model/timestamps'
4
+ require 'spontaneous/data_mapper/content_model/instance_hooks'
5
+ require 'spontaneous/data_mapper/content_model/associations'
6
+
7
+ module Spontaneous
8
+ module DataMapper
9
+ module ContentModel
10
+ def self.extended(model)
11
+ model.send :include, InstanceMethods
12
+ end
13
+
14
+ def self.generate(mapper, &block)
15
+ Class.new do
16
+ extend Spontaneous::DataMapper::ContentModel
17
+ class_eval &block if block
18
+ self.mapper = mapper
19
+ include ColumnAccessors
20
+ include Serialization
21
+ include Timestamps
22
+ include InstanceHooks
23
+ include Associations::InstanceMethods
24
+ end
25
+ end
26
+
27
+ attr_reader :mapper
28
+ attr_writer :options
29
+
30
+ def inherited(subclass)
31
+ @mapper.inherited(self, subclass)
32
+ subclass.mapper = @mapper
33
+ super
34
+ end
35
+
36
+ def mapper=(mapper)
37
+ @mapper = mapper
38
+ end
39
+
40
+ def restricted_columns
41
+ [:id, :type_sid]
42
+ end
43
+
44
+ def serialize_columns(*columns)
45
+ define_singleton_method(:serialized_columns) { columns }
46
+ end
47
+
48
+ def serialized_columns
49
+ []
50
+ end
51
+
52
+ def include_all_types
53
+ include_types()
54
+ end
55
+
56
+ def include_types(*types, &block)
57
+ if block_given?
58
+ define_singleton_method(:types, &block)
59
+ else
60
+ define_singleton_method(:types) { types }
61
+ end
62
+ end
63
+
64
+ def types
65
+ [self]
66
+ end
67
+
68
+ def subclasses
69
+ mapper.subclasses(self)
70
+ end
71
+
72
+ def columns
73
+ @columns ||= mapper.columns - restricted_columns
74
+ end
75
+
76
+ def db
77
+ mapper.db
78
+ end
79
+
80
+ def schema
81
+ mapper.schema
82
+ end
83
+
84
+ def get(id)
85
+ mapper.get(id)
86
+ end
87
+
88
+ alias_method :[], :get
89
+
90
+ def count
91
+ mapper.count(types)
92
+ end
93
+
94
+ def all
95
+ mapper.all(*types)
96
+ end
97
+
98
+ def all!(&block)
99
+ mapper.all!(&block)
100
+ end
101
+
102
+ def first(*args, &block)
103
+ mapper.first(types, *args, &block)
104
+ end
105
+
106
+ def first!(*args, &block)
107
+ mapper.first!(*args, &block)
108
+ end
109
+
110
+ def filter(*cond, &block)
111
+ mapper.filter(types, *cond, &block)
112
+ end
113
+
114
+ def filter!(*cond, &block)
115
+ mapper.filter!(*cond, &block)
116
+ end
117
+
118
+ def where(*cond, &block)
119
+ mapper.where(types, *cond, &block)
120
+ end
121
+
122
+ def where!(*cond, &block)
123
+ mapper.where!(*cond, &block)
124
+ end
125
+
126
+ def create(attrs = {})
127
+ self.new(attrs).save
128
+ end
129
+
130
+ def insert(attrs = {})
131
+ mapper.insert(attrs)
132
+ end
133
+
134
+ def update(attrs)
135
+ mapper.update(types, attrs)
136
+ end
137
+
138
+ def delete(&block)
139
+ mapper.delete(types, &block)
140
+ end
141
+
142
+ def order(*columns, &block)
143
+ mapper.order(types, *columns, &block)
144
+ end
145
+
146
+ def limit(l, o = (no_offset = true; nil))
147
+ mapper.limit(types, l, o)
148
+ end
149
+
150
+ def for_update
151
+ mapper.for_update
152
+ end
153
+
154
+ def select(*columns, &block)
155
+ mapper.select(types, *columns, &block)
156
+ end
157
+
158
+ def visible
159
+ mapper.visible.dataset(types)
160
+ end
161
+
162
+ def columns
163
+ mapper.columns
164
+ end
165
+
166
+ def primary_key
167
+ mapper.primary_key
168
+ end
169
+
170
+ def table_name
171
+ mapper.table_name
172
+ end
173
+
174
+ include Associations
175
+
176
+ module InstanceMethods
177
+
178
+ HOOKS = [ :create, :save, :update, :destroy ] unless defined?(HOOKS)
179
+
180
+ HOOKS.each do |hook|
181
+ module_eval "def before_#{hook}; end", __FILE__, __LINE__
182
+ module_eval "def after_#{hook} ; end", __FILE__, __LINE__
183
+ end
184
+
185
+ def initialize(attr = {}, from_db = false)
186
+ @modified = false
187
+ if from_db
188
+ @new = false
189
+ set_attributes!(attr)
190
+ else
191
+ @new = true
192
+ @modified = true
193
+ @attributes = {}
194
+ changed_columns.clear
195
+ set(attr)
196
+ end
197
+ trigger_hook(:after_initialize)
198
+ end
199
+
200
+ def after_initialize; end
201
+
202
+ def attributes
203
+ @attributes.dup
204
+ end
205
+
206
+ def mapper
207
+ model.mapper
208
+ end
209
+
210
+ def save
211
+ id = nil
212
+ saved = false
213
+ trigger_hook(:before_save) do
214
+ if new?
215
+ trigger_hook(:before_create) do
216
+ mapper.create(self)
217
+ @new = false
218
+ saved = true
219
+ trigger_hook(:after_create)
220
+ self
221
+ end
222
+ else
223
+ trigger_hook(:before_update) do
224
+ mapper.save(self)
225
+ saved = true
226
+ changed_columns.clear
227
+ trigger_hook(:after_update)
228
+ self
229
+ end
230
+ end
231
+ return nil unless saved
232
+ trigger_hook(:after_save)
233
+ end
234
+ @modified = false
235
+ self
236
+ end
237
+
238
+ def trigger_hook(callback)
239
+ catch :halt do
240
+ send(callback)
241
+ return yield if block_given?
242
+ return self
243
+ end
244
+ nil
245
+ end
246
+
247
+ def mark_modified!(*columns)
248
+ if columns.empty?
249
+ @modified = true
250
+ else
251
+ _mark_columns_as_modified(*columns)
252
+ end
253
+ end
254
+
255
+ def modified?
256
+ @modified || !changed_columns.empty?
257
+ end
258
+
259
+ def reload
260
+ refresh
261
+ end
262
+
263
+ def _after_save_refresh
264
+ refresh
265
+ end
266
+
267
+ def refresh
268
+ changed_columns.clear
269
+ mapper.reload(self)
270
+ self
271
+ end
272
+
273
+ def destroy
274
+ deleted = false
275
+ trigger_hook(:before_destroy) do
276
+ delete
277
+ deleted = true
278
+ trigger_hook(:after_destroy)
279
+ self
280
+ end
281
+ return nil unless deleted
282
+ self
283
+ end
284
+
285
+ def delete
286
+ mapper.delete_instance(self)
287
+ end
288
+
289
+ def new?
290
+ @new
291
+ end
292
+
293
+ def id
294
+ @attributes[:id]
295
+ end
296
+
297
+ alias_method :pk, :id
298
+
299
+ def update(values)
300
+ set(values)
301
+ save
302
+ end
303
+
304
+ def set(values)
305
+ set_restricted(values)
306
+ end
307
+
308
+ def model
309
+ self.class
310
+ end
311
+
312
+ def set_restricted(attrs)
313
+ exclude = model.restricted_columns
314
+ attrs.each do |column, value|
315
+ send("#{column}=", value) unless exclude.include?(column.to_sym)
316
+ end
317
+ end
318
+
319
+ def set_attributes!(attributes)
320
+ @attributes = attributes
321
+ end
322
+
323
+ def set_attributes_after_save!(attributes)
324
+ set_attributes!(attributes)
325
+ changed_columns.clear
326
+ @new = false
327
+ end
328
+
329
+ def ==(obj)
330
+ eql?(obj)
331
+ end
332
+
333
+ def eql?(obj)
334
+ (obj.class == model) && (obj.attributes == @attributes)
335
+ end
336
+
337
+ def hash
338
+ [model, id.nil? ? @values.sort_by{|k,v| k.to_s} : id].hash
339
+ end
340
+ end
341
+ end
342
+ end
343
+ end
@@ -0,0 +1,103 @@
1
+ module Spontaneous
2
+ module DataMapper
3
+ class ContentTable
4
+ REVISION_PADDING = 5
5
+
6
+ def self.revision_table?(base_name, table_name)
7
+ /\A__r\d+_#{base_name}\z/ === table_name.to_s
8
+ end
9
+
10
+ def self.pad_revision_number(revision_number)
11
+ revision_number.to_s.rjust(REVISION_PADDING, "0")
12
+ end
13
+
14
+ def self.revision_table(base_name, revision_number)
15
+ return base_name if revision_number.nil?
16
+ "__r#{pad_revision_number(revision_number)}_#{base_name}".to_sym
17
+ end
18
+
19
+ # Extracts the revision number from a table name
20
+ #
21
+ # revision_number :content, :__r00034_content
22
+ # => 34
23
+ #
24
+ def self.revision_from_table(base_name, table_name)
25
+ return nil if base_name == table_name
26
+ return nil unless revision_table?(base_name, table_name)
27
+ if (match = /\A__r(\d+)_#{base_name}\z/.match(table_name.to_s))
28
+ rev = match[1]
29
+ return rev.to_i(10)
30
+ end
31
+ nil
32
+ end
33
+
34
+ attr_reader :name, :database
35
+
36
+ def initialize(name, database)
37
+ @name, @database = name, database
38
+ end
39
+
40
+ def columns
41
+ dataset.columns
42
+ end
43
+
44
+ def primary_key
45
+ :id
46
+ end
47
+
48
+ def dataset(revision = nil)
49
+ @database[revision_table(revision)]
50
+ end
51
+
52
+ def logger
53
+ @database.logger
54
+ end
55
+
56
+ def logger=(logger)
57
+ @database.logger = logger
58
+ end
59
+
60
+ alias_method :db, :database
61
+
62
+ def qualify(revision, col)
63
+ Sequel::SQL::QualifiedIdentifier.new(revision_table(revision), col)
64
+ end
65
+
66
+ def quote_identifier(identifier)
67
+ dataset.quote_identifier(identifier)
68
+ end
69
+
70
+ def revision_table(revision_number)
71
+ self.class.revision_table(@name, revision_number)
72
+ end
73
+
74
+ def revision_from_table(table)
75
+ self.class.revision_from_table(@name, table)
76
+ end
77
+
78
+ def revision_table?(table_name)
79
+ self.class.revision_table?(@name, table_name)
80
+ end
81
+
82
+ def pad_revision_number(revision_number)
83
+ self.class.pad_revision_number(revision_number)
84
+ end
85
+
86
+ def revision_history_table
87
+ :"spontaneous_#{@name}_history"
88
+ end
89
+
90
+ def revision_archive_table
91
+ :"spontaneous_#{@name}_archive"
92
+ end
93
+
94
+ def revision_history_dataset
95
+ @database[revision_history_table]
96
+ end
97
+
98
+ def revision_archive_dataset
99
+ @database[revision_archive_table]
100
+ end
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,194 @@
1
+ module Spontaneous
2
+ module DataMapper
3
+ class Dataset
4
+ include Enumerable
5
+
6
+ def initialize(dataset, schema, identity_map)
7
+ @dataset, @schema, @identity_map = dataset, schema, identity_map
8
+ end
9
+
10
+ def count
11
+ @dataset.count
12
+ end
13
+
14
+ def get(id)
15
+ return get_all(id) if id.is_a?(Array)
16
+ if (instance = @identity_map[id])
17
+ instance
18
+ else
19
+ first(id: id)
20
+ end
21
+ end
22
+
23
+ # Gets all the instances for the list of ids and returns them
24
+ # in the id order of the list, rather than the db order.
25
+ def get_all(id_list)
26
+ # This pure-db solution is Postgresql only unfortunately...
27
+ #
28
+ # values = []
29
+ # id_list.each_with_index do |id, index|
30
+ # values << ("(" << [id, index].join(",") << ")")
31
+ # end
32
+ # ds = @dataset.filter(:id => id_list).qualify.
33
+ # join(Sequel::LiteralString.new("(VALUES #{values.join(",")}) AS id_order (id, pos)"), {:id_order__id => :id}, {:qualify => true}).
34
+ # order(:id_order__pos)
35
+ order = Hash[id_list.each_with_index.map { |id, index| [id, index] }]
36
+ filter(:id => id_list).all.sort { |i1, i2|
37
+ order[i1.id] <=> order[i2.id]
38
+ }
39
+ end
40
+
41
+ def first(*args, &block)
42
+ load_instance @dataset.first(*args, &block)
43
+ end
44
+
45
+ def insert(*values, &block)
46
+ @dataset.insert(*values, &block)
47
+ end
48
+
49
+ def save(instance)
50
+ attributes = instance.modified_attributes
51
+ id = instance.id
52
+ return insert(instance) if id.nil?
53
+ return instance if attributes.empty?
54
+ @dataset.where(id: id).update(attributes)
55
+ instance
56
+ end
57
+
58
+ def update(columns)
59
+ @dataset.update(columns)
60
+ end
61
+
62
+ def create(instance)
63
+ id = @dataset.insert(attributes_for_insert(instance))
64
+ instance.set_attributes_after_save!(get_unfiltered_raw(id))
65
+ instance
66
+ end
67
+
68
+ def delete
69
+ @dataset.delete
70
+ end
71
+
72
+ def delete_instance(instance)
73
+ @dataset.unfiltered.filter(id: instance.id).delete
74
+ end
75
+
76
+ def destroy
77
+ @dataset.each do |row|
78
+ load_instance(row).destroy
79
+ end
80
+ end
81
+
82
+ def filter(*cond, &block)
83
+ @dataset.filter!(*cond, &block)
84
+ self
85
+ end
86
+
87
+ def where(*cond, &block)
88
+ @dataset.where!(*cond, &block)
89
+ self
90
+ end
91
+
92
+ def all
93
+ load_instances @dataset.all
94
+ end
95
+
96
+ def reload(instance)
97
+ attrs = get_raw(instance.id)
98
+ instance.set_attributes!(attrs)
99
+ end
100
+
101
+ def and(*cond, &block)
102
+ @dataset.and!(*cond, &block)
103
+ self
104
+ end
105
+
106
+ def or(*cond, &block)
107
+ @dataset.or!(*cond, &block)
108
+ self
109
+ end
110
+
111
+ def each
112
+ @dataset.each do |r|
113
+ yield load_instance(r)
114
+ end
115
+ end
116
+
117
+ def for_update
118
+ @dataset.for_update!
119
+ self
120
+ end
121
+
122
+ def order(*columns, &block)
123
+ @dataset.order!(*columns, &block)
124
+ self
125
+ end
126
+
127
+ def limit(l, o = (no_offset = true; nil))
128
+ @dataset.limit!(l, o)
129
+ self
130
+ end
131
+
132
+ def select(*columns, &block)
133
+ @dataset.select!(*columns, &block)
134
+ self
135
+ end
136
+
137
+ def qualify_to_first_source
138
+ @dataset = @dataset.qualify_to_first_source
139
+ self
140
+ end
141
+
142
+ def qualify(col)
143
+ SQL::QualifiedIdentifier.new(table, k)
144
+ end
145
+
146
+ def to_sql
147
+ @dataset.inspect
148
+ end
149
+
150
+ private
151
+
152
+ def get_raw(id)
153
+ @dataset.first(id: id)
154
+ end
155
+
156
+ def get_unfiltered_raw(id)
157
+ @dataset.unfiltered.first(id: id)
158
+ end
159
+
160
+ def attributes_for_insert(instance)
161
+ attributes_with_type_sid(instance.class, instance.attributes)
162
+ end
163
+
164
+ def attributes_with_type_sid(model, attributes)
165
+ attributes = attributes.dup
166
+ restricted = model.restricted_columns
167
+ restricted.each { |col| attributes.delete(col)}
168
+ attributes.update({
169
+ type_sid: @schema.to_id(model)
170
+ })
171
+ end
172
+
173
+ def load_instances(rows)
174
+ rows.map { |row| load_instance(row) }
175
+ end
176
+
177
+ def load_instance(attributes)
178
+ return nil if attributes.nil?
179
+ allocate_instance(attributes)
180
+ end
181
+
182
+ def allocate_instance(attributes)
183
+ return attributes if attributes[:type_sid].nil?
184
+ if (instance = @identity_map[attributes[:id]])
185
+ instance
186
+ else
187
+ model = @schema.to_class(attributes[:type_sid])
188
+ instance = model.new(attributes, true)
189
+ @identity_map[instance.id] = instance
190
+ end
191
+ end
192
+ end
193
+ end
194
+ end