Guest User

Untitled

a guest
Apr 25th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.30 KB | None | 0 0
  1. ## model anuncio_banner_observer.rb
  2. class AnuncioBannerObserver < ActiveRecord::Observer
  3. observe :anuncio, :banner
  4.  
  5. def after_save(object)
  6.  
  7. verificar_banner(object) if object.is_a? Banner
  8. verificar_anuncio(object) if object.is_a? Anuncio
  9.  
  10. #TODO enviar e-mail dizendo se foi aprovado ou não o banner/anuncio
  11. end
  12.  
  13. private
  14. def verificar_banner(banner)
  15. Notificador.deliver_banner_aprovado banner.usuario if (banner.status_id == 1)
  16. Notificador.deliver_banner_reprovado banner.usuario if (banner.status_id == 2)
  17. end
  18.  
  19. def verificar_anuncio(anuncio)
  20. Notificador.deliver_anuncio_aprovado anuncio.usuario if (anuncio.status_id == 1)
  21. Notificador.deliver_anuncio_reprovado anuncio.usuario if (anuncio.status_id == 2)
  22. end
  23. end
  24.  
  25. ## model notificador.rb
  26. class Notificador < ActionMailer::Base
  27. # Anuncio
  28. def anuncio_aprovado( usuario )
  29. recipients usuario.email
  30. from "administrador@portaldacursino.com.br"
  31. subject "Seu anúncio foi aprovado!"
  32. body :usuario => usuario
  33. end
  34. def anuncio_reprovado( usuario )
  35. recipients usuario.email
  36. from "administrador@portaldacursino.com.br"
  37. subject "Seu anúncio foi reprovado!"
  38. body :usuario => usuario
  39. end
  40.  
  41. # Banner
  42. def banner_aprovado( usuario )
  43. recipients usuario.email
  44. from "administrador@portaldacursino.com.br"
  45. subject "Seu anúncio foi aprovado!"
  46. body :usuario => usuario
  47. end
  48. def banner_reprovado( usuario )
  49. recipients usuario.email
  50. from "administrador@portaldacursino.com.br"
  51. subject "Seu anúncio foi reprovado!"
  52. body :usuario => usuario
  53. end
  54.  
  55. # Newsletter
  56. def newsletter(email)
  57. news = Newsletter.find :first, :order => "id DESC"
  58.  
  59. dt_envio = news.dt_envio unless news.nil?
  60. dt_envio = Date.today if news.nil?
  61.  
  62. noticias = Noticia.find :all, :conditions => ["created_at >= ?", dt_envio], :order => "id"
  63. anuncios = Anuncio.find :all, :conditions => ["created_at >= ?", dt_envio], :order => "id"
  64.  
  65. recipients email
  66. #bcc email
  67. from "administrador@portaldacursino.com.br"
  68. subject "Newsletter - Portal da Cursino"
  69. body :noticias => noticias, :anuncios => anuncios
  70. end
  71. end
  72.  
  73. ## model upload_noticia.rb
  74. class UploadNoticia < ActiveRecord::Base
  75. belongs_to :noticia
  76. has_attachment :content_type => :image,
  77. :storage => :file_system,
  78. :max_size => 500.megabytes,
  79. :path_prefix => "public/images/#{table_name}"
  80.  
  81. validates_as_attachment
  82. end
  83.  
  84. ## model noticia.rb
  85. class Noticia < ActiveRecord::Base
  86. has_one :upload_noticia, :dependent => :destroy
  87.  
  88. def uploaded_data=(value)
  89. param = Hash.new
  90. param[:uploaded_data] = value
  91. upload = UploadNoticia.new(param)
  92. upload.save
  93. self.upload_noticia = upload
  94. end
  95. end
  96.  
  97. ## controller admin/noticias_controller.rb
  98. class Admin::NoticiasController < ApplicationController
  99. layout 'admin/application'
  100.  
  101. before_filter :authenticate
  102.  
  103. # GET /noticia
  104. # GET /noticia.xml
  105. def index
  106. @noticia = Noticia.find(:all)
  107.  
  108. respond_to do |format|
  109. format.html # index.html.erb
  110. format.xml { render :xml => @noticia }
  111. end
  112. end
  113.  
  114. # GET /noticia/1
  115. # GET /noticia/1.xml
  116. def show
  117. @noticia = Noticia.find(params[:id])
  118.  
  119. respond_to do |format|
  120. format.html # show.html.erb
  121. format.xml { render :xml => @noticia }
  122. end
  123. end
  124.  
  125. # GET /noticia/new
  126. # GET /noticia/new.xml
  127. def new
  128. @noticia = Noticia.new
  129. # -- Samuel --
  130. @upload = UploadNoticia.new
  131. # --
  132. respond_to do |format|
  133. format.html # new.html.erb
  134. format.xml { render :xml => @noticia }
  135. end
  136. end
  137.  
  138. # GET /noticia/1/edit
  139. def edit
  140. @noticia = Noticia.find(params[:id])
  141. end
  142.  
  143. # POST /noticia
  144. # POST /noticia.xml
  145. def create
  146. @noticia = Noticia.new(params[:noticia])
  147.  
  148. respond_to do |format|
  149. if @noticia.save
  150. flash[:notice] = 'Notícia foi criada com sucesso.'
  151. format.html { redirect_to([:admin, @noticia]) }
  152. format.xml { render :xml => @noticia, :status => :created, :location => @noticia }
  153. else
  154. format.html { render :action => "new" }
  155. format.xml { render :xml => @noticia.errors, :status => :unprocessable_entity }
  156. end
  157. end
  158. end
  159.  
  160. # PUT /noticia/1
  161. # PUT /noticia/1.xml
  162. def update
  163. @noticia = Noticia.find(params[:id])
  164.  
  165. respond_to do |format|
  166. if @noticia.update_attributes(params[:noticia])
  167. flash[:notice] = 'Notícia foi alterada com sucesso.'
  168. format.html { redirect_to(admin_noticias_url) }
  169. format.xml { head :ok }
  170. else
  171. format.html { render :action => "edit" }
  172. format.xml { render :xml => @noticia.errors, :status => :unprocessable_entity }
  173. end
  174. end
  175. end
  176.  
  177. # DELETE /noticia/1
  178. # DELETE /noticia/1.xml
  179. def destroy
  180. @noticia = Noticia.find(params[:id])
  181. @noticia.destroy
  182.  
  183. respond_to do |format|
  184. format.html { redirect_to(admin_noticias_url) }
  185. format.xml { head :ok }
  186. end
  187. end
  188. end
  189.  
  190. ## view admin/noticias/show.html.erb [html_rails]
  191. <p>
  192. <b>Titulo:</b>
  193. <%=h @noticia.titulo %>
  194. </p>
  195.  
  196. <p>
  197. <b>Texto:</b>
  198. <%= @noticia.texto %>
  199. </p>
  200.  
  201. <p>
  202. <b>Imagem:</b>
  203. <%=image_tag @noticia.upload_noticia.public_filename %>
  204. </p>
  205.  
  206. <p>
  207. <b>Data de Criação:</b>
  208. <%=h @noticia.created_at.to_s_br %>
  209. </p>
  210.  
  211. <p>
  212. <b>Autor:</b>
  213. <%=h @noticia.autor %>
  214. </p>
  215.  
  216.  
  217. <%= link_to 'Editar', edit_admin_noticia_path(@noticia) %> |
  218. <%= link_to 'Voltar', admin_noticias_path %>
  219.  
  220.  
  221. ## controller application.rb
  222. class ApplicationController < ActionController::Base
  223. helper :all # include all helpers, all the time
  224.  
  225. protect_from_forgery # :secret => 'bd9cd932112886eb636f8a3043d7430c'
  226.  
  227. private
  228. def authenticate
  229. unless session[:admin_login] == true
  230. authenticate_or_request_with_http_basic do |name, pass|
  231. admin_id = Admin.exists? name, pass
  232. unless admin_id.nil?
  233. session[:admin_login] = true;
  234. session[:admin_id] = admin_id;
  235. true
  236. else
  237. session[:admin_login] = false;
  238. session[:admin_id] = nil;
  239. false
  240. end
  241. end
  242. end
  243. end
  244.  
  245. end
Add Comment
Please, Sign In to add comment