Class: Movie

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/gimdb/model.rb

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (Object) get_code(what)



120
121
122
123
124
125
126
127
# File 'lib/gimdb/model.rb', line 120

def self.get_code(what)
  case what.to_sym
  when :to_see     then 0
  when :seen       then 1
  when :favourites then 2
  else                  nil
  end
end

+ (Object) get_kind(users, kind)



112
113
114
115
116
117
118
# File 'lib/gimdb/model.rb', line 112

def self.get_kind(users, kind)
  c = ''
  users.each { |u| c += "populars.user_id = #{u.id} OR " }
  c  = '(' + c[0..-5] + ') AND ' unless c.empty?
  c += "populars.kind = #{Movie.get_code(kind)}"
  Movie.select('movies.*').where(c).joins(:populars).order('title ASC').group('movies.id')
end

+ (Object) get_list(options)



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/gimdb/model.rb', line 89

def self.get_list(options)
  @start = options[:start] || 1
  c  = ''
  c += " AND title LIKE '%#{options[:title]}%'" if options[:title]
  if options[:release_data] && options[:release_data].include?(',')
    c += " AND year >= #{options[:release_data].split(',')[0]}"
    c += " AND year <= #{options[:release_data].split(',')[1]}"
  end
  if options[:user_rating] && options[:user_rating].include?(',')
    c += " AND rating >= #{options[:user_rating].split(',')[0]}"
    c += " AND rating <= #{options[:user_rating].split(',')[1]}"
  end
  c += " AND genre LIKE '%#{options[:genre]}%'" if options[:genre]
  c = c[5..-1]
  @movies = Movie.where(c).order('title ASC')#.limit(50)
  return @movies[@start-1..@start+48]
end

+ (Object) next



107
108
109
110
# File 'lib/gimdb/model.rb', line 107

def self.next
  @start = @start + 50
  return @movies[@start-1..@start+48] || []
end

Instance Method Details

- (Object) get_users(what = :to_see)



62
63
64
65
66
67
68
69
70
71
# File 'lib/gimdb/model.rb', line 62

def get_users(what = :to_see)
  code = Movie.get_code(what)
  unless code.nil?
    pops = Popular.where("movie_id = ? AND kind = ?", self.id, code)
    return [] if pops.nil?
    return pops.collect{|pop| pop.user}
  else
    return []
  end
end

- (Object) remove_user(user, what = :to_see)



81
82
83
84
85
86
87
# File 'lib/gimdb/model.rb', line 81

def remove_user(user, what = :to_see)
  code = Movie.get_code(what)
  unless code.nil?
    sql = "delete from populars where movie_id = #{self.id} AND user_id = #{user.id} AND kind = #{code}"
    ActiveRecord::Base.connection.execute(sql)
  end
end

- (Object) set_user(user, what = :to_see)



73
74
75
76
77
78
79
# File 'lib/gimdb/model.rb', line 73

def set_user(user, what = :to_see)
  code = Movie.get_code(what)
  unless code.nil?
    pop = Popular.new(:movie => self, :user => user, :kind => code)
    self.populars << pop
  end
end