scrivito_sdk 0.18.1 → 0.30.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (144) hide show
  1. checksums.yaml +4 -4
  2. data/app/controllers/scrivito/blobs_controller.rb +1 -2
  3. data/app/controllers/scrivito/default_cms_controller.rb +9 -75
  4. data/app/controllers/scrivito/objs_controller.rb +107 -119
  5. data/app/controllers/scrivito/tasks_controller.rb +1 -1
  6. data/app/controllers/scrivito/users_controller.rb +1 -1
  7. data/app/controllers/scrivito/webservice_controller.rb +6 -2
  8. data/app/controllers/scrivito/workspaces_controller.rb +57 -17
  9. data/app/helpers/scrivito_helper.rb +285 -0
  10. data/app/views/google_maps_widget/show.html.erb +1 -0
  11. data/app/views/scrivito/_editing_auth_warning.html.erb +43 -0
  12. data/app/views/scrivito/blobs/upload_permission.json.jbuilder +1 -0
  13. data/app/views/scrivito/objs/copy.json.jbuilder +1 -0
  14. data/app/views/scrivito/objs/copy_widget.html.erb +1 -1
  15. data/app/views/scrivito/objs/create.json.jbuilder +1 -0
  16. data/app/views/scrivito/objs/create_widget.html.erb +1 -1
  17. data/app/views/scrivito/objs/details.json.jbuilder +1 -0
  18. data/app/views/scrivito/objs/format_missing_error.json.jbuilder +1 -0
  19. data/app/views/scrivito/objs/modification.json.jbuilder +1 -0
  20. data/app/views/scrivito/objs/page_class_selection.json.jbuilder +1 -0
  21. data/app/views/scrivito/objs/search.json.jbuilder +6 -0
  22. data/app/views/scrivito/objs/search_only_size.json.jbuilder +1 -0
  23. data/app/views/scrivito/objs/update.json.jbuilder +1 -0
  24. data/app/views/scrivito/objs/widget_class_selection.json.jbuilder +1 -0
  25. data/app/views/scrivito/objs/widget_modification.json.jbuilder +1 -0
  26. data/app/views/scrivito/tasks/show.json.jbuilder +3 -0
  27. data/app/views/scrivito/users/suggest.json.jbuilder +1 -0
  28. data/app/views/scrivito/webservice/empty.json +1 -0
  29. data/app/views/scrivito/webservice/error.json.jbuilder +2 -0
  30. data/app/views/scrivito/workspaces/_workspace.json.jbuilder +5 -0
  31. data/app/views/scrivito/workspaces/check.json.jbuilder +1 -0
  32. data/app/views/scrivito/workspaces/create.json.jbuilder +1 -0
  33. data/app/views/scrivito/workspaces/index.json.jbuilder +1 -0
  34. data/app/views/scrivito/workspaces/show.json.jbuilder +1 -0
  35. data/app/views/scrivito/workspaces/task.json.jbuilder +5 -0
  36. data/config/ca-bundle.crt +259 -234
  37. data/config/cms_routes.rb +2 -2
  38. data/config/routes.rb +3 -0
  39. data/lib/assets/javascripts/{scrivito_editing.js → scrivito_sdk.js} +1662 -1180
  40. data/lib/assets/stylesheets/{scrivito_editing.css → scrivito_sdk.css} +242 -6
  41. data/lib/generators/scrivito/install/install_generator.rb +62 -0
  42. data/lib/generators/scrivito/install/templates/app/controllers/cms_controller.rb +3 -0
  43. data/lib/generators/scrivito/install/templates/app/controllers/page_controller.rb +2 -0
  44. data/lib/generators/scrivito/install/templates/app/models/headline_widget.rb +7 -0
  45. data/lib/generators/scrivito/install/templates/app/models/image.rb +5 -0
  46. data/lib/generators/scrivito/install/templates/app/models/image_widget.rb +7 -0
  47. data/lib/generators/scrivito/install/templates/app/models/obj.rb +2 -0
  48. data/lib/generators/scrivito/install/templates/app/models/page.rb +8 -0
  49. data/lib/generators/scrivito/install/templates/app/models/text_widget.rb +7 -0
  50. data/lib/generators/scrivito/install/templates/app/models/widget.rb +2 -0
  51. data/lib/generators/scrivito/install/templates/app/views/headline_widget/show.html.erb +1 -0
  52. data/lib/generators/scrivito/install/templates/app/views/headline_widget/thumbnail.html.erb +3 -0
  53. data/lib/generators/scrivito/install/templates/app/views/image_widget/show.html.erb +1 -0
  54. data/lib/generators/scrivito/install/templates/app/views/image_widget/thumbnail.html.erb +3 -0
  55. data/lib/generators/scrivito/install/templates/app/views/page/details.html.erb +1 -0
  56. data/lib/generators/scrivito/install/templates/app/views/page/index.html.erb +16 -0
  57. data/lib/generators/scrivito/install/templates/app/views/page/thumbnail.html.erb +3 -0
  58. data/lib/generators/scrivito/install/templates/app/views/text_widget/show.html.erb +1 -0
  59. data/lib/generators/scrivito/install/templates/app/views/text_widget/thumbnail.html.erb +3 -0
  60. data/lib/generators/scrivito/install/templates/config/initializers/scrivito.rb +10 -0
  61. data/lib/generators/scrivito/install/templates/scrivito/migrate/install_scrivito.rb +49 -0
  62. data/lib/generators/scrivito/migration/USAGE +9 -0
  63. data/lib/generators/{cms → scrivito}/migration/migration_generator.rb +4 -9
  64. data/lib/generators/{cms → scrivito}/migration/templates/migration.erb +3 -3
  65. data/lib/generators/{cms → scrivito}/widget/templates/details.html.erb +0 -0
  66. data/lib/generators/{cms → scrivito}/widget/templates/migration.erb +0 -0
  67. data/lib/generators/{cms → scrivito}/widget/templates/model.erb +0 -0
  68. data/lib/generators/{cms → scrivito}/widget/templates/show.html.erb +0 -0
  69. data/lib/generators/scrivito/widget/templates/thumbnail.html.erb +3 -0
  70. data/lib/generators/{cms → scrivito}/widget/widget_generator.rb +3 -2
  71. data/lib/obj.rb +17 -3
  72. data/lib/scrivito/attribute_collection.rb +7 -12
  73. data/lib/scrivito/attribute_content.rb +31 -12
  74. data/lib/scrivito/basic_obj.rb +186 -96
  75. data/lib/scrivito/basic_widget.rb +25 -10
  76. data/lib/scrivito/binary.rb +75 -0
  77. data/lib/scrivito/cache_garbage_collector.rb +19 -4
  78. data/lib/scrivito/child_list_tag.rb +108 -0
  79. data/lib/scrivito/client_config.rb +2 -1
  80. data/lib/scrivito/client_error.rb +3 -2
  81. data/lib/scrivito/cms_backend.rb +81 -16
  82. data/lib/scrivito/cms_dispatch_controller.rb +7 -0
  83. data/lib/scrivito/cms_env.rb +0 -2
  84. data/lib/scrivito/cms_field_tag.rb +44 -35
  85. data/lib/scrivito/cms_rest_api.rb +23 -6
  86. data/lib/scrivito/cms_rest_api/attribute_serializer.rb +1 -1
  87. data/lib/scrivito/cms_routing.rb +120 -0
  88. data/lib/scrivito/configuration.rb +69 -58
  89. data/lib/scrivito/connection_manager.rb +30 -1
  90. data/lib/scrivito/content_service.rb +10 -7
  91. data/lib/scrivito/controller_actions.rb +108 -0
  92. data/lib/scrivito/diff.rb +30 -15
  93. data/lib/scrivito/editing_context.rb +4 -0
  94. data/lib/scrivito/editing_context_helper.rb +19 -0
  95. data/lib/scrivito/editing_context_middleware.rb +20 -1
  96. data/lib/scrivito/image_tag_helper.rb +44 -0
  97. data/lib/scrivito/layout_tags.rb +33 -0
  98. data/lib/scrivito/link.rb +4 -2
  99. data/lib/scrivito/membership.rb +0 -8
  100. data/lib/scrivito/migrations.rb +0 -1
  101. data/lib/scrivito/migrations/installer.rb +1 -1
  102. data/lib/scrivito/migrations/migration.rb +0 -2
  103. data/lib/scrivito/migrations/migrator.rb +2 -3
  104. data/lib/scrivito/obj_class.rb +64 -16
  105. data/lib/scrivito/obj_class_data.rb +4 -0
  106. data/lib/scrivito/obj_search_enumerator.rb +1 -1
  107. data/lib/scrivito/routing_helper.rb +42 -0
  108. data/lib/scrivito/{engine.rb → sdk_engine.rb} +15 -29
  109. data/lib/scrivito/tag_renderer.rb +40 -0
  110. data/lib/scrivito/test_request.rb +40 -0
  111. data/lib/scrivito/type_computer.rb +1 -6
  112. data/lib/scrivito/user.rb +33 -15
  113. data/lib/scrivito/user_definition.rb +34 -6
  114. data/lib/scrivito/widget_tag.rb +67 -0
  115. data/lib/scrivito/workspace.rb +12 -24
  116. data/lib/scrivito_sdk.rb +17 -1
  117. data/lib/tasks/cache.rake +4 -5
  118. data/lib/tasks/migration.rake +1 -1
  119. data/lib/widget.rb +21 -3
  120. metadata +119 -70
  121. data/README +0 -6
  122. data/app/controllers/cms_controller.rb +0 -7
  123. data/app/helpers/cms_helper.rb +0 -7
  124. data/app/helpers/cms_routing_helper.rb +0 -7
  125. data/app/helpers/scrivito/cms_asset_helper.rb +0 -110
  126. data/app/helpers/scrivito/cms_tag_helper.rb +0 -232
  127. data/app/helpers/scrivito/default_cms_helper.rb +0 -21
  128. data/app/helpers/scrivito/default_cms_routing_helper.rb +0 -128
  129. data/app/helpers/scrivito/display_helper.rb +0 -64
  130. data/app/helpers/scrivito/editing_helper.rb +0 -32
  131. data/app/helpers/scrivito/layout_helper.rb +0 -21
  132. data/app/models/named_link.rb +0 -2
  133. data/app/views/cms/_index.html.erb +0 -7
  134. data/app/views/cms/index.html.erb +0 -1
  135. data/app/views/scrivito/_editing_javascript.html.erb +0 -7
  136. data/app/views/scrivito/default_cms/show_widget.html.erb +0 -1
  137. data/lib/assets/stylesheets/scrivito.css +0 -199
  138. data/lib/generators/cms/migration/USAGE +0 -9
  139. data/lib/generators/cms/widget/templates/thumbnail.html.erb +0 -2
  140. data/lib/scrivito/blob.rb +0 -48
  141. data/lib/scrivito/cms_accessible.rb +0 -30
  142. data/lib/scrivito/cms_rest_api/blob_uploader.rb +0 -18
  143. data/lib/scrivito/cms_test_request.rb +0 -23
  144. data/lib/scrivito/migrations/migration_dsl.rb +0 -180
