Class: RailsConnector::BasicObj
- Inherits:
-
Object
- Object
- RailsConnector::BasicObj
- Extended by:
- ActiveModel::Naming
- Defined in:
- lib/rails_connector/basic_obj.rb
Overview
The CMS file class
Class Method Summary collapse
-
.all ⇒ ObjSearchEnumerator
Returns a ObjSearchEnumerator of all Objs.
-
.create(attributes) ⇒ Obj
Create a new Obj in the cms.
-
.find(id_or_list) ⇒ Obj+
Find a Obj by its id.
-
.find_all_by_name(name) ⇒ ObjSearchEnumerator
Returns a ObjSearchEnumerator of all Objs with the given name.
-
.find_all_by_obj_class(obj_class) ⇒ ObjSearchEnumerator
Returns a ObjSearchEnumerator of all Objs 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 raise ResourceNotFound if no matching Obj exists.
-
.find_including_deleted(id_or_list) ⇒ Obj+
Find a Obj by its id.
-
.homepage ⇒ Obj
Returns the homepage obj.
-
.root ⇒ Obj
Returns the root Obj, i.e.
-
.valid_page_classes_beneath(parent_path) ⇒ NilClass, Array<Symbol, String>
Hook method to control which page classes should be available for a page with given path.
-
.where(field, operator, value, boost = nil) ⇒ ObjSearchEnumerator
Returns a ObjSearchEnumerator with the given initial subquery consisting of the four arguments.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Returns the value of an internal or external attribute specified by its name.
-
#active? ⇒ Boolean
deprecated
Deprecated.
Active is deprecated without substitution.
-
#ancestors ⇒ Array<Obj>
Returns an Array of all the ancestor objects, starting at the root and ending at this object’s parent.
-
#body ⇒ String
Returns the body (main content) of the Obj for non-binary Objs.
-
#body_content_type ⇒ String
returns the content type of the Obj’s body for binary Objs.
-
#body_data_url ⇒ String
returns an URL to retrieve the Obj’s body for binary Objs.
-
#body_length ⇒ Object
for binary Objs body_length equals the file size for non-binary Objs body_length equals the number of characters in the body (main content).
-
#children ⇒ Array<Obj>
return a list of all child Objs.
-
#content_type ⇒ String
(also: #mime_type)
For a binary Obj, the content_type is equal to the content_type of its body (i.e. its data).
-
#controller_action_name ⇒ String
This method determines the action that should be invoked when the
Obj
is requested. -
#controller_name ⇒ String
This method determines the controller that should be invoked when the
Obj
is requested. -
#description_for_editor ⇒ Object
This method determines the description that is shown in the changes list.
- #destroy ⇒ Object
-
#display_title ⇒ String
Returns the title of the content or the name.
-
#file_extension ⇒ String
returns the extension (the part after the last dot) from the Obj’s name.
-
#find_nearest(name) ⇒ Obj
Returns the Object with the given name next in the hierarchy returns
nil
if no object with the given name was found. -
#homepage? ⇒ Boolean
Returns true if the current obj is the BasicObj.homepage obj.
- #id ⇒ Object
- #last_changed ⇒ Object
-
#name ⇒ Object
returns the Obj‘s name, i.e.
- #obj_class ⇒ String
-
#parent ⇒ Object
return the Obj that is the parent of this Obj.
-
#path ⇒ Object
returns the Obj‘s path as a String.
-
#permalink ⇒ Object
returns the obj’s permalink.
-
#reload ⇒ Object
Reloads the attributes of this object from the database.
-
#root? ⇒ Boolean
Returns true if this object is the root object.
-
#slug ⇒ String
This method is used to calculate a part of a URL of this Obj.
-
#sorted_toclist(*args) ⇒ Array<Obj>
Returns the sorted
toclist
, respecting sort order and type of this Obj. - #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 with the attributes provided.
- #valid_from ⇒ Object
- #valid_until ⇒ Object
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class RailsConnector::AttributeContent
Class Method Details
.all ⇒ ObjSearchEnumerator
Returns a ObjSearchEnumerator of all Objs. If invoked on a subclass of Obj, the result will be restricted to instances of that subclass.
131 132 133 134 135 136 137 |
# File 'lib/rails_connector/basic_obj.rb', line 131 def self.all if superclass == RailsConnector::BasicObj search_for_all else find_all_by_obj_class(name) end end |
.create(attributes) ⇒ Obj
Create a new Obj in the cms
This allows you to set the different attributes types of an obj by providing a hash with the attributes names as key and the values you want to set as values
59 60 61 62 63 64 65 66 |
# File 'lib/rails_connector/basic_obj.rb', line 59 def self.create(attributes) converted_attributes = CmsRestApi::AttributeSerializer.convert(attributes) json = CmsRestApi.post("workspaces/#{Workspace.current.id}/objs", obj: converted_attributes) find(json['id']) end |
.find(id_or_list) ⇒ Obj+
Find a Obj by its id. If the paremeter is an Array containing ids, return a list of corresponding Objs.
91 92 93 |
# File 'lib/rails_connector/basic_obj.rb', line 91 def self.find(id_or_list) find_filtering_deleted(id_or_list, false) end |
.find_all_by_name(name) ⇒ ObjSearchEnumerator
Returns a ObjSearchEnumerator of all Objs with the given name.
174 175 176 |
# File 'lib/rails_connector/basic_obj.rb', line 174 def self.find_all_by_name(name) where(:_name, :equals, name) end |
.find_all_by_obj_class(obj_class) ⇒ ObjSearchEnumerator
Returns a ObjSearchEnumerator of all Objs with the given obj_class
.
143 144 145 |
# File 'lib/rails_connector/basic_obj.rb', line 143 def self.find_all_by_obj_class(obj_class) search_for_all.and(:_obj_class, :equals, obj_class) end |
.find_by_name(name) ⇒ Obj
Find an Obj with the given name. If several Objs with the given name exist, an arbitrary one of these Objs is chosen and returned. If no Obj with the name exits, nil
is returned.
166 167 168 |
# File 'lib/rails_connector/basic_obj.rb', line 166 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.
152 153 154 |
# File 'lib/rails_connector/basic_obj.rb', line 152 def self.find_by_path(path) find_objs_by(:path, [path]).first.first end |
.find_by_permalink(permalink) ⇒ Obj
Returns the Obj with the given permalink, or nil
if no matching Obj exists.
182 183 184 |
# File 'lib/rails_connector/basic_obj.rb', line 182 def self.find_by_permalink(permalink) find_objs_by(:permalink, [permalink]).first.first end |
.find_by_permalink!(permalink) ⇒ Obj
Returns the Obj with the given permalink, or raise ResourceNotFound if no matching Obj exists.
190 191 192 193 |
# File 'lib/rails_connector/basic_obj.rb', line 190 def self.find_by_permalink!(permalink) find_by_permalink(permalink) or raise ResourceNotFound, "Could not find Obj with permalink '#{permalink}'" end |
.find_including_deleted(id_or_list) ⇒ Obj+
Find a Obj by its id. If the paremeter is an Array containing ids, return a list of corresponding Objs. The results include deleted objects as well.
105 106 107 |
# File 'lib/rails_connector/basic_obj.rb', line 105 def self.find_including_deleted(id_or_list) find_filtering_deleted(id_or_list, true) end |
.homepage ⇒ Obj
Returns the homepage obj. This can be overwritten in your application’s Obj
. Use #homepage? to check if an obj is the homepage.
323 324 325 |
# File 'lib/rails_connector/basic_obj.rb', line 323 def self.homepage root end |
.root ⇒ Obj
Returns the root Obj, i.e. the Obj with the path “/”
314 315 316 317 |
# File 'lib/rails_connector/basic_obj.rb', line 314 def self.root BasicObj.find_by_path("/") or raise ResourceNotFound, "Obj.root not found: There is no Obj with path '/'." end |
.valid_page_classes_beneath(parent_path) ⇒ NilClass, Array<Symbol, String>
Hook method to control which page classes should be available for a page with given path. Override it to allow only certain classes or none. Must return either NilClass
, or Array
.
Be aware that the given argument is a parent path. E.g. when creating a page with path /products/shoes
then the argument will be /products
.
If NilClass
is returned, then all possible classes will be available. By default NilClass
is returned.
If Array
is returned, then it should include desired class names. Each class name must be either a String
or a Symbol
. Only this class names will be available. Order of the class names will be preserved.
229 230 |
# File 'lib/rails_connector/basic_obj.rb', line 229 def self.valid_page_classes_beneath(parent_path) end |
.where(field, operator, value, boost = nil) ⇒ ObjSearchEnumerator
Returns a 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.
ObjSearchEnumerators can be chained using one of the chainable methods (e.g. ObjSearchEnumerator#and and ObjSearchEnumerator#and_not).
123 124 125 |
# File 'lib/rails_connector/basic_obj.rb', line 123 def self.where(field, operator, value, boost = nil) ObjSearchEnumerator.new(nil).and(field, operator, value, boost) end |
Instance Method Details
#[](key) ⇒ Object
Returns the value of an internal or external attribute specified by its name. Passing an invalid key will not raise an error, but return nil
.
584 585 586 587 588 589 590 591 592 593 594 |
# File 'lib/rails_connector/basic_obj.rb', line 584 def [](key) key = key.to_s if OLD_INTERNAL_KEYS.include?(key) send(key) elsif key.start_with?('_') && OLD_INTERNAL_KEYS.include?(internal_key = key[1..-1]) # For backwards compatibility reasons send(internal_key) else super end end |
#active? ⇒ Boolean
Active is deprecated without substitution.
Returns true if this object is active.
429 430 431 432 433 |
# File 'lib/rails_connector/basic_obj.rb', line 429 def active? Deprecation.warn_method('Obj#active?') return false unless valid_from valid_from <= Time.now && (!valid_until || Time.now <= valid_until) end |
#ancestors ⇒ Array<Obj>
Returns an Array of all the ancestor objects, starting at the root and ending at this object’s parent.
277 278 279 280 281 282 283 284 |
# File 'lib/rails_connector/basic_obj.rb', line 277 def ancestors return [] if root? ancestor_paths = parent_path.scan(/\/[^\/]+/).inject([""]) do |list, component| list << list.last + component end ancestor_paths[0] = "/" BasicObj.find_many_by_paths(ancestor_paths) end |
#body ⇒ String
Returns the body (main content) of the Obj for non-binary Objs. Returns nil
for binary Objs.
658 659 660 661 662 663 664 |
# File 'lib/rails_connector/basic_obj.rb', line 658 def body if binary? nil else StringTagging.tag_as_html(read_attribute('body')) end end |
#body_content_type ⇒ String
returns the content type of the Obj’s body for binary Objs. returns nil
for non-binary Objs.
693 694 695 696 697 698 699 700 701 702 |
# File 'lib/rails_connector/basic_obj.rb', line 693 def body_content_type if binary? blob = find_blob if blob blob.content_type else "application/octet-stream" end end end |
#body_data_url ⇒ String
returns an URL to retrieve the Obj’s body for binary Objs. returns nil
for non-binary Objs.
682 683 684 685 686 687 |
# File 'lib/rails_connector/basic_obj.rb', line 682 def body_data_url if binary? blob = find_blob blob.url if blob end end |
#body_length ⇒ Object
for binary Objs body_length equals the file size for non-binary Objs body_length equals the number of characters in the body (main content)
669 670 671 672 673 674 675 676 |
# File 'lib/rails_connector/basic_obj.rb', line 669 def body_length if binary? blob = find_blob blob ? blob.length : 0 else (body || "").length end end |
#children ⇒ Array<Obj>
return a list of all child Objs.
289 290 291 |
# File 'lib/rails_connector/basic_obj.rb', line 289 def children self.class.find_objs_by(:ppath, [path]).first end |
#content_type ⇒ String Also known as: mime_type
For a binary Obj, the content_type is equal to the content_type of its body (i.e. its data). For non-binary Objs, a the default content_type is “text/html”. Override this method in subclasses to define a different content_type. Note that only Objs with content_type “text/html” will be rendered with layout and templates by the DefaultCmsController.
637 638 639 640 641 642 643 |
# File 'lib/rails_connector/basic_obj.rb', line 637 def content_type if binary? body_content_type else "text/html" end end |
#controller_action_name ⇒ String
This method determines the action that should be invoked when the Obj
is requested. The default action is ‘index’. Overwrite this method to force a different action to be used.
353 354 355 |
# File 'lib/rails_connector/basic_obj.rb', line 353 def controller_action_name "index" end |
#controller_name ⇒ String
This method determines the controller that should be invoked when the Obj
is requested. By default a controller matching the Obj’s obj_class will be used. If the controller does not exist, the CmsController will be used as a fallback. Overwrite this method to force a different controller to be used.
344 345 346 |
# File 'lib/rails_connector/basic_obj.rb', line 344 def controller_name obj_class end |
#description_for_editor ⇒ Object
This method determines the description that is shown in the changes list. It can be overriden by a custom value.
380 381 382 |
# File 'lib/rails_connector/basic_obj.rb', line 380 def description_for_editor slug.presence || path end |
#destroy ⇒ Object
253 254 255 256 257 258 259 260 261 |
# File 'lib/rails_connector/basic_obj.rb', line 253 def destroy if children.any? raise ClientError.new(I18n.t('rails_connector.errors.models.basic_obj.has_children'), 412) end CmsRestApi.delete("workspaces/#{Workspace.current.id}/objs/#{id}") Workspace.reload end |
#display_title ⇒ String
Returns the title of the content or the name.
387 388 389 |
# File 'lib/rails_connector/basic_obj.rb', line 387 def display_title self.title || name end |
#file_extension ⇒ String
returns the extension (the part after the last dot) from the Obj’s name. returns an empty string if no extension is present in the Obj’s name.
650 651 652 |
# File 'lib/rails_connector/basic_obj.rb', line 650 def file_extension File.extname(name)[1..-1] || "" end |
#find_nearest(name) ⇒ Obj
Returns the Object with the given name next in the hierarchy returns nil
if no object with the given name was found.
546 547 548 549 550 551 552 553 554 |
# File 'lib/rails_connector/basic_obj.rb', line 546 def find_nearest(name) obj = self.class.find_by_path(root? ? "/#{name}" : "#{path}/#{name}") if obj obj elsif !self.root? parent.find_nearest(name) end end |
#homepage? ⇒ Boolean
Returns true if the current obj is the homepage obj.
359 360 361 |
# File 'lib/rails_connector/basic_obj.rb', line 359 def homepage? self == self.class.homepage end |
#id ⇒ Object
80 81 82 |
# File 'lib/rails_connector/basic_obj.rb', line 80 def id read_attribute('_id') end |
#last_changed ⇒ Object
612 613 614 |
# File 'lib/rails_connector/basic_obj.rb', line 612 def last_changed read_attribute('_last_changed') end |
#name ⇒ Object
returns the Obj‘s name, i.e. the last component of the path.
303 304 305 306 307 308 309 |
# File 'lib/rails_connector/basic_obj.rb', line 303 def name if root? "" else path.match(/[^\/]+$/)[0] end end |
#obj_class ⇒ String
607 608 609 |
# File 'lib/rails_connector/basic_obj.rb', line 607 def obj_class read_attribute('_obj_class') end |
#parent ⇒ Object
return the Obj that is the parent of this Obj. returns nil
for the root Obj.
270 271 272 |
# File 'lib/rails_connector/basic_obj.rb', line 270 def parent root? ? nil : BasicObj.find_by_path(parent_path) end |
#path ⇒ Object
returns the Obj‘s path as a String.
297 298 299 |
# File 'lib/rails_connector/basic_obj.rb', line 297 def path read_attribute('_path') or raise 'Obj without path' end |
#permalink ⇒ Object
returns the obj’s permalink.
334 335 336 |
# File 'lib/rails_connector/basic_obj.rb', line 334 def permalink read_attribute('_permalink') end |
#reload ⇒ Object
Reloads the attributes of this object from the database. Notice that the ruby class of this Obj instance will NOT change, even if the obj_class in the database has changed.
600 601 602 603 |
# File 'lib/rails_connector/basic_obj.rb', line 600 def reload obj_data = CmsBackend.instance.find_obj_data_by(Workspace.current.revision, :id, [id.to_s]).first.first update_data(obj_data) end |
#root? ⇒ Boolean
Returns true if this object is the root object.
464 465 466 |
# File 'lib/rails_connector/basic_obj.rb', line 464 def root? path == "/" end |
#slug ⇒ String
This method is used to calculate a part of a URL of this Obj.
The routing schema: <obj.id>/<obj.slug>
The default is parameterize on obj.title
.
You can customize this part by overwriting #slug.
373 374 375 |
# File 'lib/rails_connector/basic_obj.rb', line 373 def slug (title || '').parameterize end |
#sorted_toclist(*args) ⇒ Array<Obj>
Returns the sorted toclist
, respecting sort order and type of this Obj.
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 |
# File 'lib/rails_connector/basic_obj.rb', line 482 def sorted_toclist(*args) list = self.toclist(*args) return [] if list.blank? cached_sort_key1 = self.sort_key1 cached_sort_type1 = self.sort_type1 sorted_list = if cached_sort_key1.blank? list.sort { |left_obj, right_obj| left_obj.name <=> right_obj.name } else cached_sort_key2 = self.sort_key2 cached_sort_type2 = self.sort_type2 cached_sort_key3 = self.sort_key3 cached_sort_type3 = self.sort_type3 list.sort do |left_obj, right_obj| compare = compare_on_sort_key(left_obj, right_obj, cached_sort_key1, cached_sort_type1) if compare == 0 && cached_sort_key2 compare = compare_on_sort_key(left_obj, right_obj, cached_sort_key2, cached_sort_type2) if compare == 0 && cached_sort_key3 compare = compare_on_sort_key(left_obj, right_obj, cached_sort_key3, cached_sort_type3) end end compare end end return self.sort_order == "descending" ? sorted_list.reverse : sorted_list end |
#title ⇒ Object
392 393 394 |
# File 'lib/rails_connector/basic_obj.rb', line 392 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 mainly used for navigations.
472 473 474 475 476 477 |
# File 'lib/rails_connector/basic_obj.rb', line 472 def toclist(*args) return [] unless publication? toclist = children toclist = toclist.reject { |toc| toc.binary? } unless args.include?(:all) toclist end |
#update(attributes) ⇒ Object
Update the Obj with the attributes provided.
For an overview of which values you can set via this method see the documentation of Obj.create.
239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/rails_connector/basic_obj.rb', line 239 def update(attributes) converted_attributes = CmsRestApi::AttributeSerializer.convert(attributes) CmsRestApi.put( "workspaces/#{Workspace.current.id}/objs/#{id}", obj: converted_attributes) Workspace.reload reload self end |
#valid_from ⇒ Object
617 618 619 |
# File 'lib/rails_connector/basic_obj.rb', line 617 def valid_from read_attribute('_valid_from') end |
#valid_until ⇒ Object
622 623 624 |
# File 'lib/rails_connector/basic_obj.rb', line 622 def valid_until read_attribute('_valid_until') end |