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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

Forwards any unknown method call to a corresponding instance of AttrDict and thus provides access to object fields, i.e. content.

In case of an invalid method call, an error is raised.

Hint: Use [] instead to suppress error messages.


516
517
518
519
520
521
522
523
524
525
526
527
528
# File 'lib/rails_connector/basic_obj.rb', line 516

def method_missing(method_id, *args)
  super
rescue NoMethodError, NameError
  # prevent infinite recursion when calling "attr_*" below,
  # since rails checks the absence of an "_attr_*" method internally
  raise if %w(_attr_dict _attr_defs _attr_values).include?(method_id.to_s)

  if attr_dict.respond_to?(method_id)
    attr_dict.send method_id, *args
  else
    raise
  end
end

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.


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

def self.homepage
  root
end

.rootObject

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


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

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.


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

def [](key)
  # convenience access to name
  return name if key.to_sym == :name

  # regular activerecord attributes
  if @attributes.key?(key.to_s)
    if key == :valid_from or key == :valid_until or key == :last_changed
      return as_date(super(key))
    else
      return super(key)
    end
  end

  # Unknown Obj attributes are delegated to the corresponding instance of AttrDict.
  begin
    return (attr_dict.send key)
  rescue NoMethodError
  end

  # fallback
  return nil
end

#active?Boolean

Returns true if this object is active.

Returns:

  • (Boolean)

201
202
203
204
# File 'lib/rails_connector/basic_obj.rb', line 201

def active?
  return false if !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.


317
318
319
320
321
322
323
# File 'lib/rails_connector/basic_obj.rb', line 317

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

#binary?Boolean

Returns true if image? or generic?

Returns:

  • (Boolean)

171
172
173
# File 'lib/rails_connector/basic_obj.rb', line 171

def binary?
  [: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.


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

def body_data_url
  nil
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)


387
388
389
# File 'lib/rails_connector/basic_obj.rb', line 387

def body_length
  attr_dict.body_length
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.


123
124
125
# File 'lib/rails_connector/basic_obj.rb', line 123

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.


115
116
117
# File 'lib/rails_connector/basic_obj.rb', line 115

def controller_name
  obj_class
end

#display_titleObject

Returns the title of the content or the name.


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

def display_title
  self.title || name
end

#document?Boolean

Returns true if object_type == :document

Returns:

  • (Boolean)

195
196
197
# File 'lib/rails_connector/basic_obj.rb', line 195

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.

Returns:

  • (Boolean)

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

def edited?
  (is_edited == 1)
end

#exportable?Boolean

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

Returns:

  • (Boolean)

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

def exportable?
  !suppressed? && active?
end

#filenameObject

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


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

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.


328
329
330
331
332
# File 'lib/rails_connector/basic_obj.rb', line 328

def find_nearest(name)
  obj = self.children.find_by_name(name)
  return obj if obj and obj.active?
  parent.find_nearest(name) unless self.root?
end

#generic?Boolean

Returns true if object_type == :generic

Returns:

  • (Boolean)

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

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.

Returns:

  • (Boolean)

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

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

#image?Boolean

Returns true if object_type == :image

Returns:

  • (Boolean)

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

def image?
  object_type == :image
end

#last_changedObject


466
467
468
# File 'lib/rails_connector/basic_obj.rb', line 466

def last_changed
  self[:last_changed]
end

#mime_typeObject

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


488
489
490
# File 'lib/rails_connector/basic_obj.rb', line 488

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(…)


380
381
382
# File 'lib/rails_connector/basic_obj.rb', line 380

def object_class
  obj_class
end

#object_typeObject

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


165
166
167
# File 'lib/rails_connector/basic_obj.rb', line 165

def object_type
  OBJECT_TYPES[obj_type_code]
end

returns the obj's permalink.


106
107
108
# File 'lib/rails_connector/basic_obj.rb', line 106

def permalink
  self[:permalink]
end

#permissionsObject


69
70
71
# File 'lib/rails_connector/basic_obj.rb', line 69

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

#permitted_for_user?(user) ⇒ Boolean

Returns:

  • (Boolean)

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

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”.


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

def permitted_groups
  attr_dict.permitted_groups
end

#publication?Boolean

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

Returns:

  • (Boolean)

189
190
191
# File 'lib/rails_connector/basic_obj.rb', line 189

def publication?
  object_type == :publication
end

#released?Boolean

Returns true if this object has released content

Returns:

  • (Boolean)

216
217
218
# File 'lib/rails_connector/basic_obj.rb', line 216

def released?
  (is_released == 1)
end

#reloadObject

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


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

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.

Returns:

  • (Boolean)

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

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.

Returns:

  • (String)

146
147
148
# File 'lib/rails_connector/basic_obj.rb', line 146

def slug
  name
end

#sorted_toclist(*args) ⇒ Object

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


272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/rails_connector/basic_obj.rb', line 272

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

#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.

Returns:

  • (Boolean)

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

def suppressed?
  suppress_export == 1
end

#toclist(*args) ⇒ Object

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


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

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

#valid_fromObject


471
472
473
# File 'lib/rails_connector/basic_obj.rb', line 471

def valid_from
  self[:valid_from]
end

#valid_untilObject


476
477
478
# File 'lib/rails_connector/basic_obj.rb', line 476

def valid_until
  self[:valid_until]
end