code_junkie

Rails ActiveRecord: Automatically Alias/Append Suffix

Nov 14th, 2011
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. attributes.each_key do | key |
  2. name = key
  3. alias_attribute key.to_sym, key[0, (key.length -2)].to_sym if key =~ /_c$/
  4. end
  5.  
  6. attributes.each_key do | key |
  7. name = key
  8. alias_attribute key.to_sym, key[0, (key.length -2)].to_sym if key =~ /_c$/
  9. end
  10.  
  11. attributes
  12.  
  13. methods.each do |method|
  14. if method.ends_with("_c") then
  15. self.send(:defind_method,method.slice(0,-2)){self.send(method)}
  16. end
  17. end
  18.  
  19. module ShittyDatabaseMethods
  20. alias :old_method_missing :method_missing
  21.  
  22. def method_missing(method)
  23. if methods.include?("#{method}_c")
  24. send("#{method}_c".to_sym)
  25. else
  26. old_method_missing(method)
  27. end
  28. end
  29.  
  30. end
  31.  
  32. class Test
  33. attr_accessor :test_c
  34. include ShittyDatabaseMethods
  35. end
Add Comment
Please, Sign In to add comment