Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # ads_controller.rb
- class AdsController < ApplicationController
- def index
- @category = Category.find_by_slug(params[:slug])
- if @category
- @ads = @category.ads.all_active
- @requested_category = @category.name + ' in ' + @category.parent_category.name
- else
- @category = ParentCategory.find_by_slug(params[:slug])
- if @category
- @ads = @category.all_ads
- @requested_category = @category.name
- else
- flash[:warning] = "invalid request"
- redirect_to root_path
- end
- end
- end
- def show
- @ad = Ad.find_by_id(params[:id])
- if (@ad.nil?)
- flash[:warning] = "ad doesn't exist"
- redirect_to root_path
- end
- end
- def new
- if (params[:email] != params[:email_confirmation])
- flash[:warning] = "e-mails don't match"
- redirect_to create_ads_path
- else
- @author = Author.find_by_email(params[:email])
- if @author.blank?
- @author = Author.new
- @author.email = params[:email]
- @author.ip = request.env['REMOTE_ADDR']
- @author.save
- end
- @ad = Category.find_by_id(params[:category]).ads.new
- @ad.title = params[:title]
- @ad.ad = params[:ad].gsub("\n", "<br />")
- @ad.expiration = Time.now + 30.days
- @ad.author = @author
- @ad.author_ip = request.env['REMOTE_ADDR']
- @ad.save
- @ad.handle_images(params["image_attachments"])
- Mail.deliver_activation(@ad, @author.email)
- flash[:notice] = "ad pending activation"
- end
- end
- def create
- @parents = ParentCategory.find :all, :order => 'name ASC'
- end
- def edit
- @ad = Ad.find_by_activation_code(params[:activation_code])
- if (@ad.nil?)
- flash[:warning] = "ad doesn't exist"
- redirect_to root_path
- else
- end
- end
- def update
- @ad = Ad.find_by_activation_code(params[:activation_code])
- if (@ad.nil?)
- flash[:warning] = "ad doesn't exist"
- redirect_to root_path
- else
- @ad.ad = params[:ad].gsub("\n", "<br />")
- @ad.title = params[:title]
- if @ad.save
- @ad.handle_images(params["image_attachments"])
- flash[:notice] = "ad updated"
- else
- flash[:warning] = "error updating ad"
- end
- redirect_to edit_ads_path, :activation_code => @ad.activation_code
- end
- end
- def destroy
- @ad = Ad.find_by_activation_code(params[:id])
- if (@ad.nil?)
- flash[:warning] = "ad doesn't exist"
- redirect_to root_path
- else
- @ad.destroy
- flash[:notice] = "ad removed"
- redirect_to root_path
- end
- end
- def feed
- @ads = Ad.all_active
- respond_to do |format|
- format.rss { render :layout => false }
- format.atom # index.atom.builder
- end
- end
- end
- # ads_activation_controller.rb
- class AdsActivationController < ApplicationController
- def new
- @ad = Ad.find_by_activation_code(params[:activation_code])
- if (@ad.nil?)
- flash[:warning] = "error activating ad"
- redirect_to root_path
- else
- if @ad.activate_ad(params[:activation_code])
- flash[:notice] = "ad activated"
- Mail.deliver_activated(@ad, @ad.author.email)
- redirect_to :action => 'edit', :activation_code => @ad.activation_code
- else
- flash[:warning] = "error activating ad"
- redirect_to root_path
- end
- end
- end
- end
Add Comment
Please, Sign In to add comment