From de0c9d06770d908e58a4902353ea2c08cd8b4b6d Mon Sep 17 00:00:00 2001 From: Nicolas Bally Date: Sun, 12 Jan 2020 20:41:57 +0100 Subject: [PATCH] suite --- Gemfile | 1 + Gemfile.lock | 4 ++ app/models/kaps_day.rb | 12 +++- app/models/kapsme.rb | 10 ++++ app/models/p_customer.rb | 25 +++++++++ app/models/p_friend.rb | 17 ++++++ app/models/p_friend_kap.rb | 4 ++ app/views/public/kaps_days/index.html.haml | 14 +---- app/views/public/kaps_days/show.html.haml | 4 +- app/views/public/kapsmes/_kapsme.html.haml | 39 ++++++++----- app/views/public/p_friends/_form.html.haml | 4 +- app/views/public/p_friends/index.html.haml | 9 ++- app/views/public/p_friends/show.html.haml | 55 ++++++++++++++++--- app/views/public/p_friends/update.js.erb | 2 +- config/deploy.rb | 2 + config/schedule.rb | 29 ++++++++++ .../20200112112644_create_p_friend_kaps.rb | 10 ++++ ...00112130214_add_points_count_to_kapsmes.rb | 9 +++ db/schema.rb | 15 ++++- test/fixtures/p_friend_kaps.yml | 9 +++ test/models/p_friend_kap_test.rb | 7 +++ 21 files changed, 237 insertions(+), 44 deletions(-) create mode 100644 app/models/p_friend_kap.rb create mode 100644 config/schedule.rb create mode 100644 db/migrate/20200112112644_create_p_friend_kaps.rb create mode 100644 db/migrate/20200112130214_add_points_count_to_kapsmes.rb create mode 100644 test/fixtures/p_friend_kaps.yml create mode 100644 test/models/p_friend_kap_test.rb diff --git a/Gemfile b/Gemfile index a969f19..2f39ca8 100644 --- a/Gemfile +++ b/Gemfile @@ -105,3 +105,4 @@ gem 'paypal-sdk-merchant' gem 'searchkick'#, "2.3.1" +gem 'whenever', require: false \ No newline at end of file diff --git a/Gemfile.lock b/Gemfile.lock index 892d1ba..8fc02a0 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -59,6 +59,7 @@ GEM activesupport (>= 3.2.0) json (>= 1.7) mime-types (>= 1.16) + chronic (0.10.2) coffee-rails (4.1.0) coffee-script (>= 2.2.0) railties (>= 4.0.0, < 5.0) @@ -308,6 +309,8 @@ GEM binding_of_caller (>= 0.7.2) railties (~> 4.0) sprockets-rails (>= 2.0, < 4.0) + whenever (1.0.0) + chronic (>= 0.6.3) wicked_pdf (0.11.0) rails xml-simple (1.1.5) @@ -364,6 +367,7 @@ DEPENDENCIES uglifier (>= 1.3.0) unicorn web-console (~> 2.0) + whenever wicked_pdf BUNDLED WITH diff --git a/app/models/kaps_day.rb b/app/models/kaps_day.rb index 1f2e0e9..53f3640 100644 --- a/app/models/kaps_day.rb +++ b/app/models/kaps_day.rb @@ -15,7 +15,7 @@ class KapsDay < ActiveRecord::Base end def generate_kapsmes - self.kapsmes.where(:kap_id => 1).destroy_all + #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| @@ -75,7 +75,7 @@ class KapsDay < ActiveRecord::Base def total_points - self.kapsmes.sum(:points) + self.kapsmes.sum(:point_to_count) end def total_points_earneds @@ -89,5 +89,13 @@ class KapsDay < ActiveRecord::Base end + def cron + PCustomer.all.each do |pc| + pc.generate_kaps_days + end + + # whenever --update-crontab + end + end diff --git a/app/models/kapsme.rb b/app/models/kapsme.rb index f32cd49..be616a5 100644 --- a/app/models/kapsme.rb +++ b/app/models/kapsme.rb @@ -14,6 +14,16 @@ class Kapsme < ActiveRecord::Base end + + before_save do + if self.paused + self.point_to_count = 0 + else + self.point_to_count = self.points + end + end + + belongs_to :kaps_day end diff --git a/app/models/p_customer.rb b/app/models/p_customer.rb index 64bd460..4cdd609 100644 --- a/app/models/p_customer.rb +++ b/app/models/p_customer.rb @@ -30,6 +30,31 @@ class PCustomer < ActiveRecord::Base has_many :p_friends + def generate_kaps_days + if last = self.kaps_days.order("date DESC").first + start_date = last.date + else + start_date = self.created_at.to_date + end + + date = start_date + + while date <= Date.today + if kaps_day = self.kaps_days.where(:date => date).first + kaps_day.generate_kapsmes + else + self.kaps_days.create(:date => date) + end + + date = date + 1.day + end + + + end + + + + def generate_kaps_day(date) if !(kap_day = self.kaps_days.where(:date => date).first) kap_day =self.kaps_days.create(:date => date) diff --git a/app/models/p_friend.rb b/app/models/p_friend.rb index 016f1b9..88664b1 100644 --- a/app/models/p_friend.rb +++ b/app/models/p_friend.rb @@ -8,6 +8,10 @@ class PFriend < ActiveRecord::Base validates :p_friend_customer_id, :presence => true + has_many :p_friend_kaps + has_many :kaps, :through => :p_friend_kaps + + before_validation do if !self.id @@ -36,5 +40,18 @@ class PFriend < ActiveRecord::Base end + def reverse + PFriend.where(:p_customer_id => self.p_friend_customer_id, :p_friend_customer_id => self.p_customer_id).first + end + + + def allow_kapsmes(kaps_day) + + kaps_day.kapsmes.where(:kap_id => self.kaps.ids) + + + end + + end \ No newline at end of file diff --git a/app/models/p_friend_kap.rb b/app/models/p_friend_kap.rb new file mode 100644 index 0000000..512780b --- /dev/null +++ b/app/models/p_friend_kap.rb @@ -0,0 +1,4 @@ +class PFriendKap < ActiveRecord::Base + belongs_to :kap + belongs_to :p_friend +end diff --git a/app/views/public/kaps_days/index.html.haml b/app/views/public/kaps_days/index.html.haml index cd8e39b..659f579 100644 --- a/app/views/public/kaps_days/index.html.haml +++ b/app/views/public/kaps_days/index.html.haml @@ -8,17 +8,7 @@ .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 + -current_p_customer.generate_kaps_days %table.table %tr @@ -27,6 +17,6 @@ %tbody#kaps_days_rows - =render current_p_customer.kaps_days.order("date DESC").all + =render current_p_customer.kaps_days.order("date DESC").where("date <= ?", Date.today).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 a9062a0..521d8e2 100644 --- a/app/views/public/kaps_days/show.html.haml +++ b/app/views/public/kaps_days/show.html.haml @@ -5,7 +5,7 @@ %p Les Kaps du jours - =@kaps_day.generate_kapsmes + -@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;"} @@ -19,7 +19,7 @@ %th Points - %th Pause ? + %th Public ? diff --git a/app/views/public/kapsmes/_kapsme.html.haml b/app/views/public/kapsmes/_kapsme.html.haml index 68537ba..7ee7f31 100644 --- a/app/views/public/kapsmes/_kapsme.html.haml +++ b/app/views/public/kapsmes/_kapsme.html.haml @@ -1,26 +1,37 @@ %tr#kapsme{:id => kapsme.id} - %td{:style => "vertical-align:middle;"}= kapsme.name %td{:style => "vertical-align:middle;"} - -if kapsme.done - %strong= kapsme.points + = kapsme.name + %td{:style => "vertical-align:middle;"} + -if kapsme.paused + Pause + -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 + -if kapsme.done + %strong= kapsme.point_to_count + -else + %span{:style => "color:rgba(0,0,0,0.2);"}= kapsme.point_to_count + + -if !@p_friend + %td{:style => "vertical-align:middle;"} + ="oui" if kapsme.public %td{:style => "font-size:25px;"} - -if kapsme.done - =link_to ic(:check), public_kapsme_path(:id => kapsme.id, :kapsme => {:done => false}, :format => :js), :method => :put, :remote => true, :style => "color:green;" - - -else - =link_to ic(:check), public_kapsme_path(:id => kapsme.id, :kapsme => {:done => true}, :format => :js), :method => :put, :remote => true, :style => "color:rgba(0,0,0,0.2);" + -if !kapsme.paused + -if kapsme.done + -if !@p_friend + =link_to ic(:check), public_kapsme_path(:id => kapsme.id, :kapsme => {:done => false}, :format => :js), :method => :put, :remote => true, :style => "color:green;" + -else + %span{:style => "color:green;"}=ic(:check) + + -else + -if !@p_friend + =link_to ic(:check), public_kapsme_path(:id => kapsme.id, :kapsme => {:done => true}, :format => :js), :method => :put, :remote => true, :style => "color:rgba(0,0,0,0.2);" + -else + %span{:style => "color:rgba(0,0,0,0.2);"}=ic(:check) diff --git a/app/views/public/p_friends/_form.html.haml b/app/views/public/p_friends/_form.html.haml index 11bd6ea..884c06f 100755 --- a/app/views/public/p_friends/_form.html.haml +++ b/app/views/public/p_friends/_form.html.haml @@ -5,8 +5,10 @@ = f.hidden_field :p_customer_id = f.input :email - =debug @p_friend.errors.messages + = f.input :kap_ids, :collection => current_p_customer.kaps, :as => :check_boxes + + .actions=f.submit "sauvegarder", :class => "btn btn-primary" diff --git a/app/views/public/p_friends/index.html.haml b/app/views/public/p_friends/index.html.haml index d86db0c..c109095 100644 --- a/app/views/public/p_friends/index.html.haml +++ b/app/views/public/p_friends/index.html.haml @@ -44,10 +44,15 @@ %tbody#kaps_rows -current_p_customer.p_friends.where(:enabled => true).each do |p_friend| - %tr#p_friend{:id => p_friend.id} + %tr#p_friend_row{:id => p_friend.id} %td= p_friend.p_friend_customer.email - %td.actions + %td + =#.actions + + = link_to i(:pencil)+" Droits", edit_public_p_friend_path(p_friend), :remote => true, :class => "btn btn-primary" + = 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/show.html.haml b/app/views/public/p_friends/show.html.haml index a5a6ca4..8f9d743 100644 --- a/app/views/public/p_friends/show.html.haml +++ b/app/views/public/p_friends/show.html.haml @@ -1,10 +1,47 @@ -%h1=@kap.name +=@p_friend.p_friend_customer.email -%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 + +-friend_p_customer = @p_friend.reverse.p_customer + + +.right{:style => "width:180px;padding-right:20px;text-align:right"} + -friend_p_customer.kaps_days.order("date DESC").where("date <= ?", Date.today).all.each do |kaps_day| + + %p= link_to l(kaps_day.date, :format => "%A %e %B %Y"), public_p_friend_path(:id => @p_friend.id, :kaps_day_id => kaps_day.id) + + + + + +-if params[:kaps_day_id] + -@kaps_day = friend_p_customer.kaps_days.find(params[:kaps_day_id]) + +-else + -@kaps_day = friend_p_customer.kaps_days.where(:date => Date.today).first + +-if @kaps_day + %div{:style => "padding-right:20px;text-align:right;margin-right:210px;"} + .qi_row + =l(@kaps_day.date, :format => "%A %e %B %Y") + .qi_pannel.qi_plain.padding{:style => "position:relative;"} + #pourcentage_inner{:style => "position:absolute;top:0;left:0;right:0;"} + =render(:partial => "public/kaps_days/pourcentage", :locals => {:kaps_day => @kaps_day}) + + + %table.table + %tr + %th + + + %th Points + + + + %th + + + %tbody#kaps_rows + =render @p_friend.reverse.allow_kapsmes(@kaps_day).order("id DESC") + + +.clear diff --git a/app/views/public/p_friends/update.js.erb b/app/views/public/p_friends/update.js.erb index b6a9879..75b9791 100644 --- a/app/views/public/p_friends/update.js.erb +++ b/app/views/public/p_friends/update.js.erb @@ -1,2 +1,2 @@ -$('#p_friends_rows').html("<%= escape_javascript(render(@p_friends))%>"); +$('#p_friend_row_<%= @p_friend.id %>').html("<%= escape_javascript(render(@p_friend))%>"); close_pane_hover(); \ No newline at end of file diff --git a/config/deploy.rb b/config/deploy.rb index a2f7ecd..cfe0c6c 100644 --- a/config/deploy.rb +++ b/config/deploy.rb @@ -82,6 +82,8 @@ namespace :deploy do sudo "sudo systemctl enable quartz_app@#{application}" + run "cd #{current_path}; bundle exec whenever --update-crontab" + diff --git a/config/schedule.rb b/config/schedule.rb new file mode 100644 index 0000000..41d682e --- /dev/null +++ b/config/schedule.rb @@ -0,0 +1,29 @@ +# Use this file to easily define all of your cron jobs. +# +# It's helpful, but not entirely necessary to understand cron before proceeding. +# http://en.wikipedia.org/wiki/Cron + +# Example: +# +# set :output, "/path/to/my/cron_log.log" +# +# every 2.hours do +# command "/usr/bin/some_great_command" +# runner "MyModel.some_method" +# rake "some:great:rake:task" +# end +# +# every 4.days do +# runner "AnotherModel.prune_old_records" +# end + +# Learn more: http://github.com/javan/whenever + + +every 1.minute do + runner "KapsDay.cron" +end + + + +#every 1.days, at: '0:00 am'do \ No newline at end of file diff --git a/db/migrate/20200112112644_create_p_friend_kaps.rb b/db/migrate/20200112112644_create_p_friend_kaps.rb new file mode 100644 index 0000000..203e1aa --- /dev/null +++ b/db/migrate/20200112112644_create_p_friend_kaps.rb @@ -0,0 +1,10 @@ +class CreatePFriendKaps < ActiveRecord::Migration + def change + create_table :p_friend_kaps do |t| + t.references :kap, index: true, foreign_key: true + t.references :p_friend, index: true, foreign_key: true + + t.timestamps null: false + end + end +end diff --git a/db/migrate/20200112130214_add_points_count_to_kapsmes.rb b/db/migrate/20200112130214_add_points_count_to_kapsmes.rb new file mode 100644 index 0000000..09dfcbd --- /dev/null +++ b/db/migrate/20200112130214_add_points_count_to_kapsmes.rb @@ -0,0 +1,9 @@ +class AddPointsCountToKapsmes < ActiveRecord::Migration + def change + add_column :kapsmes, :point_to_count, :integer + + Kapsme.all.each do |km| + km.save + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 8fe5c60..d6f2304 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: 20200111123747) do +ActiveRecord::Schema.define(version: 20200112130214) do create_table "admin_admin_roles", force: :cascade do |t| t.integer "admin_id", limit: 4 @@ -499,6 +499,7 @@ ActiveRecord::Schema.define(version: 20200111123747) do t.datetime "created_at", null: false t.datetime "updated_at", null: false t.boolean "paused", default: false + t.integer "point_to_count", limit: 4 end add_index "kapsmes", ["kap_id"], name: "index_kapsmes_on_kap_id", using: :btree @@ -993,6 +994,16 @@ ActiveRecord::Schema.define(version: 20200111123747) do t.string "tel", limit: 255 end + create_table "p_friend_kaps", force: :cascade do |t| + t.integer "kap_id", limit: 4 + t.integer "p_friend_id", limit: 4 + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + add_index "p_friend_kaps", ["kap_id"], name: "index_p_friend_kaps_on_kap_id", using: :btree + add_index "p_friend_kaps", ["p_friend_id"], name: "index_p_friend_kaps_on_p_friend_id", using: :btree + create_table "p_friends", force: :cascade do |t| t.integer "p_customer_id", limit: 4 t.integer "p_friend_customer_id", limit: 4 @@ -1629,6 +1640,8 @@ ActiveRecord::Schema.define(version: 20200111123747) 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_friend_kaps", "kaps" + add_foreign_key "p_friend_kaps", "p_friends" 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" diff --git a/test/fixtures/p_friend_kaps.yml b/test/fixtures/p_friend_kaps.yml new file mode 100644 index 0000000..daca95b --- /dev/null +++ b/test/fixtures/p_friend_kaps.yml @@ -0,0 +1,9 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + kaps_id: + p_friend_id: + +two: + kaps_id: + p_friend_id: diff --git a/test/models/p_friend_kap_test.rb b/test/models/p_friend_kap_test.rb new file mode 100644 index 0000000..c2f77ab --- /dev/null +++ b/test/models/p_friend_kap_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class PFriendKapTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end