Complete offer CRUD

This commit is contained in:
Nicolas VARROT 2015-12-10 15:47:05 +01:00
parent 2e0c9a793f
commit d6427259f8
17 changed files with 258 additions and 30 deletions

View File

@ -36,20 +36,31 @@
.customer_table{ .customer_table{
td, th{ td, th{
vertical-align:middle !important; vertical-align:middle !important;
} }
} }
.admin_table{ .admin-table{
td, th{ td, th{
vertical-align:middle !important; vertical-align:middle !important;
} }
} }
.table tbody tr.success td {
background-color: #dff0d8 !important;
}
.table tbody tr.danger td {
background-color: #f2dede !important;
}
.table tbody tr.info td {
background-color: #d9edf7 !important;
}
.table tbody tr.warning td {
background-color: lightgoldenrodyellow !important;
}
#admin_nav{ #admin_nav{
border-radius:0px; border-radius:0px;
@ -709,7 +720,7 @@ margin-top:0;
.table-striped tr:nth-child(odd) td, .table-striped tr:nth-child(odd) th{ .table-striped tr:nth-child(odd) td, .table-striped tr:nth-child(odd) th{
background:rgba(235,244,250,1) !important; background:rgba(235,244,250,1) !important;
} }

View File

@ -55,8 +55,8 @@ class Admin::NeedsController < ApplicationController
"Les plus récents" => 'created-desc', "Les plus récents" => 'created-desc',
"Les plus populaires" => 'wishes-desc', "Les plus populaires" => 'wishes-desc',
"Les plus commentés" => 'comments-desc', "Les plus commentés" => 'comments-desc',
"Alphabétique (de A à Z)" => 'alpha-asc', "Titre (de A à Z)" => 'alpha-asc',
"Alphabétique (de Z à A)" => 'alpha-desc' "Titre (de Z à A)" => 'alpha-desc'
} }
end end
@ -69,7 +69,7 @@ class Admin::NeedsController < ApplicationController
def create def create
@need = Need.new(need_params) @need = Need.new(need_params)
if @need.update_attributes(need_params) if @need.save
flash[:notice] = "Besoin créé avec succès." flash[:notice] = "Besoin créé avec succès."
@need.validate! @need.validate!
redirect_to admin_needs_path redirect_to admin_needs_path
@ -144,12 +144,17 @@ class Admin::NeedsController < ApplicationController
def accept def accept
@need = Need.find(params[:id]) @need = Need.find(params[:id])
if @need.reject! if @need.offers.length < 1
flash[:notice] = "Le besoin est maintenant négocié" flash[:error] = "Vous devez créer au moins une offre avant de passer ce besoin en négocié"
else else
flash[:error] = "L'état actuel de ce besoin ne permet pas cette action" if @need.accept!
flash[:notice] = "Le besoin est maintenant négocié"
else
flash[:error] = "L'état actuel de ce besoin ne permet pas cette action"
end
end end
redirect_to new_admin_need_offer_path(@need)
redirect_to admin_needs_path
end end
private private

View File

@ -4,6 +4,55 @@ class Admin::OffersController < ApplicationController
def index def index
# filters default value
params[:o] ||= 'created-desc'
params[:r] ||= 10
if params[:need_id]
@need = Need.find(params[:need_id])
@offers = @need.offers
else
@offers = Offer.all
end
if(params[:q] and params[:q] != '')
@offers = @offers.joins(:need).where("needs.title like ? or offers.supplier like ?" ,"%#{params[:q]}%","%#{params[:q]}%")
end
case params[:o]
when 'created-asc'
@offers = @offers.order(created_at: :asc)
when 'created-desc'
@offers = @offers.order(created_at: :desc)
when 'alpha-asc'
@offers = @offers.order(supplier: :asc)
when 'alpha-desc'
@offers = @offers.order(supplier: :desc)
when 'price-asc'
@offers = @offers.order(price: :asc)
when 'price-desc'
@offers = @offers.order(price: :desc)
when 'need-alpha-asc'
@offers = @offers.joins(:need).order('needs.title asc')
when 'need-alpha-desc'
@offers = @offers.joins(:need).order('needs.title desc')
end
@orders = {
"Les plus récentes" => 'alpha-asc',
"Fournisseurs (de A à Z)" => 'alpha-asc',
"Fournisseurs (de Z à A)" => 'alpha-desc',
"Besoin (de A à Z)" => 'need-alpha-asc',
"Besoin (de Z à A)" => 'need-alpha-desc',
"Prix négociés croissants" => 'price-asc',
"Prix négociés décroissants" => 'price-desc'
}
@offers = @offers.page(params[:page]).per(params[:r])
end end
def new def new
@ -12,19 +61,58 @@ class Admin::OffersController < ApplicationController
end end
def create def create
@need = Need.find(params[:need_id])
@offer = Offer.new(offer_params)
if @offer.save
@need.offers << @offer
flash[:notice] = "Offre créée avec succès."
@offer.validate!
redirect_to admin_need_offers_path(@need)
else
render :action => "new"
end
end end
def edit def edit
@offer = Offer.find(params[:id])
if params[:need_id]
@need = Need.find(params[:need_id])
end
end end
def update def update
@offer = Offer.find(params[:id])
if @offer.update_attributes(offer_params)
flash[:notice] = "Offre modifiée avec succès."
if params[:need_id]
@need = Need.find(params[:need_id])
redirect_to admin_need_offers_path(@need)
else
redirect_to admin_offers_path
end
else
render :action => "edit"
end
end end
def destroy def destroy
@offer = Offer.find(params[:id])
if(@offer.destroy)
flash[:notice] = "Offre supprimée avec succès."
else
flash[:error] = "Impossible de supprimer cette offre"
end
if params[:need_id]
@need = Need.find(params[:need_id])
redirect_to admin_need_offers_path(@need)
else
redirect_to admin_offers_path
end
end end
@ -32,8 +120,8 @@ class Admin::OffersController < ApplicationController
private private
def order_params def offer_params
params.require(:order).permit(:title, :description, :category_id, :author_id) params.require(:offer).permit(:supplier, :price, :fee_percentage)
end end
end end

