Sha256: 91a049c63b13d1d6cda4297644d72af3def3636b2f528bab76d7bae4ba45498a

Contents?: true

Size: 1.22 KB

Versions: 3

Compression:

Stored size: 1.22 KB

Contents

module Granite
  module Form
    module Model
      module Persistence
        extend ActiveSupport::Concern

        module ClassMethods
          def instantiate(data)
            data = data.stringify_keys
            instance = allocate

            instance.instance_variable_set(:@initial_attributes, data.slice(*attribute_names))
            instance.send(:mark_persisted!)

            instance
          end

          def instantiate_collection(data)
            collection = Array.wrap(data).map { |attrs| instantiate attrs }
            collection = scope(collection, true) if respond_to?(:scope)
            collection
          end
        end

        def persisted?
          !!@persisted
        end

        def destroyed?
          !!@destroyed
        end

        def marked_for_destruction?
          @marked_for_destruction
        end

        def mark_for_destruction
          @marked_for_destruction = true
        end

        def _destroy
          marked_for_destruction?
        end

      private

        def mark_persisted!
          @persisted = true
          @destroyed = false
        end

        def mark_destroyed!
          @persisted = false
          @destroyed = true
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
granite-form-0.2.0 lib/granite/form/model/persistence.rb
granite-form-0.1.1 lib/granite/form/model/persistence.rb
granite-form-0.1.0 lib/granite/form/model/persistence.rb