Module: Reactor::Persistence::Base

Defined in:
lib/reactor/persistence.rb

Overview

Provides API for writing into the Content Manager. It aims to be just like ActiveRecord::Persistence, so that the difference for the developer is minimal If the method is marked as exception raising, then it should be expected also to raise Reactor::Cm::XmlRequestError when generic write/connection error occurs.

It should support all generic model callbacks, plus complete set of callbacks for release action (before/after/around).

Raises:

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/reactor/persistence.rb', line 19

def self.included(base)
  base.extend(ClassMethods)
  base.send(:define_model_callbacks, :release)
  base.class_eval do
    before_create :sanitize_name
    before_create :trim_crul_attributes
  end
end

Instance Method Details

#deleteObject

Deletes the object from the CM. No callbacks are executed. Though exceptions can be raised. Freezes the object.



185
186
187
188
189
# File 'lib/reactor/persistence.rb', line 185

def delete
  crul_obj_delete if persisted?
  @destroyed = true
  freeze
end

#destroyObject

Removes the object from the CM. Runs all the callbacks. Can raise exception. Freezes the object.



193
194
195
196
197
# File 'lib/reactor/persistence.rb', line 193

def destroy
  run_callbacks(:destroy) do
    self.delete
  end
end

#destroyed?Boolean

Returns true if this object has been destroyed, otherwise returns false.

Returns:

  • (Boolean)


173
174
175
# File 'lib/reactor/persistence.rb', line 173

def destroyed?
  @destroyed == true
end

#edit(comment = nil) ⇒ Object

Creates a working version of the object. Returns true on success or when the object already has a working version. Returns false when:

  1. user lacks the permissions

  2. other error occured

Parameters:

  • comment (String) (defaults to: nil)

    comment to leave for the next user



127
128
129
130
131
132
# File 'lib/reactor/persistence.rb', line 127

def edit(comment=nil)
  edit!(comment)
  return true
rescue Reactor::Cm::XmlRequestError, Reactor::NotPermitted
  return false
end

#edit!(comment = nil) ⇒ Object

Creates a working version of the object. Returns true on success or when the object already has a working version. Raises exceptions

Parameters:

  • comment (String) (defaults to: nil)

    comment to leave for the next user

Raises:



138
139
140
141
142
# File 'lib/reactor/persistence.rb', line 138

def edit!(comment=nil)
  crul_obj.edit!(comment) unless self.really_edited?
  reload
  return true
end

#has_super_links?Boolean

Returns true, if the object has any links pointing to it.

Returns:

  • (Boolean)

Raises:



146
147
148
# File 'lib/reactor/persistence.rb', line 146

def has_super_links?
  crul_obj.get('hasSuperLinks') == '1'
end

#initialize(attributes = nil, options = {}, &block) ⇒ Object

It should excactly match ActiveRecord::Base.new in it’s behavior

See Also:

  • ActiveRecord::Base.new


246
247
248
249
250
251
252
253
254
255
256
# File 'lib/reactor/persistence.rb', line 246

def initialize(attributes = nil, &block)
  if true ||  !self.class.send(:attribute_methods_overriden?) # FIXME !!!!
    ignored_attributes = ignore_attributes(attributes)
    # supress block hijacking!
    super(attributes) {}
    load_ignored_attributes(ignored_attributes)
    yield self if block_given?
  else
    super(attributes)
  end
end

#new_record?Boolean

Returns true if this object hasn’t been saved yet – that is, a record for the object doesn’t exist in the data store yet; otherwise, returns false.

Returns:

  • (Boolean)


159
160
161
162
# File 'lib/reactor/persistence.rb', line 159

def new_record?
  #!destroyed? && (self.id.nil? || !self.class.exists?(self.id))
  !destroyed? && (self.id.nil? || self.path.blank?)
end

#persisted?Boolean

Note:

Code should not be changed without large modifications to the module.

Stolen from Rails 3. Returns if the record is persisted, i.e. stored in database (it’s not a new record and it was not destroyed.)

Returns:

  • (Boolean)


168
169
170
# File 'lib/reactor/persistence.rb', line 168

def persisted?
  !(new_record? || destroyed?)
end

#readonly?Boolean

Returns:

  • (Boolean)


178
179
180
181
# File 'lib/reactor/persistence.rb', line 178

def readonly? #:nodoc:
  # No, RailsConnector. I will not be shut-up!
  false
end

#really_edited?Boolean

Equivalent to Obj#edited?

Returns:

  • (Boolean)


276
277
278
# File 'lib/reactor/persistence.rb', line 276

def really_edited?
  self.edited?
end

#reasons_for_incomplete_stateObject

Returns an array of errors



281
282
283
# File 'lib/reactor/persistence.rb', line 281

def reasons_for_incomplete_state
  crul_obj.get('reasonsForIncompleteState') || []
end

#release(comment = nil) ⇒ Object

Releases the object. Returns true on success, false when one of the following occurs:

  1. user lacks the permissions

  2. the object has already been released

  3. object is invalid

  4. other error occoured

Parameters:

  • comment (String) (defaults to: nil)

    comment to leave for the next user



35
36
37
38
39
# File 'lib/reactor/persistence.rb', line 35

def release(comment=nil)
  return release!(comment)