View File

@ -29,7 +29,7 @@ class Need < ActiveRecord::Base
has_many :wishes, dependent: :destroy has_many :wishes, dependent: :destroy
has_many :customers, -> { uniq }, through: :wishes has_many :customers, -> { uniq }, through: :wishes
has_many :messages, dependent: :destroy has_many :messages, dependent: :destroy
has_many :offers has_many :offers, dependent: :destroy
belongs_to :category, class_name: "NeedCategory" belongs_to :category, class_name: "NeedCategory"
validates :title, :presence => true, length: {within: 4..128} validates :title, :presence => true, length: {within: 4..128}
@ -112,7 +112,7 @@ class Need < ActiveRecord::Base
when 'negociating' when 'negociating'
"En négociation" "En négociation"
when 'negociated' when 'negociated'
"Négociation effecutée" "Négociation terminée"
when 'failed' when 'failed'
"Négociation échouée" "Négociation échouée"
else else

View File

@ -1,4 +1,16 @@
class Offer < ActiveRecord::Base class Offer < ActiveRecord::Base
acts_as_paranoid
belongs_to :need belongs_to :need
validates :supplier, :presence => true, length: {within: 1..128}
validates :price,
:presence => true,
numericality: {greater_than_or_equal_to: 0}
validates :fee_percentage,
:presence => true,
numericality: {:greater_than_or_equal_to => 0, :less_than_or_equal_to => 100}
end end

View File

@ -1,7 +1,7 @@
-css_class = "warning" if need.negociating? -css_class = "warning" if need.negociating?
-css_class = "error" if need.failed? -css_class = "danger" if need.failed?
-css_class = "success" if need.negociated? -css_class = "success" if need.negociated?
%tr{:id => need.id} %tr{:id => need.id, class: css_class}
%td %td
=link_to need.title, edit_admin_need_path(need) =link_to need.title, edit_admin_need_path(need)
%td %td
@ -19,8 +19,8 @@
&nbsp;&nbsp; &nbsp;&nbsp;
=link_to i(:"comment-o") + " " + need.messages.length.to_s, admin_need_messages_path(need) =link_to i(:"comment-o") + " " + need.messages.length.to_s, admin_need_messages_path(need)
%td{style: 'text-align:center' } %td{style: 'text-align:center' }
=link_to i(:"gift") + " " + need.offers.length.to_s, admin_need_offers_path(need)
%td{class: css_class} %td
=need.human_state =need.human_state
%td.actions{:style => "width:150px;text-align:right"} %td.actions{:style => "width:150px;text-align:right"}
-if(need.verified?) -if(need.verified?)

View File

@ -9,7 +9,7 @@
Si pour une raison ou pour une autre le besoin ne respecte pas la charte, cliquez sur le bouton <i class="fa fa-remove"></i> Si pour une raison ou pour une autre le besoin ne respecte pas la charte, cliquez sur le bouton <i class="fa fa-remove"></i>
%table.table.admin_table.table-hover.table-striped %table.table.admin-table.table-hover.table-striped
%thead.rows_header %thead.rows_header
%tr %tr
@ -49,7 +49,7 @@
= link_to "Créer un besoin", new_admin_need_path, class:"btn btn-primary" = link_to "Créer un besoin", new_admin_need_path, class:"btn btn-primary"
.col-md-10 .col-md-10
%table.table.admin_table.table-hover.table-striped %table.table.admin-table.table-hover.table-striped
%thead.rows_header %thead.rows_header
%tr %tr

View File

@ -0,0 +1,9 @@
=semantic_form_for [:admin, @need, @offer] do |f|
.content
=f.inputs do
=f.input :supplier, :label => "Nom du fournisseur : "
=f.input :price, :label => "Prix négocié : "
=f.input :fee_percentage, :label => "% de frais : "
.actions= f.submit "Sauvegarder", :class => "btn btn-primary"

View File

