From 1239d54244119e04258f6dde76518e95fad2e246 Mon Sep 17 00:00:00 2001 From: Nicolas Bally Date: Sat, 11 Jan 2020 15:28:56 +0100 Subject: [PATCH] suite --- .../public/p_friends_controller.rb | 79 +++++++++++++++++++ app/models/kap.rb | 24 ++++++ app/models/kaps_day.rb | 45 +++++++++-- app/models/p_customer.rb | 2 + app/models/p_friend.rb | 40 ++++++++++ app/views/layouts/public.html.haml | 6 +- app/views/public/kaps/_form.html.haml | 10 ++- app/views/public/kaps_days/index.html.haml | 13 ++- app/views/public/kaps_days/show.html.haml | 6 +- app/views/public/kapsmes/_kapsme.html.haml | 5 +- app/views/public/p_friends/_form.html.haml | 13 +++ .../public/p_friends/_p_friend.html.haml | 7 ++ app/views/public/p_friends/create.js.erb | 2 + app/views/public/p_friends/destroy.js.erb | 1 + app/views/public/p_friends/edit.html.haml | 4 + app/views/public/p_friends/edit.js.erb | 1 + app/views/public/p_friends/index.html.haml | 53 +++++++++++++ app/views/public/p_friends/new.html.haml | 4 + app/views/public/p_friends/new.js.erb | 1 + app/views/public/p_friends/show.html.haml | 10 +++ app/views/public/p_friends/update.js.erb | 2 + config/routes.rb | 8 +- .../20200109222420_add_stuff_to_kaps.rb | 10 +++ .../20200109230851_add_paused_to_kapsmes.rb | 5 ++ db/migrate/20200111123747_create_p_friends.rb | 12 +++ db/schema.rb | 19 ++++- test/fixtures/p_friends.yml | 11 +++ test/models/p_friend_test.rb | 7 ++ 28 files changed, 383 insertions(+), 17 deletions(-) create mode 100644 app/controllers/public/p_friends_controller.rb create mode 100644 app/models/p_friend.rb create mode 100755 app/views/public/p_friends/_form.html.haml create mode 100644 app/views/public/p_friends/_p_friend.html.haml create mode 100644 app/views/public/p_friends/create.js.erb create mode 100644 app/views/public/p_friends/destroy.js.erb create mode 100644 app/views/public/p_friends/edit.html.haml create mode 100644 app/views/public/p_friends/edit.js.erb create mode 100644 app/views/public/p_friends/index.html.haml create mode 100644 app/views/public/p_friends/new.html.haml create mode 100644 app/views/public/p_friends/new.js.erb create mode 100644 app/views/public/p_friends/show.html.haml create mode 100644 app/views/public/p_friends/update.js.erb create mode 100644 db/migrate/20200109222420_add_stuff_to_kaps.rb create mode 100644 db/migrate/20200109230851_add_paused_to_kapsmes.rb create mode 100644 db/migrate/20200111123747_create_p_friends.rb create mode 100644 test/fixtures/p_friends.yml create mode 100644 test/models/p_friend_test.rb diff --git a/app/controllers/public/p_friends_controller.rb b/app/controllers/public/p_friends_controller.rb new file mode 100644 index 0000000..b3af872 --- /dev/null +++ b/app/controllers/public/p_friends_controller.rb @@ -0,0 +1,79 @@ +# -*- encoding : utf-8 -*- + +class Public::PFriendsController < ApplicationController + layout "public" + before_filter :auth_p_customer + + + def index + @p_friends = current_p_customer.p_friends.order(:name).all + + + end + + def show + @p_friend = current_p_customer.p_friends.find(params[:id]) + + end + + def new + + + @p_friend = current_p_customer.p_friends.new + + + end + + + def confirm + @p_friend = PFriend.where(:p_friend_customer_id => current_p_customer.id).find(params[:id]) + @p_friend.confirm + + redirect_to :back + end + + def edit + + + @p_friend = current_p_customer.p_friends.find(params[:id]) + end + + def create + @p_friend = current_p_customer.p_friends.new(params.require(:p_friend).permit!) + + + @p_friend.initiator = true + + if @p_friend.save + @p_friends = current_p_customer.p_friends.order(:name).all + + else + render action: "new" + + end + + end + + + def update + @p_friend = current_p_customer.p_friends.find(params[:id]) + + + if @p_friend.update_attributes(params.require(:p_friend).permit!) + + @p_friends = current_p_customer.p_friends.order(:name).all + else + render action: "edit" + + end + + end + + + def destroy + @p_friend = current_p_customer.p_friends.find(params[:id]) + @p_friend.destroy + + + end +end diff --git a/app/models/kap.rb b/app/models/kap.rb index 1601641..628f28f 100644 --- a/app/models/kap.rb +++ b/app/models/kap.rb @@ -6,8 +6,32 @@ class Kap < ActiveRecord::Base validates :name, :presence => true validates :kaps_type_id, :presence => true + + validates :start_at, :presence => true + before_validation do + if self.pause_days.to_i < 1 + self.pause_days = nil + end + + if self.active_days.to_i < 1 + self.active_days = nil + end + + if self.active_days? and !self.pause_days? + errors.add(:pause_days, 'doit être remplis si nombre de jours de pause indiqués') + end + + if self.pause_days? and !self.active_days? + errors.add(:active_days, 'doit être remplis si nombre de jours de pause indiqués') + end + + + + + + end def kaps_type if self.kaps_type_id == 1 diff --git a/app/models/kaps_day.rb b/app/models/kaps_day.rb index 1a5dc13..1f2e0e9 100644 --- a/app/models/kaps_day.rb +++ b/app/models/kaps_day.rb @@ -5,13 +5,49 @@ class KapsDay < ActiveRecord::Base after_save do - self.p_customer.kaps.where(:kaps_type_id => 1).each do |kap| + + generate_kapsmes + + + + + + end + + def generate_kapsmes + self.kapsmes.where(:kap_id => 1).destroy_all + self.p_customer.kaps.where(:kaps_type_id => 1).where("start_at <= ? and (end_at is null or end_at >= ?)", self.date, self.date).each do |kap| + + if !self.kapsmes.where(:kap_id => kap.id).first + + if kap.active_days + if last_paused_kapsme = self.p_customer.kapsmes.where(:kap_id => kap.id, :paused => true).joins(:kaps_day).order("kaps_days.date DESC").first + #sdffs = sfsf + #start_date = nil + start_date = kap.start_at + else + start_date = kap.start_at + + end + + dif = (self.date - start_date).to_i % (kap.active_days+kap.pause_days) + + if dif < kap.active_days + paused = false + else + paused = true + end + + else + paused = false + end + self.kapsmes.create( :kap_id => kap.id, - + :paused => paused, :name => kap.name, :description => kap.description, :public => kap.public, @@ -35,11 +71,6 @@ class KapsDay < ActiveRecord::Base end - - - - - end diff --git a/app/models/p_customer.rb b/app/models/p_customer.rb index 689bcef..64bd460 100644 --- a/app/models/p_customer.rb +++ b/app/models/p_customer.rb @@ -26,7 +26,9 @@ class PCustomer < ActiveRecord::Base has_many :kaps has_many :kaps_days + has_many :kapsmes + has_many :p_friends def generate_kaps_day(date) if !(kap_day = self.kaps_days.where(:date => date).first) diff --git a/app/models/p_friend.rb b/app/models/p_friend.rb new file mode 100644 index 0000000..016f1b9 --- /dev/null +++ b/app/models/p_friend.rb @@ -0,0 +1,40 @@ +class PFriend < ActiveRecord::Base + belongs_to :p_customer + belongs_to :p_friend_customer, :class_name => "PCustomer" + + attr_accessor :email + validates :email, :presence => true, on: :create + validates :p_customer_id, :presence => true + validates :p_friend_customer_id, :presence => true + + + before_validation do + if !self.id + + p_friend_customer = PCustomer.where(:email => self.email).first + if p_friend_customer + self.p_friend_customer = p_friend_customer + + if self.p_customer.p_friends.where(:p_friend_customer_id => p_friend_customer.id).first or p_friend_customer.id == self.p_customer.id + errors.add(:email, 'Vous avez déjà ajouté cet ami.') + end + else + errors.add(:email, 'Aucun compte avec cet email n\'a été trouvé.') + end + end + end + + after_create do + PFriend.create(:p_customer_id => self.p_friend_customer_id, :p_friend_customer_id => self.p_customer_id, :email => self.p_customer.email) + end + + def confirm + + self.enabled = true + self.save + PFriend.where(:p_customer_id => self.p_friend_customer_id, :p_friend_customer_id => self.p_customer_id).update_all(:enabled => true) + end + + + +end \ No newline at end of file diff --git a/app/views/layouts/public.html.haml b/app/views/layouts/public.html.haml index ce1687a..2af4c84 100644 --- a/app/views/layouts/public.html.haml +++ b/app/views/layouts/public.html.haml @@ -43,11 +43,13 @@ =link_to "Mes Kaps", public_kaps_path() ="-" =link_to "Jours", public_kaps_days_path() - + ="-" + =link_to "Mes amis", public_p_friends_path() + -else =link_to ic(:"power-off")+" Connexion", new_public_p_customer_auth_path - =link_to image_tag("/logo.png", :id => "logo"), public_p_products_path + =link_to image_tag("/logo.png", :id => "logo"), today_public_kaps_days_path .clear diff --git a/app/views/public/kaps/_form.html.haml b/app/views/public/kaps/_form.html.haml index 16b0c85..1f69564 100755 --- a/app/views/public/kaps/_form.html.haml +++ b/app/views/public/kaps/_form.html.haml @@ -9,7 +9,15 @@ = f.input :points, :label => "Points :", :collection => 0..5, :as => :radio, :include_blank => false - + =f.input :start_at, :label => "Date de début :", :as => :date + + =f.input :end_at, :label => "Date de fin :", :as => :date + + %h3 Alternance : + + =f.input :active_days, :label => "Jours actifs :" + + =f.input :pause_days, :label => "Jours de pause : " diff --git a/app/views/public/kaps_days/index.html.haml b/app/views/public/kaps_days/index.html.haml index e7bc818..cd8e39b 100644 --- a/app/views/public/kaps_days/index.html.haml +++ b/app/views/public/kaps_days/index.html.haml @@ -9,7 +9,16 @@ .qi_row .qi_pannel.qi_plain.padding - + -start_date = Date.parse("2020/01/01") + -date = start_date + -while date <= start_date + 6.month + -if kaps_day = current_p_customer.kaps_days.where(:date => date).first + -kaps_day.generate_kapsmes + -else + -current_p_customer.kaps_days.create(:date => date) + + + -date = date + 1.day %table.table %tr @@ -18,6 +27,6 @@ %tbody#kaps_days_rows - =render @kaps_days + =render current_p_customer.kaps_days.order("date DESC").all \ No newline at end of file diff --git a/app/views/public/kaps_days/show.html.haml b/app/views/public/kaps_days/show.html.haml index c30493d..a9062a0 100644 --- a/app/views/public/kaps_days/show.html.haml +++ b/app/views/public/kaps_days/show.html.haml @@ -5,6 +5,7 @@ %p Les Kaps du jours + =@kaps_day.generate_kapsmes .qi_row .qi_pannel.qi_plain.padding{:style => "position:relative;"} #pourcentage_inner{:style => "position:absolute;top:0;left:0;right:0;"} @@ -18,13 +19,16 @@ %th Points + %th Pause ? %th Public ? + + %th %tbody#kaps_rows - =render @kaps_day.kapsmes + =render @kaps_day.kapsmes.order("id DESC") =link_to "Réinitialiser", [:public, @kaps_day], :method => :delete, :data => {:confirm => "Etes vous sûr ?"} diff --git a/app/views/public/kapsmes/_kapsme.html.haml b/app/views/public/kapsmes/_kapsme.html.haml index ae15bc3..68537ba 100644 --- a/app/views/public/kapsmes/_kapsme.html.haml +++ b/app/views/public/kapsmes/_kapsme.html.haml @@ -6,10 +6,13 @@ -else %span{:style => "color:rgba(0,0,0,0.2);"}= kapsme.points - + %td + ="oui" if kapsme.paused %td{:style => "vertical-align:middle;"} ="oui" if kapsme.public + + %td{:style => "font-size:25px;"} diff --git a/app/views/public/p_friends/_form.html.haml b/app/views/public/p_friends/_form.html.haml new file mode 100755 index 0000000..11bd6ea --- /dev/null +++ b/app/views/public/p_friends/_form.html.haml @@ -0,0 +1,13 @@ +=semantic_form_for [:public, @p_friend], :remote => true do |f| + + .content + =f.inputs do + = f.hidden_field :p_customer_id + = f.input :email + + =debug @p_friend.errors.messages + + + + .actions=f.submit "sauvegarder", :class => "btn btn-primary" + \ No newline at end of file diff --git a/app/views/public/p_friends/_p_friend.html.haml b/app/views/public/p_friends/_p_friend.html.haml new file mode 100644 index 0000000..da6ca85 --- /dev/null +++ b/app/views/public/p_friends/_p_friend.html.haml @@ -0,0 +1,7 @@ +%tr#p_friend{:id => p_friend.id} + %td= p_friend.p_friend_customer.email + + + %td.actions + = link_to i(:"trash-o"), [:public, p_friend], method: :delete, data: { confirm: 'Voulez-vous vraiment supprimer cet amis ? ' } , :remote => true + = link_to i(:pencil), edit_public_p_friend_path(p_friend), :remote => true diff --git a/app/views/public/p_friends/create.js.erb b/app/views/public/p_friends/create.js.erb new file mode 100644 index 0000000..03df843 --- /dev/null +++ b/app/views/public/p_friends/create.js.erb @@ -0,0 +1,2 @@ +$('#kaps_rows_in_process').prepend("<%= escape_javascript(render(@p_friend))%>"); +close_pane_hover(); \ No newline at end of file diff --git a/app/views/public/p_friends/destroy.js.erb b/app/views/public/p_friends/destroy.js.erb new file mode 100644 index 0000000..c3f9322 --- /dev/null +++ b/app/views/public/p_friends/destroy.js.erb @@ -0,0 +1 @@ +$('#p_friend_<%= @p_friend.id %>').remove(); \ No newline at end of file diff --git a/app/views/public/p_friends/edit.html.haml b/app/views/public/p_friends/edit.html.haml new file mode 100644 index 0000000..684eb81 --- /dev/null +++ b/app/views/public/p_friends/edit.html.haml @@ -0,0 +1,4 @@ +%h1 Modifier un distributeur + += render 'form' + diff --git a/app/views/public/p_friends/edit.js.erb b/app/views/public/p_friends/edit.js.erb new file mode 100644 index 0000000..6c8f015 --- /dev/null +++ b/app/views/public/p_friends/edit.js.erb @@ -0,0 +1 @@ +show_pane_hover("<%= escape_javascript(render(:partial => "form"))%>",700,900); \ No newline at end of file diff --git a/app/views/public/p_friends/index.html.haml b/app/views/public/p_friends/index.html.haml new file mode 100644 index 0000000..d86db0c --- /dev/null +++ b/app/views/public/p_friends/index.html.haml @@ -0,0 +1,53 @@ +.qi_header + .right= link_to 'Ajouter un ami', new_public_p_friend_path(), :class => "btn btn-primary", :remote => true + %h1 + Mes amis + =current_p_customer.email + + +.qi_row + .qi_pannel.qi_plain.padding + + %h3 Invitations + + %table.table + + %tbody#kaps_rows + -PFriend.where(:enabled => false, :p_friend_customer_id => current_p_customer.id, :initiator => true).each do |p_friend| + %tr#p_friend{:id => p_friend.id} + %td= p_friend.p_customer.email + + + %td.actions + = link_to i(:check)+" Valider cette demande", confirm_public_p_friend_path(p_friend), :remote => false, :class => "btn btn-primary" + + + + + + + %h3 Demandes en cours + + %table.table + + %tbody#kaps_rows_in_process + =render current_p_customer.p_friends.where(:enabled => false, :initiator => true) + + + + + %h3 + Demandes validées + + + %table.table + + %tbody#kaps_rows + -current_p_customer.p_friends.where(:enabled => true).each do |p_friend| + %tr#p_friend{:id => p_friend.id} + %td= p_friend.p_friend_customer.email + + + %td.actions + = link_to i(:eye)+" Voir", public_p_friend_path(p_friend), :remote => false, :class => "btn btn-primary" + \ No newline at end of file diff --git a/app/views/public/p_friends/new.html.haml b/app/views/public/p_friends/new.html.haml new file mode 100644 index 0000000..3b1635e --- /dev/null +++ b/app/views/public/p_friends/new.html.haml @@ -0,0 +1,4 @@ +%h1 Ajouter un distributeur + += render 'form' + diff --git a/app/views/public/p_friends/new.js.erb b/app/views/public/p_friends/new.js.erb new file mode 100644 index 0000000..6c8f015 --- /dev/null +++ b/app/views/public/p_friends/new.js.erb @@ -0,0 +1 @@ +show_pane_hover("<%= escape_javascript(render(:partial => "form"))%>",700,900); \ No newline at end of file diff --git a/app/views/public/p_friends/show.html.haml b/app/views/public/p_friends/show.html.haml new file mode 100644 index 0000000..a5a6ca4 --- /dev/null +++ b/app/views/public/p_friends/show.html.haml @@ -0,0 +1,10 @@ +%h1=@kap.name + +%h2 Marques +=link_to "ajouter une marque", new_public_label_marque_path(:kap_id => @kap.id), :class => "btn btn-primary" +%table.table + =render @kap.label_marques.order(:name) +%br +%br +%br +%br \ No newline at end of file diff --git a/app/views/public/p_friends/update.js.erb b/app/views/public/p_friends/update.js.erb new file mode 100644 index 0000000..b6a9879 --- /dev/null +++ b/app/views/public/p_friends/update.js.erb @@ -0,0 +1,2 @@ +$('#p_friends_rows').html("<%= escape_javascript(render(@p_friends))%>"); +close_pane_hover(); \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index e0edb9f..6341a7f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -32,7 +32,11 @@ Rails.application.routes.draw do namespace :public do - + resources :p_friends do + member do + get :confirm + end + end resources :kaps resources :kapsmes @@ -379,7 +383,7 @@ Rails.application.routes.draw do get "plan" => "public/home#plan" - root "public/p_products#index" + root "public/kaps_days#today" diff --git a/db/migrate/20200109222420_add_stuff_to_kaps.rb b/db/migrate/20200109222420_add_stuff_to_kaps.rb new file mode 100644 index 0000000..198c9c5 --- /dev/null +++ b/db/migrate/20200109222420_add_stuff_to_kaps.rb @@ -0,0 +1,10 @@ +class AddStuffToKaps < ActiveRecord::Migration + def change + add_column :kaps, :pause_days, :integer + add_column :kaps, :active_days, :integer + + add_column :kaps, :start_at, :date + add_column :kaps, :end_at, :date + + end +end diff --git a/db/migrate/20200109230851_add_paused_to_kapsmes.rb b/db/migrate/20200109230851_add_paused_to_kapsmes.rb new file mode 100644 index 0000000..38622d7 --- /dev/null +++ b/db/migrate/20200109230851_add_paused_to_kapsmes.rb @@ -0,0 +1,5 @@ +class AddPausedToKapsmes < ActiveRecord::Migration + def change + add_column :kapsmes, :paused, :boolean, :default => false + end +end diff --git a/db/migrate/20200111123747_create_p_friends.rb b/db/migrate/20200111123747_create_p_friends.rb new file mode 100644 index 0000000..d2c8914 --- /dev/null +++ b/db/migrate/20200111123747_create_p_friends.rb @@ -0,0 +1,12 @@ +class CreatePFriends < ActiveRecord::Migration + def change + create_table :p_friends do |t| + t.references :p_customer, index: true, foreign_key: true + t.integer :p_friend_customer_id + t.boolean :enabled, :default => false + t.boolean :initiator, :default => false + + t.timestamps null: false + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 2727b3f..8fe5c60 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20190120212114) do +ActiveRecord::Schema.define(version: 20200111123747) do create_table "admin_admin_roles", force: :cascade do |t| t.integer "admin_id", limit: 4 @@ -461,6 +461,10 @@ ActiveRecord::Schema.define(version: 20190120212114) do t.integer "points", limit: 4, default: 1 t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.integer "pause_days", limit: 4 + t.integer "active_days", limit: 4 + t.date "start_at" + t.date "end_at" end add_index "kaps", ["p_customer_id"], name: "index_kaps_on_p_customer_id", using: :btree @@ -494,6 +498,7 @@ ActiveRecord::Schema.define(version: 20190120212114) do t.integer "kaps_month_id", limit: 4 t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.boolean "paused", default: false end add_index "kapsmes", ["kap_id"], name: "index_kapsmes_on_kap_id", using: :btree @@ -988,6 +993,17 @@ ActiveRecord::Schema.define(version: 20190120212114) do t.string "tel", limit: 255 end + create_table "p_friends", force: :cascade do |t| + t.integer "p_customer_id", limit: 4 + t.integer "p_friend_customer_id", limit: 4 + t.boolean "enabled", default: false + t.boolean "initiator", default: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + add_index "p_friends", ["p_customer_id"], name: "index_p_friends_on_p_customer_id", using: :btree + create_table "p_origines", force: :cascade do |t| t.string "name", limit: 255 t.datetime "created_at", null: false @@ -1613,6 +1629,7 @@ ActiveRecord::Schema.define(version: 20190120212114) do add_foreign_key "p_customers", "p_price_cats" add_foreign_key "p_degressifs", "p_price_cats" add_foreign_key "p_degressifs", "p_products" + add_foreign_key "p_friends", "p_customers" add_foreign_key "p_price_cat_p_degressifs", "p_degressifs" add_foreign_key "p_price_cat_p_degressifs", "p_price_cats" add_foreign_key "p_product_certifs", "p_certifs" diff --git a/test/fixtures/p_friends.yml b/test/fixtures/p_friends.yml new file mode 100644 index 0000000..abd14e0 --- /dev/null +++ b/test/fixtures/p_friends.yml @@ -0,0 +1,11 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + p_customer_id: + : + enabled: false + +two: + p_customer_id: + : + enabled: false diff --git a/test/models/p_friend_test.rb b/test/models/p_friend_test.rb new file mode 100644 index 0000000..6d41981 --- /dev/null +++ b/test/models/p_friend_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class PFriendTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end