Module: SparkApi::Models::Concerns::Savable
- Included in:
- SparkApi::Models::Comment, SparkApi::Models::Contact, IncompleteListing, ListingCart, Newsfeed, Portal, QuickSearch, SavedSearch, Video, VirtualTour, VowAccount
- Defined in:
- lib/spark_api/models/concerns/savable.rb
Instance Method Summary collapse
- #create!(arguments = {}) ⇒ Object
-
#params_for_save ⇒ Object
extra params to be passed when saving.
-
#post_data ⇒ Object
update/create hash (can be overridden).
- #save(arguments = {}) ⇒ Object
- #save!(arguments = {}) ⇒ Object
- #update!(arguments = {}) ⇒ Object
- #update_attributes(attrs = {}, options = {}) ⇒ Object
Instance Method Details
#create!(arguments = {}) ⇒ Object
23 24 25 26 27 28 29 |
# File 'lib/spark_api/models/concerns/savable.rb', line 23 def create!(arguments = {}) results = connection.post self.path, post_data.merge(params_for_save), arguments update_attributes_after_create(results.first) reset_dirty params_for_save.clear true end |
#params_for_save ⇒ Object
extra params to be passed when saving
45 46 47 |
# File 'lib/spark_api/models/concerns/savable.rb', line 45 def params_for_save @params_for_save ||= {} end |
#post_data ⇒ Object
update/create hash (can be overridden)
50 51 52 |
# File 'lib/spark_api/models/concerns/savable.rb', line 50 def post_data { resource_pluralized => [ attributes ] } end |
#save(arguments = {}) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/spark_api/models/concerns/savable.rb', line 7 def save(arguments = {}) self.errors = [] # clear the errors hash begin return save!(arguments) rescue BadResourceRequest => e self.errors << {:code => e.code, :message => e.} SparkApi.logger.warn("Failed to save resource #{self}: #{e.}") rescue NotFound => e SparkApi.logger.error("Failed to save resource #{self}: #{e.}") end false end |
#save!(arguments = {}) ⇒ Object
19 20 21 |
# File 'lib/spark_api/models/concerns/savable.rb', line 19 def save!(arguments = {}) persisted? ? update!(arguments) : create!(arguments) end |
#update!(arguments = {}) ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/spark_api/models/concerns/savable.rb', line 36 def update!(arguments = {}) return true unless changed? && persisted? connection.put resource_uri, dirty_attributes, arguments reset_dirty params_for_save.clear true end |
#update_attributes(attrs = {}, options = {}) ⇒ Object
31 32 33 34 |
# File 'lib/spark_api/models/concerns/savable.rb', line 31 def update_attributes(attrs = {}, = {}) load(attrs, ) save! end |