Class: Scrivito::BasicObj
- Inherits:
-
Object
- Object
- Scrivito::BasicObj
- Extended by:
- ActiveModel::Naming, ActiveSupport::DescendantsTracker, Associations::ClassMethods, AttributeContent::ClassMethods
- Includes:
- Associations, AttributeContent
- Defined in:
- app/cms/scrivito/basic_obj.rb
Overview
The abstract base class for CMS objects.
A CMS object is a collection of properties and their values, as defined by its object class. These properties can be accessed in views, either directly as the object itself is rendered, or indirectly when other objects are rendered. The description of an image, for example, can be retrieved from within any view that requires it, e.g. a page on which the image is displayed.
Direct Known Subclasses
Class Method Summary collapse
-
.all ⇒ Scrivito::ObjSearchEnumerator
Returns an ObjSearchEnumerator of all Objs.
-
.attribute(name, type, options = {}) ⇒ Object
extended
from AttributeContent::ClassMethods
Defines an attribute.
-
.attribute_definitions ⇒ Scrivito::AttributeDefinitionCollection
extended
from AttributeContent::ClassMethods
Returns the attribute definitions.
-
.create(attributes = {}, context = {}) ⇒ Obj
Create a new Obj in the CMS.
-
.default_for(attribute_name, &block) ⇒ Object
extended
from AttributeContent::ClassMethods
Sets the default value of an attribute defined by AttributeContent::ClassMethods#attribute or for the built-in attributes
_path
and_permalink
. -
.description_for_editor ⇒ String
extended
from AttributeContent::ClassMethods
Short description of a CMS object or widget type for the UI.
-
.find(id_or_list) ⇒ Obj+
Find an Obj by its id.
-
.find_all_by_name(name) ⇒ Scrivito::ObjSearchEnumerator
Returns an ObjSearchEnumerator of all CMS objects with the given name.
-
.find_all_by_obj_class(obj_class) ⇒ Scrivito::ObjSearchEnumerator
Returns an ObjSearchEnumerator of all CMS objects with the given
obj_class
. -
.find_by_name(name) ⇒ Obj
Find an Obj with the given name.
-
.find_by_path(path) ⇒ Obj
Find the Obj with the given path.
-
.find_by_permalink(permalink) ⇒ Obj
Returns the Obj with the given permalink, or
nil
if no matching Obj exists. -
.find_by_permalink!(permalink) ⇒ Obj
Returns the Obj with the given permalink, or raises ResourceNotFound if no matching Obj exists.
-
.find_including_deleted(id_or_list) ⇒ Obj+
Find an Obj by its id.
-
.hide_from_editor ⇒ Object
extended
from AttributeContent::ClassMethods
This method prevents UI users from creating
Objs
orWidgets
of the given type. -
.restore(obj_id) ⇒ Object
Restores a previously deleted
Obj
. -
.root ⇒ Obj
Returns the root Obj, the object whose path is “/”.
-
.valid_page_classes_beneath(parent_path) ⇒ NilClass, Array<Class>
Hook method that lets you control which page classes are made available for pages with the given path.
-
.where(field, operator, value, boost = nil) ⇒ Scrivito::ObjSearchEnumerator
Returns an ObjSearchEnumerator with the given initial subquery consisting of the four arguments.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Returns the value of a system or custom attribute specified by its name.
-
#alt_description ⇒ String
The alt description of an
Obj
used for ScrivitoHelper#scrivito_image_tag. -
#ancestors ⇒ Array<Obj>
Returns an Array of all the ancestor objects, starting at the root and ending at the parent of this object.
-
#apply_image_transformation? ⇒ Boolean
deprecated
Deprecated.
Has no effect anymore: Image transformation will be applied if possible.
-
#as_json(options = nil) ⇒ Hash
included
from AttributeContent
Returns a hash to be used for the JSON serialization.
-
#backlinks ⇒ Scrivito::ObjSearchEnumerator
included
from Associations
Searches for CMS objects containing one or more attributes linking to this CMS object.
-
#binary ⇒ Binary?
This method is intended for CMS objects that represent binary resources like images or PDF documents.
-
#binary? ⇒ Boolean
This method indicates whether the Obj represents binary data.
-
#binary_content_type ⇒ String?
This method returns the content type of the binary of this Obj if the binary is set.
-
#binary_length ⇒ Integer
This method returns the byte length of the binary of the Obj.
-
#binary_url ⇒ String?
This method returns the URL under which the content of this binary is available to the public if the binary is set.
-
#body ⇒ String
Returns the body (main content) of the Obj for non-binary objects.
-
#children ⇒ Array<Obj>
Returns a list of all child Objs.
-
#controller_action_name ⇒ String
This method determines the action to be invoked when the
Obj
is requested. -
#controller_name ⇒ String
This method determines the controller to be invoked when the
Obj
is requested. -
#copy(options = {}) ⇒ Obj
Creates a copy of the Obj.
-
#description_for_editor ⇒ Object
This method determines the description shown in the UI.
- #destroy ⇒ Object
-
#display_title ⇒ String
Calculates an appropriate title for an
Obj
. -
#file_extension ⇒ String
Returns the name extension of the Obj (the part after the last dot).
- #id ⇒ Object
- #last_changed ⇒ Object
-
#meta_data ⇒ Scrivito::MetaDataCollection
Allows accessing the meta data for binary
Objs
. -
#name ⇒ Object
Returns the name of the Obj.
-
#obj_class ⇒ String
included
from AttributeContent
Returns the object class name of this CMS object.
-
#obj_class_name ⇒ String
included
from AttributeContent
deprecated
Deprecated.
Use AttributeContent#obj_class instead.
-
#parent ⇒ Object
Returns the Obj that is the parent of this Obj.
-
#path ⇒ Object
Returns the path of the Obj as a String.
-
#permalink ⇒ Object
Returns the permalink of the Obj.
-
#reload ⇒ Object
Reloads the attributes of this Obj from the database.
-
#revert ⇒ Object
Reverts all changes made to the
Obj
in the current workspace. -
#root? ⇒ Boolean
Returns true if this Obj is the root Obj.
-
#slug ⇒ String
This method is used to calculate a part of a URL of this Obj.
-
#sorted_toclist ⇒ Array<Obj>
Returns a list of children that are sorted according to the order specifed in the
child_order
attribute. - #title ⇒ Object
-
#toclist(*args) ⇒ Array<Obj>
Returns a list of children excluding the binary? ones unless :all is specfied.
-
#update(attributes) ⇒ Object
Update the Obj using the attributes provided.
-
#valid_widget_classes_for(field_name) ⇒ nil, Array<Class>
included
from AttributeContent
Hook method that lets you control the widget classes that are made available for adding instances of them to this page or widget.
-
#widgets ⇒ Scrivito::WidgetCollection
Allows accessing the Widgets of this Obj.
Methods included from Associations::ClassMethods
belongs_to, belongs_to_many, has_many
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Scrivito::AttributeContent
Class Method Details
.all ⇒ Scrivito::ObjSearchEnumerator
Returns an ObjSearchEnumerator of all Objs. If invoked on a subclass of Obj, the result is restricted to instances of this subclass.
280 281 282 283 284 285 286 287 |
# File 'app/cms/scrivito/basic_obj.rb', line 280 def self.all assert_not_basic_obj('.all') if type_computer.special_class?(self) Workspace.current.objs.all else find_all_by_obj_class(name) end end |
.attribute(name, type, options = {}) ⇒ Object Originally defined in module AttributeContent::ClassMethods
Defines an attribute.
For the purpose of persisting model data in the CMS, the attributes of the model need to be defined. When defining an attribute, you specify the name under which Scrivito should persist its value as well as the type of content it is meant to contain, and, for the enum
and multienum
types, the selectable values.
Attributes are inherited. If, for example, the Page
model defines a title
attribute of the string
type, the SpecialPage
model, which inherits from Page
, is also equipped with title
. Inherited attributes can be overridden, i.e. you may redefine title
in SpecialPage
if you want its type to be html
, for example.
.attribute_definitions ⇒ Scrivito::AttributeDefinitionCollection Originally defined in module AttributeContent::ClassMethods
Returns the attribute definitions.
.create(attributes = {}, context = {}) ⇒ Obj
Create a new Obj in the CMS.
This allows you to set the different attributes of an Obj by providing a hash containing the attribute names as keys and the corresponding values you want to set. The defaults set via Obj.default_for are taken into account.
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'app/cms/scrivito/basic_obj.rb', line 134 def self.create(attributes = {}, context = {}) attributes = with_default_id_attribute(attributes) if obj_class = extract_obj_class_from_attributes(attributes) obj_class.create(attributes, context) else attributes = build_attributes_with_defaults(attributes, context) attributes = prepare_attributes_for_instantiation(attributes) api_attributes, = prepare_attributes_for_rest_api(attributes) workspace = Workspace.current obj_data = workspace.create_obj(obj: api_attributes) obj = BasicObj.instantiate(obj_data) obj.revision = workspace.revision CmsRestApi::WidgetExtractor.(obj, ) obj end end |
.default_for(attribute_name, &block) ⇒ Object Originally defined in module AttributeContent::ClassMethods
Sets the default value of an attribute defined by #attribute or for the built-in attributes _path
and _permalink
.
If Obj.create or Widget.new are called without providing a value for a specific custom attribute, the block
is called, and its return value is used as the initial value of this attribute.
The block
is called with two parameters.
The first parameter is an ActiveSupport::HashWithIndifferentAccess
containing the attributes that were passed to Obj.create or Widget.new.
The second parameter, context
, is a hash. If the default_for
callback is triggered by a UI user creating a CMS object or a Widget, Scrivito places the :scrivito_user
and the :current_page
(originating from the UI calling the creation method) into this hash. :current_page
won’t be present in the context
if the user creates the object or widget while viewing a page which isn’t a CMS object. The context
hash is empty if the object or widget is not created using the UI but, for example, via the console.
.description_for_editor ⇒ String Originally defined in module AttributeContent::ClassMethods
Short description of a CMS object or widget type for the UI.
The description is displayed when adding new pages or widgets, for example. As a general rule, it is used whenever no widget or object instance is available. If there is, the BasicObj#description_for_editor and, respectively, BasicWidget#description_for_editor instance methods are used instead.
This method can be overridden to customize the description displayed to editors.
.find(id_or_list) ⇒ Obj+
Find an Obj by its id. If the parameter is an Array of ids, the list of corresponding objects is returned.
207 208 209 |
# File 'app/cms/scrivito/basic_obj.rb', line 207 def self.find(id_or_list) Workspace.current.objs.find(id_or_list) end |
.find_all_by_name(name) ⇒ Scrivito::ObjSearchEnumerator
Returns an ObjSearchEnumerator of all CMS objects with the given name.
321 322 323 |
# File 'app/cms/scrivito/basic_obj.rb', line 321 def self.find_all_by_name(name) where(:_name, :equals, name) end |
.find_all_by_obj_class(obj_class) ⇒ Scrivito::ObjSearchEnumerator
Returns an ObjSearchEnumerator of all CMS objects with the given obj_class
.
293 294 295 |
# File 'app/cms/scrivito/basic_obj.rb', line 293 def self.find_all_by_obj_class(obj_class) Workspace.current.objs.find_all_by_obj_class(obj_class) end |
.find_by_name(name) ⇒ Obj
Find an Obj with the given name. If several objects with the given name exist, an arbitrary one is chosen and returned. If no Obj with this name exists, nil
is returned.
313 314 315 |
# File 'app/cms/scrivito/basic_obj.rb', line 313 def self.find_by_name(name) where(:_name, :equals, name).batch_size(1).first end |
.find_by_path(path) ⇒ Obj
Find the Obj with the given path. Returns nil
if no matching Obj exists.
302 303 304 305 |
# File 'app/cms/scrivito/basic_obj.rb', line 302 def self.find_by_path(path) Workspace.current.objs.find_one_by(:path, path, obj_class_name: type_computer.obj_class_name_for_type(self)) end |
.find_by_permalink(permalink) ⇒ Obj
Returns the Obj with the given permalink, or nil
if no matching Obj exists.
330 331 332 333 |
# File 'app/cms/scrivito/basic_obj.rb', line 330 def self.find_by_permalink(permalink) Workspace.current.objs.find_one_by(:permalink, permalink, obj_class_name: type_computer.obj_class_name_for_type(self)) end |
.find_by_permalink!(permalink) ⇒ Obj
Returns the Obj with the given permalink, or raises ResourceNotFound if no matching Obj exists.
341 342 343 344 345 346 |
# File 'app/cms/scrivito/basic_obj.rb', line 341 def self.find_by_permalink!(permalink) find_by_permalink(permalink) or begin model_name = type_computer.obj_class_name_for_type(self) || 'Obj' raise ResourceNotFound, "Could not find #{model_name} with permalink '#{permalink}'" end end |
.find_including_deleted(id_or_list) ⇒ Obj+
Find an Obj by its id. If the parameter is an Array containing ids, return a list of corresponding objects. The results include deleted objects as well.
221 222 223 |
# File 'app/cms/scrivito/basic_obj.rb', line 221 def self.find_including_deleted(id_or_list) Workspace.current.objs.find_including_deleted(id_or_list) end |
.hide_from_editor ⇒ Object Originally defined in module AttributeContent::ClassMethods
This method prevents UI users from creating Objs
or Widgets
of the given type. It does not prevent adding such objects programatically.
By default, hide_from_editor
is false
.
.restore(obj_id) ⇒ Object
Restores a previously deleted Obj
.
159 160 161 162 163 164 |
# File 'app/cms/scrivito/basic_obj.rb', line 159 def self.restore(obj_id) Workspace.current.assert_revertable base_revision_path = "revisions/#{Workspace.current.base_revision_id}/objs/#{obj_id}" obj_attributes = CmsRestApi.get(base_revision_path).merge('_id' => obj_id) Workspace.current.api_request(:post, '/objs', obj: obj_attributes) end |
.root ⇒ Obj
Returns the root Obj, the object whose path is “/”.
509 510 511 512 513 514 |
# File 'app/cms/scrivito/basic_obj.rb', line 509 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 "bundle exec rake scrivito:migrate scrivito:migrate:publish".' end |
.valid_page_classes_beneath(parent_path) ⇒ NilClass, Array<Class>
Hook method that lets you control which page classes are made available for pages with the given path. Override it to allow only specific classes or none at all. Must return either NilClass
, or Array
.
Be aware that the given argument is a parent path. E.g., when creating a page whose path is /products/shoes
, the argument must be /products
. The given parent path can also be NilClass
.
If NilClass
is returned, all page classes are made available. By default, NilClass
is returned.
If an Array
is returned, it is expected to contain the available classes. The order of the classes is preserved.
365 366 |
# File 'app/cms/scrivito/basic_obj.rb', line 365 def self.valid_page_classes_beneath(parent_path) end |
.where(field, operator, value, boost = nil) ⇒ Scrivito::ObjSearchEnumerator
If invoked on a subclass of Obj, the result will be restricted to instances of this
Returns an ObjSearchEnumerator with the given initial subquery consisting of the four arguments.
Note that field
and value
can also be arrays for searching several fields or searching for several values.
subclass.
ObjSearchEnumerators can be chained using one of the chainable methods (e.g. ObjSearchEnumerator#and and ObjSearchEnumerator#and_not).
265 266 267 268 269 270 271 272 273 |
# File 'app/cms/scrivito/basic_obj.rb', line 265 def self.where(field, operator, value, boost = nil) assert_not_basic_obj('.where') if type_computer.special_class?(self) Workspace.current.objs.where(field, operator, value, boost) else Workspace.current.objs.where(:_obj_class, :equals, name) .and(field, operator, value, boost) end end |
Instance Method Details
#[](key) ⇒ Object
Returns the value of a system or custom attribute specified by its name. Passing an invalid key will not raise an error but return nil
.
672 673 674 675 676 677 678 679 |
# File 'app/cms/scrivito/basic_obj.rb', line 672 def [](key) key = key.to_s if SYSTEM_KEYS.include?(key) read_attribute(key) else super end end |
#alt_description ⇒ String
The alt description of an Obj
used for ScrivitoHelper#scrivito_image_tag.
By default, this method returns the title
of the Obj
.
You can customize this part by overriding #alt_description.
590 591 592 |
# File 'app/cms/scrivito/basic_obj.rb', line 590 def alt_description title end |
#ancestors ⇒ Array<Obj>
Returns an Array of all the ancestor objects, starting at the root and ending at the parent of this object.
469 470 471 472 473 474 475 476 477 |
# File 'app/cms/scrivito/basic_obj.rb', line 469 def ancestors return [] unless child_path? ancestor_paths = parent_path.scan(/\/[^\/]+/).inject([""]) do |list, component| list << list.last + component end ancestor_paths[0] = "/" Workspace.current.objs.find_by_paths(ancestor_paths) end |
#apply_image_transformation? ⇒ Boolean
Has no effect anymore: Image transformation will be applied if possible.
617 618 619 620 |
# File 'app/cms/scrivito/basic_obj.rb', line 617 def apply_image_transformation? Scrivito::Deprecation.warn( 'Scrivito::BasicObj#apply_image_transformation? is deprecated and has no effect anymore.') end |
#as_json(options = nil) ⇒ Hash Originally defined in module AttributeContent
Override it in subclasses to fit your needs.
Returns a hash to be used for the JSON serialization.
#backlinks ⇒ Scrivito::ObjSearchEnumerator Originally defined in module Associations
Searches for CMS objects containing one or more attributes linking to this CMS object.
So the search returns the CMS objects in which at least one html
, link
, linklist
, reference
or referencelist
attribute links to this CMS object.
Attributes inside Widgets are considered, too.
#binary ⇒ Binary?
This method is intended for CMS objects that represent binary resources like images or PDF documents. If the Obj represents a binary file, an instance of Scrivito::Binary is returned.
This method returns the blob
attribute if its type is binary
.
817 818 819 |
# File 'app/cms/scrivito/basic_obj.rb', line 817 def binary self[:blob] if self[:blob].is_a?(Binary) end |
#binary? ⇒ Boolean
This method indicates whether the Obj represents binary data. Binaries are handled differently in that they are not rendered using the normal layout but sent as a file. Examples of binary resources are Images or PDFs.
Every Obj that has a blob
attribute of the binary
type is considered a binary
608 609 610 611 |
# File 'app/cms/scrivito/basic_obj.rb', line 608 def binary? blob_attribute_definition = attribute_definitions['blob'] blob_attribute_definition.present? && blob_attribute_definition.type == 'binary' end |
#binary_content_type ⇒ String?
This method returns the content type of the binary of this Obj if the binary is set.
831 832 833 |
# File 'app/cms/scrivito/basic_obj.rb', line 831 def binary_content_type binary.try(:content_type) end |
#binary_length ⇒ Integer
This method returns the byte length of the binary of the Obj.
824 825 826 |
# File 'app/cms/scrivito/basic_obj.rb', line 824 def binary_length binary.try(:content_length) || 0 end |
#binary_url ⇒ String?
This method returns the URL under which the content of this binary is available to the public if the binary is set.
See Scrivito::Binary#url for details
841 842 843 |
# File 'app/cms/scrivito/basic_obj.rb', line 841 def binary_url binary.try(:url) end |
#body ⇒ String
Returns the body (main content) of the Obj for non-binary objects. Returns nil
for binary objects.
801 802 803 804 805 806 807 |
# File 'app/cms/scrivito/basic_obj.rb', line 801 def body if binary? nil else read_attribute('body') end end |
#children ⇒ Array<Obj>
Returns a list of all child Objs.
482 483 484 485 486 |
# File 'app/cms/scrivito/basic_obj.rb', line 482 def children return [] unless path workspace.objs.find_by_parent_path(path) end |
#controller_action_name ⇒ String
This method determines the action to be invoked when the Obj
is requested. The default action is ‘index’. Override this method to force a different action to be used.
541 542 543 |
# File 'app/cms/scrivito/basic_obj.rb', line 541 def controller_action_name "index" end |
#controller_name ⇒ String
This method determines the controller to be invoked when the Obj
is requested. By default, a controller matching the obj_class of the Obj is used. If the controller does not exist, the CmsController is used as a fallback. Override this method to force a different controller to be used.
532 533 534 |
# File 'app/cms/scrivito/basic_obj.rb', line 532 def controller_name obj_class end |
#copy(options = {}) ⇒ Obj
Creates a copy of the Obj.
434 435 436 437 438 439 440 441 442 |
# File 'app/cms/scrivito/basic_obj.rb', line 434 def copy(={}) = .stringify_keys.assert_valid_keys('_path', '_id', '_permalink') attributes_for_copy = self.class.with_default_id_attribute(copyable_attributes) attributes_for_copy = attributes_for_copy.with_indifferent_access.merge() attributes_for_copy = copy_binaries(attributes_for_copy) json = workspace.api_request(:post, '/objs', obj: attributes_for_copy) self.class.find(json['_id']) end |
#description_for_editor ⇒ Object
This method determines the description shown in the UI. It defaults to #display_title. It can be overridden by a custom value.
565 566 567 |
# File 'app/cms/scrivito/basic_obj.rb', line 565 def description_for_editor display_title end |
#destroy ⇒ Object
446 447 448 449 450 451 |
# File 'app/cms/scrivito/basic_obj.rb', line 446 def destroy if children.any? raise ClientError.new(I18n.t('scrivito.errors.models.basic_obj.has_children')) end workspace.api_request(:delete, "/objs/#{id}") end |
#display_title ⇒ String
Calculates an appropriate title for an Obj
.
578 579 580 |
# File 'app/cms/scrivito/basic_obj.rb', line 578 def display_title (binary_title || title).presence || self.class.description_for_editor end |
#file_extension ⇒ String
Returns the name extension of the Obj (the part after the last dot). Returns an empty string if the name of the Obj doesn’t have an extension.
793 794 795 |
# File 'app/cms/scrivito/basic_obj.rb', line 793 def file_extension File.extname(name)[1..-1] || "" end |
#id ⇒ Object
185 186 187 |
# File 'app/cms/scrivito/basic_obj.rb', line 185 def id read_attribute('_id') end |
#last_changed ⇒ Object
708 709 710 |
# File 'app/cms/scrivito/basic_obj.rb', line 708 def last_changed read_attribute('_last_changed') end |
#meta_data ⇒ Scrivito::MetaDataCollection
Allows accessing the meta data for binary Objs
.
856 857 858 |
# File 'app/cms/scrivito/basic_obj.rb', line 856 def binary.try(:meta_data) || MetaDataCollection.new({}) end |
#name ⇒ Object
Returns the name of the Obj. The name is the last component of a path.
498 499 500 501 502 503 504 |
# File 'app/cms/scrivito/basic_obj.rb', line 498 def name if child_path? path.match(/[^\/]+$/)[0] else "" end end |
#obj_class ⇒ String Originally defined in module AttributeContent
Returns the object class name of this CMS object.
#obj_class_name ⇒ String Originally defined in module AttributeContent
Use #obj_class instead.
Returns the object class name of this CMS object.
#parent ⇒ Object
Returns the Obj that is the parent of this Obj. Returns nil
for the root Obj.
460 461 462 463 464 |
# File 'app/cms/scrivito/basic_obj.rb', line 460 def parent if child_path? workspace.objs.find_by_path(parent_path) end end |
#path ⇒ Object
Returns the path of the Obj as a String.
492 493 494 |
# File 'app/cms/scrivito/basic_obj.rb', line 492 def path read_attribute('_path') end |
#permalink ⇒ Object
Returns the permalink of the Obj.
522 523 524 |
# File 'app/cms/scrivito/basic_obj.rb', line 522 def permalink read_attribute('_permalink') end |
#reload ⇒ Object
Reloads the attributes of this Obj from the database. Note that the Ruby class of this Obj instance will NOT change, even if the obj_class in the database has changed.
695 696 697 698 |
# File 'app/cms/scrivito/basic_obj.rb', line 695 def reload workspace.reload reload_data end |
#revert ⇒ Object
This method does not support Objs that are new
. Please use Obj#destroy to destroy them.
This method does not support Objs that are deleted
. Please use Obj.restore to restore them.
Reverts all changes made to the Obj
in the current workspace.
914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 |
# File 'app/cms/scrivito/basic_obj.rb', line 914 def revert assert_revertable if modification == Modification::EDITED base_revision_path = "revisions/#{workspace.base_revision_id}/objs/#{id}" previous_attributes = CmsRestApi.get(base_revision_path).except('_id') = previous_attributes['_widget_pool'] = .keys - .keys .each { || [] = nil } previous_attributes = reset_blank_attributes(previous_attributes) workspace.api_request(:put, "/objs/#{id}", obj: previous_attributes) reload end end |
#root? ⇒ Boolean
Returns true if this Obj is the root Obj.
624 625 626 |
# File 'app/cms/scrivito/basic_obj.rb', line 624 def root? path == "/" end |
#slug ⇒ String
This method is used to calculate a part of a URL of this Obj.
The routing schema: <obj.slug>-<obj.id>
The default is parameterize on obj.title
.
You can customize this part by overriding #slug.
555 556 557 |
# File 'app/cms/scrivito/basic_obj.rb', line 555 def slug (title || '').parameterize end |
#sorted_toclist ⇒ Array<Obj>
Returns a list of children that are sorted according to the order specifed in the child_order
attribute. The same sort order is used when rendering navigations using the scrivito_tag_list helper.
646 647 648 |
# File 'app/cms/scrivito/basic_obj.rb', line 646 def sorted_toclist sort_toclist(toclist) end |
#title ⇒ Object
595 596 597 |
# File 'app/cms/scrivito/basic_obj.rb', line 595 def title read_attribute('title') end |
#toclist(*args) ⇒ Array<Obj>
Returns a list of children excluding the binary? ones unless :all is specfied. This is useful for creating navigations.
632 633 634 635 636 637 |
# File 'app/cms/scrivito/basic_obj.rb', line 632 def toclist(*args) return [] if binary? toclist = children toclist = toclist.reject { |toc| toc.binary? } unless args.include?(:all) toclist end |
#update(attributes) ⇒ Object
Update the Obj using the attributes provided.
For an overview of the values you can set via this method, see the documentation of Obj.create.
Additionally, update
accepts a _widget_pool
hash in attributes
to modify widgets. The keys of _widget_pool
are widget instances, the values are the modified attributes of these particular widgets.
416 417 418 419 420 421 |
# File 'app/cms/scrivito/basic_obj.rb', line 416 def update(attributes) api_attributes, = prepare_attributes_for_rest_api(attributes) update_data(workspace.update_obj(id, obj: api_attributes)) CmsRestApi::WidgetExtractor.(self, ) self end |
#valid_widget_classes_for(field_name) ⇒ nil, Array<Class> Originally defined in module AttributeContent
Hook method that lets you control the widget classes that are made available for adding instances of them to this page or widget. Override it to allow only specific classes or none at all. Must return either NilClass
, or Array
.
If nil
is returned (default), all widget classes will be available for this page or widget.
If an Array
is returned, it is expected to include the permitted classes. Their order is preserved as they are offered to the user via the widget browser.
#widgets ⇒ Scrivito::WidgetCollection
Allows accessing the Widgets of this Obj.
891 892 893 |
# File 'app/cms/scrivito/basic_obj.rb', line 891 def @widgets ||= WidgetCollection.new(self) end |