spontaneous 0.2.0.beta1 → 0.2.0.beta2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (335) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +24 -0
  3. data/.locat +42 -0
  4. data/.travis/gemfiles/Gemfile.empty +7 -0
  5. data/.travis.yml +18 -0
  6. data/Gemfile +12 -8
  7. data/LICENSE +1 -1
  8. data/Rakefile +15 -157
  9. data/Readme.markdown +1 -1
  10. data/application/css/core.css.scss +22 -146
  11. data/application/css/definitions.css.scss +7 -3
  12. data/application/css/dialogue.css.scss +26 -1
  13. data/application/css/editing.css.scss +70 -28
  14. data/application/css/font.css.scss +1 -1
  15. data/application/css/popover.css.scss +2 -0
  16. data/application/css/top.css.scss +231 -0
  17. data/application/js/add_alias_dialogue.js +1 -1
  18. data/application/js/add_home_dialogue.js +1 -1
  19. data/application/js/ajax.js +61 -31
  20. data/application/js/box.js +4 -4
  21. data/application/js/conflicted_field_dialogue.js +1 -1
  22. data/application/js/content.js +5 -5
  23. data/application/js/dom.js +5 -0
  24. data/application/js/edit_panel.js +1 -0
  25. data/application/js/editing.js +1 -1
  26. data/application/js/extensions.js +8 -0
  27. data/application/js/field/boolean.js +31 -0
  28. data/application/js/field/file.js +32 -4
  29. data/application/js/field/image.js +24 -9
  30. data/application/js/field/markdown.js +87 -59
  31. data/application/js/field/select.js +1 -1
  32. data/application/js/field/webvideo.js +6 -1
  33. data/application/js/init.js +2 -2
  34. data/application/js/jquery-selection-position.js +130 -0
  35. data/application/js/location.js +4 -25
  36. data/application/js/meta_view/user_admin.js +2 -2
  37. data/application/js/metadata.js +2 -2
  38. data/application/js/page_browser.js +1 -1
  39. data/application/js/panel/root_menu.js +0 -1
  40. data/application/js/popover.js +27 -12
  41. data/application/js/popover_view.js +20 -4
  42. data/application/js/preview.js +31 -16
  43. data/application/js/progress.js +22 -21
  44. data/application/js/publish.js +18 -7
  45. data/application/js/sharded_upload.js +9 -6
  46. data/application/js/spontaneous.js +3 -1
  47. data/application/js/top_bar.js +264 -173
  48. data/application/js/upload.js +12 -5
  49. data/application/js/upload_manager.js +4 -3
  50. data/application/js/user.js +1 -2
  51. data/application/js/views/box_view.js +1 -1
  52. data/application/js/views/page_view.js +16 -5
  53. data/application/js/views/piece_view.js +5 -4
  54. data/application/static/font/fontawesome-webfont-1c66a4738b40ef0f6b1abca0ba9a796d.ttf +0 -0
  55. data/application/views/index.erb +6 -14
  56. data/application/views/login.erb +6 -25
  57. data/application/views/schema_modification_error.html.erb +3 -7
  58. data/db/migrations/20130114120000_create_revision_tables.rb +2 -2
  59. data/db/migrations/20130813111009_increase_path_length.rb +14 -0
  60. data/gem-public_cert.pem +20 -0
  61. data/lib/spontaneous/asset/app_compiler.rb +44 -0
  62. data/lib/spontaneous/asset/environment.rb +225 -0
  63. data/lib/spontaneous/asset.rb +2 -67
  64. data/lib/spontaneous/box.rb +0 -1
  65. data/lib/spontaneous/capistrano/deploy.rb +2 -2
  66. data/lib/spontaneous/capistrano/sync.rb +1 -1
  67. data/lib/spontaneous/cli/init.rb +36 -13
  68. data/lib/spontaneous/cli/server.rb +0 -1
  69. data/lib/spontaneous/cli/site.rb +2 -1
  70. data/lib/spontaneous/cli.rb +3 -1
  71. data/lib/spontaneous/collections/entry_set.rb +4 -12
  72. data/lib/spontaneous/collections/hash_with_fallback.rb +20 -0
  73. data/lib/spontaneous/collections/prototype_set.rb +6 -5
  74. data/lib/spontaneous/crypt.rb +2 -2
  75. data/lib/spontaneous/data_mapper/content_model/associations.rb +115 -63
  76. data/lib/spontaneous/data_mapper.rb +1 -1
  77. data/lib/spontaneous/errors.rb +6 -0
  78. data/lib/spontaneous/extensions/object_space.rb +6 -0
  79. data/lib/spontaneous/facet.rb +1 -0
  80. data/lib/spontaneous/field/base.rb +86 -13
  81. data/lib/spontaneous/field/boolean.rb +65 -0
  82. data/lib/spontaneous/field/file.rb +17 -6
  83. data/lib/spontaneous/field/html.rb +13 -0
  84. data/lib/spontaneous/field/image/size.rb +76 -0
  85. data/lib/spontaneous/field/image.rb +99 -414
  86. data/lib/spontaneous/field/tags.rb +36 -0
  87. data/lib/spontaneous/field/update.rb +1 -1
  88. data/lib/spontaneous/field/webvideo/fallback.rb +41 -0
  89. data/lib/spontaneous/field/webvideo/vimeo.rb +113 -0
  90. data/lib/spontaneous/field/webvideo/vine.rb +94 -0
  91. data/lib/spontaneous/field/webvideo/youtube.rb +133 -0
  92. data/lib/spontaneous/field/webvideo.rb +100 -250
  93. data/lib/spontaneous/field.rb +1 -1
  94. data/lib/spontaneous/generators/site/Gemfile.tt +5 -14
  95. data/lib/spontaneous/generators/site/assets/README.md +20 -0
  96. data/lib/spontaneous/generators/site/assets/css/site.scss +8 -0
  97. data/lib/spontaneous/generators/site/assets/js/site.js +6 -0
  98. data/lib/spontaneous/generators/site/config/deploy.rb.tt +9 -0
  99. data/lib/spontaneous/generators/site/config/user_levels.yml +14 -3
  100. data/lib/spontaneous/generators/site/public/README.md +12 -0
  101. data/lib/spontaneous/generators/site/templates/layouts/standard.html.cut.tt +2 -2
  102. data/lib/spontaneous/generators/site.rb +77 -35
  103. data/lib/spontaneous/layout.rb +6 -7
  104. data/lib/spontaneous/loader.rb +21 -13
  105. data/lib/spontaneous/media/file.rb +22 -9
  106. data/lib/spontaneous/media/image/attributes.rb +33 -0
  107. data/lib/spontaneous/media/image/format/gif.rb +4 -0
  108. data/lib/spontaneous/media/image/format/jpg.rb +17 -0
  109. data/lib/spontaneous/media/image/format/png.rb +4 -0
  110. data/lib/spontaneous/media/image/format/webp.rb +26 -0
  111. data/lib/spontaneous/media/image/format.rb +79 -0
  112. data/lib/spontaneous/media/image/optimizer.rb +69 -0
  113. data/lib/spontaneous/media/image/processor.rb +17 -0
  114. data/lib/spontaneous/media/image/renderable.rb +52 -0
  115. data/lib/spontaneous/media/image/skeptick.rb +70 -0
  116. data/lib/spontaneous/media/image.rb +50 -0
  117. data/lib/spontaneous/media/temp_file.rb +4 -0
  118. data/lib/spontaneous/media.rb +1 -0
  119. data/lib/spontaneous/model/core/aliases.rb +14 -8
  120. data/lib/spontaneous/model/core/boxes.rb +5 -2
  121. data/lib/spontaneous/model/core/entries.rb +4 -0
  122. data/lib/spontaneous/model/core/entry.rb +1 -0
  123. data/lib/spontaneous/model/core/fields.rb +5 -2
  124. data/lib/spontaneous/model/core/locks.rb +16 -0
  125. data/lib/spontaneous/model/core/media.rb +1 -15
  126. data/lib/spontaneous/model/core.rb +31 -1
  127. data/lib/spontaneous/model/page/controllers.rb +2 -2
  128. data/lib/spontaneous/model/page/formats.rb +1 -4
  129. data/lib/spontaneous/model/page/layouts.rb +6 -2
  130. data/lib/spontaneous/model/page/locks.rb +8 -2
  131. data/lib/spontaneous/model/page/page_tree.rb +2 -2
  132. data/lib/spontaneous/model/page/paths.rb +74 -9
  133. data/lib/spontaneous/model/page.rb +11 -3
  134. data/lib/spontaneous/model.rb +6 -6
  135. data/lib/spontaneous/output/context/render_cache.rb +23 -0
  136. data/lib/spontaneous/output/context.rb +56 -30
  137. data/lib/spontaneous/output/helpers/script_helper.rb +9 -53
  138. data/lib/spontaneous/output/helpers/stylesheet_helper.rb +8 -40
  139. data/lib/spontaneous/output/template/renderer.rb +17 -5
  140. data/lib/spontaneous/output.rb +0 -1
  141. data/lib/spontaneous/paths.rb +6 -2
  142. data/lib/spontaneous/permissions/access_key.rb +18 -0
  143. data/lib/spontaneous/permissions/user.rb +1 -1
  144. data/lib/spontaneous/permissions.rb +4 -1
  145. data/lib/spontaneous/plugins/application/state.rb +19 -12
  146. data/lib/spontaneous/prototypes/field_prototype.rb +14 -8
  147. data/lib/spontaneous/published_revision.rb +7 -0
  148. data/lib/spontaneous/publishing/immediate.rb +43 -34
  149. data/lib/spontaneous/publishing/revision.rb +9 -6
  150. data/lib/spontaneous/rack/asset_server.rb +20 -0
  151. data/lib/spontaneous/rack/back/alias.rb +46 -0
  152. data/lib/spontaneous/rack/back/application_assets.rb +28 -0
  153. data/lib/spontaneous/rack/back/base.rb +34 -0
  154. data/lib/spontaneous/rack/back/changes.rb +19 -0
  155. data/lib/spontaneous/rack/back/content.rb +54 -0
  156. data/lib/spontaneous/rack/back/events.rb +38 -0
  157. data/lib/spontaneous/rack/back/field.rb +37 -0
  158. data/lib/spontaneous/rack/back/file.rb +118 -0
  159. data/lib/spontaneous/rack/back/helpers.rb +71 -0
  160. data/lib/spontaneous/rack/back/index.rb +16 -0
  161. data/lib/spontaneous/rack/back/login.rb +47 -0
  162. data/lib/spontaneous/rack/back/map.rb +24 -0
  163. data/lib/spontaneous/rack/back/page.rb +46 -0
  164. data/lib/spontaneous/rack/back/preview.rb +43 -0
  165. data/lib/spontaneous/rack/back/schema.rb +30 -0
  166. data/lib/spontaneous/rack/back/site.rb +25 -0
  167. data/lib/spontaneous/rack/back/site_assets.rb +13 -0
  168. data/lib/spontaneous/rack/back/unsupported_browser.rb +7 -0
  169. data/lib/spontaneous/rack/{user_admin.rb → back/user_admin.rb} +2 -5
  170. data/lib/spontaneous/rack/back.rb +85 -764
  171. data/lib/spontaneous/rack/cacheable_file.rb +3 -3
  172. data/lib/spontaneous/rack/front.rb +16 -9
  173. data/lib/spontaneous/rack/middleware/authenticate.rb +65 -0
  174. data/lib/spontaneous/rack/middleware/csrf.rb +66 -0
  175. data/lib/spontaneous/rack/middleware/reloader.rb +52 -0
  176. data/lib/spontaneous/rack/middleware/scope.rb +60 -0
  177. data/lib/spontaneous/rack/middleware.rb +6 -0
  178. data/lib/spontaneous/rack/page_controller.rb +18 -5
  179. data/lib/spontaneous/rack/public.rb +17 -11
  180. data/lib/spontaneous/rack.rb +34 -24
  181. data/lib/spontaneous/revision.rb +29 -2
  182. data/lib/spontaneous/schema/uid.rb +4 -3
  183. data/lib/spontaneous/schema/uid_map.rb +5 -24
  184. data/lib/spontaneous/schema.rb +1 -0
  185. data/lib/spontaneous/search/database.rb +8 -0
  186. data/lib/spontaneous/search/field.rb +1 -1
  187. data/lib/spontaneous/search/index.rb +3 -5
  188. data/lib/spontaneous/server.rb +1 -1
  189. data/lib/spontaneous/simultaneous.rb +1 -1
  190. data/lib/spontaneous/site/features.rb +4 -5
  191. data/lib/spontaneous/site/helpers.rb +22 -5
  192. data/lib/spontaneous/site/instance.rb +2 -2
  193. data/lib/spontaneous/site/selectors.rb +22 -3
  194. data/lib/spontaneous/storage/cloud.rb +13 -9
  195. data/lib/spontaneous/storage/local.rb +11 -6
  196. data/lib/spontaneous/style.rb +40 -23
  197. data/lib/spontaneous/utils/database/mysql_dumper.rb +1 -1
  198. data/lib/spontaneous/utils/smush_it.rb +1 -1
  199. data/lib/spontaneous/version.rb +1 -1
  200. data/lib/spontaneous.rb +35 -33
  201. data/spontaneous.gemspec +53 -787
  202. data/test/experimental/test_crypt.rb +56 -56
  203. data/test/experimental/test_features.rb +16 -27
  204. data/test/fixtures/assets/public1/css/data.css.scss +3 -0
  205. data/test/fixtures/assets/public1/css/image1.css.scss +4 -0
  206. data/test/fixtures/assets/public1/css/import.css.scss +1 -0
  207. data/test/fixtures/assets/public1/css/urlhash.css.scss +3 -0
  208. data/test/fixtures/assets/public1/js/a.js +1 -1
  209. data/test/fixtures/assets/public1/js/all.js +4 -0
  210. data/test/fixtures/assets/public1/js/{m.coffee → m.js.coffee} +1 -0
  211. data/test/fixtures/assets/public1/x.js +1 -0
  212. data/test/fixtures/assets/public2/css/all.css +4 -0
  213. data/test/fixtures/assets/public2/css/missing.css.scss +3 -0
  214. data/test/fixtures/assets/public2/i/y.png +0 -0
  215. data/test/fixtures/assets/public2/js/b.js +1 -1
  216. data/test/fixtures/assets/public2/js/c.js +1 -1
  217. data/test/fixtures/images/size.extended.webp +0 -0
  218. data/test/fixtures/images/size.lossless.webp +0 -0
  219. data/test/fixtures/images/size.lossy.webp +0 -0
  220. data/test/fixtures/schema/before.yml +4 -4
  221. data/test/fixtures/schema/schema.yml +1 -1
  222. data/test/fixtures/templates/aliases/aaa.html.cut +0 -0
  223. data/test/fixtures/templates/extended/partial_with_renderer.html.cut +1 -0
  224. data/test/fixtures/templates/extended/with_includes_and_renderer.html.cut +2 -0
  225. data/test/functional/test_application.rb +108 -106
  226. data/test/functional/test_back.rb +924 -930
  227. data/test/functional/test_front.rb +285 -238
  228. data/test/functional/test_user_manager.rb +75 -100
  229. data/test/integration/test_installation.rb +1 -1
  230. data/test/support/matchers.rb +12 -0
  231. data/test/support/minitest.rb +121 -0
  232. data/test/support/rack.rb +45 -0
  233. data/test/support/test_start_finish.rb +103 -0
  234. data/test/test_helper.rb +21 -68
  235. data/test/test_integration_helper.rb +1 -3
  236. data/test/unit/test_alias.rb +432 -408
  237. data/test/unit/test_asset_bundler.rb +58 -58
  238. data/test/unit/test_assets.rb +485 -155
  239. data/test/unit/test_async.rb +16 -37
  240. data/test/unit/test_authentication.rb +425 -457
  241. data/test/unit/test_boxes.rb +191 -191
  242. data/test/unit/test_changesets.rb +244 -254
  243. data/test/unit/test_config.rb +128 -142
  244. data/test/unit/test_content.rb +313 -359
  245. data/test/unit/test_content_inheritance.rb +29 -30
  246. data/test/unit/test_datamapper.rb +1205 -1080
  247. data/test/unit/test_datamapper_content.rb +49 -51
  248. data/test/unit/test_extensions.rb +23 -23
  249. data/test/unit/test_fields.rb +1488 -1180
  250. data/test/unit/test_formats.rb +158 -158
  251. data/test/unit/test_generators.rb +98 -40
  252. data/test/unit/test_helpers.rb +73 -76
  253. data/test/unit/test_image_size.rb +53 -22
  254. data/test/unit/test_images.rb +164 -165
  255. data/test/unit/test_layouts.rb +133 -122
  256. data/test/unit/test_logger.rb +14 -17
  257. data/test/unit/test_media.rb +69 -84
  258. data/test/unit/test_modifications.rb +513 -525
  259. data/test/unit/test_page.rb +462 -361
  260. data/test/unit/test_permissions.rb +379 -364
  261. data/test/unit/test_piece.rb +67 -75
  262. data/test/unit/test_plugins.rb +82 -89
  263. data/test/unit/test_prototype_set.rb +215 -216
  264. data/test/unit/test_prototypes.rb +114 -124
  265. data/test/unit/test_publishing.rb +252 -289
  266. data/test/unit/test_render.rb +167 -115
  267. data/test/unit/test_revisions.rb +436 -444
  268. data/test/unit/test_schema.rb +339 -309
  269. data/test/unit/test_search.rb +577 -574
  270. data/test/unit/test_serialisation.rb +136 -147
  271. data/test/unit/test_site.rb +252 -227
  272. data/test/unit/test_skeptick.rb +130 -0
  273. data/test/unit/test_storage.rb +46 -40
  274. data/test/unit/test_structure.rb +57 -66
  275. data/test/unit/test_styles.rb +104 -104
  276. data/test/unit/test_templates.rb +72 -57
  277. data/test/unit/test_type_hierarchy.rb +15 -16
  278. data/test/unit/test_visibility.rb +239 -257
  279. metadata +455 -326
  280. data/application/js/vendor/JS.Class-2.1.5/CHANGELOG +0 -283
  281. data/application/js/vendor/JS.Class-2.1.5/MIT-LICENSE +0 -30
  282. data/application/js/vendor/JS.Class-2.1.5/README +0 -30
  283. data/application/js/vendor/JS.Class-2.1.5/min/command.js +0 -1
  284. data/application/js/vendor/JS.Class-2.1.5/min/comparable.js +0 -1
  285. data/application/js/vendor/JS.Class-2.1.5/min/constant_scope.js +0 -1
  286. data/application/js/vendor/JS.Class-2.1.5/min/decorator.js +0 -1
  287. data/application/js/vendor/JS.Class-2.1.5/min/enumerable.js +0 -1
  288. data/application/js/vendor/JS.Class-2.1.5/min/forwardable.js +0 -1
  289. data/application/js/vendor/JS.Class-2.1.5/min/hash.js +0 -1
  290. data/application/js/vendor/JS.Class-2.1.5/min/linked_list.js +0 -1
  291. data/application/js/vendor/JS.Class-2.1.5/min/loader.js +0 -1
  292. data/application/js/vendor/JS.Class-2.1.5/min/method_chain.js +0 -1
  293. data/application/js/vendor/JS.Class-2.1.5/min/observable.js +0 -1
  294. data/application/js/vendor/JS.Class-2.1.5/min/package.js +0 -1
  295. data/application/js/vendor/JS.Class-2.1.5/min/proxy.js +0 -1
  296. data/application/js/vendor/JS.Class-2.1.5/min/ruby.js +0 -1
  297. data/application/js/vendor/JS.Class-2.1.5/min/set.js +0 -1
  298. data/application/js/vendor/JS.Class-2.1.5/min/stack_trace.js +0 -1
  299. data/application/js/vendor/JS.Class-2.1.5/min/state.js +0 -1
  300. data/application/js/vendor/JS.Class-2.1.5/min/stdlib.js +0 -16
  301. data/application/js/vendor/jquery-1.6.2.min.js +0 -18
  302. data/application/js/vendor/jquery-ui-1.8.16.custom.min.js +0 -791
  303. data/application/js/vendor/jquery-ui-1.8.9.custom.min.js +0 -415
  304. data/application/static/font/fontawesome-webfont-5c5c21100a346972a82c34c5e96ffcfe.ttf +0 -0
  305. data/application/static/select-arrow-6e7dd3745b00e934b0d7a3250c46558b.png +0 -0
  306. data/bin/limit-upload +0 -5
  307. data/bin/unlimit-upload +0 -3
  308. data/lib/spontaneous/asset/file.rb +0 -25
  309. data/lib/spontaneous/asset/source.rb +0 -28
  310. data/lib/spontaneous/image_size.rb +0 -123
  311. data/lib/spontaneous/output/assets/compression.rb +0 -58
  312. data/lib/spontaneous/output/assets.rb +0 -32
  313. data/lib/spontaneous/rack/around_back.rb +0 -20
  314. data/lib/spontaneous/rack/around_front.rb +0 -27
  315. data/lib/spontaneous/rack/around_preview.rb +0 -22
  316. data/lib/spontaneous/rack/assets.rb +0 -126
  317. data/lib/spontaneous/rack/authentication.rb +0 -20
  318. data/lib/spontaneous/rack/cookie_authentication.rb +0 -38
  319. data/lib/spontaneous/rack/helpers.rb +0 -52
  320. data/lib/spontaneous/rack/http.rb +0 -18
  321. data/lib/spontaneous/rack/media.rb +0 -30
  322. data/lib/spontaneous/rack/query_authentication.rb +0 -35
  323. data/lib/spontaneous/rack/reloader.rb +0 -45
  324. data/lib/spontaneous/rack/user_helpers.rb +0 -28
  325. /data/{README → application/js/field/markdown/text_command.js} +0 -0
  326. /data/application/js/vendor/{JS.Class-2.1.5/min/core.js → js.class-2.1.5.min.js} +0 -0
  327. /data/test/fixtures/assets/public1/css/{a.scss → a.css.scss} +0 -0
  328. /data/{lib/spontaneous/generators/site/public/css/site.scss → test/fixtures/assets/public1/x.css} +0 -0
  329. /data/{lib/spontaneous/generators/site/public/js/.empty_directory → test/fixtures/assets/public1/x.png} +0 -0
  330. /data/test/fixtures/assets/public2/css/{b.scss → b.css.scss} +0 -0
  331. /data/test/fixtures/assets/public2/js/{n.coffee → n.js.coffee} +0 -0
  332. /data/test/fixtures/back/{public → assets}/css/sass_include.scss +0 -0
  333. /data/test/fixtures/back/{public → assets}/css/sass_template.scss +0 -0
  334. /data/test/fixtures/back/{public → assets}/js/coffeescript.coffee +0 -0
  335. /data/{lib/spontaneous/generators/site/public/js/site.js → test/fixtures/templates/aliases/aa_alias.html.cut} +0 -0
