Class: Scrivito::ObjClass
- Inherits:
-
Object
- Object
- Scrivito::ObjClass
- Defined in:
- lib/scrivito/obj_class.rb
Overview
This class represents a CMS obj class. Obj classes can be created, updated and all properties can be read. The class also provides methods to find obj classes. The attributes of an obj class are defined by Attribute instances.
Instance Attribute Summary collapse
-
#attributes ⇒ Scrivito::AttributeCollection
readonly
The attributes of this obj class.
-
#id ⇒ String
readonly
Unique identifier of this obj class.
-
#is_active ⇒ Boolean
(also: #active?)
readonly
Whether instances can be created with this obj class.
-
#name ⇒ String
readonly
The name of this obj class.
Class Method Summary collapse
-
.all ⇒ Object
Returns all obj classes.
-
.create(properties) ⇒ Scrivito::ObjClass
Creates a new obj class and persists it in the CMS.
-
.find(name) ⇒ Scrivito::ObjClass
Finds an obj class by its name.
Instance Method Summary collapse
-
#is_binary ⇒ Boolean
(also: #binary?)
deprecated
Deprecated.
Please create objects without using the
is_binaryoption -
#legacy_type? ⇒ Boolean
Returns true if this ObjClass was created with a
is_binaryoption. -
#update(properties) ⇒ nil
Updates this obj class and persists the changes in the CMS.
Instance Attribute Details
#attributes ⇒ Scrivito::AttributeCollection (readonly)
Returns the attributes of this obj class.
191 192 193 |
# File 'lib/scrivito/obj_class.rb', line 191 def attributes @attributes end |
#id ⇒ String (readonly)
Returns unique identifier of this obj class.
156 |
# File 'lib/scrivito/obj_class.rb', line 156 delegate :id, :name, :is_active, to: :obj_class_data |
#is_active ⇒ Boolean (readonly) Also known as: active?
Returns whether instances can be created with this obj class.
156 |
# File 'lib/scrivito/obj_class.rb', line 156 delegate :id, :name, :is_active, to: :obj_class_data |
#name ⇒ String (readonly)
Returns the name of this obj class.
156 |
# File 'lib/scrivito/obj_class.rb', line 156 delegate :id, :name, :is_active, to: :obj_class_data |
Class Method Details
.all ⇒ Object
Returns all obj classes.
16 17 18 |
# File 'lib/scrivito/obj_class.rb', line 16 def all Workspace.current.obj_classes end |
.create(properties) ⇒ Scrivito::ObjClass
the is_binary option is deprecated
Creates a new obj class and persists it in the CMS.
This allows you to set the different properties of an obj class by providing a hash with the property names as keys and the values you want to set as values. Attributes can be either given as Attribute instances or as an attribute property hash.
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/scrivito/obj_class.rb', line 82 def create(properties) properties = properties.with_indifferent_access if properties.key?(:is_binary) Scrivito::Deprecation.warn( "`is_binary' is deprecated and should not be used anymore."\ "Please remove the parameter and specify all attributes you need "\ "including the previously provided attributes 'title', 'body', 'blob'" ) end if properties[:attributes] properties[:attributes].map! do |attribute| attribute.respond_to?(:to_cms_rest_api) ? attribute : Attribute.new(attribute) end end workspace = Workspace.current raw_obj_class_data = workspace.api_request(:post, '/obj_classes', obj_class: format_properties_for_cms(properties)) new(ObjClassData.new(raw_obj_class_data), workspace) end |
.find(name) ⇒ Scrivito::ObjClass
Finds an obj class by its name.
30 31 32 33 34 35 36 37 38 |
# File 'lib/scrivito/obj_class.rb', line 30 def find(name) obj_class = Workspace.current.obj_classes[name] unless obj_class raise ResourceNotFound, "Could not find '#{ObjClass}' with name '#{name}'." end obj_class end |
Instance Method Details
#is_binary ⇒ Boolean Also known as: binary?
Please create objects without using the is_binary option
Returns whether instances of this class are binary, e.g. images or PDFs.
162 163 164 165 166 167 168 169 |
# File 'lib/scrivito/obj_class.rb', line 162 def is_binary if legacy_type? obj_class_data.is_binary else raise ScrivitoError, %(`is_binary' and `binary?' can only be called on ObjClasses with a legacy_type.) end end |
#legacy_type? ⇒ Boolean
Returns true if this ObjClass was created with a is_binary option. Creating ObjClass with this option is deprecated and will be removed.
143 144 145 |
# File 'lib/scrivito/obj_class.rb', line 143 def legacy_type? !!obj_class_data.type end |
#update(properties) ⇒ nil
Updates this obj class and persists the changes in the CMS. It is not possible to update the name.
See create for a detailed overview of what properties are allowed and how to set them.
The is_binary option can only be removed. By passing nil for the is_binary option to update, you can convert legacy objects. This conversion removes the fields title, body (if is_binary was false) or blob (if is_binary was true). If you add the fields in the same request the values will be kept for all objs.
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'lib/scrivito/obj_class.rb', line 223 def update(properties) params = properties.with_indifferent_access if params.has_key?(:is_binary) if params[:is_binary].nil? params[:type] = params.delete(:is_binary) else raise ScrivitoError, "#{self.class} only supports removing the `is_binary' property" end end if params.has_key?(:name) raise ScrivitoError, "#{self.class} does not support changing 'name'. Please remove" \ " the key from the properties." end if params.has_key?(:attributes) params[:attributes].map! do |attribute| attribute = Attribute.new(attribute) unless attribute.respond_to?(:to_cms_rest_api) attribute.obj_class = self attribute.to_cms_rest_api end end raw_obj_class_data = workspace.api_request(:put, "/obj_classes/#{name}", obj_class: params) update_obj_class_data(ObjClassData.new(raw_obj_class_data)) workspace.reload nil end |