From 8d0139caec38a8b6d7933e6147f3e4b954bd556d Mon Sep 17 00:00:00 2001 From: Nicolas Bally Date: Mon, 13 Apr 2015 16:31:15 +0200 Subject: [PATCH] inscription newsletters --- app/assets/stylesheets/public.scss | 52 ++++++++++++ .../public/registrants_controller.rb | 49 +++++++++++ app/mailers/general.rb | 29 +++++++ app/models/newsletter.rb | 13 +++ app/models/registrant.rb | 20 +++++ app/views/general/confirm_email.html.erb | 82 +++++++++++++++++++ app/views/general/send_newsletter.html.erb | 76 +++++++++++++++++ app/views/layouts/public.html.haml | 10 ++- app/views/public/comments/_form.html.haml | 8 +- app/views/public/registrants/_form.html.haml | 34 ++++++++ app/views/public/registrants/create.js.erb | 1 + .../public/registrants/destroy_e.html.haml | 2 + .../registrants/email_validation.html.haml | 2 + app/views/public/registrants/new.html.haml | 0 app/views/public/registrants/new.js.erb | 1 + config/routes.rb | 7 ++ .../20140710140837_create_registrants.rb | 13 +++ .../20140710144544_create_newsletters.rb | 12 +++ db/schema.rb | 19 +++++ 19 files changed, 424 insertions(+), 6 deletions(-) create mode 100644 app/controllers/public/registrants_controller.rb create mode 100644 app/mailers/general.rb create mode 100644 app/models/newsletter.rb create mode 100644 app/models/registrant.rb create mode 100644 app/views/general/confirm_email.html.erb create mode 100644 app/views/general/send_newsletter.html.erb create mode 100644 app/views/public/registrants/_form.html.haml create mode 100644 app/views/public/registrants/create.js.erb create mode 100644 app/views/public/registrants/destroy_e.html.haml create mode 100644 app/views/public/registrants/email_validation.html.haml create mode 100644 app/views/public/registrants/new.html.haml create mode 100644 app/views/public/registrants/new.js.erb create mode 100644 db/migrate/20140710140837_create_registrants.rb create mode 100644 db/migrate/20140710144544_create_newsletters.rb diff --git a/app/assets/stylesheets/public.scss b/app/assets/stylesheets/public.scss index d50fae8..885f0e0 100644 --- a/app/assets/stylesheets/public.scss +++ b/app/assets/stylesheets/public.scss @@ -146,5 +146,57 @@ article{ background: rgba(250,250,250,1); margin-bottom:20px; +} +.share_in_social{ + text-align:center; + margin:1em; + margin-top:2em; + border-top:1px solid rgba(250,250,250,1); + border-bottom:1px solid rgba(250,250,250,1); + padding:10px; + +} +.btn{ + font-family:"jaf-bernino-sans", sans-serif; + font-size:1em; + padding:8px 15px; + border-radius:5px; + border:0; + color:black; + background:transparent; + cursor:pointer; + } +.input_text, textarea{ + font-family:"jaf-bernino-sans", sans-serif; + font-size:0.9em; + padding:8px 15px; + border-radius:5px; + border:0; + color:black; + background:white; + border:1px solid rgba(131,131,131,1); + + +} +.btn-primary{ + + background:rgba(64,138,199,1); + color:white; + &:hover{ + background:#357ebd;; + + + } + + +} + +.infos{ + margin-top:0; + font-size:14px; + margin-bottom:1.5em; + color:#8D8D8D; + +} \ No newline at end of file diff --git a/app/controllers/public/registrants_controller.rb b/app/controllers/public/registrants_controller.rb new file mode 100644 index 0000000..685c619 --- /dev/null +++ b/app/controllers/public/registrants_controller.rb @@ -0,0 +1,49 @@ +# -*- encoding : utf-8 -*- + +class Public::RegistrantsController < ApplicationController + + layout "public" + + + + def create + + @registrant = Registrant.new(params.require(:registrant).permit(:surname, :email)) + + test = Registrant.find_by_email(@registrant.email) + if test + @registrant = test + General.confirm_email(@registrant).deliver + else + + if @registrant.save + General.confirm_email(@registrant).deliver + else + render :action => :new + end + end + end + + def destroy_e + @registrant = Registrant.find_by_token(params[:id]) + if @registrant + @registrant.destroy + @message = "

Vous avez bien été désinscrit.

" + else + @message = "Votre adresse mail ne figure pas dans notre fichier." + end + end + + def email_validation + @registrant = Registrant.find_by_token(params[:id]) + if @registrant + @registrant.enabled = true + @registrant.save + @message = "

Merci, votre inscription a bien été prise en compte.

" + else + @message = "Votre adresse mail ne figure pas dans le fichier." + end + end + + +end diff --git a/app/mailers/general.rb b/app/mailers/general.rb new file mode 100644 index 0000000..5ffe93c --- /dev/null +++ b/app/mailers/general.rb @@ -0,0 +1,29 @@ +class General < ActionMailer::Base + default :from => "Nicolas Bally " + self.default_url_options = {:host => HOSTNAME} + + + def send_newsletter(email,newsletter ) + @newsletter = newsletter + if email.kind_of?(String) + email = email + else + @registrant = email + email = email.email + + end + mail(:to => email, :subject => @newsletter.subject) do |format| + format.html { render :action => "send_newsletter"} + end + end + + def confirm_email(registrant) + @registrant = registrant + + mail(:to => @registrant.email, :subject => "Confirmation de votre adresse email.") do |format| + format.html { render :action => "confirm_email"} + end + + end + +end diff --git a/app/models/newsletter.rb b/app/models/newsletter.rb new file mode 100644 index 0000000..96b28f3 --- /dev/null +++ b/app/models/newsletter.rb @@ -0,0 +1,13 @@ +class Newsletter < ActiveRecord::Base + after_create :after_creation + has_one :block, :as => :blockable + + def after_creation + @block = Block.new(:block_name => "Contenu") + @block.blockable = self + @block.save + + + end + +end diff --git a/app/models/registrant.rb b/app/models/registrant.rb new file mode 100644 index 0000000..dac1dbd --- /dev/null +++ b/app/models/registrant.rb @@ -0,0 +1,20 @@ +class Registrant < ActiveRecord::Base + + before_validation :verify + + + validates :email, :presence => true, :uniqueness => true, :format => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i + validates :surname, :presence => true + + + protected + + def verify(size=16) + if !self.token + s = "" + size.times { s << (i = Kernel.rand(62); i += ((i < 10) ? 48 : ((i < 36) ? 55 : 61 ))).chr } + self.token = s + end + end + +end diff --git a/app/views/general/confirm_email.html.erb b/app/views/general/confirm_email.html.erb new file mode 100644 index 0000000..ed4f5d3 --- /dev/null +++ b/app/views/general/confirm_email.html.erb @@ -0,0 +1,82 @@ + + + + + + + + +
+ <%= image_tag "http://blog.nicolasbally.com/logo.png", :style => "max-width: 300px;" %> + +
+
+ Merci pour votre inscription ! +

+ En cliquant sur le lien ci-dessous vous confirmerez votre demande d'inscription à la newsletter. +

+ Votre adresse ne sera jamais cédée à des tiers. +
+
+ <%=link_to email_validation_public_registrant_url(:id => @registrant.token) , email_validation_public_registrant_url(:id => @registrant.token) %> +
+
+ (Si vous ne pouvez pas cliquer sur ce lien, merci de bien vouloir le copier et le coller dans la barre d'adresse de votre navigateur.) + + + +
+ +
+
+ + + diff --git a/app/views/general/send_newsletter.html.erb b/app/views/general/send_newsletter.html.erb new file mode 100644 index 0000000..6700865 --- /dev/null +++ b/app/views/general/send_newsletter.html.erb @@ -0,0 +1,76 @@ + + + +La cabane du Lutin + + + + + +
+<%= image_tag "http://blog.nicolasbally.com/logo.png", :style => "max-width: 300px;" %> +
+
+<%= render :object => @newsletter.block, :partial => "public/blocks/block" %> + + + +
+
+
+Vous recevez cet email car vous êtes abonné à la newsletter du blog http://blog.nicolasbally.com. Si vous ne souhaitez plus recevoir de mail de notre part, merci de cliquer sur ce lien : <%=link_to destroy_e_public_registrant_url(:id => @registrant.token), destroy_e_public_registrant_url(:id => @registrant.token) if @registrant %> +
+
+
+ + + + \ No newline at end of file diff --git a/app/views/layouts/public.html.haml b/app/views/layouts/public.html.haml index 9420681..891c6d8 100644 --- a/app/views/layouts/public.html.haml +++ b/app/views/layouts/public.html.haml @@ -51,9 +51,15 @@ #corps{:class => ("article_corps" if @article)}=yield %section#sidebar + + + .newsletter + %h3.sidebar_title Restez informés ! + + =render :partial => "public/registrants/form" + =render :partial => "public/articles/sidebar_recents" - - + %h3.sidebar_title Mes activités diff --git a/app/views/public/comments/_form.html.haml b/app/views/public/comments/_form.html.haml index 3dc94b8..e23626d 100644 --- a/app/views/public/comments/_form.html.haml +++ b/app/views/public/comments/_form.html.haml @@ -10,17 +10,17 @@ %p =f.label :pseudo, "Pseudo :" - =f.text_field :pseudo + =f.text_field :pseudo, :class => "input_text" %p =f.label :email, "Email :" - =f.text_field :email + =f.text_field :email, :class => "input_text" %p =f.label :website, "Site :" - =f.text_field :website + =f.text_field :website, :class => "input_text" %p @@ -30,4 +30,4 @@ - =f.submit "Envoyer", :class => "submit btn" \ No newline at end of file + =f.submit "Envoyer", :class => " btn btn-primary" \ No newline at end of file diff --git a/app/views/public/registrants/_form.html.haml b/app/views/public/registrants/_form.html.haml new file mode 100644 index 0000000..03f811f --- /dev/null +++ b/app/views/public/registrants/_form.html.haml @@ -0,0 +1,34 @@ +-@registrant = @registrant || Registrant.new +.newsletter_form + %p + Inscrivez-vous à ma newsletter pour suivre l'actualité de ce blog. + + + =form_for [:public, @registrant], :remote => true do |f| + + + + %p + =f.text_field :surname, :placeholder => "Prénom", :class => "input_text" + -if f.object.errors[:email].count > 0 + %p.error + Vous devez indiquez votre prénom pour vous inscrire. + + + + %p + =f.text_field :email, :placeholder => "Email", :class => "input_text" + -if f.object.errors[:email].count == 2 + %p.error + Vous devez indiquez un email pour pouvoir vous inscrire. + -elsif f.object.errors[:email].count == 1 + %p.error + L'email que vous avez indiqué n'est pas valide. + + + =f.submit "M'inscrire !", :class => "btn btn-primary" + + %p.infos + =i(:lock) +   + Votre adresse ne sera pas partagée ou revendue à des tiers. \ No newline at end of file diff --git a/app/views/public/registrants/create.js.erb b/app/views/public/registrants/create.js.erb new file mode 100644 index 0000000..7425cca --- /dev/null +++ b/app/views/public/registrants/create.js.erb @@ -0,0 +1 @@ +$(".newsletter_form").replaceWith("Merci pour votre inscription ! Vous allez recevoir un mail avec un lien pour confirmer celle-ci."); \ No newline at end of file diff --git a/app/views/public/registrants/destroy_e.html.haml b/app/views/public/registrants/destroy_e.html.haml new file mode 100644 index 0000000..24357db --- /dev/null +++ b/app/views/public/registrants/destroy_e.html.haml @@ -0,0 +1,2 @@ +.flash_message + =raw @message \ No newline at end of file diff --git a/app/views/public/registrants/email_validation.html.haml b/app/views/public/registrants/email_validation.html.haml new file mode 100644 index 0000000..24357db --- /dev/null +++ b/app/views/public/registrants/email_validation.html.haml @@ -0,0 +1,2 @@ +.flash_message + =raw @message \ No newline at end of file diff --git a/app/views/public/registrants/new.html.haml b/app/views/public/registrants/new.html.haml new file mode 100644 index 0000000..e69de29 diff --git a/app/views/public/registrants/new.js.erb b/app/views/public/registrants/new.js.erb new file mode 100644 index 0000000..cd4264c --- /dev/null +++ b/app/views/public/registrants/new.js.erb @@ -0,0 +1 @@ +$(".newsletter_form").replaceWith("<%= escape_javascript(render(:partial => "public/registrants/form")) %>"); \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index b66cbb8..75eaec7 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -18,6 +18,13 @@ Rails.application.routes.draw do get "sitemap.:f" => "public/sitemap#sitemap" namespace :public do resources :comments + resources :registrants do + member do + get :email_validation + get :destroy_e + end + + end end namespace :portlet do diff --git a/db/migrate/20140710140837_create_registrants.rb b/db/migrate/20140710140837_create_registrants.rb new file mode 100644 index 0000000..fa9136f --- /dev/null +++ b/db/migrate/20140710140837_create_registrants.rb @@ -0,0 +1,13 @@ +class CreateRegistrants < ActiveRecord::Migration + def change + create_table :registrants do |t| + t.string :token + t.string :name + t.string :surname + t.string :email + t.boolean :enabled + + t.timestamps + end + end +end diff --git a/db/migrate/20140710144544_create_newsletters.rb b/db/migrate/20140710144544_create_newsletters.rb new file mode 100644 index 0000000..1fea60c --- /dev/null +++ b/db/migrate/20140710144544_create_newsletters.rb @@ -0,0 +1,12 @@ +class CreateNewsletters < ActiveRecord::Migration + def change + create_table :newsletters do |t| + t.string :subject + t.text :content + t.boolean :sended + t.boolean :send_at + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 272c261..75899c2 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -351,6 +351,15 @@ ActiveRecord::Schema.define(version: 20150411203736) do t.datetime "updated_at", null: false end + create_table "newsletters", force: :cascade do |t| + t.string "subject", limit: 255 + t.text "content", limit: 65535 + t.boolean "sended", limit: 1 + t.boolean "send_at", limit: 1 + t.datetime "created_at" + t.datetime "updated_at" + end + create_table "pages", force: :cascade do |t| t.text "title", limit: 65535 t.text "description", limit: 65535 @@ -368,6 +377,16 @@ ActiveRecord::Schema.define(version: 20150411203736) do t.datetime "updated_at", null: false end + create_table "registrants", force: :cascade do |t| + t.string "token", limit: 255 + t.string "name", limit: 255 + t.string "surname", limit: 255 + t.string "email", limit: 255 + t.boolean "enabled", limit: 1 + t.datetime "created_at" + t.datetime "updated_at" + end + create_table "table_contents", force: :cascade do |t| t.integer "style", limit: 4 t.integer "nbr_rows", limit: 4