@@ -2,8 +2,8 @@ module Spontaneous
2
2
  module Field
3
3
  class Base
4
4
  module ClassMethods
5
- def has_editor
6
- define_singleton_method(:editor_class) { ui_class }
5
+ def has_editor(js_class = ui_class)
6
+ define_singleton_method(:editor_class) { js_class }
7
7
  end
8
8
 
9
9
  def register(*labels)
@@ -44,6 +44,10 @@ module Spontaneous
44
44
  end
45
45
  end
46
46
 
47
+ def default_options
48
+ {}
49
+ end
50
+
47
51
  # Provides the ability for specific field types to customize the schema values
48
52
  # they return to the UI
49
53
  def export(user)
@@ -54,20 +58,57 @@ module Spontaneous
54
58
  extend ClassMethods
55
59
 
56
60
  attr_accessor :owner, :name, :unprocessed_value, :template_params, :version
57
- attr_reader :processed_values
58
-
59
- alias_method :values, :processed_values
61
+ attr_accessor :prototype
60
62
 
61
63
 
62
64
  def initialize(params={}, default_values=true)
63
65
  @processed_values = {}
64
66
  deserialize(params, default_values)
67
+ @values = nil
68
+ end
69
+
70
+ def processed_values
71
+ @values ||= processed_values_with_fallback
72
+ end
73
+
74
+ alias_method :values, :processed_values
75
+
76
+ ValueHash = Spontaneous::Collections::HashWithFallback
77
+
78
+ def processed_values_with_fallback
79
+ return @processed_values if owner.nil? || prototype.fallback.nil?
80
+ fallback = owner.fields[prototype.fallback]
81
+ if fallback.nil?
82
+ logger.warn("Missing field '#{prototype.fallback}' specified as fallback for field #{owner.class}::#{name}")
83
+ return @processed_values
84
+ end
85
+ test = proc { |val| self.blank? }
86
+ ValueHash.new(fallback, test).update(@processed_values)
87
+ end
88
+
89
+ def [](key)
90
+ processed_values[key]
65
91
  end
