Guest User

Untitled

a guest
Apr 20th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.36 KB | None | 0 0
  1.  
  2. # ads_controller.rb
  3.  
  4. class AdsController < ApplicationController
  5. def index
  6. @category = Category.find_by_slug(params[:slug])
  7. if @category
  8. @ads = @category.ads.all_active
  9. @requested_category = @category.name + ' in ' + @category.parent_category.name
  10. else
  11. @category = ParentCategory.find_by_slug(params[:slug])
  12. if @category
  13. @ads = @category.all_ads
  14. @requested_category = @category.name
  15. else
  16. flash[:warning] = "invalid request"
  17. redirect_to root_path
  18. end
  19. end
  20. end
  21. def show
  22. @ad = Ad.find_by_id(params[:id])
  23. if (@ad.nil?)
  24. flash[:warning] = "ad doesn't exist"
  25. redirect_to root_path
  26. end
  27. end
  28. def new
  29. if (params[:email] != params[:email_confirmation])
  30. flash[:warning] = "e-mails don't match"
  31. redirect_to create_ads_path
  32. else
  33. @author = Author.find_by_email(params[:email])
  34. if @author.blank?
  35. @author = Author.new
  36. @author.email = params[:email]
  37. @author.ip = request.env['REMOTE_ADDR']
  38. @author.save
  39. end
  40. @ad = Category.find_by_id(params[:category]).ads.new
  41. @ad.title = params[:title]
  42. @ad.ad = params[:ad].gsub("\n", "<br />")
  43. @ad.expiration = Time.now + 30.days
  44. @ad.author = @author
  45. @ad.author_ip = request.env['REMOTE_ADDR']
  46. @ad.save
  47. @ad.handle_images(params["image_attachments"])
  48. Mail.deliver_activation(@ad, @author.email)
  49. flash[:notice] = "ad pending activation"
  50. end
  51. end
  52. def create
  53. @parents = ParentCategory.find :all, :order => 'name ASC'
  54. end
  55. def edit
  56. @ad = Ad.find_by_activation_code(params[:activation_code])
  57. if (@ad.nil?)
  58. flash[:warning] = "ad doesn't exist"
  59. redirect_to root_path
  60. else
  61. end
  62. end
  63. def update
  64. @ad = Ad.find_by_activation_code(params[:activation_code])
  65. if (@ad.nil?)
  66. flash[:warning] = "ad doesn't exist"
  67. redirect_to root_path
  68. else
  69. @ad.ad = params[:ad].gsub("\n", "<br />")
  70. @ad.title = params[:title]
  71. if @ad.save
  72. @ad.handle_images(params["image_attachments"])
  73. flash[:notice] = "ad updated"
  74. else
  75. flash[:warning] = "error updating ad"
  76. end
  77. redirect_to edit_ads_path, :activation_code => @ad.activation_code
  78. end
  79. end
  80. def destroy
  81. @ad = Ad.find_by_activation_code(params[:id])
  82. if (@ad.nil?)
  83. flash[:warning] = "ad doesn't exist"
  84. redirect_to root_path
  85. else
  86. @ad.destroy
  87. flash[:notice] = "ad removed"
  88. redirect_to root_path
  89. end
  90. end
  91. def feed
  92. @ads = Ad.all_active
  93. respond_to do |format|
  94. format.rss { render :layout => false }
  95. format.atom # index.atom.builder
  96. end
  97. end
  98. end
  99.  
  100. # ads_activation_controller.rb
  101.  
  102. class AdsActivationController < ApplicationController
  103. def new
  104. @ad = Ad.find_by_activation_code(params[:activation_code])
  105. if (@ad.nil?)
  106. flash[:warning] = "error activating ad"
  107. redirect_to root_path
  108. else
  109. if @ad.activate_ad(params[:activation_code])
  110. flash[:notice] = "ad activated"
  111. Mail.deliver_activated(@ad, @ad.author.email)
  112. redirect_to :action => 'edit', :activation_code => @ad.activation_code
  113. else
  114. flash[:warning] = "error activating ad"
  115. redirect_to root_path
  116. end
  117. end
  118. end
  119. end
Add Comment
Please, Sign In to add comment