diff --git a/Gemfile.lock b/Gemfile.lock index 0de4dd1..2d8a94a 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -143,7 +143,7 @@ GEM rb-fsevent (~> 0.9, >= 0.9.4) rb-inotify (~> 0.9, >= 0.9.7) ruby_dep (~> 1.2) - loofah (2.2.2) + loofah (2.2.3) crass (~> 1.0.2) nokogiri (>= 1.5.9) mail (2.7.0) @@ -159,7 +159,7 @@ GEM multi_json (1.13.1) multipart-post (2.0.0) nio4r (2.3.1) - nokogiri (1.8.4) + nokogiri (1.8.5) mini_portile2 (~> 2.3.0) orm_adapter (0.5.0) pg (1.1.3) diff --git a/app/controllers/referrals_controller.rb b/app/controllers/referrals_controller.rb index e0b8fb4..cad1353 100644 --- a/app/controllers/referrals_controller.rb +++ b/app/controllers/referrals_controller.rb @@ -5,7 +5,7 @@ class ReferralsController < ApplicationController def new if @partner - @referrals = @partner.referrals.by_month(Time.zone.now.month) + @referrals = @partner.referrals.by_month(@date.month) @used_referrals = @referrals.used @total_available = @partner.max_monthly_referrals - @used_referrals.size else diff --git a/app/models/partner.rb b/app/models/partner.rb index aea986e..95aba06 100644 --- a/app/models/partner.rb +++ b/app/models/partner.rb @@ -1,5 +1,5 @@ class Partner < ApplicationRecord - has_many :referrals + has_many :referrals, dependent: :destroy has_secure_token :webhook_token diff --git a/app/models/referral.rb b/app/models/referral.rb index 41541dd..c8dcc63 100644 --- a/app/models/referral.rb +++ b/app/models/referral.rb @@ -4,7 +4,7 @@ class Referral < ApplicationRecord belongs_to :partner acts_as_sequenced scope: :partner_id - has_many :reviews + has_many :reviews, dependent: :destroy validates :last_state, inclusion: { in: STATES } validates :original_response, presence: true diff --git a/app/views/layouts/_month_picker.html.erb b/app/views/layouts/_month_picker.html.erb index 80456c4..4a042da 100644 --- a/app/views/layouts/_month_picker.html.erb +++ b/app/views/layouts/_month_picker.html.erb @@ -7,5 +7,5 @@ <%= "#{Date::MONTHNAMES[date.month]} #{Time.zone.now.year}" %> - <%= link_to('❯', send(path, date: date - 1.month), class: 'yellow no-decoration') %> + <%= link_to('❯', send(path, date: date - 1.month), class: 'yellow no-decoration', data: { turbolinks: false }) %> diff --git a/app/views/referrals/new.html.erb b/app/views/referrals/new.html.erb index f2e8a85..ec28bbc 100644 --- a/app/views/referrals/new.html.erb +++ b/app/views/referrals/new.html.erb @@ -29,37 +29,43 @@
-

Sent

+

Used

<%= tag.div( - @referrals.size, id: "#{@partner.slug}-used-referrals", + @used_referrals.size, id: "#{@partner.slug}-used-referrals", class: "lg stat-lg white bg-black" ) %>
-<% if @partner.referrals.any? %> -

Sent Referrals

+<% if @referrals.any? %> +
+

Sent Referrals

-
-
Name
-
Status
-
Date
-
+
+
Name
+
Status
+
Date
+
+ + <% @referrals.order(created_at: :desc).each do |r| %> - <% @partner.referrals.order(created_at: :desc).each do |r| %> +
+ + <%= r.response_identifier %> + + <%= status_text(r.last_state, classes: "sm fr1 truncate") %> + + <%= r.created_at.strftime('%d / %m / %y') %> + +
-
- - <%= r.response_identifier %> - - <%= status_text(r.last_state, classes: "sm fr1 truncate") %> - - <%= r.created_at.strftime('%d / %m / %y') %> - + <% end %>
+<% end %> - <% end %> +<% if user_signed_in? %> + <%= render(partial: 'layouts/month_picker', locals: { date: @date, path: :new_partner_referral_path }) %> <% end %> diff --git a/test/system/partner_referral_test.rb b/test/system/partner_referral_test.rb index edd2ede..ec407c9 100644 --- a/test/system/partner_referral_test.rb +++ b/test/system/partner_referral_test.rb @@ -73,23 +73,34 @@ class PartnerReferralTest < ApplicationSystemTestCase end test 'hides list if no referrals' do - @user = create(:user) - visit new_user_session_path - sign_in + sign_in_user visit new_partner_referral_path(@partner) assert_no_text('Sent Referrals') end test 'shows list if logged in and referrals present' do create(:referral, partner: @partner) - @user = create(:user) - visit new_user_session_path - sign_in + sign_in_user visit new_partner_referral_path(@partner) assert_text('Sent Referrals') assert_selector('.row.with-border', count: 1) end + test 'month picker hidden when logged out' do + visit new_partner_referral_path(@partner) + assert_text(month_name, count: 1) + end + + test 'month picker shown when logged in' do + sign_in_user + visit new_partner_referral_path(@partner) + assert_text(month_name, count: 2) + end + + def month_name + Date::MONTHNAMES[Date.today.month] + end + def send_fake_webhook_request(partner) fake_webhook_request( webhooks_new_response_path(partner.webhook_token), @@ -97,4 +108,10 @@ def send_fake_webhook_request(partner) body: { event_id: 'LtWXD3crgy' } ) end + + def sign_in_user + @user = create(:user) + visit new_user_session_path + sign_in + end end