Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

does not allow to update mailchimp list id #111

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions app/models/mailchimp_setting.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class MailchimpSetting < ActiveRecord::Base
validates :mailchimp_api_key, :mailchimp_store_id, :mailchimp_list_id, :mailchimp_store_name, :cart_url, presence: true
validate :validate_only_one_store, on: :create
validate :mailchimp_list_id_cannot_be_updated, on: :update

def validate_only_one_store
errors.add(:base, "only one store allowed") unless MailchimpSetting.count.zero?
Expand All @@ -13,4 +14,10 @@ def create_store_id
def accout_name
::SpreeMailchimpEcommerce::GetAccountNameJob.perform_now(self)
end

private

def mailchimp_list_id_cannot_be_updated
errors.add(:mailchimp_list_id, Spree.t(:can_not_be_updated)) if mailchimp_list_id_changed? && state == 'ready'
end
end
3 changes: 3 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
en:
spree:
can_not_be_updated: can't be updated
12 changes: 12 additions & 0 deletions spec/factories/spree/mailchimp_setting_factory.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
FactoryBot.define do
factory :mailchimp_setting, class: MailchimpSetting do
sequence(:mailchimp_api_key) { |i| "#{FFaker::Internet.password}" + i.to_s }
sequence(:mailchimp_store_id) { |i| "#{FFaker::Internet.password}" + i.to_s }
sequence(:mailchimp_list_id) { |i| "#{FFaker::Internet.password}" + i.to_s }
sequence(:mailchimp_store_name) { |i| "store_no_#{i}" }
cart_url { FFaker::Internet.http_url }
mailchimp_account_name { FFaker::Internet.user_name }
state { 'inactive' }
mailchimp_store_email { FFaker::Internet.email }
end
end
39 changes: 39 additions & 0 deletions spec/models/mailchimp_setting_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
require "spec_helper"

describe MailchimpSetting, type: :model do
subject { create(:mailchimp_setting) }

describe "validations" do
it 'allows to save mailchimp setting' do
expect(subject.persisted?).to be true
end

it 'allows to destroy mailchimp setting' do
subject.destroy

expect(subject.destroyed?).to be true
end

context 'when mailchimp is synchronised' do
before { subject.update(state: 'ready') }

it 'does not allow to update mailchimp_list_id' do
subject.update(mailchimp_list_id: FFaker::Internet.password)

expect(subject.valid?).to be false
expect(subject.errors.to_h[:mailchimp_list_id]).to eq Spree.t(:can_not_be_updated)
end
end

context 'when mailchimp is not synchronised' do
let(:new_list_id) { FFaker::Internet.password }

it 'allows to update mailchimp_list_id' do
subject.update(mailchimp_list_id: new_list_id)

expect(subject.reload.mailchimp_list_id).to eq new_list_id
end
end
end
end

4 changes: 0 additions & 4 deletions spec/support/factory_bot.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
require "factory_bot"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's not necessary


FactoryBot.find_definitions
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed that because with that method call, I couldn't add new factories (they were registed twice and I got Factory already registed error).


RSpec.configure do |config|
config.include FactoryBot::Syntax::Methods
end