@@ -3,7 +3,7 @@ module Scrivito
3
3
  class Migrator
4
4
  class << self
5
5
  def migrations_path
6
- Rails.root + 'cms/migrate'
6
+ Rails.root + Scrivito::Configuration.migration_path
7
7
  end
8
8
 
9
9
  def determine_migrations(paths)
@@ -76,8 +76,7 @@ module Scrivito
76
76
  workspace_lock.validate(workspace)
77
77
  end
78
78
  else
79
- CmsRestApi.post('workspaces', :workspace => { :title => 'rtc', :id => 'rtc' })
80
- workspace = migration_workspace
79
+ workspace = Workspace.create(title: 'Migration Working Copy', id: 'rtc')
81
80
  workspace_lock.write(workspace)
82
81
  end
83
82
 
@@ -49,11 +49,16 @@ module Scrivito
49
49
  # to set as values. Attributes can be either given as {Scrivito::Attribute} instances or as
50
50
  # an attribute property hash.
51
51
  #
52
- # @example Create a non-binary obj class.
53
- # ObjClass.create(name: 'Homepage', is_binary: false)
52
+ # @example Create a obj class
53
+ # ObjClass.create(name: 'Homepage', attributes: [
54
+ # { name: 'title', type: :string },
55
+ # { name: 'body', type: :widget }
56
+ # ])
54
57
  #
