Complete offer CRUD
This commit is contained in:
parent
2e0c9a793f
commit
d6427259f8
@ -36,20 +36,31 @@
|
||||
|
||||
.customer_table{
|
||||
td, th{
|
||||
|
||||
vertical-align:middle !important;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
.admin_table{
|
||||
.admin-table{
|
||||
td, th{
|
||||
|
||||
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{
|
||||
border-radius:0px;
|
||||
|
||||
|
@ -55,8 +55,8 @@ class Admin::NeedsController < ApplicationController
|
||||
"Les plus récents" => 'created-desc',
|
||||
"Les plus populaires" => 'wishes-desc',
|
||||
"Les plus commentés" => 'comments-desc',
|
||||
"Alphabétique (de A à Z)" => 'alpha-asc',
|
||||
"Alphabétique (de Z à A)" => 'alpha-desc'
|
||||
"Titre (de A à Z)" => 'alpha-asc',
|
||||
"Titre (de Z à A)" => 'alpha-desc'
|
||||
}
|
||||
|
||||
end
|
||||
@ -69,7 +69,7 @@ class Admin::NeedsController < ApplicationController
|
||||
def create
|
||||
@need = Need.new(need_params)
|
||||
|
||||
if @need.update_attributes(need_params)
|
||||
if @need.save
|
||||
flash[:notice] = "Besoin créé avec succès."
|
||||
@need.validate!
|
||||
redirect_to admin_needs_path
|
||||
@ -144,12 +144,17 @@ class Admin::NeedsController < ApplicationController
|
||||
|
||||
def accept
|
||||
@need = Need.find(params[:id])
|
||||
if @need.reject!
|
||||
if @need.offers.length < 1
|
||||
flash[:error] = "Vous devez créer au moins une offre avant de passer ce besoin en négocié"
|
||||
else
|
||||
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
|
||||
redirect_to new_admin_need_offer_path(@need)
|
||||
end
|
||||
|
||||
redirect_to admin_needs_path
|
||||
end
|
||||
|
||||
private
|
||||
|
@ -4,6 +4,55 @@ class Admin::OffersController < ApplicationController
|
||||
|
||||
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
|
||||
|
||||
def new
|
||||
@ -12,19 +61,58 @@ class Admin::OffersController < ApplicationController
|
||||
end
|
||||
|
||||
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
|
||||
|
||||
def edit
|
||||
|
||||
|
||||
@offer = Offer.find(params[:id])
|
||||
if params[:need_id]
|
||||
@need = Need.find(params[:need_id])
|
||||
end
|
||||
end
|
||||
|
||||
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
|
||||
|
||||
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
|
||||
|
||||
@ -32,8 +120,8 @@ class Admin::OffersController < ApplicationController
|
||||
|
||||
private
|
||||
|
||||
def order_params
|
||||
params.require(:order).permit(:title, :description, :category_id, :author_id)
|
||||
def offer_params
|
||||
params.require(:offer).permit(:supplier, :price, :fee_percentage)
|
||||
end
|
||||
|
||||
end
|
||||
|
@ -29,7 +29,7 @@ class Need < ActiveRecord::Base
|
||||
has_many :wishes, dependent: :destroy
|
||||
has_many :customers, -> { uniq }, through: :wishes
|
||||
has_many :messages, dependent: :destroy
|
||||
has_many :offers
|
||||
has_many :offers, dependent: :destroy
|
||||
belongs_to :category, class_name: "NeedCategory"
|
||||
|
||||
validates :title, :presence => true, length: {within: 4..128}
|
||||
@ -112,7 +112,7 @@ class Need < ActiveRecord::Base
|
||||
when 'negociating'
|
||||
"En négociation"
|
||||
when 'negociated'
|
||||
"Négociation effecutée"
|
||||
"Négociation terminée"
|
||||
when 'failed'
|
||||
"Négociation échouée"
|
||||
else
|
||||
|
@ -1,4 +1,16 @@
|
||||
class Offer < ActiveRecord::Base
|
||||
acts_as_paranoid
|
||||
|
||||
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
|
||||
|
@ -1,7 +1,7 @@
|
||||
-css_class = "warning" if need.negociating?
|
||||
-css_class = "error" if need.failed?
|
||||
-css_class = "danger" if need.failed?
|
||||
-css_class = "success" if need.negociated?
|
||||
%tr{:id => need.id}
|
||||
%tr{:id => need.id, class: css_class}
|
||||
%td
|
||||
=link_to need.title, edit_admin_need_path(need)
|
||||
%td
|
||||
@ -19,8 +19,8 @@
|
||||
|
||||
=link_to i(:"comment-o") + " " + need.messages.length.to_s, admin_need_messages_path(need)
|
||||
%td{style: 'text-align:center' }
|
||||
|
||||
%td{class: css_class}
|
||||
=link_to i(:"gift") + " " + need.offers.length.to_s, admin_need_offers_path(need)
|
||||
%td
|
||||
=need.human_state
|
||||
%td.actions{:style => "width:150px;text-align:right"}
|
||||
-if(need.verified?)
|
||||
|
@ -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>
|
||||
|
||||
|
||||
%table.table.admin_table.table-hover.table-striped
|
||||
%table.table.admin-table.table-hover.table-striped
|
||||
%thead.rows_header
|
||||
|
||||
%tr
|
||||
@ -49,7 +49,7 @@
|
||||
= link_to "Créer un besoin", new_admin_need_path, class:"btn btn-primary"
|
||||
|
||||
.col-md-10
|
||||
%table.table.admin_table.table-hover.table-striped
|
||||
%table.table.admin-table.table-hover.table-striped
|
||||
%thead.rows_header
|
||||
|
||||
%tr
|
||||
|
9
app/views/admin/offers/_form.html.haml
Normal file
9
app/views/admin/offers/_form.html.haml
Normal 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"
|
17
app/views/admin/offers/_offer.html.haml
Normal file
17
app/views/admin/offers/_offer.html.haml
Normal 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)
|
4
app/views/admin/offers/edit.html.haml
Normal file
4
app/views/admin/offers/edit.html.haml
Normal file
@ -0,0 +1,4 @@
|
||||
%h1
|
||||
Modifier une offre
|
||||
|
||||
=render :partial => "form"
|
@ -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"}
|
||||
|
||||
|
||||
%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()})
|
@ -0,0 +1,4 @@
|
||||
%h1
|
||||
Créer une offre pour le besoin
|
||||
%strong= @need.title
|
||||
=render :partial => "form"
|
@ -47,6 +47,7 @@
|
||||
%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 offres", admin_offers_path
|
||||
|
||||
|
||||
|
||||
|
@ -253,6 +253,7 @@ Rails.application.routes.draw do
|
||||
get :reject
|
||||
end
|
||||
end
|
||||
resources :offers
|
||||
|
||||
resources :customers do
|
||||
member do
|
||||
|
@ -5,7 +5,7 @@ class CreateOffers < ActiveRecord::Migration
|
||||
t.timestamps null: false
|
||||
t.float :price
|
||||
t.float :fee_percentage
|
||||
t.references :needs, index: true
|
||||
t.references :need, index: true
|
||||
t.string :supplier
|
||||
end
|
||||
end
|
||||
|
6
db/migrate/20151210134428_add_deleteted_at_to_offers.rb
Normal file
6
db/migrate/20151210134428_add_deleteted_at_to_offers.rb
Normal file
@ -0,0 +1,6 @@
|
||||
class AddDeletetedAtToOffers < ActiveRecord::Migration
|
||||
def change
|
||||
add_column :offers, :deleted_at, :datetime
|
||||
add_index :offers, :deleted_at
|
||||
end
|
||||
end
|
@ -11,7 +11,7 @@
|
||||
#
|
||||
# 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|
|
||||
t.string "name", limit: 255
|
||||
@ -440,11 +440,13 @@ ActiveRecord::Schema.define(version: 20151209125427) do
|
||||
t.datetime "updated_at", null: false
|
||||
t.float "price", 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.datetime "deleted_at"
|
||||
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|
|
||||
t.text "title", limit: 65535
|
||||
|
Loading…
x
Reference in New Issue
Block a user