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.
- .create_or_update_by_natural_key!(options) ⇒ Object
- .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 |
# File 'lib/natural_key/base.rb', line 18 def create_or_update_by_natural_key() record = locate_record() if(record.nil?) record = create() else record.update_attributes() end record end |
.create_or_update_by_natural_key!(options) ⇒ Object
28 29 30 31 32 33 34 35 36 |
# File 'lib/natural_key/base.rb', line 28 def create_or_update_by_natural_key!() record = locate_record() 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 |