diff --git a/lib/activity_notification/models/concerns/notifiable.rb b/lib/activity_notification/models/concerns/notifiable.rb index 76d1307a..5d2f9eef 100644 --- a/lib/activity_notification/models/concerns/notifiable.rb +++ b/lib/activity_notification/models/concerns/notifiable.rb @@ -20,6 +20,10 @@ def default_url_options end class_methods do + def available_as_notifiable? + true + end + def set_notifiable_class_defaults self._notification_targets = {} self._notification_group = {} diff --git a/lib/activity_notification/models/concerns/notifier.rb b/lib/activity_notification/models/concerns/notifier.rb index 435c61cc..d7e825b3 100644 --- a/lib/activity_notification/models/concerns/notifier.rb +++ b/lib/activity_notification/models/concerns/notifier.rb @@ -7,5 +7,11 @@ module Notifier class_name: "::ActivityNotification::Notification", as: :notifier end + + class_methods do + def available_as_notifier? + true + end + end end end \ No newline at end of file diff --git a/lib/activity_notification/models/concerns/target.rb b/lib/activity_notification/models/concerns/target.rb index 43bcee64..0b0d91ec 100644 --- a/lib/activity_notification/models/concerns/target.rb +++ b/lib/activity_notification/models/concerns/target.rb @@ -11,6 +11,10 @@ module Target end class_methods do + def available_as_target? + true + end + def set_target_class_defaults self._notification_email = nil self._notification_email_allowed = ActivityNotification.config.email_enabled diff --git a/lib/activity_notification/roles/acts_as_notifier.rb b/lib/activity_notification/roles/acts_as_notifier.rb index 0a381ffd..0feab46c 100644 --- a/lib/activity_notification/roles/acts_as_notifier.rb +++ b/lib/activity_notification/roles/acts_as_notifier.rb @@ -5,7 +5,6 @@ module ActsAsNotifier class_methods do def acts_as_notifier include Notifier - true end end end diff --git a/lib/activity_notification/roles/acts_as_target.rb b/lib/activity_notification/roles/acts_as_target.rb index dc37f81e..b997b74a 100644 --- a/lib/activity_notification/roles/acts_as_target.rb +++ b/lib/activity_notification/roles/acts_as_target.rb @@ -3,28 +3,23 @@ module ActsAsTarget extend ActiveSupport::Concern class_methods do - def acts_as_target(opts = {}) + def acts_as_target(options = {}) include Target - options = opts.clone if options[:skip_email] == true self.send("_notification_email_allowed=".to_sym, false) + options.delete(:email_allowed) end - assign_target_globals(options) - nil + [:email, :email_allowed].map { |key| + options[key] ? + [key, self.send("_notification_#{key}=".to_sym, options.delete(key))] : + [nil, nil] + }.to_h.delete_if { |k, v| k.nil? } end alias_method :acts_as_notification_target, :acts_as_target def available_target_options [:skip_email, :email, :email_allowed].freeze end - - def assign_target_globals(options) - [:email, :email_allowed].each do |key| - if options[key] - self.send("_notification_#{key}=".to_sym, options.delete(key)) - end - end - end end end end diff --git a/spec/concerns/acts_as_notifiable_spec.rb b/spec/concerns/acts_as_notifiable_spec.rb deleted file mode 100644 index 8a75a0fc..00000000 --- a/spec/concerns/acts_as_notifiable_spec.rb +++ /dev/null @@ -1,27 +0,0 @@ -shared_examples_for :acts_as_notifiable do - let(:test_class_name) { described_class.to_s.underscore.split('/').last.to_sym } - let(:test_instance) { create(test_class_name) } - - describe "as public class methods" do - describe "acts_as_notifiable" do - it "includes Notifiable" do - expect(described_class.respond_to?(:set_notifiable_class_defaults)).to be_truthy - end - - context "with no options" do - it "returns hash of specified options" do - expect(described_class.acts_as_notifiable :users).to eq({}) - end - end - - #TODO test other options - end - - describe "available_notifiable_options" do - it "returns list of available options in acts_as_notifiable" do - expect(described_class.available_notifiable_options) - .to eq([:targets, :group, :notifier, :parameters, :email_allowed, :notifiable_path]) - end - end - end -end \ No newline at end of file diff --git a/spec/concerns/notification_api_spec.rb b/spec/concerns/apis/notification_api_spec.rb similarity index 100% rename from spec/concerns/notification_api_spec.rb rename to spec/concerns/apis/notification_api_spec.rb diff --git a/spec/concerns/roles/acts_as_notifiable_spec.rb b/spec/concerns/roles/acts_as_notifiable_spec.rb new file mode 100644 index 00000000..67436309 --- /dev/null +++ b/spec/concerns/roles/acts_as_notifiable_spec.rb @@ -0,0 +1,32 @@ +describe ActivityNotification::ActsAsNotifiable do + let(:dummy_model_class) { DummyModel } + + describe "as public class methods" do + describe "acts_as_notifiable" do + it "have not included Notifiable before calling" do + expect(dummy_model_class.respond_to?(:available_as_notifiable?)).to be_falsey + end + + it "includes Notifiable" do + dummy_model_class.acts_as_notifiable :users + expect(dummy_model_class.respond_to?(:available_as_notifiable?)).to be_truthy + expect(dummy_model_class.available_as_notifiable?).to be_truthy + end + + context "with no options" do + it "returns hash of specified options" do + expect(dummy_model_class.acts_as_notifiable :users).to eq({}) + end + end + + #TODO test other options + end + + describe "available_notifiable_options" do + it "returns list of available options in acts_as_notifiable" do + expect(dummy_model_class.available_notifiable_options) + .to eq([:targets, :group, :notifier, :parameters, :email_allowed, :notifiable_path]) + end + end + end +end \ No newline at end of file diff --git a/spec/concerns/roles/acts_as_notifier_spec.rb b/spec/concerns/roles/acts_as_notifier_spec.rb new file mode 100644 index 00000000..eec94093 --- /dev/null +++ b/spec/concerns/roles/acts_as_notifier_spec.rb @@ -0,0 +1,17 @@ +describe ActivityNotification::ActsAsNotifier do + let(:dummy_model_class) { DummyModel } + + describe "as public class methods" do + describe "acts_as_notifier" do + it "have not included Notifier before calling" do + expect(dummy_model_class.respond_to?(:available_as_notifier?)).to be_falsey + end + + it "includes Notifier" do + dummy_model_class.acts_as_notifier + expect(dummy_model_class.respond_to?(:available_as_notifier?)).to be_truthy + expect(dummy_model_class.available_as_notifier?).to be_truthy + end + end + end +end \ No newline at end of file diff --git a/spec/concerns/roles/acts_as_target_spec.rb b/spec/concerns/roles/acts_as_target_spec.rb new file mode 100644 index 00000000..5d606985 --- /dev/null +++ b/spec/concerns/roles/acts_as_target_spec.rb @@ -0,0 +1,40 @@ +describe ActivityNotification::ActsAsTarget do + let(:dummy_model_class) { DummyModel } + + describe "as public class methods" do + describe "acts_as_target" do + it "have not included Target before calling" do + expect(dummy_model_class.respond_to?(:available_as_target?)).to be_falsey + end + + it "includes Target" do + dummy_model_class.acts_as_target + expect(dummy_model_class.respond_to?(:available_as_target?)).to be_truthy + expect(dummy_model_class.available_as_target?).to be_truthy + end + + context "with no options" do + it "returns hash of specified options" do + expect(dummy_model_class.acts_as_target).to eq({}) + end + end + + #TODO test other options + end + + describe "acts_as_notification_target" do + it "is alias of acts_as_target" do + #TODO better way + #expect(dummy_model_class.acts_as_notification_target).to receive(:acts_as_target) + expect(dummy_model_class.respond_to?(:acts_as_notification_target)).to be_truthy + end + end + + describe "available_target_options" do + it "returns list of available options in acts_as_target" do + expect(dummy_model_class.available_target_options) + .to eq([:skip_email, :email, :email_allowed]) + end + end + end +end \ No newline at end of file diff --git a/spec/models/test/comment_spec.rb b/spec/models/test/comment_spec.rb deleted file mode 100644 index da8bef6a..00000000 --- a/spec/models/test/comment_spec.rb +++ /dev/null @@ -1,5 +0,0 @@ -describe Comment, type: :model do - - it_behaves_like :acts_as_notifiable - -end diff --git a/spec/rails_app/app/models/dummy_model.rb b/spec/rails_app/app/models/dummy_model.rb new file mode 100644 index 00000000..db35ae17 --- /dev/null +++ b/spec/rails_app/app/models/dummy_model.rb @@ -0,0 +1,2 @@ +class DummyModel < ActiveRecord::Base +end