Class: RailsConnector::BasicObj

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Naming
Defined in:
lib/rails_connector/basic_obj.rb

Overview

The CMS file class

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object (private)



738
739
740
741
742
743
744
# File 'lib/rails_connector/basic_obj.rb', line 738

def method_missing(method_name, *args)
  if has_attribute?(method_name)
    read_attribute(method_name.to_s)
  else
    super
  end
end

Class Method Details

.allObjSearchEnumerator

Returns a ObjSearchEnumerator of all Objs. If invoked on a subclass of Obj, the result will be restricted to Obj of that subclass.

Returns:



75
76
77
78
79
80
81
# File 'lib/rails_connector/basic_obj.rb', line 75

def self.all
  if superclass == RailsConnector::BasicObj
    ObjSearchEnumerator.new(nil)
  else
    find_all_by_obj_class(self.name)
  end
end

.find(id_or_list) ⇒ Obj+

Find an Obj by it’s id. If the paremeter is an Array containing ids, return a list of corresponding Objs.

Parameters:

  • id_or_list (String, Integer, Array<String, Integer>)

Returns:

  • (Obj, Array<Obj>)


43
44
45
46
47
48
49
50
51
# File 'lib/rails_connector/basic_obj.rb', line 43

def self.find(id_or_list)
  case id_or_list
  when Array
    find_objs_by(:id, id_or_list).map(&:first)
  else
    obj = find_objs_by(:id, [id_or_list.to_s]).first.first
    obj or raise ResourceNotFound, "Could not find Obj with id #{id_or_list}"
  end
end

.find_all_by_name(name) ⇒ ObjSearchEnumerator

Returns a ObjSearchEnumerator of all Objs with the given name.

Parameters:

  • name (String)

    Name of the Obj.

Returns:



118
119
120
# File 'lib/rails_connector/basic_obj.rb', line 118

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.

Parameters:

  • obj_class (String)

    Name of the ObjClass.

Returns:



87
88
89
# File 'lib/rails_connector/basic_obj.rb', line 87

def self.find_all_by_obj_class(obj_class)
  where(:_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.

Parameters:

  • name (String)

    Name of the Obj.

Returns:

  • (Obj)


110
111
112
# File 'lib/rails_connector/basic_obj.rb', line 110

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.

Parameters:

  • path (String)

    Path of the Obj.

Returns:

  • (Obj)


96
97
98
# File 'lib/rails_connector/basic_obj.rb', line 96

def self.find_by_path(path)
  find_objs_by(:path, [path]).first.first
end

Returns the Obj with the given permalink, or nil if no matching Obj exists.

Parameters:

  • permalink (String)

    The permalink of the Obj.

Returns:

  • (Obj)


126
127
128
# File 'lib/rails_connector/basic_obj.rb', line 126

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.

Parameters:

  • permalink (String)

    The permalink of the Obj.

Returns:

  • (Obj)


134
135
136
137
# File 'lib/rails_connector/basic_obj.rb', line 134

def self.find_by_permalink!(permalink)
  find_by_permalink(permalink) or
    raise ResourceNotFound, "Could not find Obj with permalink '#{permalink}'"
end

.homepageObj

Returns the homepage object. This can be overwritten in your application’s Obj. Use Obj#homepage? to check if an object is the homepage.

Returns:

  • (Obj)


254
255
256
# File 'lib/rails_connector/basic_obj.rb', line 254

def self.homepage
  root
end

.rootObj

Returns the root Obj, i.e. the Obj with the path “/”

Returns:

  • (Obj)


245
246
247
248
# File 'lib/rails_connector/basic_obj.rb', line 245

def self.root
  BasicObj.find_by_path("/") or raise ResourceNotFound,
      "Obj.root not found: There is no Obj with 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).

Examples:

Look for the first 10 Objs whose ObjClass is “Pressrelease” and whose title contains “quarterly”:

Obj.where(:_obj_class, :equals, 'Pressrelease').and(:title, :contains, 'quarterly').take(10).map{ |obj| obj.valid_from }

Parameters:

Returns:



67
68
69
# File 'lib/rails_connector/basic_obj.rb', line 67

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.



511
512
513
514
515
516
517
518
519
520
521
522
523
# File 'lib/rails_connector/basic_obj.rb', line 511

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)
  elsif has_attribute?(key)
    read_attribute(key)
  else
    nil
  end
end

#active?Boolean

Returns true if this object is active.

Returns:

  • (Boolean)


347
348
349
350
# File 'lib/rails_connector/basic_obj.rb', line 347

def active?
  return false unless valid_from
  valid_from <= Time.now && (!valid_until || Time.now <= valid_until)
end

#ancestorsArray<Obj>

Returns an Array of all the ancestor objects, starting at the root and ending at this object’s parent.

Returns:

  • (Array<Obj>)


185
186
187
188
189
190
191
192
# File 'lib/rails_connector/basic_obj.rb', line 185

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

#bodyString

Returns the body (main content) of the Obj for non-binary Objs. Returns nil for binary Objs.

Returns:

  • (String)


587
588
589
590
591
592
593
# File 'lib/rails_connector/basic_obj.rb', line 587

def body
  if binary?
    nil
  else
    StringTagging.tag_as_html(read_attribute('body'), self)
  end
end

#body_content_typeString

returns the content type of the Obj’s body for binary Objs. returns nil for non-binary Objs.

Returns:

  • (String)


627
628
629
630
631
632
633
634
635
636
# File 'lib/rails_connector/basic_obj.rb', line 627

def body_content_type
  if binary?
    blob = find_blob
    if blob
      blob.content_type
    else
      "application/octet-stream"
    end
  end
end

#body_data_urlString

returns an URL to retrieve the Obj’s body for binary Objs. returns nil for non-binary Objs.

