Class: ActiveRecord::Base
- Inherits:
-
Object
- Object
- ActiveRecord::Base
- Defined in:
- lib/natural_key/base.rb
Class Method Summary collapse
-
.create_or_update_by_natural_key(options) ⇒ Object
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.
- .natural_key(*key_attributes) ⇒ Object
Class Method Details
.create_or_update_by_natural_key(options) ⇒ Object
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?)
18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/natural_key/base.rb', line 18 def create_or_update_by_natural_key() .symbolize_keys! #TODO options should contain all key_attributes, and their values should be non-nil = .reject { |k,v| !key_attributes.include?(k) } raise "key_options cannot be empty" if .empty? record = find(:first, :conditions => ) if(record.nil?) record = create() else record.update_attributes!() end record end |
.natural_key(*key_attributes) ⇒ Object
7 8 9 10 11 12 13 |
# File 'lib/natural_key/base.rb', line 7 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 |