Module: CapsuleCRM::Persistence::Persistable
- Extended by:
- ActiveSupport::Concern
- Included in:
- Case, History, Opportunity, Organization, CapsuleCRM::Person, Task
- Defined in:
- lib/capsule_crm/persistence/persistable.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
- #build_create_path ⇒ Object
- #build_update_path ⇒ Object
- #create_record ⇒ Object
- #new_record? ⇒ Boolean
- #persisted? ⇒ Boolean
- #save ⇒ Object
- #save! ⇒ Object
- #update_attributes(attributes = {}) ⇒ Object
- #update_attributes!(attributes = {}) ⇒ Object
- #update_record ⇒ Object
Class Method Details
.included(base) ⇒ Object
6 7 8 9 10 11 |
# File 'lib/capsule_crm/persistence/persistable.rb', line 6 def self.included(base) base.send :include, CapsuleCRM::Persistence::Configuration base.extend CapsuleCRM::Persistence::Persistable::ClassMethods base.extend ActiveModel::Callbacks base.send :define_model_callbacks, :save end |
Instance Method Details
#build_create_path ⇒ Object
60 61 62 |
# File 'lib/capsule_crm/persistence/persistable.rb', line 60 def build_create_path "/api/#{self.class..create.call(self)}" end |
#build_update_path ⇒ Object
64 65 66 |
# File 'lib/capsule_crm/persistence/persistable.rb', line 64 def build_update_path "/api/#{self.class..update.call(self)}" end |
#create_record ⇒ Object
53 54 55 56 57 58 |
# File 'lib/capsule_crm/persistence/persistable.rb', line 53 def create_record self.attributes = CapsuleCRM::Connection.post( build_create_path, to_capsule_json ) self end |
#new_record? ⇒ Boolean
45 46 47 |
# File 'lib/capsule_crm/persistence/persistable.rb', line 45 def new_record? !id end |
#persisted? ⇒ Boolean
49 50 51 |
# File 'lib/capsule_crm/persistence/persistable.rb', line 49 def persisted? !new_record? end |
#save ⇒ Object
23 24 25 26 27 28 29 |
# File 'lib/capsule_crm/persistence/persistable.rb', line 23 def save if valid? run_callbacks(:save) { new_record? ? create_record : update_record } else false end end |
#save! ⇒ Object
31 32 33 |
# File 'lib/capsule_crm/persistence/persistable.rb', line 31 def save! save || raise(CapsuleCRM::Errors::RecordInvalid.new(self)) end |
#update_attributes(attributes = {}) ⇒ Object
35 36 37 38 |
# File 'lib/capsule_crm/persistence/persistable.rb', line 35 def update_attributes(attributes = {}) self.attributes = attributes save end |
#update_attributes!(attributes = {}) ⇒ Object
40 41 42 43 |
# File 'lib/capsule_crm/persistence/persistable.rb', line 40 def update_attributes!(attributes = {}) self.attributes = attributes save! end |
#update_record ⇒ Object
68 69 70 71 |
# File 'lib/capsule_crm/persistence/persistable.rb', line 68 def update_record CapsuleCRM::Connection.put(build_update_path, to_capsule_json) self end |