Guest User

Untitled

a guest
Jul 17th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.88 KB | None | 0 0
  1. ## Model
  2.  
  3. class User < ActiveRecord::Base
  4. has_many :comments
  5. has_many :entries
  6.  
  7. validates_presence_of :name
  8.  
  9. def before_validation
  10. if self.name.blank? then
  11. self.name = "Anonymous"
  12. end
  13. end
  14.  
  15. def after_validation_on_create # encrypt tripcode, try to retrieve user with name / tripcode before saving a new one
  16. self.crypted_tripcode = entrip(self.crypted_tripcode)
  17. end
  18.  
  19. def entrip(key)
  20. if key.blank? then return "" end
  21. key = CGI.escapeHTML(key)
  22. key += "H.."
  23. salt = key[1,2]
  24. return key.crypt(salt)[3..12]
  25. end
  26.  
  27. def anonymous?
  28. if self.crypted_tripcode == ""
  29. return true
  30. else
  31. return false
  32. end
  33. end
  34.  
  35. def display_name
  36. if anonymous?
  37. return self.name
  38. else
  39. return self.name + "◆" + self.crypted_tripcode
  40. end
  41. end
  42. end
  43.  
  44.  
  45. ## Controller
  46.  
  47. class CommentsController < ApplicationController
  48. before_filter :select_user
  49. before_filter :get_entry
  50. # GET /comments
  51. # GET /comments.xml
  52. def index
  53. @comments = Comment.all(:conditions => {:entry_id => @entry.id, :parent_id => nil})
  54.  
  55. respond_to do |format|
  56. format.html # index.html.erb
  57. format.xml { render :xml => @comments }
  58. end
  59. end
  60.  
  61. # GET /comments/1
  62. # GET /comments/1.xml
  63. def show
  64. @comment = Comment.find(params[:id])
  65.  
  66. respond_to do |format|
  67. format.html # show.html.erb
  68. format.xml { render :xml => @comment }
  69. end
  70. end
  71.  
  72. def reply
  73. @comment = Comment.new
  74. @comment.parent_id = params[:id]
  75. @comment.user = @user
  76. @comment.entry = @entry
  77.  
  78. @parent = Comment.find(params[:id])
  79.  
  80. respond_to do |format|
  81. format.html
  82. format.xml { render :xml => @comment}
  83. end
  84. end
  85.  
  86. # GET /comments/new
  87. # GET /comments/new.xml
  88. def new
  89. @comment = Comment.new
  90. @comment.user = @user
  91. @comment.entry = @entry
  92.  
  93. respond_to do |format|
  94. format.html # new.html.erb
  95. format.xml { render :xml => @comment }
  96. end
  97. end
  98.  
  99. # GET /comments/1/edit
  100. def edit
  101. @comment = Comment.find(params[:id])
  102. end
  103.  
  104. # POST /comments
  105. # POST /comments.xml
  106. def create
  107. @comment = Comment.new(params[:comment])
  108. @potentially_new_user = User.new(params[:user])
  109. @user = User.find(:first, :conditions => {:name => @potentially_new_user.name, :crypted_tripcode => User.entrip(@potentially_new_user.crypted_tripcode)})
  110. @comment.user = @user || @potentially_new_user
  111. @comment.entry = @entry
  112.  
  113. respond_to do |format|
  114. if @comment.save
  115. session[:user] = @comment.user.id
  116. flash[:notice] = 'Comment was successfully created.'
  117. format.html { redirect_to([@entry,@comment]) }
  118. format.xml { render :xml => @comment, :status => :created, :location => @comment }
  119. else
  120. format.html { render :action => "new" }
  121. format.xml { render :xml => @comment.errors, :status => :unprocessable_entity }
  122. end
  123. end
  124. end
  125.  
  126. # PUT /comments/1
  127. # PUT /comments/1.xml
  128. def update
  129. @comment = Comment.find(params[:id])
  130.  
  131. respond_to do |format|
  132. if @comment.update_attributes(params[:comment])
  133. flash[:notice] = 'Comment was successfully updated.'
  134. format.html { redirect_to(@comment) }
  135. format.xml { head :ok }
  136. else
  137. format.html { render :action => "edit" }
  138. format.xml { render :xml => @comment.errors, :status => :unprocessable_entity }
  139. end
  140. end
  141. end
  142.  
  143. # DELETE /comments/1
  144. # DELETE /comments/1.xml
  145. def destroy
  146. @comment = Comment.find(params[:id])
  147. @comment.destroy
  148.  
  149. respond_to do |format|
  150. format.html { redirect_to(comments_url(@entry)) }
  151. format.xml { head :ok }
  152. end
  153. end
  154.  
  155. def select_user
  156. @user = User.find_by_id(session[:user]) || User.new
  157. end
  158. def get_entry
  159. @entry = Entry.find(params[:entry_id])
  160. end
  161.  
  162. end
Add Comment
Please, Sign In to add comment