lib/supermodel/base.rb in supermodel-0.1.4 vs lib/supermodel/base.rb in supermodel-0.1.5
- old
+ new
@@ -1,13 +1,17 @@
module SuperModel
class Base
- class_inheritable_array :known_attributes
+ class_attribute :known_attributes
self.known_attributes = []
class << self
- attr_accessor_with_default(:primary_key, 'id') #:nodoc:
+ attr_accessor(:primary_key) #:nodoc:
+ def primary_key
+ @primary_key ||= 'id'
+ end
+
def collection(&block)
@collection ||= Class.new(Array)
@collection.class_eval(&block) if block_given?
@collection
end
@@ -118,16 +122,17 @@
attr_accessor :attributes
attr_writer :new_record
def known_attributes
- self.class.known_attributes + self.attributes.keys.map(&:to_s)
+ self.class.known_attributes | self.attributes.keys.map(&:to_s)
end
def initialize(attributes = {})
@new_record = true
@attributes = {}.with_indifferent_access
+ @attributes.merge!(known_attributes.inject({}) {|h, n| h[n] = nil; h })
@changed_attributes = {}
load(attributes)
end
def clone
@@ -183,11 +188,13 @@
end
def exists?
!new?
end
+ alias_method :persisted?, :exists?
def load(attributes) #:nodoc:
+ return unless attributes
attributes.each do |(name, value)|
self.send("#{name}=".to_sym, value)
end
end
\ No newline at end of file