Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Content:
- controllers/ads_controller.rb
- models/ad.rb
- models/category.rb
- models/parent_category.rb
- --
- controllers/ads_controller.rb
- class AdsController < ApplicationController
- # show one particular ad
- def show
- @ad = Ad.find_by_id(params[:id])
- if (@ad.nil?)
- flash[:warning] = 'Error - That Ad Does Not Exist'
- redirect_to root_path
- end
- end
- # destroy ad with that particular hash
- def destroy
- if request.post?
- @ad = Ad.find_by_activation_hash(params[:id])
- if (@ad.nil?)
- flash[:warning] = 'Error - That Ad Does Not Exist'
- redirect_to root_path
- # report to admin?
- else
- # destroy the ad
- @ad.destroy
- flash[:notice] = 'Your Ad Has Been Removed'
- redirect_to root_path
- end
- end
- end
- # show a list of ads in a category
- # we need a clever way to do this - if both parent and child categories
- # have a page slug how to identify one vs. the other? might be easier to
- # inherit then do this code checking tfor a blank array..
- def list
- @category = Category.find_by_slug(params[:slug])
- if @category
- @ads = @category.ads.all_active
- @requested_category = @category.parent_category.name + ' >> ' + @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
- # show parent category list for new ad
- def post
- @parents = ParentCategory.find :all, :order => 'name ASC'
- end
- # ajaxy function to show subcategories when they select a parent
- def select_category
- @parent_category = ParentCategory.find_by_id(params[:id])
- end
- # show the ajaxy form for a new ad
- def show_form
- @category = Category.find_by_id(params[:id])
- end
- # ajaxy - create new ad
- def new
- if (params[:email] != params[:email_verify])
- # return an error since email and confirmation don't match
- flash[:warning] = 'Email Address and Email Confirmation Do Not Match'
- #@category = params[:category]
- # TODO
- # can we somehow send them to the last screen?
- redirect_to :controller => 'ads', :action => 'post'
- else
- # email and confirmation match
- @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
- # record author IP address
- @ad.author_ip = request.env['REMOTE_ADDR']
- @ad.save
- # handle image attachments
- @ad.handle_images(params["image_attachments"])
- # send confirmation email with activation url
- Mailman.deliver_confirmation_email(@ad, @author.email)
- flash[:notice] = 'A Confirmation Email Has Been Sent To ' + @author.email
- end
- end
- # activate an ad that is new but not active yet
- def activate
- @ad = Ad.find_by_activation_hash(params[:activation_hash])
- if (@ad.nil?)
- flash[:warning] = 'Error Activating Your Ad'
- redirect_to root_path
- #report to admin?
- else
- # not sure we need this?
- #respond_to do |format|
- #redirect to confirmation thank you page
- if @ad.activate(params[:activation_hash])
- flash[:notice] = 'Your Ad Has Been Activated - An Email Has Been Sent To ' + @ad.author.email
- Mailman.deliver_activation_email(@ad, @ad.author.email)
- #redirect_to :action => 'show', :id => @ad
- redirect_to :action => 'edit', :activation_hash => @ad.activation_hash
- else
- flash[:warning] = 'Error Activating Your Ad'
- redirect_to root_path
- end
- #format.html {render :action => "confirmed"}
- #end
- end
- end
- # manage an ad based on the hash
- def manage
- @ad = Ad.find_by_activation_hash(params[:activation_hash])
- if (@ad.nil?)
- flash[:warning] = 'Error - That Ad Does Not Exist'
- redirect_to root_path
- # report to admin?
- else
- # show the ad and let them edit it
- # going to use our own rhtml with edit buttons etc.
- #render :action => 'show'
- end
- end
- # edit an ad based on the hash
- def edit
- @ad = Ad.find_by_activation_hash(params[:activation_hash])
- if (@ad.nil?)
- flash[:warning] = 'Error - That Ad Does Not Exist'
- redirect_to root_path
- # report to admin?
- else
- # show the ad and let them edit it
- end
- end
- # update an ad after someone edits (via the edit form) and hits 'submit'
- def update
- @ad = Ad.find_by_activation_hash(params[:activation_hash])
- if (@ad.nil?)
- flash[:warning] = 'Error - That Ad Does Not Exist'
- redirect_to root_path
- # report to admin?
- else
- @ad.ad = params[:ad].gsub("\n", "<br/>")
- @ad.title = params[:title]
- if @ad.save
- # handle image attachments
- @ad.handle_images(params["image_attachments"])
- flash[:notice] = "Ad Updated Successfully"
- else
- flash[:warning] = "Error Updating Ad"
- end
- redirect_to :controller => 'ads', :action => 'manage', :activation_hash => @ad.activation_hash
- end
- end
- def delete_image
- @ad = Ad.find_by_activation_hash(params[:activation_hash])
- @ad_image = @ad.ad_images.find_by_id(params[:id])
- if @ad_image.destroy
- flash[:notice] = "Ad Image Deleted"
- else
- flash[:warning] = "Unable to Find Ad Image to Delete"
- end
- redirect_to :controller => 'ads', :action => 'manage', :activation_hash => @ad.activation_hash
- end
- # rss feed
- def feed
- @ads = Ad.all_active
- respond_to do |format|
- format.rss { render :layout => false }
- format.atom # index.atom.builder
- end
- end
- end
- --
- models/ad.rb
- require 'uuid'
- class Ad < ActiveRecord::Base
- belongs_to :category
- belongs_to :author
- has_many :ad_images
- validate :expiration_is_set
- def handle_images(image_attachments)
- return unless image_attachments.respond_to? :each
- image_attachments.each do |image|
- ad_image = self.ad_images.build(:uploaded_data => image)
- ad_image.save
- end
- end
- def expiration_is_set
- if expiration.nil?
- expiration = Time.now + 30.days
- end
- end
- def expire!
- self.expiration = Time.now - 1.day
- self.save
- end
- def extend!
- self.expiration = Time.now + 30.days
- self.save
- end
- def reset!
- self.active = false
- self.save
- end
- def expired?
- self.expiration < Time.now
- end
- # TODO
- def self.fetch_by_category_slug(slug)
- end
- # activate an ad
- # pass the activation hash/params as "conf"
- # let the model validate it for us
- def activate(conf)
- if self.activation_hash == conf and !self.active?
- self.active = true
- if self.save
- return true
- else
- return false
- end
- else
- return false
- end
- end
- def self.all_active
- find(:all, :conditions => ['expiration > ? and active = ?', Time.now, true], :order => 'created_at ASC')
- end
- def self.display_paged_data(page)
- paginate(:page => page, :per_page => 10,:order => "id")
- end
- protected
- def before_create
- self.activation_hash = UUID.new
- #self.email = DateTime.now.to_s(:generate_hash) + rand(10000).to_s.rjust(5, '0') + "@chuckslist.com"
- self.email = DateTime.now.to_s(:generate_hash) + $generatedKeyCount.to_s.rjust(5, '0') + "@" + DOMAIN
- $generatedKeyCount = $generatedKeyCount + 1
- if ($generatedKeyCount > 99999)
- $generatedKeyCount = 1
- end
- end
- end
- --
- models/category.rb
- class Category < ActiveRecord::Base
- has_many :ads
- belongs_to :parent_category
- validates_presence_of :name
- validates_uniqueness_of :name, :case_sensitive => false
- validates_length_of :name, :within => 4..40
- validates_presence_of :slug
- validates_uniqueness_of :slug, :case_sensitive => false
- validates_length_of :slug, :within => 3..40
- def self.display_paged_data(page)
- paginate(:page => page, :per_page => 10,:order => "name")
- end
- end
- --
- models/parent_category.rb
- class ParentCategory < ActiveRecord::Base
- has_many :categories do
- def in_order
- find(:all, :order => 'name ASC')
- end
- end
- def all_ads
- @subcats = self.categories
- @results = []
- @subcats.each { |c| c.ads.all_active.each { |a| @results << a } }
- return @results
- end
- def self.display_paged_data(page)
- self.paginate(:page => page, :per_page => 10,:order => "name")
- end
- end
Add Comment
Please, Sign In to add comment