@ -0,0 +1,17 @@
%tr{:id => offer.id}
%td
= link_to offer.need.title, edit_admin_need_path(offer.need)
%td
=offer.supplier
%td
=number_to_currency(offer.price, locale: :fr)
%td
=number_to_percentage(offer.fee_percentage, significant: true )
%td.actions{:style => "width:150px;text-align:right"}
-if @need
= link_to i(:"trash"), [:admin,@need, offer], :data => {:confirm => 'Voulez-vous vraiment supprimer cette offre ?'}, :method => :delete
= link_to i(:pencil), edit_admin_need_offer_path(@need, offer)
-else
= link_to i(:"trash"), [:admin, offer], :data => {:confirm => 'Voulez-vous vraiment supprimer cette offre ?'}, :method => :delete
= link_to i(:pencil), edit_admin_offer_path(offer)

View File

@ -0,0 +1,4 @@
%h1
Modifier une offre
=render :partial => "form"

View File

@ -0,0 +1,68 @@
-if @need
%h1
Gestion des offres pour le besoin
%strong= @need.title
%p.alert.alert-info
Seul les offres concernant le besoin
%strong= @need.title
sont affichées sur cette page.
%br
Pour voir toutes les offres, aller dans l'onglet
%strong Gestion des offres
depuis la barre de navigation
-else
%h1
Gestion des offres
%p.alert.alert-info
Cette page affiche toutes les offres existantes.
%br
Pour créer une offre, il faut d'abord choisir un besoin. Pour cela, passer par l'onglet
%strong Gestion des besoins
puis cliquer sur le bouton
%strong= i(:gift)
en face du besoin de votre choix.
-if @offers.length < 1
Aucune offre actuellement
-else
.row
.col-md-2
= semantic_form_for :search, :html => {id: :search_form, :method => :get } do |f|
= f.inputs do
= f.inputs do
=f.input :q, :as => :search, label: "Recherche", input_html: {value: params[:q], :name => 'q' }, placeholder: "Besoin ou fournisseur"
.clear
= f.input :o, as: :order, selected: params[:o], input_html: {:name => 'o' }, label: "Ordre d'affichage", :include_blank => false , :as => :select, :collection => @orders
.clear
= f.input :r, as: :result, selected: params[:r], input_html: {:name => 'r' }, label: 'Résultats par page', :include_blank => false , :as => :select, :collection => [10,20,50,100]
.clear
.col-md-10
%table.table.admin-table.table-hover.table-striped
%thead.rows_header
%tr
%th
Besoin
%th
Fournisseur
%th
Prix négocié
%th
\% de frais
%th{:style => "width:100px"}
&nbsp;
%tbody.rows
=render @offers
.pagination.pull-right= paginate @offers
%br
%br
-if @need
=link_to "Ajouter une offre", new_admin_need_offer_path(@need),class:"btn btn-primary"
:javascript
$('#search_o').change(function(){$('#search_form').submit()})
$('#search_r').change(function(){$('#search_form').submit()})

View File

@ -0,0 +1,4 @@
%h1
Créer une offre pour le besoin
%strong= @need.title
=render :partial => "form"

View File

@ -47,6 +47,7 @@
%li= link_to " Gestion des besoins", admin_needs_path %li= link_to " Gestion des besoins", admin_needs_path
%li= link_to " Gestion des catégories", admin_need_categories_path %li= link_to " Gestion des catégories", admin_need_categories_path
%li= link_to " Gestion des offres", admin_offers_path

View File

@ -253,6 +253,7 @@ Rails.application.routes.draw do
get :reject get :reject
end end
end end
resources :offers
resources :customers do resources :customers do
member do member do

View File

@ -5,7 +5,7 @@ class CreateOffers < ActiveRecord::Migration
t.timestamps null: false t.timestamps null: false
t.float :price t.float :price
t.float :fee_percentage t.float :fee_percentage
t.references :needs, index: true t.references :need, index: true
t.string :supplier t.string :supplier
end end
end end

View File

@ -0,0 +1,6 @@
class AddDeletetedAtToOffers < ActiveRecord::Migration
def change
add_column :offers, :deleted_at, :datetime
add_index :offers, :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: 20151209125427) do ActiveRecord::Schema.define(version: 20151210134428) 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
@ -440,11 +440,13 @@ ActiveRecord::Schema.define(version: 20151209125427) do
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
t.float "price", limit: 24 t.float "price", limit: 24
t.float "fee_percentage", limit: 24 t.float "fee_percentage", limit: 24
t.integer "needs_id", limit: 4 t.integer "need_id", limit: 4
t.string "supplier", limit: 255 t.string "supplier", limit: 255
t.datetime "deleted_at"
end end
add_index "offers", ["needs_id"], name: "index_offers_on_needs_id", using: :btree add_index "offers", ["deleted_at"], name: "index_offers_on_deleted_at", using: :btree
add_index "offers", ["need_id"], name: "index_offers_on_need_id", using: :btree
create_table "pages", force: :cascade do |t| create_table "pages", force: :cascade do |t|
t.text "title", limit: 65535 t.text "title", limit: 65535