Sha256: 05fd0cb30950af553d4a77a378fb50dbaa9b3668fd03407f01df381bab946fbb

Contents?: true

Size: 1.07 KB

Versions: 1

Compression:

Stored size: 1.07 KB

Contents

module Ubiquitously
  module Restful
    module Post
      def self.included(base)
        base.extend ClassMethods
        base.restful
        base.send :include, InstanceMethods
      end
    
      module ClassMethods
        def restful
          subclassable_callbacks :create, :update, :destroy
        end
        
        def create(attributes = {})
          record = new(attributes)
          record.save
          record
        end
        
        def create!(attributes = {})
          record = new(attributes)
          record.save!
          record
        end
        
        def find(options = {})
          nil
        end
      end
      
      module InstanceMethods
      
        def save
          create_or_update
        end
        
        def create_or_update
          new_record? ? create : update
        end
        
        def save!
          save || raise(Ubiquitously::RecordInvalid.new("Record is invalid: #{self.errors.full_messages}"))
        end
        
        def new_record?
          self.remote.blank?
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ubiquitously-0.1.0 lib/ubiquitously/models/service/post/restful.rb