Sha256: b1ea31c8be2dacd000e1697bfedf0bce44ad4ddc301f7477da3854ad092c853b
Contents?: true
Size: 1.35 KB
Versions: 2
Compression:
Stored size: 1.35 KB
Contents
require 'activerecord' module ActiveRecord class Base class_inheritable_accessor :key_attributes class << self def natural_key(*key_attributes) # TODO validate the supplied key_attributes are subset of all attributes # TODO key_attributes should default to primary_key/surrogate_key (:id) # TODO if natural_key is composed of non-primary-key, then it should not contain primary_key raise "natural key attributes cannot be empty" if key_attributes.empty? self.key_attributes = key_attributes end # can't use method "create_or_update" since there is already a private method with that name # in ActiveRecord::Base # TODO The following method should only be made available when natural_key is called with proper # attributes. (hint: use class_eval?) def create_or_update_by_natural_key(options) options.symbolize_keys! #TODO options should contain all key_attributes, and their values should be non-nil key_options = options.reject { |k,v| !key_attributes.include?(k) } raise "key_options cannot be empty" if key_options.empty? record = find(:first, :conditions => key_options) if(record.nil?) record = create(options) else record.update_attributes!(options) end record end end end end
Version data entries
2 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
natural_key-0.2.0 | lib/natural_key/base.rb |
natural_key-0.2.0 | pkg/natural_key-0.1.0/lib/natural_key/base.rb |