Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1. ads_controller.rb
- 2. categories_controller.rb
- 3. parent_categories_controller.rb
- 4. routes.rb
- # 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
- @parents = ParentCategory.find :all, :order => 'name ASC'
- end
- def create
- 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.images(params["image_attachments"])
- Mail.deliver_activation(@ad, @author.email)
- flash[:notice] = "ad pending activation"
- end
- end
- def ad_activate
- @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
- 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.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
- # categories_controller.rb
- class CategoriesController < ApplicationController
- def index
- @display_data = Category.display_paged_data(params[:page])
- @parents = ParentCategory.find(:all)
- @parent_categories = @parents.collect { |p| [p.name, p.id] }
- end
- def create
- if request.post? && params[:new_category] != ""
- begin
- @category = Category.new
- @category.name = params[:new_category]
- @category.slug = params[:new_category].split.join.downcase
- @category.parent_category_id = params[:ParentCategory][:id]
- @category.save!
- flash[:notice] = "new category created"
- end
- end
- redirect_to :action => 'category'
- end
- def update
- @category = Category.find_by_id(params[:id])
- if @category.nil?
- flash[:warning] = "invalid category"
- end
- if @category.update_attributes(params[:category])
- flash[:notice] = "category updated"
- else
- if params[:category].nil?
- flash[:warning] = "nothing to do"
- else
- flash[:warning] = "error updating category"
- end
- end
- redirect_to :action => 'category'
- end
- def destroy
- if request.post?
- category = Category.find(params[:id])
- category.destroy
- flash[:notice] = "category deleted"
- redirect_to :action => 'category'
- end
- redirect_to :action => 'category'
- end
- end
- # parent_categories_controller.rb
- class ParentCategoriesController < ApplicationController
- def show
- @display_data = ParentCategory.display_paged_data(params[:page])
- end
- def create
- if request.post? && params[:parent_category] != ""
- begin
- @parent = ParentCategory.new
- @parent.name = params[:parent_category]
- @parent.slug = params[:parent_category].split.join.downcase
- @parent.save!
- flash[:notice] = "parent category created"
- end
- end
- redirect_to :action => 'parent_category'
- end
- def update
- @parent_category = ParentCategory.find_by_id(params[:id])
- if @parent_category.nil?
- flash[:warning] = "invalid parent category"
- redirect_to :action => 'parent_category'
- end
- if @parent_category.update_attributes(params[:parent_category])
- flash[:notice] = "parent category updated"
- else
- if params[:parent_category].nil?
- flash[:warning] = "nothing to do"
- else
- flash[:warning] = "error updating parent category"
- end
- end
- redirect_to :action => 'parent_category'
- end
- def destroy
- if request.post?
- parent = ParentCategory.find(params[:id])
- parent.destroy
- flash[:notice] = "parent category deleted"
- redirect_to :action => 'parent_category'
- end
- end
- end
- # routes.rb
- ActionController::Routing::Routes.draw do |map|
- map.resources :sites, :moderatorships
- #
- # map.resources :topics
- #
- map.resources :forums, :has_many => :posts do |forum|
- forum.resources :topics do |topic|
- topic.resources :posts
- topic.resource :monitorship
- end
- forum.resources :posts
- end
- map.resources :ads, :collection => { :feed => :get }, :member => { :ad_activate => :put }
- map.resources :posts, :collection => { :search => :get }
- map.resources :users, :member => { :suspend => :put, :settings => :get, :unsuspend => :put, :purge => :delete }, :has_many => [:posts]
- map.activate '/activate/:user_activation_code', :controller => 'users', :action => 'user_activate', :user_activation_code => nil
- map.signup '/join', :controller => 'users', :action => 'new'
- map.login '/login', :controller => 'sessions', :action => 'new'
- map.logout '/logout', :controller => 'sessions', :action => 'destroy'
- map.settings '/settings', :controller => 'users', :action => 'settings'
- map.resource :session
- map.resource :pages
- map.slug ':slug', :controller => 'ads', :action => 'list'
- map.root :controller => 'main', :action => 'index'
- map.connect ':controller/:action/:id'
- map.connect ':controller/:action/:id.:format'
- end
Add Comment
Please, Sign In to add comment