Skip to content

Commit

Permalink
Added Spree::Tracker validation on active field - only one active tra…
Browse files Browse the repository at this point in the history
…cker per store permitted
  • Loading branch information
nnande committed Dec 16, 2020
1 parent 38e58ca commit fe861f3
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 16 deletions.
18 changes: 18 additions & 0 deletions app/models/spree/tracker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class Tracker < Spree::Base

validates :analytics_id, presence: true, uniqueness: { scope: [:engine, :store_id], case_sensitive: false }
validates :store, presence: true
validate :ensure_one_active_tracker_assigned_to_store, if: -> { (new_record? || active_changed?) && active? }

scope :active, -> { where(active: true) }

Expand All @@ -27,5 +28,22 @@ def clear_cache
Rails.cache.delete("current_tracker/#{engine}/#{store_id}")
end
end

private

def ensure_one_active_tracker_assigned_to_store
if store_has_other_active_trackers?
errors.add(
:active,
I18n.t('activemodel.errors.models.spree/tracker.attributes.active.store_has_already_assigned_tracker',
store_name: store.name
)
)
end
end

def store_has_other_active_trackers?
Spree::Tracker.active.where.not(id: id).where(store_id: store_id).exists?
end
end
end
7 changes: 7 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
en:
activemodel:
errors:
models:
spree/tracker:
attributes:
active:
store_has_already_assigned_tracker: "%{store_name} has already assigned an active tracker - please choose a different store or deactivate the other tracker"
activerecord:
attributes:
spree/tracker:
Expand Down
83 changes: 67 additions & 16 deletions spec/features/admin/configuration/analytics_tracker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
let!(:default_store) { Spree::Store.default }
let!(:another_store) { create(:store, name: 'Eurozone') }

context 'index' do
context '#index' do
before(:each) do
create(:tracker, store: default_store)
create(:tracker, analytics_id: 'A200', store: another_store)
Expand Down Expand Up @@ -34,27 +34,78 @@
end
end

context 'create' do
before(:each) do
visit spree.admin_trackers_path
context '#update' do
let!(:tracker) { create(:tracker, store: default_store, active: false) }

context 'with no other active tracker already assigned to store' do
it 'should be able to toggle the analytics tracker to active', js: true do
visit spree.edit_admin_tracker_path(tracker)

choose 'tracker_active_true'
click_button 'Update'

expect(page).to have_content('successfully updated!')
within_row(1) do
expect(column_text(1)).to eq('A100')
expect(column_text(2)).to eq('Google analytics')
expect(column_text(3)).to eq(default_store.name)
expect(column_text(4)).to eq('Yes')
end
end
end

it 'should be able to create a new analytics tracker', js: true do
within('.content-header') do
click_link 'admin_new_tracker_link'
context 'with some other active tracker already assigned to store' do
before do
create(:tracker, store: default_store, analytics_id: 'A200')
end
fill_in 'tracker_analytics_id', with: 'A100'

select2 default_store.name, from: 'Store'
it 'should not be able to toggle the analytics tracker to active', js: true do
visit spree.edit_admin_tracker_path(tracker)

click_button 'Create'
choose 'tracker_active_true'
click_button 'Update'

expect(page).to have_content('successfully created!')
within_row(1) do
expect(column_text(1)).to eq('A100')
expect(column_text(2)).to eq('Google analytics')
expect(column_text(3)).to eq(default_store.name)
expect(column_text(4)).to eq('Yes')
expect(page).to have_content "#{default_store.name} has already assigned an active tracker"
end
end
end

context '#create' do
context 'with no other active tracker already assigned to store' do
it 'should be able to create a new active analytics tracker', js: true do
visit spree.admin_trackers_path

within('.content-header') { click_link 'admin_new_tracker_link' }
fill_in 'tracker_analytics_id', with: 'A100'
select2 default_store.name, from: 'Store'

click_button 'Create'

expect(page).to have_content('successfully created!')
within_row(1) do
expect(column_text(1)).to eq('A100')
expect(column_text(2)).to eq('Google analytics')
expect(column_text(3)).to eq(default_store.name)
expect(column_text(4)).to eq('Yes')
end
end
end

context 'with some other active tracker already assigned to store' do
before do
create(:tracker, store: default_store)
end

it 'should not be able to create a new active analytics tracker', js: true do
visit spree.admin_trackers_path

within('.content-header') { click_link 'admin_new_tracker_link' }
fill_in 'tracker_analytics_id', with: 'A200'
select2 default_store.name, from: 'Store'

click_button 'Create'

expect(page).to have_content "#{default_store.name} has already assigned an active tracker"
end
end
end
Expand Down

0 comments on commit fe861f3

Please sign in to comment.