Class: RailsConnector::BasicObj

Inherits:
CmsBaseModel
  • Object
show all
Defined in:
lib/rails_connector/basic_obj.rb

Overview

The CMS file class

children

an Array of objects, Obj, of which this one is the parent

parent

the Obj of which this one is a child - nil for the root object

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.homepageObject

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



160
161
162
# File 'lib/rails_connector/basic_obj.rb', line 160

def self.homepage
  root
end

.rootObject

Returns the root Obj. Its id is 2001 and cannot be changed.



153
154
155
# File 'lib/rails_connector/basic_obj.rb', line 153

def self.root
  Obj.find(2001)
end

Instance Method Details

#[](key) ⇒ Object

Returns the value of the attribute specified by its name.

Passing an invalid key will not raise an error, but return nil.



387
388
389
390
391
392
393
394
395
396
397
# File 'lib/rails_connector/basic_obj.rb', line 387

def [](key)
  if has_attribute?(key)
    begin
      return attr_dict.send(key) if attr_dict.respond_to?(key)

      read_attribute(key)
    rescue NoMethodError
      nil
    end
  end
end

#active?Boolean

Returns true if this object is active.



262
263
264
265
266
# File 'lib/rails_connector/basic_obj.rb', line 262

def active?
  return false unless valid_from

  valid_from <= Time.now && (!valid_until || Time.now <= valid_until)
end

#ancestorsObject

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



365
366
367
368
369
370
371
# File 'lib/rails_connector/basic_obj.rb', line 365

def ancestors
  if parent
    parent.ancestors + [parent]
  else
    []
  end
end

#binary?Boolean

Returns true if image? or generic?



232
233
234
# File 'lib/rails_connector/basic_obj.rb', line 232

def binary?
  i(image generic).include? object_type
end

#body_data_urlObject

Override this method to provide an external url where the content (the body) of this Obj can be downloaded. The Rails Connector will then use this url when creating links to this Obj. This is useful when delivering images via a content delivery network (CDN), for example. Returns nil by default.

Note: When this method returns an url, the Rails Connector’s DefaultCmsController will redirect to this url instead of delivering this Obj’s body. And the Rails Connector’s cms_path and cms_url helpers will link to this url instead of linking to the Obj.

Note also that the url will be used regardless of the Obj’s permissions, so be careful not to provide urls that contain unprotected secrets.



441
442
443
# File 'lib/rails_connector/basic_obj.rb', line 441

def body_data_url
  nil
end

#controller_action_nameObject

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.



182
183
184
# File 'lib/rails_connector/basic_obj.rb', line 182

def controller_action_name
  "index"
end

#controller_nameObject

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.



174
175
176
# File 'lib/rails_connector/basic_obj.rb', line 174

def controller_name
  obj_class
end

#display_titleObject

Returns the title of the content or the name.



211
212
213
# File 'lib/rails_connector/basic_obj.rb', line 211

def display_title
  title || name
end

#document?Boolean

Returns true if object_type == :document



256
257
258
# File 'lib/rails_connector/basic_obj.rb', line 256

def document?
  object_type == :document
end

#edited?Boolean

Returns true if this object has edited content. Note that edited content is not available when Configuration.mode == :live and edited? will always return false in this case.



272
273
274
# File 'lib/rails_connector/basic_obj.rb', line 272

def edited?
  is_edited
end

#exportable?Boolean

Returns true if the export of the object is not suppressed and the content is active?



293
294
295
# File 'lib/rails_connector/basic_obj.rb', line 293

def exportable?
  !suppressed? && active?
end

#filenameObject

Returns the file name to which the Content.file_extension has been appended.



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

def filename
  extension = ".#{file_extension}" unless file_extension.blank?
  "#{name}#{extension}"
rescue NoMethodError
  name
end

#find_nearest(name) ⇒ Object

Returns the Object with the given name next in the hierarchy returns nil if no object with the given name was found.



376
377
378
379
380
381
# File 'lib/rails_connector/basic_obj.rb', line 376

def find_nearest(name)
  obj = children.find_by_name(name)
  return obj if obj&.active?

  parent.find_nearest(name) unless root?
end

#generic?Boolean

