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,17 @@
1
+ Sequel.migration do
2
+ up do
3
+ create_table!(:spontaneous_page_lock, :engine => "INNODB", :charset => "UTF8", :collate => "utf8_general_ci") do
4
+ primary_key :id
5
+
6
+ Integer :page_id, :index => true
7
+ Integer :content_id, :index => true
8
+ String :field_id
9
+ DateTime :created_at
10
+ String :description
11
+ end
12
+ end
13
+
14
+ down do
15
+ drop_table :spontaneous_page_lock
16
+ end
17
+ end
@@ -0,0 +1,22 @@
1
+
2
+ Sequel.migration do
3
+ up do
4
+ scheme = Spontaneous::Crypt::Version::SHALegacy
5
+
6
+ dataset = self[:spontaneous_users]
7
+
8
+ dataset.each do |user|
9
+ hash = scheme.create(user[:crypted_password], user[:salt])
10
+ dataset.filter(:id => user[:id]).update(:crypted_password => hash)
11
+ end
12
+
13
+ drop_column :spontaneous_users, :salt
14
+ end
15
+
16
+ down do
17
+ # There's no way to recover from the conversion -- the
18
+ # hashed password is lost so all we can do is re-add the salt
19
+ # column
20
+ add_column :spontaneous_users, :salt
21
+ end
22
+ end
@@ -0,0 +1,106 @@
1
+
2
+ require 'logger'
3
+ require 'spontaneous'
4
+ require 'benchmark'
5
+
6
+ Sequel.migration do
7
+ ContentTable = S::DataMapper::ContentTable
8
+ revision_table = :spontaneous_content_revisions
9
+ archive_table = :spontaneous_content_archive
10
+ content_table = :content
11
+
12
+ keep_revisions = 10
13
+
14
+ logger = ::Logger.new($stdout)
15
+
16
+ up do
17
+
18
+ add_column content_table, :revision, :integer, :default => nil
19
+
20
+ state = self[:spontaneous_state].first
21
+
22
+ current_revision = state.nil? ? 0 : state[:published_revision]
23
+
24
+ [revision_table, archive_table].each do |table|
25
+ drop_table?(table)
26
+ run %(CREATE TABLE #{literal(table)} AS (SELECT * FROM #{literal(content_table)} LIMIT 1);)
27
+ end
28
+
29
+
30
+ tables = self.tables
31
+ revisions = tables.select { |t| ContentTable.revision_table?(content_table, t)}.sort
32
+
33
+ revisions = revisions.map { |name| [name, ContentTable.revision_number(content_table, name)] }
34
+
35
+ unless revisions.empty?
36
+
37
+ max_revision = revisions.map { |(table, revision)| revision }.max
38
+
39
+
40
+ keep_revision = max_revision - keep_revisions
41
+
42
+ columns = self[content_table].columns.
43
+ reject { |c| c == :revision }.
44
+ map { |c| literal(c)}
45
+
46
+ insert_columns = (columns + [literal(:revision)]).join(", ")
47
+ select_columns = columns.join(", ")
48
+
49
+ self.logger = nil
50
+
51
+ revisions.each do |(table, revision)|
52
+ begin
53
+ print ">> Copying revision #{revision} from table #{table} ... "
54
+ bm = Benchmark.measure do
55
+ transaction do
56
+ dest = archive_table
57
+ dest = revision_table if revision > keep_revision
58
+ run <<-SQL
59
+ INSERT #{literal(dest)} (#{insert_columns})
60
+ SELECT #{select_columns}, #{revision}
61
+ FROM #{literal(table)}
62
+ SQL
63
+ drop_table table unless revision == current_revision
64
+ end
65
+ end
66
+ puts " done (#{"%.4f" % bm.real}s)"
67
+ rescue Sequel::DatabaseError => e
68
+ logger.error(e.message)
69
+ end
70
+ end
71
+
72
+ self.logger = logger
73
+
74
+ # Dont want an index on the archive because the assumption is that we won't
75
+ # need it except for archival purposes and the index will add space & slow
76
+ # things down.
77
+ add_index revision_table, :revision
78
+
79
+ # Because of errors some of the revisions will have been left.
80
+ # Just delete them.
81
+ current_revision_table = ContentTable.revision_table(content_table, current_revision)
82
+ drop_tables = self.tables.
83
+ select { |t| ContentTable.revision_table?(content_table, t) }.
84
+ reject { |t| t == current_revision_table }
85
+
86
+ drop_tables.each do |table|
87
+ drop_table table
88
+ end
89
+ end
90
+ self.logger = nil
91
+ end
92
+
93
+ down do
94
+ # Not going to do a full down implementation until I need to
95
+ # If things have gone wrong then all is lost anyway, and I won't be able
96
+ # to recover the lost revision data.
97
+ # If all is well then recovering the revision tables from the archives
98
+ # is trivial (but dull).
99
+ [revision_table, archive_table].each do |table|
100
+ drop_table table
101
+ end
102
+
103
+ drop_column content_table, :revision
104
+ end
105
+ end
106
+
@@ -0,0 +1,9 @@
1
+ Sequel.migration do
2
+ up do
3
+ rename_table :spontaneous_content_revisions, :spontaneous_content_history
4
+ end
5
+
6
+ down do
7
+ rename_table :spontaneous_content_history, :spontaneous_content_revisions
8
+ end
9
+ end
@@ -4,14 +4,14 @@ module Spontaneous
4
4
  class Box
5
5
  include Enumerable
6
6
 
7
- include Plugins::SchemaHierarchy
8
- include Plugins::Fields
9
- include Plugins::Styles
10
- include Plugins::Serialisation
11
- include Plugins::Render
12
- include Plugins::AllowedTypes
13
- include Plugins::Permissions
14
- include Plugins::Media
7
+ include Spontaneous::Model::Core::SchemaHierarchy
8
+ include Spontaneous::Model::Core::Fields
9
+ include Spontaneous::Model::Core::Styles
10
+ include Spontaneous::Model::Core::Serialisation
11
+ include Spontaneous::Model::Core::Render
12
+ include Spontaneous::Model::Box::AllowedTypes
13
+ include Spontaneous::Model::Core::Permissions
14
+ include Spontaneous::Model::Core::Media
15
15
 
16
16
  # use underscores to protect against field name conflicts
17
17
  attr_reader :_name, :_prototype, :owner
@@ -23,6 +23,10 @@ module Spontaneous
23
23
  # Returns: the owning Content object
24
24
  alias_method :parent, :owner
25
25
 
26
+ class << self
27
+ attr_reader :mapper
28
+ end
29
+
26
30
  def self.page?
27
31
  false
28
32
  end
@@ -41,7 +45,7 @@ module Spontaneous
41
45
  end
42
46
 
43
47
  def self.schema_id
44
- Spontaneous.schema.uids[@schema_id] || Spontaneous.schema.schema_id(self)
48
+ mapper.schema.uids[@schema_id] || mapper.schema.to_id(self)
45
49
  end
46
50
 
47
51
  # This is overridden by anonymous classes defined by box prototypes
@@ -50,7 +54,6 @@ module Spontaneous
50
54
  "type//#{self.name}"
51
55
  end
52
56
 
53
-
54
57
  def self.supertype
55
58
  if self == Spontaneous::Box
56
59
  nil
@@ -72,6 +75,9 @@ module Spontaneous
72
75
  @field_initialization = false
73
76
  end
74
77
 
78
+ def model
79
+ @owner.model
80
+ end
75
81
 
76
82
  def page?
77
83
  false
@@ -87,8 +93,10 @@ module Spontaneous
87
93
  self.class.schema_id
88
94
  end
89
95
 
96
+ # A boxes "identity" must be a combination of its owner's id and
97
+ # its schema_id.
90
98
  def id
91
- schema_id.to_s
99
+ [owner.id, schema_id.to_s].join("/")
92
100
  end
93
101
 
94
102
  def schema_name
@@ -123,6 +131,9 @@ module Spontaneous
123
131
  _name.to_s
124
132
  end
125
133
 
134
+ def reload
135
+ owner.reload
136
+ end
126
137
  # needed by Render::Context
127
138
  def box?(box_name)
128
139
  false
@@ -141,7 +152,7 @@ module Spontaneous
141
152
  default_values.each do |field_name, value|
142
153
  if self.field?(field_name)
143
154
  field = self.class.field_prototypes[field_name].to_field(self)
144
- field.unprocessed_value = value
155
+ field.value = value
145
156
  field_store << field.serialize_db
146
157
  end
147
158
  end
@@ -149,16 +160,36 @@ module Spontaneous
149
160
  field_store
150
161
  end
151
162
 
152
- def field_modified!(modified_field)
163
+ def field_modified!(modified_field = nil)
164
+ save_fields!
165
+ end
166
+
167
+ def save_fields(fields = nil)
168
+ save_fields!(fields)
169
+ save
170
+ end
171
+
172
+ # Use @serialized_fields to temporarily overwrite the value of
173
+ # #serialized_fields because this call may be coming from an async
174
+ # process that only wants to update a subset of the field values
175
+ # and because we don't have direct access to the serialization
176
+ # store we have to control our serialization output.
177
+ # TODO: Make boxes responsible for directly writing their serialized
178
+ # form
179
+ def save_fields!(fields = nil)
153
180
  @modified = true
181
+ @serialized_fields = update_serialized_fields(fields)
154
182
  owner.box_modified!(self)
183
+ @serialized_fields = nil
155
184
  end
156
185
 
157
186
  def serialize_db
158
- {
159
- :box_id => schema_id.to_s,
160
- :fields => fields.serialize_db
161
- }
187
+ { :box_id => schema_id.to_s,
188
+ :fields => serialized_fields }
189
+ end
190
+
191
+ def serialized_fields
192
+ @serialized_fields || fields.serialize_db
162
193
  end
163
194
 
164
195
  def self.resolve_style(box)
@@ -177,6 +208,10 @@ module Spontaneous
177
208
  owner
178
209
  end
179
210
 
211
+ def content_instance
212
+ owner
213
+ end
214
+
180
215
  def page
181
216
  owner.page
182
217
  end
@@ -224,7 +259,7 @@ module Spontaneous
224
259
  end
225
260
 
226
261
  def pieces
227
- contents.select { |e| e.is_a?(Spontaneous::Piece) }
262
+ contents.select { |e| e.is_a?(Spontaneous::Model::Piece) }
228
263
  end
229
264
 
230
265
  def [](index)
@@ -6,7 +6,7 @@ module Spontaneous
6
6
  attr_reader :box
7
7
 
8
8
  def self.excluded_classes
9
- [Spontaneous::Box].tap do |classes|
9
+ [Spontaneous::Box, Spontaneous::Content::Box].tap do |classes|
10
10
  classes.push(::Box) if defined?(::Box)
11
11
  end
12
12
  end
@@ -74,8 +74,7 @@ module Spontaneous
74
74
  end
75
75
 
76
76
  def anonymous_template
77
- Proc.new { '#{ render_content }' }
77
+ Proc.new { '${ render_content }' }
78
78
  end
79
79
  end
80
80
  end
81
-
@@ -6,7 +6,10 @@ module Spontaneous
6
6
 
7
7
  class << self
8
8
  def outstanding
9
- unpublished_changes
9
+ outstanding = { :published_revision => Spontaneous::Site.published_revision,
10
+ :changes => unpublished_changes }
11
+ outstanding[:first_publish] = true if outstanding[:published_revision] == 0
12
+ outstanding
10
13
  end
11
14
 
12
15
  def unpublished_changes
@@ -14,7 +17,7 @@ module Spontaneous
14
17
  end
15
18
 
16
19
  def unpublished_pages
17
- S::Page.filter { (modified_at > last_published_at) | {:first_published_at => nil} }.order(:modified_at.desc).all
20
+ Spontaneous::Content::Page.filter { (modified_at > last_published_at) | {:first_published_at => nil} }.order(:modified_at.desc).all
18
21
  end
19
22
 
20
23
  def include_dependencies(page_list)
@@ -24,7 +27,16 @@ module Spontaneous
24
27
  end
25
28
 
26
29
  def export
27
- outstanding.map { |change_set| change_set.export }
30
+ exported = {}
31
+ outstanding.each do |k, v|
32
+ case k
33
+ when :changes
34
+ exported[k] = v.map { |change_set| change_set.export }
35
+ else
36
+ exported[k] = v
37
+ end
38
+ end
39
+ exported
28
40
  end
29
41
 
30
42
  def serialise_http(user = nil)
@@ -61,19 +73,21 @@ module Spontaneous
61
73
  :url => page.path,
62
74
  :side_effects => export_side_effects(page),
63
75
  :published_at => export_timestamp(page.last_published_at),
64
- :modified_at => export_timestamp(page.modified_at) }
76
+ :modified_at => export_timestamp(page.modified_at),
77
+ :update_locks => export_update_locks(page) }
78
+ end
79
+
80
+ def export_update_locks(page)
81
+ keys = [:id, :content_id, :field_id, :field_name, :location, :description, :created_at]
82
+ page.update_locks.map { |lock| export_object(lock, keys) }
65
83
  end
