spontaneous 0.2.0.beta5 → 0.2.0.beta6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (227) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/CHANGELOG.md +39 -0
  4. data/Gemfile +2 -0
  5. data/Readme.markdown +4 -4
  6. data/application/css/core.css.scss +144 -43
  7. data/application/css/definitions.css.scss +50 -16
  8. data/application/css/dialogue.css.scss +5 -2
  9. data/application/css/editing.css.scss +7 -7
  10. data/application/css/font.css.scss +1 -1
  11. data/application/css/meta.css.scss +6 -6
  12. data/application/css/popover.css.scss +6 -6
  13. data/application/css/top.css.scss +8 -1
  14. data/application/js/add_alias_dialogue.js +137 -36
  15. data/application/js/add_home_dialogue.js +10 -10
  16. data/application/js/ajax.js +26 -26
  17. data/application/js/authentication.js +2 -2
  18. data/application/js/box.js +21 -10
  19. data/application/js/box_container.js +13 -7
  20. data/application/js/compatibility.js +19 -17
  21. data/application/js/conflicted_field_dialogue.js +5 -5
  22. data/application/js/content.js +22 -16
  23. data/application/js/content_area.js +62 -33
  24. data/application/js/dialogue.js +16 -16
  25. data/application/js/dom.js +9 -10
  26. data/application/js/edit_panel.js +25 -20
  27. data/application/js/editing.js +21 -8
  28. data/application/js/entry.js +1 -1
  29. data/application/js/extensions.js +11 -11
  30. data/application/js/field/boolean.js +6 -6
  31. data/application/js/field/date.js +1 -1
  32. data/application/js/field/file.js +17 -17
  33. data/application/js/field/image.js +27 -27
  34. data/application/js/field/markdown.js +72 -71
  35. data/application/js/field/select.js +9 -9
  36. data/application/js/field/string.js +3 -3
  37. data/application/js/field/webvideo.js +2 -2
  38. data/application/js/field_preview.js +3 -0
  39. data/application/js/init.js +3 -2
  40. data/application/js/jquery-selection-position.js +13 -13
  41. data/application/js/location.js +17 -12
  42. data/application/js/login.js +2 -2
  43. data/application/js/meta_view/user_admin.js +101 -101
  44. data/application/js/metadata.js +1 -1
  45. data/application/js/page.js +2 -2
  46. data/application/js/page_browser.js +13 -13
  47. data/application/js/page_entry.js +1 -1
  48. data/application/js/panel/root_menu.js +10 -10
  49. data/application/js/popover.js +6 -5
  50. data/application/js/popover_view.js +5 -5
  51. data/application/js/preview.js +10 -4
  52. data/application/js/progress.js +6 -6
  53. data/application/js/properties.js +35 -6
  54. data/application/js/publish.js +43 -43
  55. data/application/js/require.js +14 -14
  56. data/application/js/services.js +3 -3
  57. data/application/js/sharded_upload.js +9 -8
  58. data/application/js/side_bar.js +5 -5
  59. data/application/js/state.js +2 -2
  60. data/application/js/status_bar.js +6 -6
  61. data/application/js/top_bar.js +97 -65
  62. data/application/js/types.js +9 -6
  63. data/application/js/upload.js +4 -4
  64. data/application/js/upload_manager.js +21 -21
  65. data/application/js/user.js +1 -1
  66. data/application/js/vendor/jquery.velocity.min.js +7 -0
  67. data/application/js/views.js +32 -8
  68. data/application/js/views/box_view.js +51 -31
  69. data/application/js/views/page_piece_view.js +17 -15
  70. data/application/js/views/page_view.js +54 -43
  71. data/application/js/views/piece_view.js +44 -37
  72. data/application/static/font/fontawesome-webfont-4f0022f25672c7f501c339cbf98d9117.ttf +0 -0
  73. data/application/views/index.erb +1 -0
  74. data/db/migrations/20130114120000_create_revision_tables.rb +2 -1
  75. data/db/migrations/20130813111009_increase_path_length.rb +11 -2
  76. data/db/migrations/20140506171823_add_index_to_target_id.rb +11 -0
  77. data/db/migrations/20140514090204_add_content_hash.rb +59 -0
  78. data/db/migrations/20140519150253_add_content_hash_timestamp.rb +20 -0
  79. data/lib/spontaneous.rb +0 -1
  80. data/lib/spontaneous/asset/environment.rb +77 -15
  81. data/lib/spontaneous/box.rb +21 -0
  82. data/lib/spontaneous/capistrano/deploy.rb +1 -1
  83. data/lib/spontaneous/capistrano/sync.rb +8 -7
  84. data/lib/spontaneous/change.rb +4 -2
  85. data/lib/spontaneous/cli/fields.rb +7 -3
  86. data/lib/spontaneous/cli/generate.rb +2 -0
  87. data/lib/spontaneous/cli/init.rb +24 -93
  88. data/lib/spontaneous/cli/init/db.rb +94 -0
  89. data/lib/spontaneous/cli/init/mysql.rb +17 -0
  90. data/lib/spontaneous/cli/init/postgresql.rb +33 -0
  91. data/lib/spontaneous/cli/init/sqlite.rb +14 -0
  92. data/lib/spontaneous/cli/site.rb +45 -20
  93. data/lib/spontaneous/collections/box_set.rb +3 -0
  94. data/lib/spontaneous/collections/entry_set.rb +43 -4
  95. data/lib/spontaneous/collections/field_set.rb +14 -2
  96. data/lib/spontaneous/data_mapper.rb +40 -7
  97. data/lib/spontaneous/data_mapper/content_model.rb +1 -1
  98. data/lib/spontaneous/data_mapper/content_model/associations.rb +63 -12
  99. data/lib/spontaneous/data_mapper/content_model/timestamps.rb +9 -14
  100. data/lib/spontaneous/data_mapper/content_table.rb +4 -2
  101. data/lib/spontaneous/data_mapper/dataset.rb +31 -2
  102. data/lib/spontaneous/data_mapper/scope.rb +37 -20
  103. data/lib/spontaneous/errors.rb +6 -0
  104. data/lib/spontaneous/facet.rb +20 -10
  105. data/lib/spontaneous/field/base.rb +8 -1
  106. data/lib/spontaneous/field/file.rb +28 -3
  107. data/lib/spontaneous/field/image.rb +2 -0
  108. data/lib/spontaneous/field/update.rb +6 -0
  109. data/lib/spontaneous/field/webvideo/vimeo.rb +6 -1
  110. data/lib/spontaneous/field/webvideo/vine.rb +1 -1
  111. data/lib/spontaneous/field/webvideo/youtube.rb +1 -1
  112. data/lib/spontaneous/generators/site.rb +6 -4
  113. data/lib/spontaneous/generators/site/.gitignore +1 -0
  114. data/lib/spontaneous/generators/site/Gemfile.tt +3 -3
  115. data/lib/spontaneous/generators/site/config/{indexes.rb.tt → initializers/indexes.rb.tt} +0 -0
  116. data/lib/spontaneous/generators/site/config/initializers/publishing.rb.tt +78 -0
  117. data/lib/spontaneous/generators/site/{config/database.yml.tt → db/mysql2.yml.tt} +7 -6
  118. data/lib/spontaneous/generators/site/db/postgres.yml.tt +25 -0
  119. data/lib/spontaneous/generators/site/db/sqlite3.yml.tt +18 -0
  120. data/lib/spontaneous/generators/site/public/humans.txt.tt +14 -0
  121. data/lib/spontaneous/generators/site/templates/layouts/standard.html.cut.tt +51 -0
  122. data/lib/spontaneous/loader.rb +1 -1
  123. data/lib/spontaneous/logger.rb +1 -1
  124. data/lib/spontaneous/media/image/optimizer.rb +1 -1
  125. data/lib/spontaneous/media/image/processor.rb +11 -2
  126. data/lib/spontaneous/media/image/renderable.rb +2 -0
  127. data/lib/spontaneous/model.rb +3 -0
  128. data/lib/spontaneous/model/box/allowed_types.rb +17 -4
  129. data/lib/spontaneous/model/core.rb +36 -3
  130. data/lib/spontaneous/model/core/aliases.rb +5 -2
  131. data/lib/spontaneous/model/core/boxes.rb +6 -0
  132. data/lib/spontaneous/model/core/cascading_change.rb +38 -0
  133. data/lib/spontaneous/model/core/content_hash.rb +171 -0
  134. data/lib/spontaneous/model/core/entries.rb +0 -19
  135. data/lib/spontaneous/model/core/fields.rb +11 -0
  136. data/lib/spontaneous/model/core/modifications.rb +22 -21
  137. data/lib/spontaneous/model/core/render.rb +3 -0
  138. data/lib/spontaneous/model/core/serialisation.rb +18 -17
  139. data/lib/spontaneous/model/page.rb +35 -8
  140. data/lib/spontaneous/model/page/page_tree.rb +20 -8
  141. data/lib/spontaneous/model/page/paths.rb +79 -50
  142. data/lib/spontaneous/model/page/singleton.rb +71 -0
  143. data/lib/spontaneous/model/page/site_map.rb +2 -1
  144. data/lib/spontaneous/model/page/site_timestamps.rb +2 -2
  145. data/lib/spontaneous/model/piece.rb +10 -0
  146. data/lib/spontaneous/output/context.rb +13 -6
  147. data/lib/spontaneous/output/format.rb +30 -5
  148. data/lib/spontaneous/output/helpers/script_helper.rb +8 -0
  149. data/lib/spontaneous/output/helpers/stylesheet_helper.rb +7 -0
  150. data/lib/spontaneous/output/renderable.rb +16 -0
  151. data/lib/spontaneous/output/store.rb +1 -1
  152. data/lib/spontaneous/output/template/renderer.rb +2 -2
  153. data/lib/spontaneous/page_piece.rb +25 -1
  154. data/lib/spontaneous/prototypes/box_prototype.rb +13 -0
  155. data/lib/spontaneous/prototypes/field_prototype.rb +7 -4
  156. data/lib/spontaneous/publishing.rb +10 -5
  157. data/lib/spontaneous/publishing/immediate.rb +32 -349
  158. data/lib/spontaneous/publishing/pipeline.rb +43 -0
  159. data/lib/spontaneous/publishing/progress.rb +186 -0
  160. data/lib/spontaneous/publishing/publish.rb +107 -0
  161. data/lib/spontaneous/publishing/rerender.rb +17 -0
  162. data/lib/spontaneous/publishing/revision.rb +53 -18
  163. data/lib/spontaneous/publishing/simultaneous.rb +1 -1
  164. data/lib/spontaneous/publishing/steps.rb +154 -0
  165. data/lib/spontaneous/publishing/steps/activate_revision.rb +45 -0
  166. data/lib/spontaneous/publishing/steps/archive_old_revisions.rb +22 -0
  167. data/lib/spontaneous/publishing/steps/base_step.rb +49 -0
  168. data/lib/spontaneous/publishing/steps/copy_static_files.rb +74 -0
  169. data/lib/spontaneous/publishing/steps/create_revision_directory.rb +24 -0
  170. data/lib/spontaneous/publishing/steps/generate_rackup_file.rb +51 -0
  171. data/lib/spontaneous/publishing/steps/generate_search_indexes.rb +24 -0
  172. data/lib/spontaneous/publishing/steps/render_revision.rb +69 -0
  173. data/lib/spontaneous/publishing/steps/write_revision_file.rb +43 -0
  174. data/lib/spontaneous/rack/back.rb +3 -1
  175. data/lib/spontaneous/rack/back/alias.rb +9 -8
  176. data/lib/spontaneous/rack/front.rb +1 -1
  177. data/lib/spontaneous/rack/middleware.rb +7 -4
  178. data/lib/spontaneous/rack/middleware/transaction.rb +14 -0
  179. data/lib/spontaneous/rack/page_controller.rb +23 -8
  180. data/lib/spontaneous/revision.rb +5 -10
  181. data/lib/spontaneous/schema.rb +5 -0
  182. data/lib/spontaneous/server.rb +3 -1
  183. data/lib/spontaneous/site.rb +17 -10
  184. data/lib/spontaneous/site/publishing.rb +25 -3
  185. data/lib/spontaneous/site/state.rb +7 -3
  186. data/lib/spontaneous/tasks/database.rake +5 -10
  187. data/lib/spontaneous/utils/database/mysql_dumper.rb +5 -1
  188. data/lib/spontaneous/version.rb +1 -1
  189. data/spontaneous.gemspec +4 -3
  190. data/test/fixtures/example_application/config/initializers/initializer1.rb +1 -0
  191. data/test/fixtures/example_application/config/initializers/initializer2.rb +1 -0
  192. data/test/fixtures/example_application/config/initializers/publishing.rb +13 -0
  193. data/test/fixtures/search/config/{indexes.rb → initializers/indexes.rb} +0 -0
  194. data/test/fixtures/serialisation/root_hash.yaml.erb +10 -4
  195. data/test/functional/test_application.rb +10 -0
  196. data/test/functional/test_back.rb +23 -5
  197. data/test/functional/test_cli.rb +98 -34
  198. data/test/functional/test_front.rb +7 -3
  199. data/test/test_helper.rb +35 -28
  200. data/test/unit/test_alias.rb +20 -3
  201. data/test/unit/test_assets.rb +58 -30
  202. data/test/unit/test_changesets.rb +20 -12
  203. data/test/unit/test_content_hash.rb +496 -0
  204. data/test/unit/test_context.rb +28 -1
  205. data/test/unit/test_controllers.rb +96 -61
  206. data/test/unit/test_crypt.rb +1 -8
  207. data/test/unit/test_datamapper.rb +95 -19
  208. data/test/unit/test_features.rb +1 -4
  209. data/test/unit/test_fields.rb +61 -12
  210. data/test/unit/test_generators.rb +39 -2
  211. data/test/unit/test_images.rb +3 -1
  212. data/test/unit/test_modifications.rb +224 -219
  213. data/test/unit/test_output_store.rb +10 -0
  214. data/test/unit/{test_formats.rb → test_outputs.rb} +75 -6
  215. data/test/unit/test_page.rb +61 -15
  216. data/test/unit/test_plugins.rb +2 -42
  217. data/test/unit/test_publishing_pipeline.rb +1050 -0
  218. data/test/unit/test_render.rb +30 -0
  219. data/test/unit/test_revisions.rb +110 -2
  220. data/test/unit/test_schema.rb +4 -0
  221. data/test/unit/test_search.rb +1 -1
  222. data/test/unit/test_serialisation.rb +6 -1
  223. data/test/unit/test_singletons.rb +159 -0
  224. data/test/unit/test_site.rb +71 -44
  225. metadata +140 -86
  226. data/application/static/font/fontawesome-webfont-1c66a4738b40ef0f6b1abca0ba9a796d.ttf +0 -0
  227. data/test/unit/test_publishing.rb +0 -330
