Skip to content

Commit

Permalink
Hide expire button in price edit form if price is expired
Browse files Browse the repository at this point in the history
  • Loading branch information
Artur Beljajev committed Aug 29, 2017
1 parent d807703 commit af6f745
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 7 deletions.
25 changes: 25 additions & 0 deletions app/presenters/billing/price_presenter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module Billing
class PricePresenter
def initialize(price:, view:)
@price = price
@view = view
end

def expire_btn
return if price.expired?

view.link_to(view.t('admin.billing.prices.expire_btn.label'),
view.expire_admin_price_path(price),
method: :patch,
data: {
confirm: view.t('admin.billing.prices.expire_btn.confirm'),
},
class: 'btn btn-danger')
end

private

attr_reader :price
attr_reader :view
end
end
7 changes: 3 additions & 4 deletions app/views/admin/billing/prices/edit.html.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
<% price = Billing::PricePresenter.new(price: @price, view: self) %>

<ol class="breadcrumb">
<li><%= link_to t('admin.billing.prices.index.title'), admin_prices_path %></li>
</ol>
Expand All @@ -9,10 +11,7 @@
</div>

<div class="col-sm-2 text-right">
<%= link_to(t('.expire_btn'), expire_admin_price_path(@price),
method: :patch,
data: { confirm: t('.expire_btn_confirm') },
class: 'btn btn-danger') %>
<%= price.expire_btn %>
</div>
</div>
</div>
Expand Down
6 changes: 4 additions & 2 deletions config/locales/admin/billing/prices.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ en:

edit:
title: Edit price
expire_btn: Expire
expire_btn_confirm: Are you sure you want to expire price?

update:
updated: Price has been updated
Expand All @@ -32,3 +30,7 @@ en:
all: All
search_btn: Search
reset_btn: Reset

expire_btn:
label: Expire
confirm: Are you sure you want to expire price?
2 changes: 1 addition & 1 deletion spec/features/admin/billing/prices/expire_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ def open_edit_form
end

def expire
click_link_or_button t('admin.billing.prices.edit.expire_btn')
click_link_or_button t('admin.billing.prices.expire_btn.label')
end
end
26 changes: 26 additions & 0 deletions spec/presenters/billing/price_presenter_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require 'rails_helper'

RSpec.describe Billing::PricePresenter do
let(:presenter) { described_class.new(price: price, view: view) }

describe '#expire_btn' do
context 'when price is not expired' do
let(:price) { instance_double(Billing::Price, expired?: false) }

it 'returns expire button' do
html = view.link_to(t('admin.billing.prices.expire_btn.label'), expire_admin_price_path(price),
method: :patch,
data: {
confirm: view.t('admin.billing.prices.expire_btn.confirm'),
},
class: 'btn btn-danger')
expect(presenter.expire_btn).to eq(html)
end
end

context 'when price is expired' do
let(:price) { instance_double(Billing::Price, expired?: true) }
specify { expect(presenter.expire_btn).to be_nil }
end
end
end

0 comments on commit af6f745

Please sign in to comment.