scrivito_sdk 0.12.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (123) hide show
  1. checksums.yaml +7 -0
  2. data/.yardopts +5 -0
  3. data/README +6 -0
  4. data/app/controllers/cms_controller.rb +7 -0
  5. data/app/controllers/scrivito/blobs_controller.rb +10 -0
  6. data/app/controllers/scrivito/default_cms_controller.rb +61 -0
  7. data/app/controllers/scrivito/objs_controller.rb +200 -0
  8. data/app/controllers/scrivito/tasks_controller.rb +11 -0
  9. data/app/controllers/scrivito/webservice_controller.rb +36 -0
  10. data/app/controllers/scrivito/workspaces_controller.rb +41 -0
  11. data/app/helpers/cms_helper.rb +7 -0
  12. data/app/helpers/cms_routing_helper.rb +7 -0
  13. data/app/helpers/scrivito/cms_asset_helper.rb +103 -0
  14. data/app/helpers/scrivito/cms_tag_helper.rb +231 -0
  15. data/app/helpers/scrivito/default_cms_helper.rb +21 -0
  16. data/app/helpers/scrivito/default_cms_routing_helper.rb +130 -0
  17. data/app/helpers/scrivito/display_helper.rb +71 -0
  18. data/app/helpers/scrivito/editing_helper.rb +26 -0
  19. data/app/helpers/scrivito/layout_helper.rb +28 -0
  20. data/app/models/named_link.rb +2 -0
  21. data/app/views/cms/_index.html.erb +7 -0
  22. data/app/views/cms/index.html.erb +1 -0
  23. data/app/views/scrivito/_editing_javascript.html.erb +7 -0
  24. data/app/views/scrivito/default_cms/show_widget.html.erb +1 -0
  25. data/app/views/scrivito/objs/copy_widget.html.erb +1 -0
  26. data/app/views/scrivito/objs/create_widget.html.erb +1 -0
  27. data/app/views/scrivito/widget_thumbnail.html.erb +9 -0
  28. data/config/ca-bundle.crt +3509 -0
  29. data/config/cms_routes.rb +17 -0
  30. data/config/locales/de.scrivito.errors.yml +7 -0
  31. data/config/locales/de.scrivito.lib.yml +6 -0
  32. data/config/locales/de.scrivito.models.yml +6 -0
  33. data/config/locales/en.scrivito.errors.yml +7 -0
  34. data/config/locales/en.scrivito.lib.yml +6 -0
  35. data/config/locales/en.scrivito.models.yml +6 -0
  36. data/config/routes.rb +37 -0
  37. data/lib/assets/images/180x120.gif +0 -0
  38. data/lib/assets/images/scrivito/image_placeholder.png +0 -0
  39. data/lib/assets/javascripts/scrivito_editing.js +14642 -0
  40. data/lib/assets/stylesheets/scrivito.css +180 -0
  41. data/lib/assets/stylesheets/scrivito_editing.css +2213 -0
  42. data/lib/generators/cms/migration/USAGE +9 -0
  43. data/lib/generators/cms/migration/migration_generator.rb +21 -0
  44. data/lib/generators/cms/migration/templates/migration.erb +10 -0
  45. data/lib/obj.rb +3 -0
  46. data/lib/scrivito/access_denied.rb +6 -0
  47. data/lib/scrivito/attribute_content.rb +194 -0
  48. data/lib/scrivito/backend_error.rb +4 -0
  49. data/lib/scrivito/basic_obj.rb +840 -0
  50. data/lib/scrivito/basic_widget.rb +238 -0
  51. data/lib/scrivito/blob.rb +48 -0
  52. data/lib/scrivito/cache.rb +41 -0
  53. data/lib/scrivito/cache_garbage_collector.rb +83 -0
  54. data/lib/scrivito/cache_middleware.rb +17 -0
  55. data/lib/scrivito/client_config.rb +62 -0
  56. data/lib/scrivito/client_error.rb +12 -0
  57. data/lib/scrivito/cms_accessible.rb +30 -0
  58. data/lib/scrivito/cms_backend.rb +238 -0
  59. data/lib/scrivito/cms_cache_storage.rb +51 -0
  60. data/lib/scrivito/cms_dispatch_controller.rb +46 -0
  61. data/lib/scrivito/cms_env.rb +63 -0
  62. data/lib/scrivito/cms_field_tag.rb +112 -0
  63. data/lib/scrivito/cms_rest_api.rb +151 -0
  64. data/lib/scrivito/cms_rest_api/attribute_serializer.rb +98 -0
  65. data/lib/scrivito/cms_rest_api/blob_uploader.rb +18 -0
  66. data/lib/scrivito/cms_rest_api/widget_extractor.rb +42 -0
  67. data/lib/scrivito/cms_test_request.rb +23 -0
  68. data/lib/scrivito/communication_error.rb +17 -0
  69. data/lib/scrivito/comparison.rb +67 -0
  70. data/lib/scrivito/configuration.rb +221 -0
  71. data/lib/scrivito/connection_manager.rb +100 -0
  72. data/lib/scrivito/content_conversion.rb +43 -0
  73. data/lib/scrivito/content_service.rb +118 -0
  74. data/lib/scrivito/content_state.rb +109 -0
  75. data/lib/scrivito/content_state_caching.rb +47 -0
  76. data/lib/scrivito/content_state_visitor.rb +19 -0
  77. data/lib/scrivito/controller_runtime.rb +35 -0
  78. data/lib/scrivito/date_attribute.rb +16 -0
  79. data/lib/scrivito/deprecation.rb +21 -0
  80. data/lib/scrivito/diff.rb +110 -0
  81. data/lib/scrivito/editing_context.rb +106 -0
  82. data/lib/scrivito/editing_context_middleware.rb +60 -0
  83. data/lib/scrivito/engine.rb +65 -0
  84. data/lib/scrivito/errors.rb +11 -0
  85. data/lib/scrivito/html_string.rb +18 -0
  86. data/lib/scrivito/link.rb +187 -0
  87. data/lib/scrivito/link_parser.rb +81 -0
  88. data/lib/scrivito/log_subscriber.rb +29 -0
  89. data/lib/scrivito/migration.rb +2 -0
  90. data/lib/scrivito/migrations.rb +12 -0
  91. data/lib/scrivito/migrations/cms_backend.rb +94 -0
  92. data/lib/scrivito/migrations/installer.rb +45 -0
  93. data/lib/scrivito/migrations/migration.rb +93 -0
  94. data/lib/scrivito/migrations/migration_dsl.rb +143 -0
  95. data/lib/scrivito/migrations/migration_store.rb +23 -0
  96. data/lib/scrivito/migrations/migrator.rb +135 -0
  97. data/lib/scrivito/migrations/workspace_lock.rb +39 -0
  98. data/lib/scrivito/model_identity.rb +13 -0
  99. data/lib/scrivito/modification.rb +8 -0
  100. data/lib/scrivito/named_link.rb +75 -0
  101. data/lib/scrivito/network_error.rb +11 -0
  102. data/lib/scrivito/obj_data.rb +140 -0
  103. data/lib/scrivito/obj_data_from_hash.rb +31 -0
  104. data/lib/scrivito/obj_data_from_service.rb +84 -0
  105. data/lib/scrivito/obj_params_parser.rb +61 -0
  106. data/lib/scrivito/obj_search_builder.rb +62 -0
  107. data/lib/scrivito/obj_search_enumerator.rb +374 -0
  108. data/lib/scrivito/rate_limit_exceeded.rb +5 -0
  109. data/lib/scrivito/revision.rb +9 -0
  110. data/lib/scrivito/string_tagging.rb +18 -0
  111. data/lib/scrivito/text_link.rb +52 -0
  112. data/lib/scrivito/text_link_conversion.rb +52 -0
  113. data/lib/scrivito/type_computer.rb +34 -0
  114. data/lib/scrivito/widget_field_params.rb +61 -0
  115. data/lib/scrivito/widget_garbage_collection.rb +97 -0
  116. data/lib/scrivito/workspace.rb +222 -0
  117. data/lib/scrivito/workspace_data_from_service.rb +80 -0
  118. data/lib/scrivito/workspace_selection_middleware.rb +23 -0
  119. data/lib/scrivito_sdk.rb +19 -0
  120. data/lib/tasks/cache.rake +12 -0
  121. data/lib/tasks/migration.rake +35 -0
  122. data/lib/widget.rb +3 -0
  123. metadata +291 -0
