Need added (migrations+model+controller+templates+soft-deleting)

This commit is contained in:
Nicolas VARROT 2015-11-30 19:13:50 +01:00
parent 11d419aba1
commit a867e95951
20 changed files with 320 additions and 128 deletions

View File

@ -66,3 +66,5 @@ gem 'wicked_pdf'
gem 'wkhtmltopdf-binary' gem 'wkhtmltopdf-binary'
gem "geocoder" gem "geocoder"
gem "paranoia", "~> 2.0"

View File

@ -128,6 +128,8 @@ GEM
net-ssh (>= 2.6.5) net-ssh (>= 2.6.5)
nokogiri (1.6.6.2) nokogiri (1.6.6.2)
mini_portile (~> 0.6.0) mini_portile (~> 0.6.0)
paranoia (2.1.4)
activerecord (~> 4.0)
rack (1.6.0) rack (1.6.0)
rack-test (0.6.3) rack-test (0.6.3)
rack (>= 1.0) rack (>= 1.0)
@ -235,6 +237,7 @@ DEPENDENCIES
kaminari-bootstrap kaminari-bootstrap
mysql2 mysql2
net-ssh (~> 2.7.0) net-ssh (~> 2.7.0)
paranoia (~> 2.0)
rails (= 4.2.0) rails (= 4.2.0)
rmagick rmagick
rvm-capistrano (= 1.4.1) rvm-capistrano (= 1.4.1)

View File

@ -5,7 +5,7 @@ class Public::MyAccountController < ApplicationController
before_filter :auth_customer before_filter :auth_customer
def index def index
@needs = Kaminari.paginate_array(current_customer.needs).page(params[:page]).per(2)
end end

View File

@ -0,0 +1,48 @@
class Public::NeedsController < ApplicationController
layout "public"
before_filter :auth_customer
def new
@need = Need.new()
end
def destroy
@need = Need.find(params[:id])
if(@need.destroy)
flash[:notice] = "Besoin supprimé avec succès."
end
redirect_to public_my_account_path
end
def edit
@need = Need.find(params[:id])
end
def update
@need = Need.find(params[:id])
if @need.update_attributes(need_params)
flash[:notice] = "Besoin sauvegardé avec succès."
redirect_to public_my_account_path
else
render :action => "edit"
end
end
def create
@need = Need.new(need_params)
@need.author = current_customer
if @need.save
flash[:notice] = "Votre besoin à été créé avec succès."
redirect_to public_my_account_path
else
render :action => "new"
end
end
def need_params
params.require(:need).permit(:title, :description)
end
end

View File

@ -2,6 +2,8 @@ class Customer < ActiveRecord::Base
# Relationships # Relationships
has_many :needs, foreign_key: 'author_id'
has_many :customer_favs has_many :customer_favs
has_many :annonce_favs, :through => :customer_favs, :class_name => "Annonce", :source => :annonce has_many :annonce_favs, :through => :customer_favs, :class_name => "Annonce", :source => :annonce
@ -44,6 +46,7 @@ class Customer < ActiveRecord::Base
validates :need_1 , :presence => true, :if => :step3 validates :need_1 , :presence => true, :if => :step3
# Geocoder # Geocoder
reverse_geocoded_by :latitude, :longitude reverse_geocoded_by :latitude, :longitude
@ -213,6 +216,10 @@ class Customer < ActiveRecord::Base
"#{address} #{address2} #{cp} #{city}" "#{address} #{address2} #{cp} #{city}"
end end
def fullname
"#{firstname.capitalize} #{name.upcase}"
end
def pseudo def pseudo
if self.pro if self.pro
self.organisation self.organisation

15
app/models/need.rb Normal file
View File

@ -0,0 +1,15 @@
class Need < ActiveRecord::Base
max_paginates_per 10
validates :title, :presence => true,
:length => {:within => 4..128}
validates :description, :presence => true, length: {maximum: 65535}
belongs_to :author, class_name: 'Customer'
acts_as_paranoid
end

View File

@ -12,11 +12,11 @@
=link_to "renvoyer le mail de confirmation", public_reconfirm_email_path, :class => "btn btn-primary" =link_to "renvoyer le mail de confirmation", public_reconfirm_email_path, :class => "btn btn-primary"
.padding.center.white -if !current_customer.address or current_customer.need_1 == nil or !current_customer.account_validated
.padding.center.white
%h2 %h2
=current_customer.organisation =current_customer.organisation
-if !current_customer.address or current_customer.need_1 == nil
-if !current_customer.address -if !current_customer.address
%p %p
@ -24,9 +24,8 @@
=render :partial => "public/my_account/step2" =render :partial => "public/my_account/step2"
-elsif !current_customer.need_1 -elsif !current_customer.need_1
%center %center
=render :partial => "public/my_account/step3" =render :partial => "public/my_account/step3"
-else -elsif !current_customer.account_validated
%p %p
Merci ! Votre profil est complet, nous vous contacterons prochainement. Merci ! Votre profil est complet, nous vous contacterons prochainement.
%p %p
@ -34,5 +33,28 @@
-if current_customer.account_validated
.padding.center.white
%h2
=current_customer.organisation
%p
%i.fa.fa-user
#{current_customer.fullname}
%p
%i.fa.fa-map-marker
#{ current_customer.full_address}
%p
=link_to "Modifier mes infos", public_edit_infos_path, :class => "btn btn-primary"
.padding.center.white
%h3
Mes besoins
-if @needs.length > 0
=render "public/needs/index", needs: @needs
-else
%p
Vous n'avez pas encore créé de besoin
=link_to "Déclarer un besoin", new_public_need_path, :class => "btn btn-primary"
.padding.center.white
%h3
Ma liste de souhait

