Sha256: d0d55ffc79d132472e3d7887597e889f84a4b4058a908d69376296dd739b3806

Contents?: true

Size: 1.01 KB

Versions: 1

Compression:

Stored size: 1.01 KB

Contents

require 'active_support/core_ext/object/deep_dup'
require 'active_model'
require 'virtus'
require 'act_form/combinable'

module ActForm
  module Model
    extend ActiveSupport::Concern
    include ActiveModel::Validations
    include ActiveModel::Conversion

    included do
      extend ActiveModel::Naming
      extend ActiveModel::Translation
      include Virtus.model
    end

    def sync(target)
      self.class.attribute_set.map(&:name).each do |attr|
        target.public_send("#{attr}=", public_send(attr)) if target.respond_to?(attr)
      end
    end

    def save(target)
      if valid?
        sync(target)
        @persisted = target.save
      else
        false
      end
    end

    def persisted?
      !!@persisted
    end

    class_methods do
      private
      def inherited(child_class)
        child_class._validators = self._validators.deep_dup
        child_class._validate_callbacks = self._validate_callbacks.deep_dup
        child_class.include Combinable
        super
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
act_form-0.1.0 lib/act_form/model.rb