@@ -22,7 +22,7 @@ module Spontaneous::Media::Image
22
22
 
23
23
  def run
24
24
  format = Spontaneous::Media::Image.format(@image)
25
- if respond_to?(format)
25
+ if format && respond_to?(format)
26
26
  send(format)
27
27
  end
28
28
  end
@@ -7,11 +7,20 @@ module Spontaneous::Media::Image
7
7
  end
8
8
 
9
9
  def apply(process)
10
- cmd = convert(@input, :to => @output, &process)
11
- cmd.run
10
+ if imagemagick_installed?
11
+ cmd = convert(@input, :to => @output, &process)
12
+ cmd.run
13
+ else
14
+ logger.warn("Unable to re-size image, Imagemagick is not installed.")
15
+ FileUtils::Verbose.cp(@input, @output)
16
+ end
12
17
  end
13
18
 
14
19
  def optimize!
15
20
  end
21
+
22
+ def imagemagick_installed?
23
+ Kernel.system('which convert >/dev/null 2>&1')
24
+ end
16
25
  end
17
26
  end
@@ -12,6 +12,8 @@ module Spontaneous::Media::Image
12
12
  end
13
13
  end
14
14
 
15
+ alias_method :render_inline, :render
16
+
15
17
  def to_html(attr={})
16
18
  default_attr = {
17
19
  :src => src,
@@ -1,6 +1,9 @@
1
+
1
2
  module Spontaneous
2
3
 
3
4
  module Model
5
+ ANCESTOR_SEP = "."
6
+
4
7
  autoload :Core, "spontaneous/model/core"
5
8
  autoload :Page, "spontaneous/model/page"
6
9
  autoload :Piece, "spontaneous/model/piece"
@@ -5,6 +5,8 @@ module Spontaneous::Model::Box
5
5
  extend Spontaneous::Concern
6
6
 
7
7
  class AllowedType
8
+ prepend Spontaneous::Model::Page::Singleton::AllowedTypeMethods
9
+
8
10
  attr_accessor :allow_subclasses
9
11
 
10
12
  def initialize(box, type, options={}, &definition)
@@ -81,6 +83,10 @@ module Spontaneous::Model::Box
81
83
  end
82
84
 
83
85
  def instance_classes
86
+ unfiltered_instance_classes.reject {|type| exclude_type?(type) }
87
+ end
88
+
89
+ def unfiltered_instance_classes
84
90
  if allow_subclasses
85
91
  instance_class.subclasses
86
92
  else
@@ -88,14 +94,20 @@ module Spontaneous::Model::Box
88
94
  end
89
95
  end
90
96
 
97
+ def exclude_type?(type)
98
+ false
99
+ end
100
+
91
101
  def export
92
102
  if allow_subclasses
93
103
  # can't configure interface name using allow_subclasses
94
104
  instance_class.subclasses.map { |c| { type: c.ui_class } }
95
105
  else
96
- exported = {type: instance_class.ui_class }
97
- exported[:as] = @options[:as] if @options.key?(:as)
98
- [exported]
106
+ instance_classes.map {|instance_class|
107
+ exported = {type: instance_class.ui_class }
108
+ exported[:as] = @options[:as] if @options.key?(:as)
109
+ exported
110
+ }
99
111
  end
100
112
  end
101
113
 
@@ -127,6 +139,7 @@ module Spontaneous::Model::Box
127
139
  name.to_s.constantize
128
140
  end
129
141
  end
142
+
130
143
  end
131
144
 
132
145
  class AllowedGroup < AllowedType
@@ -134,7 +147,7 @@ module Spontaneous::Model::Box
134
147
  @type
135
148
  end
136
149
 
137
- def instance_classes
150
+ def unfiltered_instance_classes
138
151
  names = groups.flat_map { |name| schema.groups[name] }
139
152
  names.map { |name| resolve_instance_class(name) }.uniq
140
153
  end
@@ -5,6 +5,7 @@ module Spontaneous::Model
5
5
  autoload :Aliases, "spontaneous/model/core/aliases"
6
6
  autoload :Boxes, "spontaneous/model/core/boxes"
7
7
  autoload :ContentGroups, "spontaneous/model/core/content_groups"
8
+ autoload :ContentHash, "spontaneous/model/core/content_hash"
8
9
  autoload :EditorClass, "spontaneous/model/core/editor_class"
9
10
  autoload :Entries, "spontaneous/model/core/entries"
10
11
  autoload :Entry, "spontaneous/model/core/entry"
@@ -25,10 +26,13 @@ module Spontaneous::Model
25
26
  autoload :Styles, "spontaneous/model/core/styles"
26
27
  autoload :Supertype, "spontaneous/model/core/supertype"
27
28
  autoload :Visibility, "spontaneous/model/core/visibility"
29
+ autoload :CascadingChange, "spontaneous/model/core/cascading_change"
28
30
 
29
31
  extend Spontaneous::Concern
30
32
 
31
33
  module ClassMethods
34
+ include Spontaneous::Model::Page::Singleton::ContentClassMethods
35
+
32
36
  def page?
33
37
  false
34
38
  end
@@ -46,10 +50,12 @@ module Spontaneous::Model
46
50
  !supertype.nil?
47
51
  end
48
52
 
49
- def root
53
+ def public_root
50
54
  content_model::Page.root
51
55
  end
52
56
 
57
+ alias_method :root, :public_root
58
+
53
59
  def log_sql(io = $stdout)
54
60
  mapper.logger = ::Logger.new(io)
55
61
  yield if block_given?
@@ -70,6 +76,7 @@ module Spontaneous::Model
70
76
 
71
77
  include Enumerable
72
78
 
79
+ include CascadingChange
73
80
  include SchemaId
74
81
  include Entry
75
82
  include Boxes
@@ -88,6 +95,7 @@ module Spontaneous::Model
88
95
  include Prototypes
89
96
  include Permissions
90
97
  include ContentGroups
98
+ include ContentHash
91
99
  include SchemaHierarchy
92
100
  include PageSearch
93
101
  include Locks
@@ -101,8 +109,19 @@ module Spontaneous::Model
101
109
  false
102
110
  end
103
111
 
104
- def root
105
- content_model::Page.root
112
+ # Provide a default implementation of this that is later over-ridden by the page methods
113
+ def is_public_root?
114
+ false
115
+ end
116
+
117
+ def public_root
118
+ is_public_root? ? self : content_model::Page.root
119
+ end
120
+
121
+ alias_method :root, :public_root
122
+
123
+ def visibility_path_ids
124
+ (self[:visibility_path] || "").split(ANCESTOR_SEP).map { |id| id.to_i }
106
125
  end
107
126
 
108
127
  def content_instance
@@ -116,12 +135,26 @@ module Spontaneous::Model
116
135
  self
117
136
  end
118
137
 
138
+ # Returns a version of this object as a page.
139
+ # This is used by the rendering system to guarantee that the object
140
+ # it is recieving is a page & not a PagePiece proxy. See PagePiece#to_page
141
+ def to_page
142
+ page
143
+ end
144
+
119
145
  def page?
120
146
  false
121
147
  end
122
148
 
123
149
  alias_method :is_page?, :page?
124
150
 
151
+ # Do not overwrite this method directly.
152
+ # If you want a page to render the content of another configure
153
+ # the type using Content::render
154
+ def renderable
155
+ self
156
+ end
157
+
125
158
  def start_inline_edit_marker
126
159
  "spontaneous:previewedit:start:content id:#{id}"
127
160
  end
@@ -120,6 +120,10 @@ module Spontaneous::Model::Core
120
120
  __target
121
121
  end
122
122
 
123
+ def fields
124
+ @field_set ||= Spontaneous::Collections::FieldSet.new(self, field_store, target, :fields)
125
+ end
126
+
123
127
  def field?(field_name)
124
128
  super || target.class.field?(field_name)
125
129
  end
@@ -146,7 +150,6 @@ module Spontaneous::Model::Core
146
150
  end
147
151
  end
148
152
 
149
-
150
153
  def find_named_style(style_name)
151
154
  super or target.find_named_style(style_name)
152
155
  end
@@ -161,7 +164,7 @@ module Spontaneous::Model::Core
161
164
  @styles ||= Spontaneous::Collections::PrototypeSet.new(target, :styles)
162
165
  end
163
166
 
164
- def export(user = nil)
167
+ def shallow_export(user = nil)
165
168
  super.merge(:target => exported_target(user), :alias_title => target.alias_title, :alias_icon => target.exported_alias_icon)
166
169
  end
167
170
 
@@ -78,6 +78,12 @@ module Spontaneous::Model::Core
78
78
  @boxes ||= Spontaneous::Collections::BoxSet.new(self)
79
79
  end
80
80
 
81
+ # Used by #content_hash to attempt to preserve content hashes across
82
+ # schema changes
83
+ def boxes_with_consistent_order
84
+ boxes.sort { |b1, b2| b1.schema_id <=> b2.schema_id }
85
+ end
86
+
81
87
  def pieces
82
88
  boxes.flat_map { |b| b.pieces }
83
89
  end
@@ -0,0 +1,38 @@
1
+ # encoding: UTF-8
2
+
3
+ module Spontaneous::Model::Core
4
+ module CascadingChange
5
+ extend Spontaneous::Concern
6
+
7
+ module ClassMethods
8
+ def cascading_change(attr_name, &block)
9
+ define_method "#{attr_name}=" do |value|
10
+ append_cascading_change(attr_name, self[attr_name], value, &block)
11
+ super(value)
12
+ end
13
+ end
14
+ end
15
+
16
+ private
17
+
18
+ def changes_to_cascade
19
+ @changes_to_cascade ||= {}
20
+ end
21
+
22
+ def append_cascading_change(attribute, old_value, new_value, &block)
23
+ changes_to_cascade[attribute] = block.call(self, old_value, new_value) if new_value != old_value
24
+ end
25
+
26
+ def after_save
27
+ super
28
+ cascade_all_changes
29
+ end
30
+
31
+ def cascade_all_changes
32
+ changes_to_cascade.each do |attribute, propagator|
33
+ propagator.propagate
34
+ end
35
+ changes_to_cascade.clear
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,171 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'digest/md5'
4
+
5
+ module Spontaneous::Model::Core
6
+ module ContentHash
7
+ extend Spontaneous::Concern
8
+
9
+ def self.calculate(*values)
10
+ values = Array(values).flatten
11
+ md5 = Digest::MD5.new
12
+ values.each do |value|
13
+ md5.update(value.to_s)
14
+ end
15
+ md5.hexdigest
16
+ end
17
+
18
+ class ContentHashChange
19
+ def initialize(origin)
20
+ @origin = origin
21
+ end
22
+
23
+ def propagate
24
+ content = owner
25
+ while propagate_to?(content)# && content.modification_tracking_enabled?
26
+ content.recalculate_content_hash!# if propagate_to_owner?
27
+ break if content == @origin.page
28
+ content = content.owner
29
+ end
30
+ end
31
+
32
+ def propagate_to?(content)
33
+ !(content.nil? || (@origin.page? && content.page?)) && content.modification_tracking_enabled?
34
+ end
35
+
36
+ def owner
37
+ @origin.owner
38
+ end
39
+ end
40
+
41
+ included do
42
+ cascading_change :content_hash do |origin, old_value, new_value|
43
+ ContentHashChange.new(origin)
44
+ end
45
+ end
46
+
47
+ def content_hash_changed?
48
+ content_hash_changed
49
+ end
50
+
51
+ def content_hash
52
+ super || calculate_content_hash
53
+ end
54
+
55
+ def calculate_content_hash
56
+ Spontaneous::Model::Core::ContentHash.calculate(content_hash_dependencies { |el| el.content_hash })
57
+ end
58
+
59
+ def calculate_content_hash!
60
+ Spontaneous::Model::Core::ContentHash.calculate(content_hash_dependencies { |el| el.calculate_content_hash! })
61
+ end
62
+
63
+ def content_hash_dependencies(&calculation)
64
+ fields = fields_with_consistent_order.map(&calculation)
65
+ boxes = boxes_with_consistent_order.reject(&:generated?).map(&calculation)
66
+ content_hash_columns.concat(fields).concat(boxes)
67
+ end
68
+
69
+ def content_hash_columns
70
+ [schema_id, hidden?, target_id]
71
+ end
72
+
73
+ def before_save
74
+ # Only recalculate the content hash if we're in the editable dataset, otherwise
75
+ # the published data can end up with a different content hash even after being published
76
+ recalculate_content_hash if modification_tracking_enabled?
77
+ enable_modification_tracking
78
+ super
79
+ true
80
+ end
81
+
82
+ def recalculate_content_hash
83
+ attrs, changed = content_hash_attributes
84
+ set(attrs) if changed
85
+ end
86
+
87
+ # Update the instances content hash by writing direct to the db & without triggering any futher cascading changes
88
+ # Without the #reload call existing cached versions of the
89
+ # content tree may be used and changes will not propagate
90
+ def recalculate_content_hash!
91
+ reload unless modified?
92
+ attrs, changed = content_hash_attributes
93
+ model.where(id: id).update(attrs) if changed
94
+ end
95
+
96
+ def content_hash_attributes
97
+ db, current, published = self[:content_hash], calculate_content_hash, published_content_hash
98
+ changed = published.blank? || current != published
99
+ [{content_hash: current, content_hash_changed: changed, content_hash_changed_at: ::Sequel.datetime_class.now}, db != current]
100
+ end
101
+
102
+ def modification_tracking_enabled?
103
+ mapper.editable? && !@modification_tracking_disabled
104
+ end
105
+
106
+ def disable_modification_tracking!
107
+ @modification_tracking_disabled = true
108
+ end
109
+
110
+ def enable_modification_tracking
111
+ @modification_tracking_disabled = false
112
+ end
113
+
114
+ def insert_page(index, child_page, box)
115
+ page.disable_modification_tracking! unless page.nil?
116
+ super
117
+ end
118
+
119
+ module PageMethods
120
+ # Make page modification state depend on its path
121
+ def content_hash_dependencies
122
+ super.push(slug)
123
+ end
124
+ end
125
+
126
+ module BoxMethods
127
+ def generated?
128
+ _prototype.generated?
129
+ end
130
+
131
+ def content_hash
132
+ _content_hash { |el| el.content_hash }
133
+ end
134
+
135
+ # boxes have no persisted value for their content hash
136
+ alias_method :calculate_content_hash, :content_hash
137
+
138
+ def calculate_content_hash!
139
+ _content_hash { |el| el.calculate_content_hash! }
140
+ end
141
+
142
+ def _content_hash(&calculation)
143
+ return "" if fields.empty? && empty?
144
+ fields = fields_with_consistent_order.map(&calculation)
145
+ entry_hashes = map(&calculation)
146
+ Spontaneous::Model::Core::ContentHash.calculate(fields, entry_hashes)
147
+ end
148
+ end
149
+
150
+ module FieldMethods
151
+ def content_hash
152
+ Spontaneous::Model::Core::ContentHash.calculate(unprocessed_value)
153
+ end
154
+
155
+ alias_method :calculate_content_hash, :content_hash
156
+ alias_method :calculate_content_hash!, :content_hash
157
+ end
158
+
159
+ module PagePieceMethods
160
+ # Because pages all publish independently we don't want the content hash
161
+ # of boxes to change if a contained page is modified, so make the hash
162
+ # of pages inside boxes only depend on the id (so the box hash does change
163
+ # when the page is added, moved or deleted)
164
+ def content_hash
165
+ Spontaneous::Model::Core::ContentHash.calculate(id)
166
+ end
167
+ alias_method :calculate_content_hash, :content_hash
168
+ alias_method :calculate_content_hash!, :content_hash
169
+ end
170
+ end
171
+ end