View File

@ -0,0 +1,6 @@
= semantic_form_for [:public, @need] do |f|
=f.inputs do
= f.input :title, :label => "Titre de votre besoin"
= f.input :description, :label => "Description", :rows => 5, :input_html => {:style => "height:100px;"}
%br
=f.submit "Sauvegarder", :class => "btn btn-primary"

View File

@ -0,0 +1,18 @@
%table.table.table-striped
%thead
%tr
%th
Titre du besoin
%th
État
%th
Souhaité par
%th{:style => "width:100px"}
&nbsp;
%tbody
=render @needs
.pagination= paginate @needs

View File

@ -0,0 +1,10 @@
%tr{:id => need.id}
%td
=need.title
%td
%td
%td.actions{:style => "width:150px;text-align:right"}
= link_to i(:"trash-o btn btn-default"), [:public, need], :data => {:confirm => 'Voulez-vous vraiment supprimer ce besoin ?'}, method: :delete
= link_to i(:"pencil btn btn-primary"), edit_public_need_path(need)

View File

@ -0,0 +1,4 @@
.center.padding.white
%h2
Modification d'un besoin existant
=render :partial => "public/needs/form"

View File

@ -0,0 +1,4 @@
.center.padding.white
%h2
Déclaration d'un nouveau besoin
=render :partial => "public/needs/form"

View File

@ -20,6 +20,7 @@ Rails.application.routes.draw do
get 'p/:mlm_token' => "public/customers#parrainage", :as => :mlm_token get 'p/:mlm_token' => "public/customers#parrainage", :as => :mlm_token
namespace :public do namespace :public do
resources :customer_favs resources :customer_favs
resources :customer_ribs do resources :customer_ribs do
@ -86,6 +87,9 @@ Rails.application.routes.draw do
get 'my_account/edit_infos', :as => "edit_infos" get 'my_account/edit_infos', :as => "edit_infos"
get 'my_account/my_annonces', :as => "my_annonces" get 'my_account/my_annonces', :as => "my_annonces"
get 'my_account/reconfirm', :as => "reconfirm_email" get 'my_account/reconfirm', :as => "reconfirm_email"
resources :needs
end end

View File

@ -0,0 +1,10 @@
class CreateNeeds < ActiveRecord::Migration
def change
create_table :needs do |t|
t.string :title
t.text :description
t.timestamps null: false
end
end
end

View File

@ -0,0 +1,5 @@
class AddAuthorReferenceToNeed < ActiveRecord::Migration
def change
add_reference :needs, :author, index: true
end
end

View File

@ -0,0 +1,6 @@
class AddDeleteAtToNeeds < ActiveRecord::Migration
def change
add_column :needs, :deleted_at, :datetime
add_index :needs, :deleted_at
end
end

View File

@ -11,7 +11,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20151130111157) do ActiveRecord::Schema.define(version: 20151130173626) do
create_table "admins", force: :cascade do |t| create_table "admins", force: :cascade do |t|
t.string "name", limit: 255 t.string "name", limit: 255
@ -382,6 +382,18 @@ ActiveRecord::Schema.define(version: 20151130111157) do
t.datetime "updated_at" t.datetime "updated_at"
end end
create_table "needs", force: :cascade do |t|
t.string "title", limit: 255
t.text "description", limit: 65535
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "author_id", limit: 4
t.datetime "deleted_at"
end
add_index "needs", ["author_id"], name: "index_needs_on_author_id", using: :btree
add_index "needs", ["deleted_at"], name: "index_needs_on_deleted_at", using: :btree
create_table "newsgroups", force: :cascade do |t| create_table "newsgroups", force: :cascade do |t|
t.string "name", limit: 255 t.string "name", limit: 255
t.string "slug", limit: 255 t.string "slug", limit: 255

9
test/fixtures/needs.yml vendored Normal file
View File

@ -0,0 +1,9 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
one:
title: MyString
description: MyText
two:
title: MyString
description: MyText

7
test/models/need_test.rb Normal file
View File

@ -0,0 +1,7 @@
require 'test_helper'
class NeedTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end