Returns true if object_type == :generic



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

def generic?
  object_type == :generic
end

#homepage?Boolean

Returns true if the current object has the same id as the homepage object. Always use this method instead of manually comparing an object to Obj.homepage, as Obj#homepage? caches the object id and thus requires no extra database access.



192
193
194
# File 'lib/rails_connector/basic_obj.rb', line 192

def homepage?
  id == (@@homepage_id ||= self.class.homepage.id)
end

#image?Boolean

Returns true if object_type == :image



238
239
240
# File 'lib/rails_connector/basic_obj.rb', line 238

def image?
  object_type == :image
end

#mime_typeObject

Returns the MIME-type as determined from the file_extension - see MIME::Types



505
506
507
# File 'lib/rails_connector/basic_obj.rb', line 505

def mime_type
  @mime_type ||= compute_mime_type
end

#object_classObject

object_class is a legacy alias to name. Please use obj_class instead, because only obj_class works with ActiveRecord’s dynamic finder methods like find_by_obj_class(…)



418
419
420
# File 'lib/rails_connector/basic_obj.rb', line 418

def object_class
  obj_class
end

#object_typeObject

Returns the type of the object: :document, :publication, :image or :generic



226
227
228
# File 'lib/rails_connector/basic_obj.rb', line 226

def object_type
  OBJECT_TYPES[obj_type_code]
end

#permissionsObject



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

def permissions
  @@use_cached_permissions ? attr_dict.permissions : arel_permissions
end

#permitted_for_user?(user) ⇒ Boolean



139
140
141
142
143
144
145
146
147
148
149
# File 'lib/rails_connector/basic_obj.rb', line 139

def permitted_for_user?(user)
  if permitted_groups.blank?
    true
  else
    if user
      (permitted_groups & user.live_server_groups).any?
    else
      false
    end
  end
end

#permitted_groupsObject

Returns an array with the names of groups that are permitted to access this Obj. This corresponds to the cms permission “permissionLiveServerRead”.



309
310
311
# File 'lib/rails_connector/basic_obj.rb', line 309

def permitted_groups
  attr_dict.permitted_groups
end

#publication?Boolean

Returns true if object_type == :publication (for folders)



250
251
252
# File 'lib/rails_connector/basic_obj.rb', line 250

def publication?
  object_type == :publication
end

#released?Boolean

Returns true if this object has released content



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

def released?
  is_released
end

#reloadObject

Reloads the attributes of this object from the database, invalidates custom attributes



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

def reload
  super
  @attr_dict = nil
  @attr_values = nil
  @attr_defs = nil
  self
end

#root?Boolean

Returns true if this object is the root object.



315
316
317
# File 'lib/rails_connector/basic_obj.rb', line 315

def root?
  parent_obj_id.nil?
end

#slugString

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

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

The default is obj.name.

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



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

def slug
  name
end

#sorted_toclist(*args) ⇒ Object

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



332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
# File 'lib/rails_connector/basic_obj.rb', line 332

def sorted_toclist(*args)
  list = toclist(*args)
  return [] if list.blank?

  cached_sort_key1 = sort_key1
  cached_sort_type1 = sort_type1

  sorted_list =
    if cached_sort_key1.blank?
      list.sort_by(&:name)
    else
      cached_sort_key2 = sort_key2
      cached_sort_type2 = sort_type2
      cached_sort_key3 = sort_key3
      cached_sort_type3 = 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

  sort_order == "descending" ? sorted_list.reverse : sorted_list
end

#suppressed?Boolean

Returns true if the Obj is suppressed. A suppressed Obj does not represent an entire web page, but only a part of a page (for example a teaser) and will not be delivered by the rails application as a standalone web page.



287
288
289
# File 'lib/rails_connector/basic_obj.rb', line 287

def suppressed?
  suppress_export
end

#toclist(*args) ⇒ Object

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



322
323
324
325
326
327
328
# File 'lib/rails_connector/basic_obj.rb', line 322

def toclist(*args)
  return [] unless publication?

  toclist = children.to_a.select { |toc| toc.exportable? }
  toclist = toclist.reject { |toc| toc.binary? } unless args.include?(:all)
  toclist
end