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).
Class Method Summary collapse
Instance Method Summary collapse
-
#delete ⇒ Object
Deletes the object from the CM.
-
#destroy ⇒ Object
Removes the object from the CM.
-
#destroyed? ⇒ Boolean
Returns true if this object has been destroyed, otherwise returns false.
-
#edit(comment = nil) ⇒ Object
Creates a working version of the object.
-
#edit!(comment = nil) ⇒ Object
Creates a working version of the object.
- #edited_or_committed? ⇒ Boolean
-
#has_super_links? ⇒ Boolean
Returns true, if the object has any links pointing to it.
-
#initialize(attributes = nil, _options = {}) ⇒ Object
It should excactly match ActiveRecord::Base.new in it’s behavior.
-
#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.
-
#persisted? ⇒ Boolean
Stolen from Rails 3.
- #readonly? ⇒ Boolean
- #really_edited? ⇒ Boolean
-
#reasons_for_incomplete_state ⇒ Object
Returns an array of errors.
-
#release(comment = nil) ⇒ Object
Releases the object.
-
#release!(comment = nil) ⇒ Object
Releases the object.
-
#reload(options = nil) ⇒ Object
Reloads object attributes.
-
#resolve_refs ⇒ Object
Resolves references in any of the html fields.
-
#resolve_refs! ⇒ Object
Resolves references in any of the html fields.
-
#revert(comment = nil) ⇒ true
Removes the working version of the object, if it exists.
-
#revert!(comment = nil) ⇒ true
Removes the working version of the object, if it exists.
-
#super_objects ⇒ Object
Return an array of RailsConnector::AbstractObj that contain a link to this file.
-
#take(comment = nil) ⇒ Object
Makes the current user the editor of the object.
-
#take!(comment = nil) ⇒ Object
Makes the current user the editor of the object.
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
#delete ⇒ Object
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 |
#destroy ⇒ Object
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.
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:
-
user lacks the permissions
-
other error occured
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
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
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.
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
226 227 228 229 230 231 232 233 234 235 236 237 238 |
# File 'lib/reactor/persistence.rb', line 226 def initialize(attributes = nil, = {}) 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.
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
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.)
149 150 151 |
# File 'lib/reactor/persistence.rb', line 149 def persisted? !(new_record? || destroyed?) end |
#readonly? ⇒ 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
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_state ⇒ Object
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:
-
user lacks the permissions
-
the object has already been released
-
object is invalid
-
other error occoured
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
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( = 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, ) @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_refs ⇒ Object
Resolves references in any of the html fields. Returns true on success, or false when:
-
user lacks the permissions
-
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.
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
44 45 46 |
# File 'lib/reactor/persistence.rb', line 44 def revert(comment = nil) revert!(comment) end |
#revert!(comment = nil) ⇒ true
There is no difference between #revert and #revert!
Removes the working version of the object, if it exists
53 54 55 56 57 |
# File 'lib/reactor/persistence.rb', line 53 def revert!(comment = nil) crul_obj.revert!(comment) reload true end |
#super_objects ⇒ Object
Return an array of RailsConnector::AbstractObj that contain a link to this file.
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:
-
user lacks the permissions
-
the object has not beed edited
-
other error occured
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
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 |