Guest User

Untitled

a guest
Apr 25th, 2018
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.37 KB | None | 0 0
  1.  
  2. 1. ads_controller.rb
  3. 2. categories_controller.rb
  4. 3. parent_categories_controller.rb
  5. 4. routes.rb
  6.  
  7. # ads_controller.rb
  8.  
  9. class AdsController < ApplicationController
  10. def index
  11. @category = Category.find_by_slug(params[:slug])
  12. if @category
  13. @ads = @category.ads.all_active
  14. @requested_category = @category.name + ' in ' + @category.parent_category.name
  15. else
  16. @category = ParentCategory.find_by_slug(params[:slug])
  17. if @category
  18. @ads = @category.all_ads
  19. @requested_category = @category.name
  20. else
  21. flash[:warning] = "invalid request"
  22. redirect_to root_path
  23. end
  24. end
  25. end
  26. def show
  27. @ad = Ad.find_by_id(params[:id])
  28. if (@ad.nil?)
  29. flash[:warning] = "ad doesn't exist"
  30. redirect_to root_path
  31. end
  32. end
  33. def new
  34. @parents = ParentCategory.find :all, :order => 'name ASC'
  35. end
  36. def create
  37. if (params[:email] != params[:email_confirmation])
  38. flash[:warning] = "e-mails don't match"
  39. redirect_to create_ads_path
  40. else
  41. @author = Author.find_by_email(params[:email])
  42. if @author.blank?
  43. @author = Author.new
  44. @author.email = params[:email]
  45. @author.ip = request.env['REMOTE_ADDR']
  46. @author.save
  47. end
  48. @ad = Category.find_by_id(params[:category]).ads.new
  49. @ad.title = params[:title]
  50. @ad.ad = params[:ad].gsub("\n", "<br />")
  51. @ad.expiration = Time.now + 30.days
  52. @ad.author = @author
  53. @ad.author_ip = request.env['REMOTE_ADDR']
  54. @ad.save
  55. @ad.images(params["image_attachments"])
  56. Mail.deliver_activation(@ad, @author.email)
  57. flash[:notice] = "ad pending activation"
  58. end
  59. end
  60. def ad_activate
  61. @ad = Ad.find_by_activation_code(params[:activation_code])
  62. if (@ad.nil?)
  63. flash[:warning] = "error activating ad"
  64. redirect_to root_path
  65. else
  66. if @ad.activate_ad(params[:activation_code])
  67. flash[:notice] = "ad activated"
  68. Mail.deliver_activated(@ad, @ad.author.email)
  69. redirect_to :action => 'edit', :activation_code => @ad.activation_code
  70. else
  71. flash[:warning] = "error activating ad"
  72. redirect_to root_path
  73. end
  74. end
  75. end
  76. def edit
  77. @ad = Ad.find_by_activation_code(params[:activation_code])
  78. if (@ad.nil?)
  79. flash[:warning] = "ad doesn't exist"
  80. redirect_to root_path
  81. else
  82. end
  83. end
  84. def update
  85. @ad = Ad.find_by_activation_code(params[:activation_code])
  86. if (@ad.nil?)
  87. flash[:warning] = "ad doesn't exist"
  88. redirect_to root_path
  89. else
  90. @ad.ad = params[:ad].gsub("\n", "<br />")
  91. @ad.title = params[:title]
  92. if @ad.save
  93. @ad.images(params["image_attachments"])
  94. flash[:notice] = "ad updated"
  95. else
  96. flash[:warning] = "error updating ad"
  97. end
  98. redirect_to edit_ads_path, :activation_code => @ad.activation_code
  99. end
  100. end
  101. def destroy
  102. @ad = Ad.find_by_activation_code(params[:id])
  103. if (@ad.nil?)
  104. flash[:warning] = "ad doesn't exist"
  105. redirect_to root_path
  106. else
  107. @ad.destroy
  108. flash[:notice] = "ad removed"
  109. redirect_to root_path
  110. end
  111. end
  112. def feed
  113. @ads = Ad.all_active
  114. respond_to do |format|
  115. format.rss { render :layout => false }
  116. format.atom # index.atom.builder
  117. end
  118. end
  119. end
  120.  
  121. # categories_controller.rb
  122.  
  123. class CategoriesController < ApplicationController
  124. def index
  125. @display_data = Category.display_paged_data(params[:page])
  126. @parents = ParentCategory.find(:all)
  127. @parent_categories = @parents.collect { |p| [p.name, p.id] }
  128. end
  129. def create
  130. if request.post? && params[:new_category] != ""
  131. begin
  132. @category = Category.new
  133. @category.name = params[:new_category]
  134. @category.slug = params[:new_category].split.join.downcase
  135. @category.parent_category_id = params[:ParentCategory][:id]
  136. @category.save!
  137. flash[:notice] = "new category created"
  138. end
  139. end
  140. redirect_to :action => 'category'
  141. end
  142. def update
  143. @category = Category.find_by_id(params[:id])
  144. if @category.nil?
  145. flash[:warning] = "invalid category"
  146. end
  147. if @category.update_attributes(params[:category])
  148. flash[:notice] = "category updated"
  149. else
  150. if params[:category].nil?
  151. flash[:warning] = "nothing to do"
  152. else
  153. flash[:warning] = "error updating category"
  154. end
  155. end
  156. redirect_to :action => 'category'
  157. end
  158. def destroy
  159. if request.post?
  160. category = Category.find(params[:id])
  161. category.destroy
  162. flash[:notice] = "category deleted"
  163. redirect_to :action => 'category'
  164. end
  165. redirect_to :action => 'category'
  166. end
  167. end
  168.  
  169. # parent_categories_controller.rb
  170.  
  171. class ParentCategoriesController < ApplicationController
  172. def show
  173. @display_data = ParentCategory.display_paged_data(params[:page])
  174. end
  175. def create
  176. if request.post? && params[:parent_category] != ""
  177. begin
  178. @parent = ParentCategory.new
  179. @parent.name = params[:parent_category]
  180. @parent.slug = params[:parent_category].split.join.downcase
  181. @parent.save!
  182. flash[:notice] = "parent category created"
  183. end
  184. end
  185. redirect_to :action => 'parent_category'
  186. end
  187. def update
  188. @parent_category = ParentCategory.find_by_id(params[:id])
  189. if @parent_category.nil?
  190. flash[:warning] = "invalid parent category"
  191. redirect_to :action => 'parent_category'
  192. end
  193. if @parent_category.update_attributes(params[:parent_category])
  194. flash[:notice] = "parent category updated"
  195. else
  196. if params[:parent_category].nil?
  197. flash[:warning] = "nothing to do"
  198. else
  199. flash[:warning] = "error updating parent category"
  200. end
  201. end
  202. redirect_to :action => 'parent_category'
  203. end
  204. def destroy
  205. if request.post?
  206. parent = ParentCategory.find(params[:id])
  207. parent.destroy
  208. flash[:notice] = "parent category deleted"
  209. redirect_to :action => 'parent_category'
  210. end
  211. end
  212. end
  213.  
  214. # routes.rb
  215.  
  216. ActionController::Routing::Routes.draw do |map|
  217. map.resources :sites, :moderatorships
  218. #
  219. # map.resources :topics
  220. #
  221. map.resources :forums, :has_many => :posts do |forum|
  222. forum.resources :topics do |topic|
  223. topic.resources :posts
  224. topic.resource :monitorship
  225. end
  226. forum.resources :posts
  227. end
  228. map.resources :ads, :collection => { :feed => :get }, :member => { :ad_activate => :put }
  229. map.resources :posts, :collection => { :search => :get }
  230. map.resources :users, :member => { :suspend => :put, :settings => :get, :unsuspend => :put, :purge => :delete }, :has_many => [:posts]
  231. map.activate '/activate/:user_activation_code', :controller => 'users', :action => 'user_activate', :user_activation_code => nil
  232. map.signup '/join', :controller => 'users', :action => 'new'
  233. map.login '/login', :controller => 'sessions', :action => 'new'
  234. map.logout '/logout', :controller => 'sessions', :action => 'destroy'
  235. map.settings '/settings', :controller => 'users', :action => 'settings'
  236. map.resource :session
  237. map.resource :pages
  238. map.slug ':slug', :controller => 'ads', :action => 'list'
  239. map.root :controller => 'main', :action => 'index'
  240. map.connect ':controller/:action/:id'
  241. map.connect ':controller/:action/:id.:format'
  242. end
Add Comment
Please, Sign In to add comment