Sha256: 15f289cc499693169eb4f1883036821038a71a4395adebebb91f18f517c7c484

Contents?: true

Size: 1.36 KB

Versions: 4

Compression:

Stored size: 1.36 KB

Contents

# frozen_string_literal: true

module ActionTracker
  module Templates
    class BaseTemplate
      attr_reader :target, :options

      def initialize(target, options = {})
        @target = target
        @options = options
      end

      def form
        @form ||= ActionTracker::Models::TransitionRecord.new(
          payload: payload,
          reference: reference,
          created_at: created_at
        ).with_target(target)
      end

      protected

      def payload
        @payload ||= build_payload
      end

      def build_payload
        payload_instance = ActionTracker::Models::Payload.new

        payload_instance
          .with_user(user)
          .with_content(content)
          .for_event(event_title)
      end

      def user
        @user ||= options[:user] || ActionTracker::Models::User.default_user
      end

      # Override
      #
      def content
        options[:content] || target.try(:id)
      end

      def event_title
        [title_prepend, event_name, title_append].compact.join(' ').humanize
      end

      def event_name
        # Override
      end

      def title_prepend
        options[:title_prepend]
      end

      def title_append
        options[:title_append]
      end

      def created_at
        options[:created_at] || Time.zone&.now
      end

      def reference
        options[:reference]
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
action_tracker_client-0.1.8 lib/action_tracker/templates/base_template.rb
action_tracker_client-0.1.7 lib/action_tracker/templates/base_template.rb
action_tracker_client-0.1.6 lib/action_tracker/templates/base_template.rb
action_tracker_client-0.1.5 lib/action_tracker/templates/base_template.rb