code_junkie

Rails model without database

Nov 12th, 2011
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. class Tableless < ActiveRecord::Base
  2. def self.columns
  3. @columns ||= [];
  4. end
  5.  
  6. def self.column(name, sql_type = nil, default = nil, null = true)
  7. columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default,
  8. sql_type.to_s, null)
  9. end
  10.  
  11. # Override the save method to prevent exceptions.
  12. def save(validate = true)
  13. validate ? valid? : true
  14. end
  15. end
  16.  
  17. class Foo < Tableless
  18. column :bar, :string
  19. validates_presence_of :bar
  20. end
  21.  
  22. Loading development environment (Rails 2.2.2)
  23. >> foo = Foo.new
  24. => #<Foo bar: nil>
  25. >> foo.valid?
  26. => false
  27. >> foo.errors
  28. => #<ActiveRecord::Errors:0x235b270 @errors={"bar"=>["can't be blank"]}, @base=#<Foo bar: nil>>
  29.  
  30. class Car
  31. # here goes all your model's stuff
  32. end
  33.  
  34. class Car
  35. attr_accessor :wheels # this will create for you the reader and writer for this attribute
  36. attr_accessor :doors # ya, this will do the same
  37.  
  38. # here goes all your model's stuff
  39. end
  40.  
  41. class Car < ActiveRecord::Base
  42. self.abstract = true
  43. end
  44.  
  45. my_car = Car.new
  46.  
  47. config.frameworks -= [:active_record]
Add Comment
Please, Sign In to add comment