Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## model anuncio_banner_observer.rb
- class AnuncioBannerObserver < ActiveRecord::Observer
- observe :anuncio, :banner
- def after_save(object)
- verificar_banner(object) if object.is_a? Banner
- verificar_anuncio(object) if object.is_a? Anuncio
- #TODO enviar e-mail dizendo se foi aprovado ou não o banner/anuncio
- end
- private
- def verificar_banner(banner)
- Notificador.deliver_banner_aprovado banner.usuario if (banner.status_id == 1)
- Notificador.deliver_banner_reprovado banner.usuario if (banner.status_id == 2)
- end
- def verificar_anuncio(anuncio)
- Notificador.deliver_anuncio_aprovado anuncio.usuario if (anuncio.status_id == 1)
- Notificador.deliver_anuncio_reprovado anuncio.usuario if (anuncio.status_id == 2)
- end
- end
- ## model notificador.rb
- class Notificador < ActionMailer::Base
- # Anuncio
- def anuncio_aprovado( usuario )
- recipients usuario.email
- from "administrador@portaldacursino.com.br"
- subject "Seu anúncio foi aprovado!"
- body :usuario => usuario
- end
- def anuncio_reprovado( usuario )
- recipients usuario.email
- from "administrador@portaldacursino.com.br"
- subject "Seu anúncio foi reprovado!"
- body :usuario => usuario
- end
- # Banner
- def banner_aprovado( usuario )
- recipients usuario.email
- from "administrador@portaldacursino.com.br"
- subject "Seu anúncio foi aprovado!"
- body :usuario => usuario
- end
- def banner_reprovado( usuario )
- recipients usuario.email
- from "administrador@portaldacursino.com.br"
- subject "Seu anúncio foi reprovado!"
- body :usuario => usuario
- end
- # Newsletter
- def newsletter(email)
- news = Newsletter.find :first, :order => "id DESC"
- dt_envio = news.dt_envio unless news.nil?
- dt_envio = Date.today if news.nil?
- noticias = Noticia.find :all, :conditions => ["created_at >= ?", dt_envio], :order => "id"
- anuncios = Anuncio.find :all, :conditions => ["created_at >= ?", dt_envio], :order => "id"
- recipients email
- #bcc email
- from "administrador@portaldacursino.com.br"
- subject "Newsletter - Portal da Cursino"
- body :noticias => noticias, :anuncios => anuncios
- end
- end
- ## model upload_noticia.rb
- class UploadNoticia < ActiveRecord::Base
- belongs_to :noticia
- has_attachment :content_type => :image,
- :storage => :file_system,
- :max_size => 500.megabytes,
- :path_prefix => "public/images/#{table_name}"
- validates_as_attachment
- end
- ## model noticia.rb
- class Noticia < ActiveRecord::Base
- has_one :upload_noticia, :dependent => :destroy
- def uploaded_data=(value)
- param = Hash.new
- param[:uploaded_data] = value
- upload = UploadNoticia.new(param)
- upload.save
- self.upload_noticia = upload
- end
- end
- ## controller admin/noticias_controller.rb
- class Admin::NoticiasController < ApplicationController
- layout 'admin/application'
- before_filter :authenticate
- # GET /noticia
- # GET /noticia.xml
- def index
- @noticia = Noticia.find(:all)
- respond_to do |format|
- format.html # index.html.erb
- format.xml { render :xml => @noticia }
- end
- end
- # GET /noticia/1
- # GET /noticia/1.xml
- def show
- @noticia = Noticia.find(params[:id])
- respond_to do |format|
- format.html # show.html.erb
- format.xml { render :xml => @noticia }
- end
- end
- # GET /noticia/new
- # GET /noticia/new.xml
- def new
- @noticia = Noticia.new
- # -- Samuel --
- @upload = UploadNoticia.new
- # --
- respond_to do |format|
- format.html # new.html.erb
- format.xml { render :xml => @noticia }
- end
- end
- # GET /noticia/1/edit
- def edit
- @noticia = Noticia.find(params[:id])
- end
- # POST /noticia
- # POST /noticia.xml
- def create
- @noticia = Noticia.new(params[:noticia])
- respond_to do |format|
- if @noticia.save
- flash[:notice] = 'NotÃcia foi criada com sucesso.'
- format.html { redirect_to([:admin, @noticia]) }
- format.xml { render :xml => @noticia, :status => :created, :location => @noticia }
- else
- format.html { render :action => "new" }
- format.xml { render :xml => @noticia.errors, :status => :unprocessable_entity }
- end
- end
- end
- # PUT /noticia/1
- # PUT /noticia/1.xml
- def update
- @noticia = Noticia.find(params[:id])
- respond_to do |format|
- if @noticia.update_attributes(params[:noticia])
- flash[:notice] = 'NotÃcia foi alterada com sucesso.'
- format.html { redirect_to(admin_noticias_url) }
- format.xml { head :ok }
- else
- format.html { render :action => "edit" }
- format.xml { render :xml => @noticia.errors, :status => :unprocessable_entity }
- end
- end
- end
- # DELETE /noticia/1
- # DELETE /noticia/1.xml
- def destroy
- @noticia = Noticia.find(params[:id])
- @noticia.destroy
- respond_to do |format|
- format.html { redirect_to(admin_noticias_url) }
- format.xml { head :ok }
- end
- end
- end
- ## view admin/noticias/show.html.erb [html_rails]
- <p>
- <b>Titulo:</b>
- <%=h @noticia.titulo %>
- </p>
- <p>
- <b>Texto:</b>
- <%= @noticia.texto %>
- </p>
- <p>
- <b>Imagem:</b>
- <%=image_tag @noticia.upload_noticia.public_filename %>
- </p>
- <p>
- <b>Data de Criação:</b>
- <%=h @noticia.created_at.to_s_br %>
- </p>
- <p>
- <b>Autor:</b>
- <%=h @noticia.autor %>
- </p>
- <%= link_to 'Editar', edit_admin_noticia_path(@noticia) %> |
- <%= link_to 'Voltar', admin_noticias_path %>
- ## controller application.rb
- class ApplicationController < ActionController::Base
- helper :all # include all helpers, all the time
- protect_from_forgery # :secret => 'bd9cd932112886eb636f8a3043d7430c'
- private
- def authenticate
- unless session[:admin_login] == true
- authenticate_or_request_with_http_basic do |name, pass|
- admin_id = Admin.exists? name, pass
- unless admin_id.nil?
- session[:admin_login] = true;
- session[:admin_id] = admin_id;
- true
- else
- session[:admin_login] = false;
- session[:admin_id] = nil;
- false
- end
- end
- end
- end
- end
Add Comment
Please, Sign In to add comment