66
92
 
67
93
  def id
68
94
  [owner.id, schema_id].join("/")
69
95
  end
70
96
 
97
+ # This is used exclusively to compute the filename/path of
98
+ # temp files.
99
+ #
100
+ # To avoid creating a deep hierarchy under /media/tmp
101
+ # which would be hard to cleanup we replace all dir separators
102
+ # with underscores.
103
+ #
104
+ # The timestamp is added because otherwise serial modifications
105
+ # to the same field might overwrite the value (though this is
106
+ # unlikely)
107
+ def media_id
108
+ ids = [owner.id, schema_id, timestamp]
109
+ ids.join("_").gsub(/\//, "_")
110
+ end
111
+
71
112
  def writable?(user)
72
113
  owner.field_writable?(user, name)
73
114
  end
@@ -83,10 +124,18 @@ module Spontaneous
83
124
  values[:__pending__] = {
84
125
  :value => value,
85
126
  :version => version + 1,
86
- :timestamp => Spontaneous::Field.timestamp
127
+ :timestamp => timestamp
87
128
  }
88
129
  end
89
130
 
131
+ # A timestamp value consistent for a particular field instance
132
+ #
133
+ # This is used to solve conflicts for async updates and to
134
+ # tag a tempfile to a particular pending value.
135
+ def timestamp
136
+ @timestamp ||= Spontaneous::Field.timestamp
137
+ end
138
+
90
139
  def pending_value
91
140
  values[:__pending__]
92
141
  end
@@ -100,23 +149,39 @@ module Spontaneous
100
149
  end
101
150
 
102
151
  def process_pending_value
103
- process_pending_value!
152
+ if (pending = process_pending_value!)
153
+ cleanup_pending_value!(pending)
154
+ end
104
155
  save
105
156
  end
106
157
 
107
158
  def process_pending_value!
108
159
  if has_pending_value?
160
+ pending = pending_value
109
161
  @previous_values = values.dup
110
162
  set_value!(pending_value[:value])
163
+ pending
111
164
  end
112
165
  end
113
166
 
167
+ # Ensures that this update can still run
168
+ def invalid_update?
169
+ return true if reload.nil?
170
+ false
171
+ end
172
+
173
+ # Ensures that the pending value we have hasn't been superceded by
174
+ # a later one.
114
175
  def conflicted_update?
115
176
  return false if is_valid_pending_value?
116
177
  self.processed_values = @previous_values
117
178
  true
118
179
  end
119
180
 
181
+ # Reloads the field and compares the timestamps -- if our timestamp
182
+ # is the same or greater than the reloaded value then we are the
183
+ # most up-to-date update available. If not then we're not and
184
+ # should abort.
120
185
  def is_valid_pending_value?
121
186
  return true if @previous_values.nil?
122
187
  reloaded = reload
@@ -131,6 +196,15 @@ module Spontaneous
131
196
  end
132
197
  end
133
198
 
199
+ def cleanup_pending_value!(pending)
200
+ clear_pending_value
201
+ if pending && (v = pending[:value]) && v.is_a?(Hash)
202
+ if (tempfile = v[:tempfile]) && ::File.exist?(tempfile)
203
+ FileUtils.rm_r(::File.dirname(tempfile))
204
+ end
205
+ end
206
+ end
207
+
134
208
  def reload
135
209
  Spontaneous::Content.scope! do
136
210
  Spontaneous::Field.find(self.id)
@@ -230,8 +304,11 @@ module Spontaneous
230
304
  # override this to return custom values derived from (un)processed_value
231
305
  # alias_method :value, :processed_value
232
306
  def value(format=:html)
233
- processed_values[format.to_sym] || unprocessed_value
307
+ format = format.to_sym
308
+ return unprocessed_value unless processed_values.key?(format)
309
+ processed_values[format]
234
310
  end
311
+
235
312
  alias_method :processed_value, :value
236
313
 
237
314
  def image?
@@ -280,11 +357,6 @@ module Spontaneous
280
357
  @modified
281
358
  end
282
359
 
283
- attr_accessor :prototype
284
- # def prototype
285
- # self.class.prototype
286
- # end
287
-
288
360
  def schema_id
289
361
  self.prototype.schema_id
290
362
  end
@@ -397,6 +469,7 @@ module Spontaneous
397
469
  end
398
470
 
399
471
  def processed_values=(values)
472
+ @values = nil
400
473
  @processed_values = values
401
474
  end
402
475
 
@@ -0,0 +1,65 @@
1
+ # encoding: UTF-8
2
+
3
+ module Spontaneous::Field
4
+ class Boolean < Base
5
+ has_editor "Spontaneous.Field.Boolean"
6
+
7
+ def self.default_options
8
+ {default: true, true: "Yes", false: "No"}
9
+ end
10
+
11
+ def self.export(user)
12
+ super.merge({
13
+ labels: { true: prototype.options[:true], false: prototype.options[:false] }
14
+ })
15
+ end
16
+
17
+ def outputs
18
+ [:boolean, :html, :string]
19
+ end
20
+
21
+ def value(format = :boolean)
22
+ super(format)
23
+ end
24
+
25
+ def checked?
26
+ value(:boolean)
27
+ end
28
+
29
+ alias_method :on?, :checked?
30
+ alias_method :enabled?, :checked?
31
+
32
+ def preprocess(value)
33
+ case value
34
+ when TrueClass, FalseClass
35
+ value
36
+ when "", nil
37
+ o = self.class.prototype.options
38
+ return o[:default] if o.key?(:default)
39
+ true
40
+ when "1", "true"
41
+ true
42
+ else
43
+ false
44
+ end
45
+ end
46
+
47
+ def generate_boolean(state)
48
+ state
49
+ end
50
+
51
+ def generate_html(state)
52
+ string_value(state)
53
+ end
54
+
55
+ def generate_string(state)
56
+ string_value(state)
57
+ end
58
+
59
+ def string_value(state)
60
+ self.class.prototype.options[(state ? :true : :false)]
61
+ end
62
+
63
+ self.register(:boolean, :switch)
64
+ end
65
+ end
@@ -11,7 +11,7 @@ module Spontaneous::Field
11
11
  end
12
12
 
13
13
  def outputs
14
- [:html, :filesize, :filename]
14
+ [:html, :path, :filesize, :filename]
15
15
  end
16
16
 
17
17
  def pending_value=(value)
@@ -30,7 +30,15 @@ module Spontaneous::Field
30
30
  end
31
31
 
32
32
  def serialize_pending_file(file)
33
- { :tempfile => file.path, :type => file.mimetype, :filename => file.filename, :src => file.url }
33
+ { :tempfile => file.path, :type => file.mimetype, :filename => file.filename, :filesize => file.filesize, :src => file.url }
34
+ end
35
+
36
+ def storage_headers(content_type, filename)
37
+ headers = { content_type: content_type }
38
+ if prototype.options[:attachment]
39
+ headers.update(content_disposition: %(attachment; filename=#{Rack::Utils.escape(filename)}))
40
+ end
41
+ headers
34
42
  end
35
43
 
36
44
  def preprocess(image)
@@ -38,7 +46,7 @@ module Spontaneous::Field
38
46
  return "" if file.nil?
39
47
  return file unless ::File.exist?(file)
40
48
 
41
- media_file = Spontaneous::Media::File.new(owner, filename, mimetype)
49
+ media_file = Spontaneous::Media::File.new(owner, filename, storage_headers(mimetype, filename))
42
50
  media_file.copy(file)
43
51
  set_unprocessed_value(media_file.path)
44
52
  media_file
@@ -46,7 +54,7 @@ module Spontaneous::Field
46
54
 
47
55
  def process_upload(value)
48
56
  file, filename, mimetype = fileinfo(value)
49
- media_file = Spontaneous::Media::TempFile.new(owner, filename, mimetype)
57
+ media_file = Spontaneous::Media::TempFile.new(self, filename, storage_headers(mimetype, filename))
50
58
  media_file.copy(file)
51
59
  media_file
52
60
  end
@@ -63,7 +71,6 @@ module Spontaneous::Field
63
71
  [file, filename, mimetype]
64
72
  end
65
73
 
66
-
67
74
  def generate_filesize(input)
68
75
  if input.respond_to?(:filesize)
69
76
  input.filesize
@@ -80,11 +87,15 @@ module Spontaneous::Field
80
87
  end
81
88
  end
82
89
 
83
- def generate_html(input)
90
+ def generate_path(input)
84
91
  return input if input.is_a?(::String)
85
92
  input.url
86
93
  end
87
94
 
95
+ def generate_html(input)
96
+ generate_path(input)
97
+ end
98
+
88
99
  def export(user = nil)
89
100
  super(user).merge({
90
101
  :processed_value => processed_values
@@ -0,0 +1,13 @@
1
+ # encoding: UTF-8
2
+
3
+ module Spontaneous::Field
4
+ class HTML < LongString
5
+
6
+ # Just pass through the value without any kind of escaping
7
+ def generate_html(value)
8
+ value
9
+ end
10
+
11
+ self.register
12
+ end
13
+ end
@@ -0,0 +1,76 @@
1
+ module Spontaneous::Field
2
+ class Image
3
+ class Size
4
+ include Spontaneous::Media::Image::Renderable
5
+
6
+ attr_reader :path
7
+
8
+ def initialize(input, name, options, process)
9
+ @input, @name, @process = input, name, process
10
+ @options = {:optimize => true}.merge(options)
11
+ end
12
+
13
+ def generate
14
+ tempfile do |tempfile|
15
+ convert(tempfile)
16
+ file = @input.rename(filename)
17
+ file.copy(tempfile)
18
+ serialize(file)
19
+ end
20
+ end
21
+
22
+ def serialize(file)
23
+ values = file.serialize
24
+ path = values.delete(:path)
25
+ src = values.delete(:url)
26
+ image = Spontaneous::Media::Image.new(file.source)
27
+ values.update(image.attributes).update(:src => src)
28
+ end
29
+
30
+ def convert(tempfile)
31
+ processor = Spontaneous::Media::Image::Processor.new(path, tempfile.path)
32
+ processor.apply(process)
33
+ optimize(tempfile) if @options[:optimize]
34
+ end
35
+
36
+ def optimize(tempfile)
37
+ Spontaneous::Media::Image::Optimizer.run(tempfile.path)
38
+ end
39
+
40
+ def process
41
+ @process || Proc.new { }
42
+ end
43
+
44
+ def path
45
+ @input.source
46
+ end
47
+
48
+ def extname
49
+ (format = @options[:format]) ? ".#{format}" : @input.extname
50
+ end
51
+
52
+ def tempfile(&block)
53
+ dir = Dir.mktmpdir
54
+ name = "#{@name}#{extname}"
55
+ path = ::File.join(dir, name)
56
+ ::File.open(path, "w+b") do |file|
57
+ block.call file
58
+ end
59
+ ensure
60
+ FileUtils.rm_rf(dir) rescue nil
61
+ end
62
+
63
+ def filename
64
+ original_filename = @input.filename
65
+ parts = original_filename.split('.')
66
+ base = parts[0..-2].join('.')
67
+ size = @name == :original ? "" : ".#{@name}"
68
+ "#{base}#{size}#{extname}"
69
+ end
70
+
71
+ def inspect
72
+ %(#<Spontaneous::Field::Image::Size name=:#{@name} options=#{@options.inspect}>)
73
+ end
74
+ end
75
+ end
76
+ end