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



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

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.



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

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.



174
175
176
177
178
# File 'lib/reactor/persistence.rb', line 174

def destroy
  run_callbacks(:destroy) do
    delete
  end
end

#destroyed?Boolean

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

Returns:

  • (Boolean)


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

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



108
109
110
111
112
113
# File 'lib/reactor/persistence.rb', line 108

def edit(comment = nil)
  edit!(comment)
  true
rescue Reactor::Cm::XmlRequestError, Reactor::NotPermitted
  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:



119
120
121
122
123
# File 'lib/reactor/persistence.rb', line 119

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

#edited_or_committed?Boolean

Returns:

  • (Boolean)


245
246
247
# File 'lib/reactor/persistence.rb', line 245

def edited_or_committed?
  really_edited? || committed?
end

#has_super_links?Boolean

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

Returns:

  • (Boolean)

Raises:



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

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

#initialize(attributes = nil, _options = {}) ⇒ Object

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

See Also:

  • ActiveRecord::Base.new


226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/reactor/persistence.rb', line 226

def initialize(attributes = nil, _options = {})
  if true || !self.class.send(:attribute_methods_overriden?)
    ignored_attributes = ignore_attributes(attributes)
    # supress block hijacking!
    super(attributes) {}
    load_ignored_attributes(ignored_attributes)
    yield self if block_given?
  else
    # TODO
    # here we get 'ActiveRecord::AssociationTypeMismatch'
    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)


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

def new_record?
  # !destroyed? && (self.id.nil? || !self.class.exists?(self.id))
  !destroyed? && (id.nil? || 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)


149
150
151
# File 'lib/reactor/persistence.rb', line 149

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

#readonly?Boolean

Returns:

  • (Boolean)


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

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

#really_edited?Boolean

Returns:

  • (Boolean)


240
241
242
243
# File 'lib/reactor/persistence.rb', line 240

def really_edited?
  # check if really edited with curl request
  crul_obj.edited?
end

#reasons_for_incomplete_stateObject

Returns an array of errors



250
251
252
# File 'lib/reactor/persistence.rb', line 250

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



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

def release(comment = nil)
  release!(comment)
rescue Reactor::Cm::XmlRequestError, ActiveRecord::RecordInvalid, Reactor::NotPermitted, Reactor::AlreadyReleased
  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:



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

def release!(comment = nil)
  run_callbacks(:release) do
    raise(Reactor::AlreadyReleased) unless edited_or_committed?

    crul_obj.release!(comment)
    reload
  end
  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.



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/reactor/persistence.rb', line 183

def reload(options = nil)
  RailsConnector::AbstractObj.uncached do
    # super # Throws RecordNotFound when changing obj_class
    # AR reload
    send(:clear_aggregation_cache) if respond_to?(:clear_aggregation_cache, true)
    send(:clear_association_cache) if respond_to?(:clear_association_cache, true)
    fresh_object = RailsConnector::AbstractObj.find(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
     = nil
    # meta reload
    @editor = nil
     = 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



209
210
211
212
213
214
# File 'lib/reactor/persistence.rb', line 209

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

#resolve_refs!Object

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

Raises:



219
220
221
222
# File 'lib/reactor/persistence.rb', line 219

def resolve_refs!
  crul_obj.resolve_refs!
  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)


44
45
46
# File 'lib/reactor/persistence.rb', line 44

def revert(comment = nil)
  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)


53
54
55
56
57
# File 'lib/reactor/persistence.rb', line 53

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

#super_objectsObject

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

Raises:



134
135
136
# File 'lib/reactor/persistence.rb', line 134

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



81
82
83
84
85
86
# File 'lib/reactor/persistence.rb', line 81

def take(comment = nil)
  take!(comment)
  true
rescue Reactor::Cm::XmlRequestError, Reactor::NotPermitted, Reactor::NoWorkingVersion
  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:



93
94
95
96
97
98
99
100
101
# File 'lib/reactor/persistence.rb', line 93

def take!(comment = nil)
  raise(Reactor::NoWorkingVersion) unless 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
  true
end