Module: E2Model::InstanceMethods
- Defined in:
- lib/engine2/core.rb
Instance Attribute Summary collapse
-
#skip_save_refresh ⇒ Object
Returns the value of attribute skip_save_refresh.
-
#validate_fields ⇒ Object
Returns the value of attribute validate_fields.
Instance Method Summary collapse
- #_save_refresh ⇒ Object
- #after_destroy ⇒ Object
- #after_save ⇒ Object
- #auto_validate ⇒ Object
- #before_destroy ⇒ Object
- #before_save ⇒ Object
- #has_primary_key? ⇒ Boolean
- #key?(key) ⇒ Boolean
- #primary_key_values ⇒ Object
- #validate ⇒ Object
- #validation ⇒ Object
Instance Attribute Details
#skip_save_refresh ⇒ Object
Returns the value of attribute skip_save_refresh.
214 215 216 |
# File 'lib/engine2/core.rb', line 214 def skip_save_refresh @skip_save_refresh end |
#validate_fields ⇒ Object
Returns the value of attribute validate_fields.
214 215 216 |
# File 'lib/engine2/core.rb', line 214 def validate_fields @validate_fields end |
Instance Method Details
#_save_refresh ⇒ Object
229 230 231 |
# File 'lib/engine2/core.rb', line 229 def _save_refresh super unless skip_save_refresh end |
#after_destroy ⇒ Object
275 276 277 278 279 280 |
# File 'lib/engine2/core.rb', line 275 def after_destroy model.after_destroy_processors.each_pair do |name, proc| proc.(self, name, model.type_info.fetch(name)) end if model.after_destroy_processors super end |
#after_save ⇒ Object
256 257 258 259 260 261 262 263 264 265 266 |
# File 'lib/engine2/core.rb', line 256 def after_save unless model.dummies.empty? @values.merge!(@dummy_fields) @dummy_fields = nil end model.after_save_processors.each_pair do |name, proc| proc.(self, name, model.type_info.fetch(name)) end if model.after_save_processors super end |
#auto_validate ⇒ Object
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 |
# File 'lib/engine2/core.rb', line 288 def auto_validate type_info = model.type_info @validate_fields.each do |name| # || type_info.keys info = type_info[name] next if info[:primary_key] && !model.natural_key value = values[name].to_s value.strip! unless value.frozen? || info[:dont_strip] if value.empty? if req = info[:required] errors.add(name, req[:message]) if !req[:if] || req[:if].(self) end else info[:validations].each_pair do |validation, args| validation_proc = Engine2::Validations[validation] || args[:lambda] # swap ? raise E2Error.new("Validation not found for field '#{name}' of type #{validation}") unless validation_proc if result = validation_proc.(self, name, info) errors.add(name, result) break end end end end # if errors.empty? && model.natural_key && new? # unless model.dataset.where(model.primary_keys_hash(primary_key_values)).empty? # optimize the keys part # model.primary_keys.each{|pk| errors.add(pk, "must be unique")} # end # end end |
#before_destroy ⇒ Object
268 269 270 271 272 273 |
# File 'lib/engine2/core.rb', line 268 def before_destroy model.before_destroy_processors.each_pair do |name, proc| proc.(self, name, model.type_info.fetch(name)) end if model.before_destroy_processors super end |
#before_save ⇒ Object
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
# File 'lib/engine2/core.rb', line 236 def before_save super model.before_save_processors.each_pair do |name, proc| proc.(self, name, model.type_info.fetch(name)) end if model.before_save_processors unless model.dummies.empty? dummies = {} model.dummies.each do |d| dummies[d] = values.delete(d) end @dummy_fields = dummies end unless self.pk sequence = model.type_info[model.primary_key][:sequence] self[model.primary_key] = sequence.lit if sequence end end |
#has_primary_key? ⇒ Boolean
220 221 222 223 |
# File 'lib/engine2/core.rb', line 220 def has_primary_key? pk = self.pk pk.is_a?(Array) ? !pk.all?{|k|k.nil?} : !pk.nil? end |
#key?(key) ⇒ Boolean
216 217 218 |
# File 'lib/engine2/core.rb', line 216 def key? key @values.key? key end |
#primary_key_values ⇒ Object
225 226 227 |
# File 'lib/engine2/core.rb', line 225 def primary_key_values model.primary_keys.map{|k|@values[k]} end |
#validate ⇒ Object
282 283 284 285 286 |
# File 'lib/engine2/core.rb', line 282 def validate super auto_validate validation end |
#validation ⇒ Object
233 234 |
# File 'lib/engine2/core.rb', line 233 def validation end |