@@ -0,0 +1,81 @@
1
+ require 'addressable/uri'
2
+
3
+ module Scrivito
4
+ class LinkParser
5
+ def initialize(host, port)
6
+ @host = host
7
+ @port = port
8
+ end
9
+
10
+ def parse(url)
11
+ uri = Addressable::URI.parse(url)
12
+ link_params = {}
13
+
14
+ if obj = find_obj(uri)
15
+ link_params[:obj] = obj
16
+ else
17
+ if application_uri?(uri)
18
+ uri.path = '/' unless uri.path.present?
19
+ uri.port = nil
20
+ uri.host = nil
21
+ uri.scheme = nil
22
+ end
23
+
24
+ link_params[:url] = uri.to_s
25
+ end
26
+
27
+ link_params[:query] = remove_editing_context_params(uri.query)
28
+ link_params[:fragment] = uri.fragment
29
+
30
+ Link.new(link_params)
31
+ rescue Addressable::URI::InvalidURIError
32
+ Link.new(url: url)
33
+ end
34
+
35
+ private
36
+
37
+ def application_uri?(uri)
38
+ uri.absolute? && internal_uri?(uri) || uri.relative? && uri.path.present?
39
+ end
40
+
41
+ def internal_uri?(uri)
42
+ if uri.port.present?
43
+ uri.host == @host && uri.port == Integer(@port)
44
+ else
45
+ uri.host == @host
46
+ end
47
+ end
48
+
49
+ def find_obj(uri)
50
+ return nil unless application_uri?(uri) && application_route?(uri)
51
+
52
+ route_params = route(uri)
53
+
54
+ if id = route_params[:id]
55
+ Obj.find(id)
56
+ elsif permalink = route_params[:permalink]
57
+ Obj.find_by_permalink(permalink)
58
+ end
59
+ rescue Scrivito::ResourceNotFound
60
+ end
61
+
62
+ def application_route?(uri)
63
+ route_params = route(uri)
64
+ route_params && route_params[:controller] == 'scrivito/cms_dispatch'
65
+ end
66
+
67
+ def route(uri)
68
+ Rails.application.routes.recognize_path(uri.to_s, method: :get)
69
+ rescue ActionController::RoutingError
70
+ end
71
+
72
+ def remove_editing_context_params(query)
73
+ if query
74
+ params = Rack::Utils.parse_nested_query(query)
75
+ params.delete('_scrivito_display_mode')
76
+ params.delete('_scrivito_workspace_id')
77
+ params.to_query
78
+ end
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,29 @@
1
+ module Scrivito
2
+ class LogSubscriber < ActiveSupport::LogSubscriber
3
+ def self.runtime=(value)
4
+ Thread.current["scrivito_cms_load_runtime"] = value
5
+ end
6
+
7
+ def self.runtime
8
+ Thread.current["scrivito_cms_load_runtime"] ||= 0
9
+ end
10
+
11
+ def self.reset_runtime
12
+ rt, self.runtime = runtime, 0
13
+ rt
14
+ end
15
+
16
+ def cms_load(event)
17
+ self.class.runtime += event.duration
18
+ return unless logger.debug?
19
+
20
+ name = '%s (%.1fms)' % [event.payload[:name], event.duration]
21
+ debug " #{name} #{event.payload[:index]} #{event.payload[:keys].inspect}"
22
+ end
23
+
24
+ def logger
25
+ Rails.logger
26
+ end
27
+ end
28
+ end
29
+
@@ -0,0 +1,2 @@
1
+ # ::Scrivito::Migration is just a shortcut
2
+ ::Scrivito::Migration = ::Scrivito::Migrations::Migration
@@ -0,0 +1,12 @@
1
+ module Scrivito
2
+ module Migrations
3
+ extend ActiveSupport::Autoload
4
+
5
+ autoload :Migration
6
+ autoload :Migrator
7
+ autoload :MigrationStore
8
+ autoload :MigrationDsl
9
+ autoload :CmsBackend
10
+ autoload :WorkspaceLock
11
+ end
12
+ end
@@ -0,0 +1,94 @@
1
+ module Scrivito
2
+ module Migrations
3
+ class CmsBackend
4
+ def read
5
+ obj = find_migration_store_obj
6
+
7
+ if obj
8
+ obj[attribute_name]
9
+ else
10
+ initial_value
11
+ end
12
+ end
13
+
14
+ def save(value)
15
+ CmsRestApi.put(
16
+ endpoint("objs/#{migration_store_obj.id}"),
17
+ :obj => {
18
+ attribute_name => value
19
+ }
20
+ )
21
+ end
22
+
23
+ private
24
+
25
+ def create
26
+ # Create obj class
27
+ CmsRestApi.post(
28
+ endpoint('obj_classes'),
29
+ :obj_class => {
30
+ :name => obj_class_name,
31
+ :attributes => [
32
+ {
33
+ :name => attribute_name,
34
+ :type => :text,
35
+ },
36
+ ],
37
+ :type => :publication,
38
+ :title => obj_class_name,
39
+ }
40
+ )
41
+
42
+ # Create obj
43
+ CmsRestApi.post(
44
+ endpoint('objs'),
45
+ :obj => {
46
+ :_path => path,
47
+ :_obj_class => obj_class_name,
48
+ attribute_name => initial_value,
49
+ }
50
+ )
51
+
52
+ # Deactivate obj class
53
+ CmsRestApi.put(
54
+ endpoint("obj_classes/#{obj_class_name}"),
55
+ :obj_class => {
56
+ :is_active => false,
57
+ }
58
+ )
59
+ end
60
+
61
+ def migration_store_obj
62
+ unless find_migration_store_obj
63
+ create
64
+ end
65
+
66
+ find_migration_store_obj
67
+ end
68
+
69
+ def find_migration_store_obj
70
+ Obj.find_by_path(path)
71
+ end
72
+
73
+ def attribute_name
74
+ 'versions'
75
+ end
76
+
77
+ def obj_class_name
78
+ 'MigrationStore'
79
+ end
80
+
81
+ def path
82
+ '/_internal/migration-store'
83
+ end
84
+
85
+ def initial_value
86
+ ''
87
+ end
88
+
89
+ def endpoint(path)
90
+ "workspaces/#{Workspace.current.id}/#{path}"
91
+ end
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,45 @@
1
+ module Scrivito
2
+ module Migrations
3
+ class Installer
4
+ def initialize(from)
5
+ @to_load = from.blank? ? :all : from.split(',').map(&:strip)
6
+ end
7
+
8
+ def install
9
+ Scrivito::Migrations::Migration.copy(destination, find_engines_with_migrations)
10
+ end
11
+
12
+ private
13
+
14
+ def destination
15
+ Scrivito::Migrations::Migrator.migrations_path
16
+ end
17
+
18
+ def find_engines_with_migrations
19
+ Rails.application.railties.inject({}) do |railties, railtie|
20
+ next railties unless @to_load == :all || @to_load.include?(railtie.railtie_name)
21
+
22
+ if path = railtie_migration_path_for(railtie)
23
+ railties[railtie.railtie_name] = path
24
+ end
25
+
26
+ railties
27
+ end
28
+ end
29
+
30
+ def railtie_migration_path_for(railtie)
31
+ if railtie.respond_to?(:paths)
32
+ path = railtie.paths.path + relative_migration_path
33
+
34
+ if File.exist?(path)
35
+ path
36
+ end
37
+ end
38
+ end
39
+
40
+ def relative_migration_path
41
+ 'cms/migrate'
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,93 @@
1
+ module Scrivito
2
+ module Migrations
3
+ # CMS Migrations can alter the structure and content of the CMS and allow
4
+ # other developers to simultaneously apply changes. Migrations provide a set
5
+ # of methods to create, update or delete CMS objects, attributes and object
6
+ # classes.
7
+ #
8
+ # @example Simple Migration
9
+ #
10
+ # class CreateTestAttribute < Scrivito::Migrations::Migration
11
+ # def up
12
+ # add_attribute_to('homepage', :name => 'test', :type => 'string')
13
+ # end
14
+ # end
15
+ class Migration
16
+ include MigrationDsl
17
+
18
+ class << self
19
+ def copy(destination, sources)
20
+ unless File.exist?(destination)
21
+ FileUtils.mkdir_p(destination)
22
+ end
23
+
24
+ destination_migrations = Scrivito::Migrations::Migrator.determine_migrations(destination)
25
+ last = destination_migrations.last
26
+
27
+ sources.each do |scope, path|
28
+ source_migrations = Scrivito::Migrations::Migrator.determine_migrations(path)
29
+
30
+ source_migrations.each do |migration|
31
+ next if duplicate_migration?(destination_migrations, migration.name, scope)
32
+
33
+ name = migration.name
34
+ version = next_migration_number(last ? last.version : 0)
35
+ filename = File.join(destination, "#{version}_#{name.underscore}.#{scope}.rb")
36
+
37
+ new_migration = self.new(name, version, filename, scope)
38
+
39
+ FileUtils.cp(migration.filename, new_migration.filename)
40
+
41
+ last = new_migration
42
+ destination_migrations << new_migration
43
+ end
44
+ end
45
+ end
46
+
47
+ def duplicate_migration?(migrations, name, scope)
48
+ migrations.detect do |migration|
49
+ migration.name == name && migration.scope == scope.to_s
50
+ end
51
+ end
52
+
53
+ def next_migration_number(max)
54
+ timestamp = Time.now.utc.strftime("%Y%m%d%H%M%S").to_i
55
+
56
+ if max >= timestamp
57
+ timestamp = max + 1
58
+ end
59
+
60
+ timestamp
61
+ end
62
+ end
63
+
64
+ attr_accessor :name
65
+ attr_accessor :version
66
+ attr_accessor :filename
67
+ attr_accessor :scope
68
+
69
+ def initialize(name, version, filename, scope)
70
+ @name = name
71
+ @version = version.to_i
72
+ @filename = filename
73
+ @scope = scope.to_s
74
+ end
75
+
76
+ def migrate
77
+ announce 'migrating'
78
+
79
+ time = Benchmark.measure { up }
80
+
81
+ announce 'migrated (%.4fs)' % time.real; puts
82
+ end
83
+
84
+ private
85
+
86
+ def announce(message)
87
+ text = "#{version} #{name}: #{message}"
88
+ length = [0, 75 - text.length].max
89
+ puts('== %s %s' % [text, '=' * length])
90
+ end
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,143 @@
1
+ module Scrivito
2
+ # @api public
3
+ module Migrations
4
+ # @api public
5
+ module MigrationDsl
6
+ # Adds an attribute to an object class.
7
+ #
8
+ # @example Add "test" attribute to object class "Foo.
9
+ #
10
+ # add_attribute_to('Foo', { name: 'test', type: 'string' })
11
+ #
12
+ # @param obj_class_name [String] The class name of the object class.
13
+ # @param params [Hash] The definition of the new attribute.
14
+ #
15
+ # @return nothing
16
+ def add_attribute_to(obj_class_name, params)
17
+ attributes = get_obj_class(obj_class_name)['attributes']
18
+ attributes << params
19
+
20
+ update_obj_class(obj_class_name, attributes: attributes)
21
+ end
22
+
23
+ # Deletes an attribute from an object class.
24
+ #
25
+ # @example Delete "test" attribute from object class "Foo.
26
+ #
27
+ # delete_attribute_from('Foo', 'test')
28
+ #
29
+ # @param obj_class_name [String] The class name of the object class.
30
+ # @param attribute_name [String] The name of the attribute that should be
31
+ # deleted.
32
+ #
33
+ # @return nothing
34
+ def delete_attribute_from(obj_class_name, attribute_name)
35
+ attributes = get_obj_class(obj_class_name)['attributes']
36
+ attributes = attributes.delete_if do |attribute|
37
+ attribute['name'] == attribute_name.to_s
38
+ end
39
+
40
+ update_obj_class(obj_class_name, attributes: attributes)
41
+ end
42
+
43
+ # Updates an attribute for an object class.
44
+ #
45
+ # @example Update "test" attribute for object class "Foo.
46
+ #
47
+ # update_attribute_for('Foo', 'test', { title: 'New Title' })
48
+ #
49
+ # @param obj_class_name [String] The class name of the object class.
50
+ # @param attribute_name [String] The name of the attribute that should be
51
+ # updated.
52
+ # @param params [Hash] Hash of updated attributes.
53
+ #
54
+ # @return nothing
55
+ def update_attribute_for(obj_class_name, attribute_name, params)
56
+ attribute_name = attribute_name.to_s
57
+ attributes = get_obj_class(obj_class_name)['attributes']
58
+ found = false
59
+
60
+ attributes.each_with_index do |attribute, index|
61
+ if attribute['name'] == attribute_name
62
+ attributes[index] = attribute.merge(params.stringify_keys)
63
+ update_obj_class(obj_class_name, attributes: attributes)
64
+
65
+ found = true
66
+
67
+ break
68
+ end
69
+ end
70
+
71
+ unless found
72
+ raise ArgumentError.new("Object class '#{obj_class_name}' does not have attribute '#{attribute_name}'.")
73
+ end
74
+ end
75
+
76
+ # Creates a CMS object class.
77
+ #
78
+ # @example Create "Test" Object Class
79
+ #
80
+ # create_obj_class(name: 'Test', type: 'publication')
81
+ #
82
+ # @param attributes [Hash] The attributes and their values of the new
83
+ # CMS object class.
84
+ #
85
+ # @return nothing
86
+ # @api public
87
+ def create_obj_class(attributes = {})
88
+ endpoint = "workspaces/#{Workspace.current.id}/obj_classes"
89
+
90
+ CmsRestApi.post(endpoint, obj_class: attributes)
91
+ end
92
+
93
+ # Fetches all object attributes and their values.
94
+ #
95
+ # @example Get all attributes for the obj with id "abc123"
96
+ #
97
+ # get_obj('abc123')
98
+ #
99
+ # @param id [String] The ID of the CMS object.
100
+ #
101
+ # @return [Hash] a hash with attributes and their values.
102
+ # @api public
103
+ def get_obj(id)
104
+ endpoint = "workspaces/#{Workspace.current.id}/objs/#{id}"
105
+
106
+ CmsRestApi.get(endpoint)
107
+ end
108
+
109
+ # Fetches all object class attributes and their values.
110
+ #
111
+ # @example Get all attributes for the object class "Test"
112
+ #
113
+ # get_obj_class('Test')
114
+ #
115
+ # @param id [String] The ID of the object class.
116
+ #
117
+ # @return [Hash] a hash with attributes and their values.
118
+ # @api public
119
+ def get_obj_class(id)
120
+ endpoint = "workspaces/#{Workspace.current.id}/obj_classes/#{id}"
121
+
122
+ CmsRestApi.get(endpoint)
123
+ end
124
+
125
+ # Updates a CMS object class.
126
+ #
127
+ # @example Update the title of the "Test" Object Class
128
+ #
129
+ # update_obj_class('Test', title: 'Test Title')
130
+ #
131
+ # @param id [String] The ID of the CMS object class.
132
+ # @param attributes [Hash] The updated attributes and their values.
133
+ #
134
+ # @return nothing
135
+ # @api public
136
+ def update_obj_class(id, attributes = {})
137
+ endpoint = "workspaces/#{Workspace.current.id}/obj_classes/#{id}"
138
+
139
+ CmsRestApi.put(endpoint, obj_class: attributes)
140
+ end
141
+ end
142
+ end
143
+ end