rescue Reactor::Cm::XmlRequestError, ActiveRecord::RecordInvalid, Reactor::NotPermitted, Reactor::AlreadyReleased
  return false
end

#release!(comment = nil) ⇒ Object

Releases the object. Returns true on succes, can raise exceptions

Parameters:

  • comment (String) (defaults to: nil)

    comment to leave for the next user

Raises:



77
78
79
80
81
82
83
84
# File 'lib/reactor/persistence.rb', line 77

def release!(comment=nil)
  run_callbacks(:release) do
    raise(Reactor::AlreadyReleased) unless self.really_edited?
    crul_obj.release!(comment)
    reload
  end
  return true
end

#reload(options = nil) ⇒ Object

Reloads object attributes. Invalidates caches. Does not call any other reload methods (neither from RailsConnector nor from ActiveRecord) but tries to mimmic their behaviour.



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/reactor/persistence.rb', line 202

def reload(options = nil)
  RailsConnector::AbstractObj.uncached do
    #super # Throws RecordNotFound when changing obj_class
    # AR reload
    clear_aggregation_cache
    clear_association_cache
    fresh_object = RailsConnector::AbstractObj.find(self.id, options)
    @attributes = fresh_object.instance_variable_get('@attributes')
    @attributes_cache = {}
    # RC reload
    @attr_values = nil
    @attr_defs = nil
    @attr_dict = nil
    @obj_class_definition = nil
    @object_with_meta_data = nil
    # meta reload
    @editor = nil
    @object_with_meta_data = nil
    self
  end
end

#resolve_refsObject

Resolves references in any of the html fields. Returns true on success, or false when:

  1. user lacks the permissions

  2. generic error occoured



228
229
230
231
232
233
# File 'lib/reactor/persistence.rb', line 228

def resolve_refs
  resolve_refs!
  return true
rescue Reactor::Cm::XmlRequestError, Reactor::NotPermitted
  return false
end

#resolve_refs!Object

Resolves references in any of the html fields. Returns true on success, raises exceptions.

Raises:



238
239
240
241
# File 'lib/reactor/persistence.rb', line 238

def resolve_refs!
  crul_obj.resolve_refs!
  return true
end

#revert(comment = nil) ⇒ true

Removes the working version of the object, if it exists

Parameters:

  • comment (String) (defaults to: nil)

    comment to leave for the next user

Returns:

  • (true)


57
58
59
# File 'lib/reactor/persistence.rb', line 57

def revert(comment=nil)
  return revert!(comment)
end

#revert!(comment = nil) ⇒ true

Note:

There is no difference between #revert and #revert!

Removes the working version of the object, if it exists

Parameters:

  • comment (String) (defaults to: nil)

    comment to leave for the next user

Returns:

  • (true)


66
67
68
69
70
# File 'lib/reactor/persistence.rb', line 66

def revert!(comment=nil)
  crul_obj.revert!(comment)
  reload
  return true
end

#super_objectsObject

Return an array of RailsConnector::AbstractObj that contain a link to this file.

Raises:



153
154
155
# File 'lib/reactor/persistence.rb', line 153

def super_objects
  RailsConnector::AbstractObj.where(:obj_id => crul_obj.get('superObjects')).to_a
end

#take(comment = nil) ⇒ Object

Makes the current user the editor of the object. Returns true when user is already the editor or take succeded, false when one of the following occurs:

  1. user lacks the permissions

  2. the object has not beed edited

  3. other error occured

Parameters:

  • comment (String) (defaults to: nil)

    comment to leave for the next user



101
102
103
104
105
106
# File 'lib/reactor/persistence.rb', line 101

def take(comment=nil)
  take!(comment)
  return true
rescue Reactor::Cm::XmlRequestError, Reactor::NotPermitted, Reactor::NoWorkingVersion
  return false
end

#take!(comment = nil) ⇒ Object

Makes the current user the editor of the object. Returns true when user is already the editor or take succeded. Raises exceptions

Parameters:

  • comment (String) (defaults to: nil)

    comment to leave for the next user

Raises:



113
114
115
116
117
118
119
120
# File 'lib/reactor/persistence.rb', line 113

def take!(comment=nil)
  raise(Reactor::NoWorkingVersion) unless self.really_edited?
  # TODO: refactor the if condition
  crul_obj.take!(comment) if (crul_obj.editor != Reactor::Configuration::xml_access[:username])
  # neccessary to recalculate #editor
  reload
  return true
end

#unrelease(comment = nil) ⇒ Object

Unreleases the object. Returns true on success, false when one of the following occurs:

  1. user lacks the permissions

  2. the object is not released

  3. object is invalid

  4. other error occoured



47
48
49
50
51
# File 'lib/reactor/persistence.rb', line 47

def unrelease(comment=nil)
  return unrelease!(comment)
rescue Reactor::Cm::XmlRequestError, ActiveRecord::RecordInvalid, Reactor::NotPermitted
  return false
end

#unrelease!(comment = nil) ⇒ Object

Unreleases the object. Returns true on succes, can raise exceptions

Parameters:

  • comment (String) (defaults to: nil)

    comment to leave for the next user



88
89
90
91
92
# File 'lib/reactor/persistence.rb', line 88

def unrelease!(comment=nil)
  crul_obj.unrelease!(comment)
  reload
  return true
end