Sha256: 0f78b249cb6f0c7d790714293dc94bc926473a40e2b951fb8ee0330041b77054

Contents?: true

Size: 1.09 KB

Versions: 2

Compression:

Stored size: 1.09 KB

Contents

# frozen_string_literal: true

module ActiveCampaign
  class Model # :nodoc:
    include ActiveModel::Model
    include ActiveModel::Dirty

    include ActiveCampaign::ApiHttp
    include ActiveCampaign::Attributes

    define_attributes(*DEFAULT_ATTRS)

    class << self
      def all
        get endpoint
      end

      def filter(args = {})
        get "#{endpoint}?#{args}"
      end

      def find_by(args = {})
        filter(args.to_query).last
      end

      def find(id)
        get "#{endpoint}/#{id}"
      end

      def save(**args)
        new(args).save
      end

      def create(**args)
        new(args).create
      end
    end

    def save
      return update if id.present?

      create
    end

    def update
      return nil unless changed?

      result = self.class.put "#{self.class.endpoint}/#{id}", only_changes_to_params

      changes_applied

      result
    end

    def create
      result = self.class.post self.class.endpoint, to_params

      changes_applied

      result
    end

    def destroy
      self.class.delete "#{self.class.endpoint}/#{id}"
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ractive_campaign-0.1.0 lib/active_campaign/models/model.rb
ractive_campaign-0.0.1 lib/active_campaign/models/model.rb