Sha256: 578089ee0ec23aa240f0eaafae4bf36ff7b5b8e17e3640d28efbb381ceee5231

Contents?: true

Size: 720 Bytes

Versions: 2

Compression:

Stored size: 720 Bytes

Contents

require_relative 'serializers/indifferent_json'

module Sojourn
  class Event < ActiveRecord::Base
    DEFAULT_FIELDS = [:id, :sojourner_uuid, :name, :properties, :sojourn_request_id, :user_id, :created_at]

    belongs_to :request, foreign_key: :sojourn_request_id
    belongs_to :user
    has_one :campaign, through: :request
    has_one :browser, through: :request

    serialize :properties, Serializers::IndifferentJSON

    before_save do
      properties.keys.map(&:to_sym).each do |key|
        send("#{key}=", properties[key]) if self.class.available_fields.include?(key)
      end
    end

    def self.available_fields
      @available_fields ||= column_names.map(&:to_sym) - DEFAULT_FIELDS
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
sojourn-1.0.0.pre2 lib/sojourn/event.rb
sojourn-1.0.0.pre lib/sojourn/event.rb