55
58
  # @example Create a binary obj class.
56
- # ObjClass.create(name: 'Image', is_binary: true)
59
+ # ObjClass.create(name: 'Image', attributes: [
60
+ # { name: 'blob', type: :binary }
61
+ # ])
57
62
  #
58
63
  # @example Create an obj class with attributes passed in as an Array of attribute property hashes.
59
64
  # ObjClass.create(name: 'Blog', attributes: [
@@ -69,19 +74,23 @@ module Scrivito
69
74
  #
70
75
  # @param [Hash] properties
71
76
  # @option properties [String] :name The name of the obj class.
72
- # @option properties [Boolean] :is_binary Is this a binary or non-binary obj class?
73
77
  # @option properties [Boolean] :is_active Is it possible to create instances of this obj class?
74
78
  # @option properties [Array<Scrivito::Attribute>, Array<Hash>] :attributes A list of
75
79
  # attributes for this obj class. Can be either a list of attribute instances or attribute
76
80
  # property hashes.
77
81
  #
82
+ # @note the +is_binary+ option is deprecated
83
+ #
78
84
  # @return [Scrivito::ObjClass]
79
85
  def create(properties)
80
86
  properties = properties.with_indifferent_access
81
87
 
82
- unless properties.has_key?(:is_binary)
83
- raise ScrivitoError, 'The "is_binary" property is mandatory. Please provide' \
84
- ' a hash containing an "is_binary" key with true or false value.'
88
+ if properties.key?(:is_binary)
89
+ Scrivito::Deprecation.warn(
90
+ "`is_binary' is deprecated and should not be used anymore."\
91
+ "Please remove the parameter and specify all attributes you need "\
92
+ "including the previously provided attributes 'title', 'body', 'blob'"
93
+ )
85
94
  end