Returns:

  • (String)


611
612
613
614
615
616
# File 'lib/rails_connector/basic_obj.rb', line 611

def body_data_url
  if binary?
    blob = find_blob
    blob.url if blob
  end
end

#body_lengthObject

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)



598
599
600
601
602
603
604
605
# File 'lib/rails_connector/basic_obj.rb', line 598

def body_length
  if binary?
    blob = find_blob
    blob ? blob.length : 0
  else
    (body || "").length
  end
end

#childrenArray<Obj>

return a list of all child Objs.

Returns:

  • (Array<Obj>)


197
198
199
# File 'lib/rails_connector/basic_obj.rb', line 197

def children
  self.class.find_objs_by(:ppath, [path]).first
end

#content_typeString Also known as: mime_type

For a binary Obj, the content_type is equal to the content_type of it’s body (i.e. it’s 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.

Returns:

  • (String)


566
567
568
569
570
571
572
# File 'lib/rails_connector/basic_obj.rb', line 566

def content_type
  if binary?
    body_content_type
  else
    "text/html"
  end
end

#controller_action_nameString

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.

Returns:

  • (String)


279
280
281
# File 'lib/rails_connector/basic_obj.rb', line 279

def controller_action_name
  "index"
end

#controller_nameString

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.

Returns:

  • (String)


270
271
272
# File 'lib/rails_connector/basic_obj.rb', line 270

def controller_name
  obj_class
end

#display_titleString

Returns the title of the content or the name.

Returns:

  • (String)


306
307
308
# File 'lib/rails_connector/basic_obj.rb', line 306

def display_title
  self.title || name
end

#file_extensionString

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.

Returns:

  • (String)


579
580
581
# File 'lib/rails_connector/basic_obj.rb', line 579

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.

Parameters:

  • name (String)

Returns:

  • (Obj)


477
478
479
480
481
# File 'lib/rails_connector/basic_obj.rb', line 477

def find_nearest(name)
  obj = self.class.find_by_path(root? ? "/#{name}" : "#{path}/#{name}")
  return obj if obj and obj.active?
  parent.find_nearest(name) unless self.root?
end

#homepage?Boolean

Returns true if the current object is the homepage object.

Returns:

  • (Boolean)


285
286
287
# File 'lib/rails_connector/basic_obj.rb', line 285

def homepage?
  self == self.class.homepage
end

#idObject



32
33
34
# File 'lib/rails_connector/basic_obj.rb', line 32

def id
  read_attribute('_id')
end

#last_changedObject



545
546
547
# File 'lib/rails_connector/basic_obj.rb', line 545

def last_changed
  read_attribute('_last_changed')
end

#nameObject

returns the Obj’s name, i.e. the last component of the path.



211
212
213
214
215
216
217
# File 'lib/rails_connector/basic_obj.rb', line 211

def name
  if root?
    ""
  else
    path.match(/[^\/]+$/)[0]
  end
end

#obj_classString

Returns:

  • (String)


540
541
542
# File 'lib/rails_connector/basic_obj.rb', line 540

def obj_class
  read_attribute('_obj_class')
end

#parentObject

return the Obj that is the parent of this Obj. returns nil for the root Obj.



178
179
180
# File 'lib/rails_connector/basic_obj.rb', line 178

def parent
  root? ? nil : BasicObj.find_by_path(parent_path)
end

#pathObject

returns the Obj’s path as a String.



205
206
207
# File 'lib/rails_connector/basic_obj.rb', line 205

def path
  read_attribute('_path') or raise 'Obj without path'
end

returns the obj’s permalink.



260
261
262
# File 'lib/rails_connector/basic_obj.rb', line 260

def permalink
  read_attribute('_permalink')
end

#reloadObject

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.



529
530
531
532
# File 'lib/rails_connector/basic_obj.rb', line 529

def reload
  obj_data = CmsBackend.find_obj_data_by(Workspace.current.data, :id, [id.to_s]).first.first
  update_data(obj_data)
end

#root?Boolean

Returns true if this object is the root object.

Returns:

  • (Boolean)


395
396
397
# File 'lib/rails_connector/basic_obj.rb', line 395

def root?
  path == "/"
end

#slugString

This method is used to calculate part a part of a URL of an obj.

The routing schema: <obj.id>/<obj.slug>

The default is parameterize on obj.title.

You can customize this part by overwriting obj.slug in Obj.

Returns:

  • (String)


299
300
301
# File 'lib/rails_connector/basic_obj.rb', line 299

def slug
  (title || '').parameterize
end

#sorted_toclist(*args) ⇒ Array<Obj>

Returns the sorted toclist, respecting sort order and type of this Obj.

Returns:

  • (Array<Obj>)


413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
# File 'lib/rails_connector/basic_obj.rb', line 413

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

#titleObject



311
312
313
# File 'lib/rails_connector/basic_obj.rb', line 311

def title
  read_attribute('title')
end

#toclist(*args) ⇒ Array<Obj>

Returns a list of exportable? children excluding the binary? ones unless :all is specfied. This is mainly used for navigations.

Returns:

  • (Array<Obj>)


403
404
405
406
407
408
# File 'lib/rails_connector/basic_obj.rb', line 403

def toclist(*args)
  return [] unless publication?
  toclist = children.select{ |toc| toc.exportable? }
  toclist = toclist.reject { |toc| toc.binary? } unless args.include?(:all)
  toclist
end

#valid_fromObject



550
551
552
# File 'lib/rails_connector/basic_obj.rb', line 550

def valid_from
  read_attribute('_valid_from')
end

#valid_untilObject



555
556
557
# File 'lib/rails_connector/basic_obj.rb', line 555

def valid_until
  read_attribute('_valid_until')
end