Sha256: 1fe74df855769bf0bf24223516902df1607ebfdc02d4fef69e16b67916a40416

Contents?: true

Size: 896 Bytes

Versions: 2

Compression:

Stored size: 896 Bytes

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 marked_for_destruction?
          false
        end

        private

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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
granite-form-0.6.1 lib/granite/form/model/persistence.rb
granite-form-0.6.0 lib/granite/form/model/persistence.rb