Sha256: a7074e45341b263d5a03d48eb1de815d1a86d8cd3d49e0b2183ccb01490870b0

Contents?: true

Size: 766 Bytes

Versions: 1

Compression:

Stored size: 766 Bytes

Contents

module Inter
  class Action < ActiveRecord::Base
    self.table_name = :interactions

    belongs_to :interactable, :polymorphic => true

    validates :interactable, presence: true
    validates :key, presence: true

    after_create :expire_previous_states

    serialize :value, JSON

    scope :search, ->(key, value, relation, ids){ where(key: key, value: value.to_json, current: true.to_json, interactable_type: relation.singularize.capitalize, interactable_id: ids) }

    protected

    def expire_previous_states
      previous_states.update_all(current: false)
    end

    def previous_states
      Inter::Action.where("interactable_id = ? AND interactable_type = ? AND key = ? AND id != ?", interactable_id, interactable_type, key, id)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
inter-0.0.1 lib/inter/action.rb