Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## Model
- class User < ActiveRecord::Base
- has_many :comments
- has_many :entries
- validates_presence_of :name
- def before_validation
- if self.name.blank? then
- self.name = "Anonymous"
- end
- end
- def after_validation_on_create # encrypt tripcode, try to retrieve user with name / tripcode before saving a new one
- self.crypted_tripcode = entrip(self.crypted_tripcode)
- end
- def entrip(key)
- if key.blank? then return "" end
- key = CGI.escapeHTML(key)
- key += "H.."
- salt = key[1,2]
- return key.crypt(salt)[3..12]
- end
- def anonymous?
- if self.crypted_tripcode == ""
- return true
- else
- return false
- end
- end
- def display_name
- if anonymous?
- return self.name
- else
- return self.name + "◆" + self.crypted_tripcode
- end
- end
- end
- ## Controller
- class CommentsController < ApplicationController
- before_filter :select_user
- before_filter :get_entry
- # GET /comments
- # GET /comments.xml
- def index
- @comments = Comment.all(:conditions => {:entry_id => @entry.id, :parent_id => nil})
- respond_to do |format|
- format.html # index.html.erb
- format.xml { render :xml => @comments }
- end
- end
- # GET /comments/1
- # GET /comments/1.xml
- def show
- @comment = Comment.find(params[:id])
- respond_to do |format|
- format.html # show.html.erb
- format.xml { render :xml => @comment }
- end
- end
- def reply
- @comment = Comment.new
- @comment.parent_id = params[:id]
- @comment.user = @user
- @comment.entry = @entry
- @parent = Comment.find(params[:id])
- respond_to do |format|
- format.html
- format.xml { render :xml => @comment}
- end
- end
- # GET /comments/new
- # GET /comments/new.xml
- def new
- @comment = Comment.new
- @comment.user = @user
- @comment.entry = @entry
- respond_to do |format|
- format.html # new.html.erb
- format.xml { render :xml => @comment }
- end
- end
- # GET /comments/1/edit
- def edit
- @comment = Comment.find(params[:id])
- end
- # POST /comments
- # POST /comments.xml
- def create
- @comment = Comment.new(params[:comment])
- @potentially_new_user = User.new(params[:user])
- @user = User.find(:first, :conditions => {:name => @potentially_new_user.name, :crypted_tripcode => User.entrip(@potentially_new_user.crypted_tripcode)})
- @comment.user = @user || @potentially_new_user
- @comment.entry = @entry
- respond_to do |format|
- if @comment.save
- session[:user] = @comment.user.id
- flash[:notice] = 'Comment was successfully created.'
- format.html { redirect_to([@entry,@comment]) }
- format.xml { render :xml => @comment, :status => :created, :location => @comment }
- else
- format.html { render :action => "new" }
- format.xml { render :xml => @comment.errors, :status => :unprocessable_entity }
- end
- end
- end
- # PUT /comments/1
- # PUT /comments/1.xml
- def update
- @comment = Comment.find(params[:id])
- respond_to do |format|
- if @comment.update_attributes(params[:comment])
- flash[:notice] = 'Comment was successfully updated.'
- format.html { redirect_to(@comment) }
- format.xml { head :ok }
- else
- format.html { render :action => "edit" }
- format.xml { render :xml => @comment.errors, :status => :unprocessable_entity }
- end
- end
- end
- # DELETE /comments/1
- # DELETE /comments/1.xml
- def destroy
- @comment = Comment.find(params[:id])
- @comment.destroy
- respond_to do |format|
- format.html { redirect_to(comments_url(@entry)) }
- format.xml { head :ok }
- end
- end
- def select_user
- @user = User.find_by_id(session[:user]) || User.new
- end
- def get_entry
- @entry = Entry.find(params[:entry_id])
- end
- end
Add Comment
Please, Sign In to add comment