Class: FeatureBox::Suggestion

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/feature_box/suggestion.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.find_my(type_of_impact, hash) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'app/models/feature_box/suggestion.rb', line 81

def self.find_my type_of_impact, hash

  user    = hash[:user]
  limit   = hash[:limit]
  offset  = hash[:offset]
  case type_of_impact
    when :suggestions
      suggestions = user.suggestions.order("created_at DESC").limit(limit).offset(offset)
      total = user.suggestions.size
    when :votes, :comments
      impact_table_name = eval("FeatureBox::"+type_of_impact.to_s.capitalize.chomp("s")).table_name
      suggestions = Suggestion.joins(type_of_impact).where("#{impact_table_name}.user_id = ?",user.id).order("created_at DESC").uniq.limit(limit).offset(offset)
      total = Suggestion.count_by_sql(["SELECT COUNT(DISTINCT #{self.table_name}.id) FROM #{self.table_name} INNER JOIN #{impact_table_name} ON #{impact_table_name}.suggestion_id = #{self.table_name}.id WHERE #{self.table_name}.user_id = ?", user.id])
    else 
      raise ArgumentError.new("Unknown type of impact")
  end
  return suggestions, total
end

.find_those_with(hash) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'app/models/feature_box/suggestion.rb', line 45

def self.find_those_with hash
  category    = hash[:category]
  order_type  = hash[:order_type]
  limit       = hash[:limit]
  offset      = hash[:offset]

  category_filter = if category.id then Suggestion.where(:category_id => category.id) else Suggestion end
    
  case order_type
    when :most_popular
      where_part = "WHERE status <> 'complete' "
      if category.id!=nil then 
        where_part += "AND #{self.table_name}.category_id = #{category.id.to_s}"
      end
      suggestions = Suggestion.find_by_sql(
        "SELECT COUNT(*) AS count_all, #{self.table_name}.*
         FROM #{self.table_name} 
         INNER JOIN #{Vote.table_name} 
         ON #{Vote.table_name}.suggestion_id = #{self.table_name}.id 
         #{where_part}
         GROUP BY #{self.table_name}.id
         ORDER BY count_All DESC 
         LIMIT #{limit.to_s} OFFSET #{offset.to_s}")
      total = category_filter.where("status <> ?", :complete).size
    when :in_progress, :complete
      suggestions = category_filter.where(:status => order_type).order("created_at DESC").limit(limit).offset(offset)
      total = category_filter.where(:status => order_type).size
    when :newest
      suggestions = category_filter.where("status <> ?", :complete).order("created_at DESC").limit(limit).offset(offset)
      total = category_filter.where("status <> ?", :complete).size
    else
      raise ArgumentError.new("Unknown order_type")
  end
  return suggestions, total
end

Instance Method Details

#statusObject



24
25
26
# File 'app/models/feature_box/suggestion.rb', line 24

def status
  read_attribute(:status).to_sym
end

#status=(value) ⇒ Object



28
29
30
# File 'app/models/feature_box/suggestion.rb', line 28

def status= (value)
  write_attribute(:status, value.to_s)
end

#user_votes(user) ⇒ Object



32
33
34
# File 'app/models/feature_box/suggestion.rb', line 32

def user_votes(user)
  Vote.where(:suggestion_id=>id,:user_id=>user.id).size()
end

#vote(user) ⇒ Object



36
37
38
39
40
41
42
43
# File 'app/models/feature_box/suggestion.rb', line 36

def vote(user)
  if  user.votes_left > 0 && user.can_vote?(self)
    v=Vote.new
    v.user=user
    v.suggestion=self
    v.save
  end
end