Sha256: c880aa10b0fbb73f4ee8327ab14295ecbac31b27b61a5b075354c08702c06a06

Contents?: true

Size: 1.62 KB

Versions: 1

Compression:

Stored size: 1.62 KB

Contents

class BrainstormingIdea < ActiveRecord::Base
  belongs_to :brainstorming
  belongs_to :user
  
  has_many :arguments, as: :argumentable, dependent: :destroy
  has_many :votes, class_name: 'BrainstormingIdeaVote', dependent: :destroy, foreign_key: 'idea_id'
  
  scope :with_current_user_vote, ->(user_id) do
    select('brainstorming_ideas.*, brainstorming_idea_votes.user_id').joins(
      'LEFT JOIN brainstorming_idea_votes ON brainstorming_idea_votes.idea_id = brainstorming_ideas.id AND ' +
      "brainstorming_idea_votes.user_id = #{sanitize(user_id)}"
    )  
  end
  
  validates :brainstorming_id, presence: true
  validates :user_id, presence: true
  validates :name, presence: true, uniqueness: { scope: :brainstorming_id }
  
  attr_accessible :brainstorming_id, :name, :text
  
  after_create :publish_create
  after_update :publish_update
  after_destroy :publish_destroy
  
  private
  
  def publish_create
    return if Rails.env.test? || Rails.env.cucumber?
    
    MessageBus.publish(
      "/brainstormings/#{brainstorming.slug}", 
      { message: "#{I18n.t('brainstorming_ideas.model.publish_create')}: #{name}" }
    )
  end
  
  def publish_update
    return if Rails.env.test? || Rails.env.cucumber?
    
    MessageBus.publish(
      "/brainstormings/#{brainstorming.slug}", 
      { message: "#{I18n.t('brainstorming_ideas.model.publish_update')}: #{name}" }
    )
  end
  
  def publish_destroy
    return if Rails.env.test? || Rails.env.cucumber?
    
    MessageBus.publish(
      "/brainstormings/#{brainstorming.slug}", 
      { message: "#{I18n.t('brainstorming_ideas.model.publish_destroy')}: #{name}" }
    )
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
voluntary_brainstorming-0.0.2 app/models/brainstorming_idea.rb