Sha256: db7709030298cb4f22a0d6283eb51a4a8dd1cfbdac908ef35823042269f56c5e

Contents?: true

Size: 1.39 KB

Versions: 2

Compression:

Stored size: 1.39 KB

Contents

# frozen_string_literal: true

require 'tramway/forms/properties'
require 'tramway/forms/normalizations'

module Tramway
  # Provides form object for Tramway
  #
  class BaseForm
    include Tramway::Forms::Properties
    include Tramway::Forms::Normalizations

    attr_reader :object

    %i[model_name to_key to_model errors attributes].each do |method_name|
      delegate method_name, to: :object
    end

    def initialize(object)
      @object = object

      self.class.delegate object.class.primary_key, to: :object
    end

    class << self
      def inherited(subclass)
        __initialize_properties subclass
        __initialize_normalizations subclass

        super
      end
    end

    def submit(params)
      __submit params

      object.save.tap do
        __object
      end
    end

    def submit!(params)
      __submit params

      object.save!.tap do
        __object
      end
    end

    def method_missing(method_name, *args)
      if method_name.to_s.end_with?('=') && args.count == 1
        object.public_send(method_name, args.first)
      else
        super
      end
    end

    def respond_to_missing?(method_name, include_private = false)
      method_name.to_s.end_with?('=') || super
    end

    private

    def __submit(params)
      __apply_properties __apply_normalizations params
    end

    def __object
      object.persisted? ? object.reload : object
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
tramway-0.4.2.1 lib/tramway/base_form.rb
tramway-0.4.2 lib/tramway/base_form.rb