lib/scrivito/basic_obj.rb in scrivito_sdk-0.30.0 vs lib/scrivito/basic_obj.rb in scrivito_sdk-0.40.0.rc1
- old
+ new
@@ -74,16 +74,16 @@
# Obj.create(:title => "My Title")
#
# @example Arrays of {String Strings} allow you to set multi enum fields
# Obj.create(:tags => ["ruby", "rails"])
#
- # @example Simply pass an Array of {Scrivito::BasicWidget Widgets} to change a widget field. See {Scrivito::BasicWidget#clone Widget#clone} on how to clone a widget.
+ # @example Simply pass an Array of {Scrivito::BasicWidget Widgets} to change a widget field. See {Scrivito::BasicWidget#copy Widget#copy} on how to copy a widget.
# # Add new widgets
# Obj.create(:widgets => [Widget.new(_obj_class: 'TitleWidget', title: 'My Title')])
#
- # # Add a widget clone
- # Obj.create(:widgets => [another_obj.widgets.first.clone])
+ # # Add a widget copy
+ # Obj.create(:widgets => [another_obj.widgets.first.copy])
#
# # Changing a widget field
# obj.update(:widgets => [obj.widgets.first])
#
# # Clear a widget field
@@ -302,22 +302,22 @@
reload_data
CmsRestApi::WidgetExtractor.notify_persisted_widgets(self, widget_properties)
self
end
- # Copies itself to a given path.
+ # Creates a copy of the +Obj+.
# @api public
# @param [Hash] options
# @option options [String,Symbol] :_path (nil) the path of the copy.
# @option options [String,Symbol] :_id (nil) the id of the copy.
# @option options [String,Symbol] :_permalink (nil) the permalink of the copy.
# @raise [ArgumentError] if +options+ includes invalid keys.
# @return [Obj] the created copy
# @example Copy a blog post.
# blog_post = Obj.find_by_path('/blog/first_post')
# blog_post.copy(_path: '/blog/second_post')
- def copy(options)
+ def copy(options={})
options = options.stringify_keys.assert_valid_keys('_path', '_id', '_permalink')
json = workspace.api_request(:post, '/objs', obj: copyable_attributes.merge(options))
self.class.find(json['_id'])
end
@@ -388,11 +388,11 @@
# @api public
def self.root
BasicObj.find_by_path('/') or raise ResourceNotFound,
'"Obj.root" not found: There is no "Obj" with path "/". '\
'Maybe you forgot the migration when setting up your Scrivito application? '\
- 'Try "rake scrivito:migrate" and "rake scrivito:migrate:publish".'
+ 'Try "bundle exec rake scrivito:migrate scrivito:migrate:publish".'
end
# Returns the homepage obj. This can be overwritten in your application's +Obj+.
# Use {#homepage?} to check if an obj is the homepage.
# @return [Obj]
@@ -718,10 +718,21 @@
def widget_from_pool(widget_id)
widget_data = widget_data_from_pool(widget_id)
instantiate_widget(widget_id, widget_data) if widget_data
end
+ # @api public
+ # Allows accessing the {Scrivito::BasicWidget Widgets} of this Obj
+ #
+ # @example Access a widget by its id
+ # obj.widgets['widget_id']
+ #
+ # @return [Scrivito::WidgetCollection]
+ def widgets
+ @widgets ||= WidgetCollection.new(self)
+ end
+
# for internal testing purposes only
def blob_id
find_blob.try(:id)
end
@@ -754,10 +765,34 @@
workspace.api_request(:put, "/objs/#{id}", obj: previous_attributes)
reload
end
end
+ def restore_widget(widget_id)
+ Workspace.current.assert_revertable
+
+ return if modification == Modification::UNMODIFIED
+ if modification == Modification::DELETED
+ raise ScrivitoError, 'Can not restore a widget inside a deleted obj'
+ end
+
+ unless widget = in_revision(Workspace.current.base_revision).widgets[widget_id]
+ raise ResourceNotFound, "Could not find widget with id #{widget_id}"
+ end
+
+ container = widget.container
+ if container.kind_of?(BasicWidget) && !widgets[container.id]
+ raise ScrivitoError, 'Can not restore a widget inside a deleted widget'
+ end
+
+ widget_copy = widget.copy_for_restore(read_widget_pool.keys)
+ field_name = widget.container_field_name
+ current_container = widgets[container.id] || self
+ current_container.update(field_name =>
+ current_container[field_name].insert(widget.container_field_index, widget_copy))
+ end
+
def mark_resolved
workspace.api_request(:put, "/objs/#{id}", obj: {_conflicts: nil})
reload
end
@@ -800,29 +835,33 @@
end
raise ScrivitoError.new('Could not generate a new unused widget id')
end
+ #
# Generates a +Hash+ containing all public attributes. The hash will _not_ include attributes,
# which are read-only or used for internal purpose.
- # @api public
+ #
+ # @api private
# @return [Hash] a hash containing all public attributes.
+ #
# @example
# old_obj = Obj.homepage
# attrs = old_obj.to_h
# puts attrs
# #=> {"_obj_class"=>"Publication", "_path"=>"/", "_id"=>"f64a68258ca15faf", "_widget_pool"=>{}}
# old_obj.destroy
#
# new_obj = Obj.create(attrs)
# puts new_obj == old_obj
# #=> true
+ #
def to_h
data_from_cms.to_h.except(*GENERATED_ATTRIBUTES)
end
def parent_path
- unless root?
+ unless root? || path.nil?
path.gsub(/\/[^\/]+$/, '').presence || '/'
end
end
private