Sha256: 42fb1fe89ece509fb29b87301fdbb7f9df5af3e36b7bd19026b44eb2400a5a96

Contents?: true

Size: 981 Bytes

Versions: 25

Compression:

Stored size: 981 Bytes

Contents

class ActivityObjectProperty < ActiveRecord::Base
  belongs_to :activity_object,
             inverse_of: :activity_object_properties

  belongs_to :property,
             class_name: "ActivityObject",
             inverse_of: :activity_object_holds

  before_create :set_main
  after_update :update_main

  scope :main, where(main: true)

  def siblings
    self.
      class.
      includes(:property).
      where(activity_object_id: activity_object_id).
      where("property_id != ?", property_id).
      merge(ActivityObject.where(object_type: property.object_type))
  end

  private

  # before_create callback
  def set_main
    if !main? && siblings.blank?
      self.main = true
    elsif main? && siblings.present?
      remove_main_from_siblings
    end
  end

  # after_update callback
  def update_main
    remove_main_from_siblings if main?
  end

  def remove_main_from_siblings
    siblings.main.each do |s|
      s.update_attribute :main, false
    end
  end
end

Version data entries

25 entries across 25 versions & 2 rubygems

Version Path
social_stream-2.2.2 base/app/models/activity_object_property.rb
social_stream-base-2.2.2 app/models/activity_object_property.rb
social_stream-2.2.1 base/app/models/activity_object_property.rb
social_stream-base-2.2.1 app/models/activity_object_property.rb
social_stream-2.2.0 base/app/models/activity_object_property.rb
social_stream-base-2.2.0 app/models/activity_object_property.rb
social_stream-2.1.1 base/app/models/activity_object_property.rb
social_stream-2.1.0 base/app/models/activity_object_property.rb
social_stream-base-2.1.0 app/models/activity_object_property.rb
social_stream-2.0.4 base/app/models/activity_object_property.rb
social_stream-base-2.0.4 app/models/activity_object_property.rb
social_stream-2.0.3 base/app/models/activity_object_property.rb
social_stream-base-2.0.3 app/models/activity_object_property.rb
social_stream-2.0.2 base/app/models/activity_object_property.rb
social_stream-base-2.0.2 app/models/activity_object_property.rb
social_stream-2.0.1 base/app/models/activity_object_property.rb
social_stream-base-2.0.1 app/models/activity_object_property.rb
social_stream-2.0.0 base/app/models/activity_object_property.rb
social_stream-base-2.0.0 app/models/activity_object_property.rb
social_stream-2.0.0.beta3 base/app/models/activity_object_property.rb