Class: ActiveRecord::Base
- Inherits:
-
Object
- Object
- ActiveRecord::Base
- Defined in:
- lib/initializers/morse.rb
Instance Method Summary collapse
- #associated_object_exists(klass, fn, required = true) ⇒ Object
- #blank_to_nil(thing) ⇒ Object
- #process_attachment(options = {}) ⇒ Object
- #survive_if(thing) ⇒ Object
- #there_can_be_only_one(thing) ⇒ Object
- #there_must_be_one(thing) ⇒ Object
- #validate_integer_or_default(thing, default) ⇒ Object
- #validate_mandatory_boolean(thing, message = "must be true") ⇒ Object
Instance Method Details
#associated_object_exists(klass, fn, required = true) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 |
# File 'lib/initializers/morse.rb', line 3 def associated_object_exists(klass,fn,required=true) id=self.send(fn) id_setter=(fn.to_s+'=').to_sym unless klass.find_by_id(id) self.send(id_setter,nil) id=nil end if required==true and id.nil? errors.add(fn,"does not exist") return false end end |
#blank_to_nil(thing) ⇒ Object
16 17 18 19 20 |
# File 'lib/initializers/morse.rb', line 16 def blank_to_nil(thing) if self.send(thing).blank? self.send(thing.to_s+'=',nil) end end |
#process_attachment(options = {}) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/initializers/morse.rb', line 22 def (={}) ={:model_name=>:asset, :uploader_name=>:attachment}.merge() model_name=[:model_name] uploader_name=[:uploader_name] klass=eval(model_name.to_s.camelize) if self.send(uploader_name) a=klass.new(uploader_name=>self.send(uploader_name)) if a.save self.send(model_name,a) self.send(uploader_name,nil) save else errors.add(uploader_name,'had a problem saving') end end if klass.find_by_id(self.id) self.reload end end |
#survive_if(thing) ⇒ Object
42 43 44 45 46 47 48 49 50 |
# File 'lib/initializers/morse.rb', line 42 def survive_if(thing) if self.send(thing)==true errors.add(:base,"Cannot destroy while #{thing} is true") false elsif self.send(thing).is_a?(Array) and self.send(thing).any? errors.add(:base,"Cannot destroy while #{thing} has members") false end end |
#there_can_be_only_one(thing) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/initializers/morse.rb', line 52 def there_can_be_only_one(thing) if new_record? enemies=self.class.where("#{thing} = ?",true) else enemies=self.class.where("#{thing} = ? and id != ? ",true,id) end if enemies.any? enemies.each do |e| e.default_land=false e.save end end end |
#there_must_be_one(thing) ⇒ Object
66 67 68 69 70 71 |
# File 'lib/initializers/morse.rb', line 66 def there_must_be_one(thing) existing=self.class.where("#{thing} = ?",true) if existing.empty? self.send("#{thing}=",true) end end |
#validate_integer_or_default(thing, default) ⇒ Object
73 74 75 76 77 |
# File 'lib/initializers/morse.rb', line 73 def validate_integer_or_default(thing,default) unless self.send(thing) self.send("#{thing}=",default) end end |
#validate_mandatory_boolean(thing, message = "must be true") ⇒ Object
79 80 81 82 83 |
# File 'lib/initializers/morse.rb', line 79 def validate_mandatory_boolean(thing,="must be true") unless self.send(thing)==true errors.add(thing,) end end |