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

#voteObject

Returns the value of attribute vote.



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

def vote
  @vote
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

.matrix(argumentables) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'app/models/argument.rb', line 59

def self.matrix(argumentables)
  arguments = []
  
  argumentables.each do |index, argumentable|
    arguments += Argument.where(
      argumentable_type: argumentable['type'], argumentable_id: argumentable['id']
    ).includes(:topic).to_a
  end
  
  arguments_by_name = {}
  
  arguments.each do |argument| 
    arguments_by_name[argument.topic.name] ||= []
    arguments_by_name[argument.topic.name] << argument
  end
  
  json = { argumentables: [], matrix: [] }
  
  argumentables.keys.map(&:to_i).sort.each do |argumentable_index|
    argumentable = argumentables[argumentable_index.to_s]
    argumentable = argumentable['type'].constantize.find(argumentable['id'])
    
    json[:argumentables] << { id: argumentable.id, slug: argumentable.try(:slug), name: argumentable.name }
  end
  
  arguments_by_name.keys.sort.each do |topic_name|
    item = { topic_name: topic_name, values: [] }
    arguments = arguments_by_name[topic_name]
    
    argumentables.keys.map(&:to_i).sort.each do |argumentable_index|
      argumentable = argumentables[argumentable_index.to_s]
      item[:values] << arguments.select{|a| a.argumentable_type == argumentable['type'] && a.argumentable_id == argumentable['id'].to_i }.first.try(:value)
    end
    
    json[:matrix] << item
  end
  
  json
end