66
84
 
67
85
  def export_side_effects(page)
68
- side_effects = Hash.new { |h, k| h[k] = [] }
69
- page.pending_modifications.map do |modification|
70
- side_effects[modification.type] << {
71
- :count => modification.count,
72
- :created_at => export_timestamp(modification.created_at),
73
- :old_value => modification.old_value,
74
- :new_value => modification.new_value
75
- }
76
- end
86
+ keys = [:count, :created_at, :old_value, :new_value]
87
+ side_effects = Hash.new { |h, k| h[k] = [] }
88
+ page.pending_modifications.map { |modification|
89
+ side_effects[modification.type] << export_object(modification, keys)
90
+ }
77
91
  side_effects
78
92
  end
79
93
 
@@ -95,5 +109,17 @@ module Spontaneous
95
109
  def inspect
96
110
  %(#<Spontaneous::Change page=#{page.id} dependent=#{dependent.map(&:id).inspect}>)
97
111
  end
112
+
113
+ def export_object(target, keys)
114
+ translate = proc { |value|
115
+ case value
116
+ when Time, Date
117
+ export_timestamp(value)
118
+ else
119
+ value
120
+ end
121
+ }
122
+ Hash[ keys.map { |k| [k, translate[target.send(k)] ] } ]
123
+ end
98
124
  end
99
125
  end
@@ -0,0 +1,29 @@
1
+
2
+ module Spontaneous
3
+ module Cli
4
+ class Fields < ::Thor
5
+ include Spontaneous::Cli::TaskUtils
6
+ include ::Simultaneous::Task
7
+
8
+ namespace :fields
9
+
10
+ desc "update", "Performs asynchronous updates on provided fields"
11
+ method_option :fields, :type => :array, :desc => "List of field IDs to update"
12
+ def update
13
+ prepare! :update, :console
14
+ fields = Spontaneous::Field.find(*options.fields)
15
+ updater = Spontaneous::Field::Update::Immediate.new(fields)
16
+ updater.run
17
+ send_completion_event(updater)
18
+ end
19
+
20
+ private
21
+
22
+ def send_completion_event(updater)
23
+ unlocked_pages = updater.pages.reject { |p| p.locked_for_update? }
24
+ simultaneous_event('page_lock_status', unlocked_pages.map(&:id).to_json)
25
+ end
26
+ end
27
+ end
28
+ end
29
+
@@ -29,7 +29,7 @@ module Spontaneous::Cli
29
29
  [database, "#{database}_test"].each do |db|
30
30
  config = site_connection_params.merge(:database => db)
31
31
  create(db, admin_connection_params, config)
32
- migrate(db, admin_connection_params, config)
32
+ migrate(db, site_connection_params, config)
33
33
  end
34
34
 
35
35
  boot!
@@ -102,7 +102,7 @@ module Spontaneous::Cli
102
102
  when :postgres
103
103
  [
104
104
  [%(CREATE ROLE "#{config[:user]}" LOGIN PASSWORD '#{config[:password]}'), false],
105
- [%(CREATE DATABASE "#{config[:database]}" TEMPLATE=template0 ENCODING='UTF8' OWNER="#{config[:user]}"), true]
105
+ [%(CREATE DATABASE "#{config[:database]}" WITH TEMPLATE=template0 ENCODING='UTF8' LC_COLLATE='C.UTF-8' LC_CTYPE='C.UTF-8' OWNER="#{config[:user]}"), true]
106
106
  ]
107
107
  when :mysql
108
108
  host = config[:host].blank? ? "" : "@#{config[:host]}"
@@ -17,6 +17,5 @@ module Spontaneous::Cli
17
17
  Sequel::Migrator.apply(Spontaneous.database, ::Spontaneous.gem_dir('db/migrations'))
18
18
  say " >> Done"
19
19
  end
20
-
21
20
  end # Migrate
22
21
  end # Spontaneous::Cli
@@ -16,14 +16,7 @@ module Spontaneous
16
16
 
17
17
  desc "start", "Starts Spontaneous in development mode"
18
18
  def start
19
- root = File.expand_path(options.site)
20
- engine = ::Foreman::Engine::CLI.new(root: options.site)
21
-
22
- %w(back front publish).each do |process|
23
- engine.register(process, "#{binary} server #{process} --root=#{root}")
24
- end
25
-
26
- engine.start
19
+ launch %w(back front tasks)
27
20
  end
28
21
 
29
22
  desc "front", "Starts Spontaneous in front/public mode"
@@ -47,15 +40,26 @@ module Spontaneous
47
40
  def simultaneous
48
41
  prepare! :start
49
42
  connection = options[:connection] || ::Spontaneous.config.simultaneous_connection
50
- exec({"BUNDLE_GEMFILE" => nil}, "#{Simultaneous.server_binary} -c #{connection} --debug")
43
+ exec({"BUNDLE_GEMFILE" => nil}, "#{::Simultaneous.server_binary} -c #{connection} --debug")
51
44
  end
52
45
 
53
46
  # A shorter name for the 'simultaneous' task is useful (Foreman appends
54
47
  # it to each line of output)
55
- map %w(bg publish) => :simultaneous
48
+ map %w(bg publish tasks) => :simultaneous
56
49
 
57
50
  private
58
51
 
52
+ def launch(processes)
53
+ root = File.expand_path(options.site)
54
+ engine = ::Foreman::Engine::CLI.new(root: options.site)
55
+
56
+ processes.each do |process|
57
+ engine.register(process, "#{binary} server #{process} --root=#{root}")
58
+ end
59
+
60
+ engine.start
61
+ end
62
+
59
63
  def binary
60
64
  ::Spontaneous.gem_dir("bin/spot")
61
65
  end
@@ -1,9 +1,11 @@
1
1
  require 'spontaneous/cli'
2
+ require 'simultaneous'
2
3
 
3
4
  module Spontaneous
4
5
  module Cli
5
6
  class Site < ::Thor
6
7
  include Spontaneous::Cli::TaskUtils
8
+ include ::Simultaneous::Task
7
9
  namespace :site
8
10
 
9
11
  default_task :browse
@@ -116,37 +118,46 @@ module Spontaneous
116
118
  method_option :logfile, :type => :string, :desc => "Location of logfile"
117
119
  def publish
118
120
  prepare! :publish
119
- ::Site.publishing_method = :immediate
121
+ Spontaneous::Site.background_mode = :immediate
120
122
  ::Spontaneous::Logger.setup(:logfile => options.logfile) if options.logfile
121
- say "Creating revision #{::Site.revision}", :green, true
123
+ say "Creating revision #{Spontaneous::Site.revision}", :green, true
122
124
  if options.pages
123
125
  say "> Publishing pages #{options.pages.inspect}", :green, true
124
- ::Site.publish_pages(options.pages)
126
+ Spontaneous::Site.publish_pages(options.pages)
125
127
  else
126
128
  say "> Publishing all", :green, true
127
- ::Site.publish_all
129
+ Spontaneous::Site.publish_all
128
130
  end
131
+ # Rescue all errors to feed back to the UI
132
+ rescue ::Exception => e
133
+ send_error_notification(e)
129
134
  end
130
135
 
131
136
  desc "render", "Re-renders the current content"
132
137
  def render
133
138
  prepare! :render
134
- ::Site.publishing_method = :immediate
135
- ::Site.rerender
139
+ Spontaneous::Site.background_mode = :immediate
140
+ Spontaneous::Site.rerender
136
141
  end
137
142
 
138
143
  desc "revision", "Shows the site status"
139
144
  def revision
140
145
  prepare! :revision
141
- say "Site is at revision #{::Site.revision}", :green
146
+ say "Site is at revision #{Spontaneous::Site.revision}", :green
142
147
  end
143
148
 
144
- desc "browse", "Launces a browser pointing to the current development CMS"
149
+ desc "browse", "Launches a browser pointing to the current development CMS"
145
150
  def browse
146
151
  prepare :browse
147
152
  require 'launchy'
148
153
  boot!
149
- ::Launchy.open("http://localhost:#{::Site.config.port}/@spontaneous")
154
+ ::Launchy.open("http://localhost:#{Spontaneous::Site.config.port}/@spontaneous")
155
+ end
156
+
157
+ private
158
+
159
+ def send_error_notification(error)
160
+ simultaneous_event('publish_progress', {:state => "error", :progress => error}.to_json)
150
161
  end
151
162
  end
152
163
  end
@@ -83,16 +83,17 @@ module Spontaneous
83
83
  end
84
84
  end
85
85
 
86
+ autoload :Assets, "spontaneous/cli/assets"
86
87
  autoload :Console, "spontaneous/cli/console"
87
- autoload :Site, "spontaneous/cli/site"
88
- autoload :Init, "spontaneous/cli/init"
89
- autoload :User, "spontaneous/cli/user"
88
+ autoload :Fields, "spontaneous/cli/fields"
90
89
  autoload :Generate, "spontaneous/cli/generate"
91
- autoload :Server, "spontaneous/cli/server"
90
+ autoload :Init, "spontaneous/cli/init"
92
91
  autoload :Media, "spontaneous/cli/media"
93
- autoload :Sync, "spontaneous/cli/sync"
94
92
  autoload :Migrate, "spontaneous/cli/migrate"
95
- autoload :Assets, "spontaneous/cli/assets"
93
+ autoload :Server, "spontaneous/cli/server"
94
+ autoload :Site, "spontaneous/cli/site"
95
+ autoload :Sync, "spontaneous/cli/sync"
96
+ autoload :User, "spontaneous/cli/user"
96
97
 
97
98
  class Root < ::Thor
98
99
  register Spontaneous::Cli::Console, "console", "console", "Gives you console access to the current site"
@@ -105,6 +106,7 @@ module Spontaneous
105
106
  register Spontaneous::Cli::Sync, "sync", "sync [DIRECTION]", "Sync database and media to and from the production server"
106
107
  register Spontaneous::Cli::Migrate, "migrate", "migrate", "Runs Spontaneous migrations"
107
108
  register Spontaneous::Cli::Assets, "assets", "assets [ACTION]", "Manage Spontaneous assets"
109
+ register Spontaneous::Cli::Fields, "fields", "fields [ACTION]", "Manage Spontaneous fields"
108
110
 
109
111
  desc :browse, "Launces a browser pointing to the current development CMS"
110
112
  def browse
@@ -37,6 +37,17 @@ module Spontaneous::Collections
37
37
  self.select { |box| box._prototype.group == group_name }
38
38
  end
39
39
 
40
+ # A call to ${ content } within a layout template will call
41
+ # this #render method. The obvious result of this should be
42
+ # to just render each of the contained boxes.
43
+ def render(format = :html, params = {}, *args)
44
+ map { |box| box.render(format, params, *args) }.join("\n")
45
+ end
46
+
47
+ def render_using(renderer, format = :html, params = {}, *args)
48
+ map { |box| box.render_using(renderer, format, params, *args) }.join("\n")
49
+ end
50
+
40
51
  protected
41
52
 
42
53
  def get_single(index)
@@ -32,8 +32,31 @@ module Spontaneous::Collections
32
32
  super || load_field(owner.field_prototypes[name.to_sym])
33
33
  end
34
34
 
35
+ # A call to ${ fields } within a template will call
36
+ # this #render method.
37
+ # This should only be used during development
38
+ #
39
+ def render(format = :html, params = {}, *args)
40
+ map { |field| wrap_field_value(field, field.render(format, params, *args), format) }.join("\n")
41
+ end
42
+
43
+ def render_using(renderer, format = :html, params = {}, *args)
44
+ map { |field| wrap_field_value(field, field.render_using(renderer, format, params, *args), format) }.join("\n")
45
+ end
46
+
35
47
  protected
36
48
 
49
+ def wrap_field_value(field, value, format)
50
+ case format
51
+ when "html", :html
52
+ classes = [owner.class.to_s.dasherize.downcase, "field", field.name].join(" ")
53
+ id = "field-#{field.id.gsub(/\//, "-")}"
54
+ [ %(<div class="#{classes}" id="#{id}">), value, "</div>" ].join
55
+ else
56
+ value
57
+ end
58
+ end
59
+
37
60
  def initialize_from_prototypes
38
61
  owner.field_prototypes.each do |field_prototype|
39
62
  add_field(field_prototype)
@@ -46,7 +69,7 @@ module Spontaneous::Collections
46
69
 
47
70
  def parse_field_data(initial_values)
48
71
  values = (initial_values || []).map do |value|
49
- value = S::FieldTypes.deserialize_field(value)
72
+ value = S::Field.deserialize_field(value)
50
73
  [Spontaneous.schema.uids[value[:id]], value]
51
74
  end
52
75
  Hash[values]