Class: Argument

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Likeable
Defined in:
app/models/argument.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Methods included from Likeable

#update_likes_counter

Instance Attribute Details

#positiveObject

Returns the value of attribute positive.



30
31
32
# File 'app/models/argument.rb', line 30

def positive
  @positive
end

Class Method Details

.create_with_topic(user_id, attributes) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/models/argument.rb', line 32

def self.create_with_topic(user_id, attributes)
  topic = ArgumentTopic.find_or_create_by_name attributes[:topic_name]
  
  if topic.valid?
    argumentable_id = if attributes[:argumentable_name].present?
      attributes[:argumentable_type].constantize.where('LOWER(name) = ?', attributes[:argumentable_name].downcase).first.id
    else
      attributes[:argumentable_type].constantize.where(id: attributes[:argumentable_id]).first.try(:id)
    end
    
    argument = Argument.new(
      topic_id: topic.id, argumentable_type: attributes[:argumentable_type], argumentable_id: argumentable_id, 
      value: attributes[:value], vote: attributes[:vote]
    )
    argument.user_id = user_id
    argument.save
    
    if argument.valid?
      argument
    else
      { errors: argument.errors.to_hash }
    end
  else
    { errors: { topic: topic.errors.to_hash } }
  end
end