diff --git a/lib/crm/mailing.rb b/lib/crm/mailing.rb index c37ff1b..38d1190 100644 --- a/lib/crm/mailing.rb +++ b/lib/crm/mailing.rb @@ -19,6 +19,16 @@ class Mailing < Core::BasicResource # @!parse extend Core::Mixins::Modifiable::ClassMethods # @!parse extend Core::Mixins::Searchable::ClassMethods + # Clones a mailing. + # @example + # mailing.clone + # # => Crm::Mailing + # @return [BasicResource] the cloned mailing. + # @api public + def clone + self.class.new(Core::RestApi.instance.post("#{path}/clone", {})) + end + # Renders a preview of the email for the given contact. # @example # mailing.html_body diff --git a/spec/crm/mailing_spec.rb b/spec/crm/mailing_spec.rb index 880099b..1f09ece 100644 --- a/spec/crm/mailing_spec.rb +++ b/spec/crm/mailing_spec.rb @@ -40,6 +40,17 @@ module Crm end end + describe '#create' do + let(:mailing) { Mailing.new("id" => "abc") } + + it 'clones a mailing' do + expect(Core::RestApi.instance).to receive(:post).with('mailings/abc/clone', {}) + .and_return({"id" => "def"}) + clone = mailing.clone + expect(clone.id).to eq('def') + end + end + describe '#render_preview' do let(:mailing) { Mailing.new("id" => "abc") } let(:preview_output) { diff --git a/spec/features/mailing_feature_spec.rb b/spec/features/mailing_feature_spec.rb index 7017126..6853298 100644 --- a/spec/features/mailing_feature_spec.rb +++ b/spec/features/mailing_feature_spec.rb @@ -101,6 +101,21 @@ end end + describe 'clone' do + let(:mailing) do + Crm::Mailing.create({ + title: 'My Mailing', + type_id: 'newsletter', + }) + end + + it 'clones the mailing' do + clone = mailing.clone + expect(clone.id).not_to eq(mailing.id) + expect(clone.title).to eq('My Mailing (Copy)') + end + end + describe 'changes' do let(:mailing) do Crm::Mailing.create({