PageRenderTime 55ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 1ms

/app/controllers/candies_controller.rb

https://github.com/stevesng/Spotcandy
Ruby | 297 lines | 219 code | 48 blank | 30 comment | 26 complexity | 31383313ab84d9ec082669b9cfd8ccc2 MD5 | raw file
  1. class CandiesController < ApplicationController
  2. layout 'application'
  3. def cleanup
  4. @list = []
  5. Candy.where("score = 0 AND flag_status = 0").order("created_at").limit(500).each do |c|
  6. diff = (Time.now.to_i - c.created_at.to_i)/(60*60*24)
  7. if diff >= 14
  8. c.flag_status = 2
  9. c.save
  10. @list << c.id
  11. end
  12. end
  13. end
  14. def privileges
  15. render :layout => 'mobile'
  16. end
  17. def profile
  18. @candy = Candy.find_by_pid(params[:pid])
  19. render :layout => 'mobile'
  20. end
  21. def display
  22. @candy = Candy.find(params[:id])
  23. render :layout => false
  24. end
  25. def grid
  26. #rand_id = rand(Candy.count)
  27. if params[:type] == 'nearby'
  28. @candies = Candy.where("is_candy = true").limit(15).order("created_at DESC").find_all_by_venue_id(session[:trending])
  29. end
  30. if params[:type] == 'recent'
  31. order = "created_at DESC"
  32. @candies = Candy.where("is_candy = true").order(order).limit(15)
  33. end
  34. if params[:type] == 'popular'
  35. order = "score DESC"
  36. @candies = Candy.where("is_candy = true AND score > 1").order(order)
  37. @candies = @candies.sort_by{rand}.slice(0,15)
  38. end
  39. if params[:type].nil?
  40. @candies = Candy.where("is_candy = true").order("RANDOM()").limit(15)
  41. end
  42. respond_to do |format|
  43. format.html { render :layout => false }
  44. format.json { render :json => @candies }
  45. end
  46. end
  47. def detail
  48. authenticate_user!
  49. @candy = Candy.find(params[:id])
  50. respond_to do |format|
  51. format.html {render :layout => false}
  52. format.json { render :json => @candy }
  53. end
  54. end
  55. def update_profile
  56. @candy = Candy.find(params[:id])
  57. @@auth = Authentication.get_keys
  58. refresh_photo = true
  59. #access_token = User.find_by_email('wilson@spotcandy.com').authentications.where("provider = ?",'foursquare').first.access_token
  60. foursquare_candy = JSON.parse(Authentication.req('foursquare').request(
  61. :get, "https://api.foursquare.com/v2/users/#{@candy.pid}",
  62. :client_id => @@auth[:key],
  63. :client_secret => @@auth[:secret],
  64. :oauth_token => session[:oauth]['foursquare']
  65. ))
  66. @candy.data['photo'] = foursquare_candy['response']['user']['photo']
  67. @candy.save
  68. # Save photos with paperclip for candy
  69. candy_photos = @candy.photos
  70. if @candy.is_candy && !candy_photos.empty?
  71. candy_current_photo = @candy.data['photo'].gsub('userpix_thumbs', 'userpix')
  72. # New Foursquare Photo
  73. if @candy.photos.find_by_photo_file_name(candy_current_photo[-20,20]).nil?
  74. attr = {:default_photo => @candy.photos.first.id, :photos_attributes => {@candy.photos.count => {:photo_remote_url => candy_current_photo}}}
  75. @candy.update_attributes(attr);
  76. refresh_photo = true
  77. else
  78. refresh_photo = false
  79. end
  80. end
  81. if refresh_photo
  82. pic = @candy.data['photo'].gsub('userpix_thumbs', 'userpix')
  83. thumbnail = @candy.data['photo']
  84. else
  85. pic = @candy.get_pic('original')
  86. thumbnail = @candy.get_pic('thumbnail')
  87. end
  88. respond_to do |format|
  89. format.json { render :json => {:candy => @candy, :pic => pic, :thumbnail => thumbnail} }
  90. end
  91. end
  92. def flag
  93. @candy = Candy.find(params[:id])
  94. # Notified only if never flagged before
  95. if @candy.flag_status == 0
  96. if @candy.recommendations.where("recommended = ?", true).empty? || current_user.verified
  97. @candy.flag_status = 2
  98. @candy.save
  99. else
  100. flag_mail = UserMailer.flag_mail(@candy, current_user).deliver
  101. end
  102. else
  103. @candy = nil
  104. end
  105. # redirect_to({:controller => 'venues', :action => 'candies', :id => params[:venue_id],
  106. # :anchor => "candy_#{params[:id]}"}, :notice => "We have been notified.")
  107. render :json => {:result => @candy}
  108. # respond_to do |format|
  109. # format.html # index.html.erb
  110. # format.json { render :json => {:notified => @notified} }
  111. # end
  112. end
  113. # GET /candies
  114. # GET /candies.xml
  115. def index
  116. authenticate_admin!
  117. @candies = Candy.all
  118. respond_to do |format|
  119. format.html # index.html.erb
  120. format.xml { render :xml => @candies }
  121. end
  122. end
  123. # GET /candies/1
  124. # GET /candies/1.xml
  125. def show
  126. #authenticate_admin!
  127. #require 'open-uri'
  128. @candy = Candy.find(params[:id])
  129. @candy_photo = @candy.data['photo']
  130. @photo = @candy.photos.first
  131. # dir_pic = "#{RAILS_ROOT}/public/images/candies/#{@candy.id}"
  132. #
  133. # FileUtils.mkdir_p dir_pic if !File.directory?(dir_pic)
  134. # open(@candy_photo) do |o|
  135. # File.open("#{dir_pic}/#{@candy.pid}.jpg", "w") { |f| f << o.read }
  136. # end
  137. respond_to do |format|
  138. format.html # show.html.erb
  139. format.xml { render :xml => @candy }
  140. format.json { render :json => @candy }
  141. end
  142. end
  143. # GET /candies/new
  144. # GET /candies/new.xml
  145. def new
  146. @candy = Candy.new
  147. respond_to do |format|
  148. format.html # new.html.erb
  149. format.xml { render :xml => @candy }
  150. end
  151. end
  152. # GET /candies/1/edit
  153. def edit
  154. authenticate_admin!
  155. @candy = Candy.includes(:users, {:venue => :users}).find(params[:id])
  156. @watchers = @candy.venue.users.where("watched = true")
  157. end
  158. # POST /candies
  159. # POST /candies.xml
  160. def create
  161. @candy = Candy.new(params[:candy])
  162. respond_to do |format|
  163. if @candy.save
  164. format.html { redirect_to(@candy, :notice => 'Candy was successfully created.') }
  165. format.xml { render :xml => @candy, :status => :created, :location => @candy }
  166. else
  167. format.html { render :action => "new" }
  168. format.xml { render :xml => @candy.errors, :status => :unprocessable_entity }
  169. end
  170. end
  171. end
  172. # PUT /candies/1
  173. # PUT /candies/1.xml
  174. def update
  175. @candy = Candy.includes(:users, {:venue => :users}).find(params[:id])
  176. if params[:send_mail] && params[:candy][:is_candy] == '1' && !@candy.is_candy
  177. @watchers = @candy.venue.users.where("watched = true")
  178. rand_id = rand(Candy.count)
  179. @watchers.each do |w|
  180. UserMailer.watch_new_candy_mail(w.email, @candy, rand_id).deliver if @candy.users.find_by_id(w.id).nil?
  181. end
  182. end
  183. if params[:candy][:facebook_update] == '1' && !@candy.facebook_update
  184. @facebook_page = Variable.find_by_name('facebook')
  185. if @facebook_page
  186. auth = Authentication.get_keys('facebook')
  187. @facebook_obj = JSON.parse(Authentication.req('facebook').request(
  188. :post, "https://graph.facebook.com/spotcandy/feed",
  189. :message => "Candy of the day spotted @\n#{@candy.venue.name}, #{@candy.venue.location['state']}",
  190. :picture => @candy.get_pic('thumbnail'),
  191. :name => "Candy of the Day",
  192. :description => "The world's most attractive people.",
  193. :link => "http://spotcandy.com/venue/#{@candy.venue.id}/candies",
  194. :caption => "http://m.spotcandy.com",
  195. :client_id => auth[:key],
  196. :client_secret => auth[:secret],
  197. :oauth_token => @facebook_page[:value]))
  198. end
  199. end
  200. @candy.update_attributes(params[:candy])
  201. if params[:candy][:flag_status] == '2' || params[:candy][:rejected] == '1'
  202. @candy.photos.destroy_all if !@candy.photos.empty?
  203. # params[:candy]['data'] = @candy.data
  204. # params[:candy]['data']['photo'] = @foursquare_candy['response']['user']['photo']
  205. end
  206. # Save photo with paperclip
  207. # photo = @candy.photos.first
  208. #
  209. # if photo
  210. # photo.photo_remote_url = @foursquare_candy['response']['user']['photo']
  211. # photo.save
  212. # else
  213. # params[:candy]['photos_attributes'] = {0 => {:photo_remote_url => @foursquare_candy['response']['user']['photo']}}
  214. # @candy.update_attributes(params[:candy])
  215. # end
  216. redirect_to(@candy, :notice => 'Candy was successfully updated.')
  217. # respond_to do |format|
  218. # if @candy.update_attributes(params[:candy])
  219. ## format.html { redirect_to(@candy, :notice => 'Candy was successfully updated.') }
  220. ## format.xml { head :ok }
  221. # format.html { render :action => "edit" }
  222. # else
  223. # format.html { render :action => "edit" }
  224. # format.xml { render :xml => @candy.errors, :status => :unprocessable_entity }
  225. # end
  226. # end
  227. end
  228. # DELETE /candies/1
  229. # DELETE /candies/1.xml
  230. def destroy
  231. @candy = Candy.find(params[:id])
  232. @candy.destroy
  233. respond_to do |format|
  234. format.html { redirect_to(candies_url) }
  235. format.xml { head :ok }
  236. end
  237. end
  238. end