86
95
 
87
96
  if properties[:attributes]
@@ -129,6 +138,15 @@ module Scrivito
129
138
  @workspace = workspace
130
139
  end
131
140
 
141
+ # @api public
142
+ # Returns true if this ObjClass was created with a +is_binary+ option.
143
+ # Creating ObjClass with this option is deprecated and will be removed.
144
+ #
145
+ # @return [Boolean]
146
+ def legacy_type?
147
+ !!obj_class_data.type
148
+ end
149
+
132
150
  # @!attribute [r] id
133
151
  # @api public
134
152
  # @return [String] unique identifier of this obj class
@@ -138,10 +156,20 @@ module Scrivito
138
156
  # @!attribute [r] is_active
139
157
  # @api public
140
158
  # @return [Boolean] whether instances can be created with this obj class
141
- # @!attribute [r] is_binary
142
- # @api public
143
- # @return [Boolean] whether instances of this class are binary, e.g. images or PDFs
144
- delegate :id, :name, :is_active, :is_binary, to: :obj_class_data
159
+ delegate :id, :name, :is_active, to: :obj_class_data
160
+
161
+ # @api public
162
+ # @return [Boolean] whether instances of this class are binary, e.g. images or PDFs
163
+ # @deprecated Please create objects without using the +is_binary+ option
164
+ # @raise [ScrivitoError] if the method is called on a non-legacy-type ObjClass
165
+ def is_binary
166
+ if legacy_type?
167
+ obj_class_data.is_binary
168
+ else
169
+ raise ScrivitoError,
170
+ %(`is_binary' and `binary?' can only be called on ObjClasses with a legacy_type.)
171
+ end
172
+ end
145
173
 
146
174
  alias_method :active?, :is_active
147
175
  alias_method :binary?, :is_binary
@@ -165,14 +193,31 @@ module Scrivito
165
193
  # end
166
194
  attr_reader :attributes
167
195
 
168
- # Updates this obj class and persists the changes in the CMS. It is not possible to
169
- # update the +name+ or +is_binary+ property.
170
- #
171
196
  # @api public
197
+ # Updates this obj class and persists the changes in the CMS. It is not possible to
198
+ # update the +name+.
172
199
  #
173
200
  # See {Scrivito::ObjClass.create} for a detailed overview of
174
201
  # what properties are allowed and how to set them.
175
202
  #
203
+ # The +is_binary+ option can only be removed. By passing +nil+ for the
204
+ # +is_binary+ option to update, you can convert legacy objects. This conversion
205
+ # removes the fields +title+, +body+ (if +is_binary+ was +false+) or +blob+ (if
206
+ # +is_binary+ was +true+). If you add the fields in the same request the values
207
+ # will be kept for all objs.
208
+ #
209
+ # @example Removing +is_binary+ but keeping the previously predefined fields
210
+ # ObjClass.find('Homepage').update(is_binary: nil, attributes: [
211
+ # {name: 'title', type: 'string'},
212
+ # {name: 'body', type: 'html'}
213
+ # ])
214
+ #
215
+ # # Binary ObjClass
216
+ # ObjClass.find('Image').update(is_binary: nil, attributes: [
217
+ # {name: 'title', type: 'string'},
218
+ # {name: 'blob', type: 'binary'}
219
+ # ])
220
+ #
176
221
  # @param [Hash] properties
177
222
  #
178
223
  # @raise [ScrivitoError] Raised when trying to change +name+ or +is_binary+.
@@ -182,8 +227,11 @@ module Scrivito
182
227
  params = properties.with_indifferent_access
183
228
 
184
229
  if params.has_key?(:is_binary)
185
- raise ScrivitoError, "#{self.class} does not support changing 'is_binary'. Please remove" \
186
- " the key from the properties."
230
+ if params[:is_binary].nil?
231
+ params[:type] = params.delete(:is_binary)
232
+ else
233
+ raise ScrivitoError, "#{self.class} only supports removing the `is_binary' property"
234
+ end
187
235
  end
188
236
 
189
237
  if params.has_key?(:name)
@@ -25,6 +25,10 @@ class ObjClassData
25
25
  !!@raw_data['is_binary']
26
26
  end
27
27
 
28
+ def type
29
+ @raw_data['type']
30
+ end
31
+
28
32
  def attributes
29
33
  @raw_data['attributes'] || []
30
34
  end
@@ -1,7 +1,7 @@
1
1
  # encoding: UTF-8
2
2
  module Scrivito
3
3
  # Provides an enumerator for iterating over obj search results and retrieving obj instances.
4
- # This is done using the {http://ruby-doc.org/core-1.8.7/Enumerable.html <code>Enumerable</code> mixin},
4
+ # This is done using the {http://ruby-doc.org/core-2.1.3/Enumerable.html <code>Enumerable</code> mixin},
5
5
  # which provides methods such as <code>map</code>, <code>select</code> or <code>take</code>.
6
6
  #
7
7
  # This enumerator is lazy. If for example you are looking for {BasicObj Obj}s with the ObjClass "Publication",
@@ -0,0 +1,42 @@
1
+ module Scrivito
2
+ #
3
+ # The helper contains methods to generate URLs to CMS content.
4
+ # Use {Scrivito::RoutingHelper#scrivito_path} or {Scrivito::RoutingHelper#scrivito_url} when linking or
5
+ # redirecting to CMS objects. These methods are available in boths your views and your controllers.
6
+ #
7
+ # @api public
8
+ #
9
+ module RoutingHelper
10
+ #
11
+ # Returns the (URL-)path for a cms object.
12
+ #
13
+ # @api public
14
+ #
15
+ # @param target [Obj, Link, Array<Link>]
16
+ # If +target+ is an +Array+ of +Links+, it must be non-empty.
17
+ # The first {Link} from the +Array+ will be used.
18
+ # @param options [Hash] include url settings such as path parameters or protocol.
19
+ #
20
+ # @return [String]
21
+ #
22
+ def scrivito_path(target, options = {})
23
+ CmsRouting.new(request, main_app).path_or_url(target, "path", options)
24
+ end
25
+
26
+ #
27
+ # Returns the absolute URL for a cms object.
28
+ #
29
+ # @api public
30
+ #
31
+ # @param target [Obj, Link, Array<Link>]
32
+ # If +target+ is an +Array+ of +Links+, it must be non-empty.
33
+ # The first {Link} from the +Array+ will be used.
34
+ # @param options [Hash] include url settings such as path parameters or protocol.
35
+ #
36
+ # @return [String]
37
+ #
38
+ def scrivito_url(target, options = {})
39
+ CmsRouting.new(request, main_app).path_or_url(target, "url", options)
40
+ end
41
+ end
42
+ end
@@ -1,13 +1,11 @@
1
- require "rails"
2
- require "action_view"
3
- require "active_model"
1
+ require 'rails'
4
2
 
3
+ require 'jbuilder'
5
4
  require 'jquery-rails'
6
-
7
- require 'scrivito/configuration'
5
+ require 'net/http/post/multipart'
8
6
 
9
7
  module ::Scrivito
10
- class Engine < Rails::Engine
8
+ class SdkEngine < Rails::Engine
11
9
  config.to_prepare { Scrivito::Configuration.to_prepare }
12
10
 
13
11
  # make sure our exceptions cause an adequate error page and http status code
@@ -20,37 +18,25 @@ module ::Scrivito
20
18
 
21
19
  # Expose Scrivito SDK runtime to controller for logging.
22
20
  initializer "scrivito.log_runtime" do |app|
23
- runtime =
24
- begin
25
- Scrivito::ControllerRuntime
26
- rescue NameError
27
- nil
28
- end
29
-
30
- if runtime
31
- Scrivito::LogSubscriber.attach_to :scrivito
32
- ActiveSupport.on_load(:action_controller) do
33
- include runtime
34
- end
21
+ Scrivito::LogSubscriber.attach_to :scrivito
22
+ ActiveSupport.on_load(:action_controller) do
23
+ include Scrivito::ControllerRuntime
35
24
  end
36
25
  end
37
26
 
38
- initializer "scrivito.configure_database", :before => :load_config_initializers do |app|
39
- Scrivito::Configuration.configure_cms_database
40
- end
41
-
42
27
  initializer "scrivito.routing_helpers" do
43
- if Rails.env == 'test'
44
- ActionDispatch::TestRequest.__send__(:include, Scrivito::CmsTestRequest)
45
- end
28
+ ActiveSupport.on_load(:action_controller) do
29
+ if Rails.env == 'test'
30
+ ActionDispatch::TestRequest.__send__(:include, Scrivito::TestRequest)
31
+ end
46
32
 
47
- ActionController::Base.__send__(:include, CmsRoutingHelper)
48
- ActionView::Base.__send__(:include, CmsRoutingHelper)
33
+ include Scrivito::RoutingHelper
34
+ end
49
35
  end
50
36
 
51
- initializer "scrivito.add_assets" do |app|
37
+ initializer 'scrivito.add_sdk_assets' do |app|
52
38
  # Specify which file should be precompiled for packaging
53
- app.config.assets.precompile += %w( scrivito_editing.js scrivito_editing.css scrivito.css )
39
+ app.config.assets.precompile += %w(scrivito_sdk.js scrivito_sdk.css)
54
40
  app.config.assets.precompile += %w(*.png *.jpg *.jpeg *.gif)
55
41
  end
56
42
 
@@ -0,0 +1,40 @@
1
+ module Scrivito
2
+
3
+ module TagRenderer
4
+
5
+ VOID_TAGS = %w[area base br col command embed hr img input keygen
6
+ link meta param source track wbr]
7
+
8
+ def render(given_options = {}, &block)
9
+ TagRenderer.render(view, self, given_options, &block)
10
+ end
11
+
12
+ class << self
13
+
14
+ def render(view, tag, given_options, &block)
15
+ tag_name = tag.tag_name
16
+ options = given_options.merge(formatted_options(tag))
17
+
18
+ if VOID_TAGS.include?(tag_name.to_s)
19
+ view.tag(tag_name, options)
20
+ else
21
+ view.content_tag(tag_name, tag.content(&block), options)
22
+ end
23
+ end
24
+
25
+ private
26
+
27
+ def formatted_options(tag)
28
+ formatted_options = {}
29
+ tag.options.each do |key, value|
30
+ formatted_options["data-scrivito-#{key}"] = value
31
+ end
32
+
33
+ formatted_options
34
+ end
35
+
36
+ end
37
+
38
+ end
39
+
40
+ end
@@ -0,0 +1,40 @@
1
+ module Scrivito
2
+ #
3
+ # @api public
4
+ #
5
+ module TestRequest
6
+ #
7
+ # Make a test request to act as a CMS request.
8
+ # This is necessary for testing controllers that include {Scrivito::ControllerActions}, because
9
+ # these controllers do not have explicit routes by default.
10
+ #
11
+ # @api public
12
+ # @param [Scrivito::BasicObj Obj] test_obj requested +Obj+
13
+ #
14
+ # @example
15
+ # class MyPageControllerTest < ActionController::TestCase
16
+ # test 'should respond with success' do
17
+ # request.for_scrivito_obj
18
+ # get :index
19
+ # assert_response :success
20
+ # end
21
+ # end
22
+ #
23
+ def for_scrivito_obj(test_obj = nil)
24
+ # prepare the env as if the given obj was loaded by Scrivito::CmsEnv
25
+ env[Scrivito::CmsEnv::OBJ_ENV_KEY] = test_obj if test_obj
26
+
27
+ class << self
28
+ def assign_parameters(routes, controller_path, action, parameters = {})
29
+ super(routes, controller_path, action, parameters)
30
+ rescue ActionController::RoutingError, AbstractController::ActionNotFound => e
31
+ begin
32
+ super(routes, "scrivito/cms_dispatch", action, parameters)
33
+ rescue ActionController::RoutingError, AbstractController::ActionNotFound
34
+ raise e
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -3,11 +3,10 @@ module Scrivito
3
3
  class TypeComputer
4
4
  def initialize(base_class, fallback_class)
5
5
  @base_class, @fallback_class = base_class, fallback_class
6
- @type_cache = {}
7
6
  end
8
7
 
9
8
  def compute_type(obj_class)
10
- @type_cache.fetch(obj_class) { calculate_type(obj_class) }
9
+ load_class(obj_class) || @fallback_class
11
10
  end
12
11
 
13
12
  def special_class?(klass)
@@ -16,10 +15,6 @@ class TypeComputer
16
15
 
17
16
  private
18
17
 
19
- def calculate_type(obj_class)
20
- load_class(obj_class) || @fallback_class
21
- end
22
-
23
18
  def load_class(obj_class)
24
19
  klass = obj_class.constantize
25
20
  klass if inherits_from_base?(klass)
@@ -46,17 +46,20 @@ module Scrivito
46
46
  # end
47
47
  # end
48
48
  # end
49
- def define(id)
49
+ def define(id, &block)
50
50
  assert_valid_id(id)
51
- user_definition = UserDefinition.new(id)
52
- yield user_definition if block_given?
53
- user_definition.user
51
+ define_user(id, &block)
54
52
  end
55
53
 
56
- def anonymous_admin
57
- explicit_rules = {}
58
- VERBS.each { |verb| explicit_rules[[:can_always, verb, :workspace]] = nil }
59
- new(id: nil, explicit_rules: explicit_rules)
54
+ #
55
+ # Returns an anonymous system user, who can always create workspaces, can always read, write,
56
+ # publish, delete and invite to any workspace.
57
+ #
58
+ # @api public
59
+ # @return [Scrivito::User] the system user
60
+ #
61
+ def system_user
62
+ define_user { |user| user.is_admin! }
60
63
  end
61
64
 
62
65
  def unknown_user(id)
@@ -73,6 +76,12 @@ module Scrivito
73
76
 
74
77
  private
75
78
 
79
+ def define_user(id = nil)
80
+ user_definition = UserDefinition.new(id)
81
+ yield user_definition if block_given?
82
+ user_definition.user
83
+ end
84
+
76
85
  def assert_valid_id(id)
77
86
  raise ScrivitoError.new('User id can not be blank') if id.blank?
78
87
  raise ScrivitoError.new('User id is too long (max length 64)') if id.length > 64
@@ -99,7 +108,8 @@ module Scrivito
99
108
 
100
109
  def can?(verb, workspace)
101
110
  assert_valid_verb(verb)
102
- can_always?(verb, :workspace) || owner_of?(workspace) && !can_never?(verb, :workspace)
111
+ verb == :read && workspace.published? || can_always?(verb, :workspace) ||
112
+ owner_of?(workspace) && !can_never?(verb, :workspace)
103
113
  end
104
114
 
105
115
  def can_always?(verb, subject)
@@ -152,7 +162,7 @@ module Scrivito
152
162
 
153
163
  def suggest_users(input)
154
164
  if suggest_users_proc
155
- suggested_users = suggest_users_proc.call(input)
165
+ suggested_users = sandbox_suggest_user_proc(input)
156
166
  suggested_users.nil? ? [] : suggested_users
157
167
  else
158
168
  user = self.class.find(input)
@@ -160,11 +170,9 @@ module Scrivito
160
170
  end
161
171
  end
162
172
 
163
- def as_json(options = nil)
164
- {
165
- id: id,
166
- description: description,
167
- }
173
+ # Per convention only anonymous admin has a nil id.
174
+ def system_user?
175
+ id.nil?
168
176
  end
169
177
 
170
178
  private
@@ -176,5 +184,15 @@ module Scrivito
176
184
  def assert_valid_verb(verb)
177
185
  raise ScrivitoError.new("Invalid verb '#{verb}'") unless VERBS.include?(verb)
178
186
  end
187
+
188
+ def sandbox_suggest_user_proc(input)
189
+ suggest_users_proc.call(input)
190
+ rescue Exception => e
191
+ Rails.logger.error(%{
192
+ Method `suggest_users' of the user "#{id}" raised an error on input "#{input}": #{e}
193
+ #{e.backtrace.join("\n")}
194
+ })
195
+ nil
196
+ end
179
197
  end
180
198
  end