Guest User

Untitled

a guest
Apr 25th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.10 KB | None | 0 0
  1.  
  2. Content:
  3.  
  4. controllers/ads_controller.rb
  5. models/ad.rb
  6. models/category.rb
  7. models/parent_category.rb
  8.  
  9. --
  10.  
  11. controllers/ads_controller.rb
  12.  
  13. class AdsController < ApplicationController
  14.  
  15. # show one particular ad
  16. def show
  17. @ad = Ad.find_by_id(params[:id])
  18. if (@ad.nil?)
  19. flash[:warning] = 'Error - That Ad Does Not Exist'
  20. redirect_to root_path
  21. end
  22. end
  23.  
  24. # destroy ad with that particular hash
  25. def destroy
  26. if request.post?
  27. @ad = Ad.find_by_activation_hash(params[:id])
  28. if (@ad.nil?)
  29. flash[:warning] = 'Error - That Ad Does Not Exist'
  30. redirect_to root_path
  31. # report to admin?
  32. else
  33. # destroy the ad
  34. @ad.destroy
  35. flash[:notice] = 'Your Ad Has Been Removed'
  36. redirect_to root_path
  37. end
  38. end
  39. end
  40.  
  41. # show a list of ads in a category
  42. # we need a clever way to do this - if both parent and child categories
  43. # have a page slug how to identify one vs. the other? might be easier to
  44. # inherit then do this code checking tfor a blank array..
  45. def list
  46. @category = Category.find_by_slug(params[:slug])
  47. if @category
  48. @ads = @category.ads.all_active
  49. @requested_category = @category.parent_category.name + ' >> ' + @category.name
  50. else
  51. @category = ParentCategory.find_by_slug(params[:slug])
  52. if @category
  53. @ads = @category.all_ads
  54. @requested_category = @category.name
  55. else
  56. flash[:warning] = 'Invalid Request'
  57. redirect_to root_path
  58. end
  59. end
  60. end
  61.  
  62.  
  63. # show parent category list for new ad
  64. def post
  65. @parents = ParentCategory.find :all, :order => 'name ASC'
  66. end
  67.  
  68.  
  69. # ajaxy function to show subcategories when they select a parent
  70. def select_category
  71. @parent_category = ParentCategory.find_by_id(params[:id])
  72. end
  73.  
  74. # show the ajaxy form for a new ad
  75. def show_form
  76. @category = Category.find_by_id(params[:id])
  77. end
  78.  
  79. # ajaxy - create new ad
  80. def new
  81. if (params[:email] != params[:email_verify])
  82. # return an error since email and confirmation don't match
  83. flash[:warning] = 'Email Address and Email Confirmation Do Not Match'
  84. #@category = params[:category]
  85. # TODO
  86. # can we somehow send them to the last screen?
  87. redirect_to :controller => 'ads', :action => 'post'
  88. else
  89. # email and confirmation match
  90. @author = Author.find_by_email(params[:email])
  91. if @author.blank?
  92. @author = Author.new
  93. @author.email = params[:email]
  94. @author.ip = request.env['REMOTE_ADDR']
  95. @author.save
  96. end
  97. @ad = Category.find_by_id(params[:category]).ads.new
  98. @ad.title = params[:title]
  99. @ad.ad = params[:ad].gsub("\n", "<br/>")
  100. @ad.expiration = Time.now + 30.days
  101. @ad.author = @author
  102.  
  103. # record author IP address
  104. @ad.author_ip = request.env['REMOTE_ADDR']
  105. @ad.save
  106.  
  107. # handle image attachments
  108. @ad.handle_images(params["image_attachments"])
  109.  
  110. # send confirmation email with activation url
  111. Mailman.deliver_confirmation_email(@ad, @author.email)
  112. flash[:notice] = 'A Confirmation Email Has Been Sent To ' + @author.email
  113.  
  114. end
  115. end
  116.  
  117. # activate an ad that is new but not active yet
  118. def activate
  119. @ad = Ad.find_by_activation_hash(params[:activation_hash])
  120. if (@ad.nil?)
  121. flash[:warning] = 'Error Activating Your Ad'
  122. redirect_to root_path
  123. #report to admin?
  124. else
  125. # not sure we need this?
  126. #respond_to do |format|
  127. #redirect to confirmation thank you page
  128. if @ad.activate(params[:activation_hash])
  129. flash[:notice] = 'Your Ad Has Been Activated - An Email Has Been Sent To ' + @ad.author.email
  130. Mailman.deliver_activation_email(@ad, @ad.author.email)
  131. #redirect_to :action => 'show', :id => @ad
  132. redirect_to :action => 'edit', :activation_hash => @ad.activation_hash
  133. else
  134. flash[:warning] = 'Error Activating Your Ad'
  135. redirect_to root_path
  136. end
  137. #format.html {render :action => "confirmed"}
  138. #end
  139. end
  140. end
  141.  
  142.  
  143. # manage an ad based on the hash
  144. def manage
  145. @ad = Ad.find_by_activation_hash(params[:activation_hash])
  146. if (@ad.nil?)
  147. flash[:warning] = 'Error - That Ad Does Not Exist'
  148. redirect_to root_path
  149. # report to admin?
  150. else
  151. # show the ad and let them edit it
  152. # going to use our own rhtml with edit buttons etc.
  153. #render :action => 'show'
  154. end
  155. end
  156.  
  157. # edit an ad based on the hash
  158. def edit
  159. @ad = Ad.find_by_activation_hash(params[:activation_hash])
  160. if (@ad.nil?)
  161. flash[:warning] = 'Error - That Ad Does Not Exist'
  162. redirect_to root_path
  163. # report to admin?
  164. else
  165. # show the ad and let them edit it
  166. end
  167. end
  168.  
  169. # update an ad after someone edits (via the edit form) and hits 'submit'
  170. def update
  171. @ad = Ad.find_by_activation_hash(params[:activation_hash])
  172. if (@ad.nil?)
  173. flash[:warning] = 'Error - That Ad Does Not Exist'
  174. redirect_to root_path
  175. # report to admin?
  176. else
  177. @ad.ad = params[:ad].gsub("\n", "<br/>")
  178. @ad.title = params[:title]
  179. if @ad.save
  180.  
  181. # handle image attachments
  182. @ad.handle_images(params["image_attachments"])
  183.  
  184. flash[:notice] = "Ad Updated Successfully"
  185. else
  186. flash[:warning] = "Error Updating Ad"
  187. end
  188. redirect_to :controller => 'ads', :action => 'manage', :activation_hash => @ad.activation_hash
  189. end
  190. end
  191.  
  192. def delete_image
  193. @ad = Ad.find_by_activation_hash(params[:activation_hash])
  194. @ad_image = @ad.ad_images.find_by_id(params[:id])
  195.  
  196. if @ad_image.destroy
  197. flash[:notice] = "Ad Image Deleted"
  198. else
  199. flash[:warning] = "Unable to Find Ad Image to Delete"
  200. end
  201.  
  202. redirect_to :controller => 'ads', :action => 'manage', :activation_hash => @ad.activation_hash
  203. end
  204.  
  205.  
  206. # rss feed
  207. def feed
  208. @ads = Ad.all_active
  209.  
  210. respond_to do |format|
  211. format.rss { render :layout => false }
  212. format.atom # index.atom.builder
  213. end
  214. end
  215.  
  216.  
  217. end
  218.  
  219. --
  220.  
  221. models/ad.rb
  222.  
  223. require 'uuid'
  224. class Ad < ActiveRecord::Base
  225. belongs_to :category
  226. belongs_to :author
  227.  
  228. has_many :ad_images
  229.  
  230. validate :expiration_is_set
  231.  
  232. def handle_images(image_attachments)
  233. return unless image_attachments.respond_to? :each
  234.  
  235. image_attachments.each do |image|
  236. ad_image = self.ad_images.build(:uploaded_data => image)
  237. ad_image.save
  238. end
  239. end
  240.  
  241. def expiration_is_set
  242. if expiration.nil?
  243. expiration = Time.now + 30.days
  244. end
  245. end
  246.  
  247. def expire!
  248. self.expiration = Time.now - 1.day
  249. self.save
  250. end
  251.  
  252. def extend!
  253. self.expiration = Time.now + 30.days
  254. self.save
  255. end
  256.  
  257. def reset!
  258. self.active = false
  259. self.save
  260. end
  261.  
  262. def expired?
  263. self.expiration < Time.now
  264. end
  265.  
  266. # TODO
  267. def self.fetch_by_category_slug(slug)
  268. end
  269.  
  270. # activate an ad
  271. # pass the activation hash/params as "conf"
  272. # let the model validate it for us
  273. def activate(conf)
  274. if self.activation_hash == conf and !self.active?
  275. self.active = true
  276. if self.save
  277. return true
  278. else
  279. return false
  280. end
  281. else
  282. return false
  283. end
  284. end
  285.  
  286.  
  287. def self.all_active
  288. find(:all, :conditions => ['expiration > ? and active = ?', Time.now, true], :order => 'created_at ASC')
  289. end
  290.  
  291. def self.display_paged_data(page)
  292. paginate(:page => page, :per_page => 10,:order => "id")
  293. end
  294.  
  295.  
  296. protected
  297. def before_create
  298. self.activation_hash = UUID.new
  299. #self.email = DateTime.now.to_s(:generate_hash) + rand(10000).to_s.rjust(5, '0') + "@chuckslist.com"
  300. self.email = DateTime.now.to_s(:generate_hash) + $generatedKeyCount.to_s.rjust(5, '0') + "@" + DOMAIN
  301. $generatedKeyCount = $generatedKeyCount + 1
  302. if ($generatedKeyCount > 99999)
  303. $generatedKeyCount = 1
  304. end
  305. end
  306.  
  307.  
  308.  
  309.  
  310.  
  311. end
  312.  
  313. --
  314.  
  315. models/category.rb
  316.  
  317. class Category < ActiveRecord::Base
  318. has_many :ads
  319. belongs_to :parent_category
  320.  
  321. validates_presence_of :name
  322. validates_uniqueness_of :name, :case_sensitive => false
  323. validates_length_of :name, :within => 4..40
  324. validates_presence_of :slug
  325. validates_uniqueness_of :slug, :case_sensitive => false
  326. validates_length_of :slug, :within => 3..40
  327.  
  328.  
  329.  
  330. def self.display_paged_data(page)
  331. paginate(:page => page, :per_page => 10,:order => "name")
  332. end
  333.  
  334. end
  335.  
  336. --
  337.  
  338. models/parent_category.rb
  339.  
  340. class ParentCategory < ActiveRecord::Base
  341. has_many :categories do
  342.  
  343. def in_order
  344. find(:all, :order => 'name ASC')
  345. end
  346.  
  347.  
  348. end
  349.  
  350.  
  351. def all_ads
  352. @subcats = self.categories
  353. @results = []
  354. @subcats.each { |c| c.ads.all_active.each { |a| @results << a } }
  355.  
  356. return @results
  357. end
  358.  
  359. def self.display_paged_data(page)
  360. self.paginate(:page => page, :per_page => 10,:order => "name")
  361. end
  362. end
Add Comment
Please, Sign In to add comment