lib/mailkick/model.rb in mailkick-0.4.3 vs lib/mailkick/model.rb in mailkick-1.0.0
- old
+ new
@@ -1,34 +1,23 @@
module Mailkick
module Model
- def mailkick_user(opts = {})
- email_key = opts[:email_key] || :email
+ def has_subscriptions
class_eval do
- scope :opted_out, lambda { |options = {}|
- binds = [name, true]
- if options[:list]
- query = "(mailkick_opt_outs.list IS NULL OR mailkick_opt_outs.list = ?)"
- binds << options[:list]
- else
- query = "mailkick_opt_outs.list IS NULL"
- end
- where("#{options[:not] ? 'NOT ' : ''}EXISTS(SELECT * FROM mailkick_opt_outs WHERE (#{table_name}.#{email_key} = mailkick_opt_outs.email OR (#{table_name}.#{primary_key} = mailkick_opt_outs.user_id AND mailkick_opt_outs.user_type = ?)) AND mailkick_opt_outs.active = ? AND #{query})", *binds)
- }
+ has_many :mailkick_subscriptions, class_name: "Mailkick::Subscription", as: :subscriber
+ scope :subscribed, -> (list) { joins(:mailkick_subscriptions).where(mailkick_subscriptions: {list: list}) }
- scope :not_opted_out, lambda { |options = {}|
- opted_out(options.merge(not: true))
- }
-
- define_method :opted_out? do |options = {}|
- Mailkick.opted_out?({email: send(email_key), user: self}.merge(options))
+ def subscribe(list)
+ mailkick_subscriptions.where(list: list).first_or_create!
+ nil
end
- define_method :opt_out do |options = {}|
- Mailkick.opt_out({email: send(email_key), user: self}.merge(options))
+ def unsubscribe(list)
+ mailkick_subscriptions.where(list: list).delete_all
+ nil
end
- define_method :opt_in do |options = {}|
- Mailkick.opt_in({email: send(email_key), user: self}.merge(options))
+ def subscribed?(list)
+ mailkick_subscriptions.where(list: list).exists?
end
end
end
end
end