Sha256: 019b7b430a3fbfc1c2268de0ee2f0f817d4560085bd55f7538b420a015cde795

Contents?: true

Size: 1.02 KB

Versions: 1

Compression:

Stored size: 1.02 KB

Contents

module Mailkick
  module Model

    def mailkick_user(options = {})
      class_eval do
        scope :subscribed, proc{ joins(sanitize_sql_array(["LEFT JOIN mailkick_opt_outs ON #{table_name}.email = mailkick_opt_outs.email OR (#{table_name}.id = mailkick_opt_outs.user_id AND mailkick_opt_outs.user_type = ?)", name])).where("active != ?", true) }

        def opt_outs
          Mailkick::OptOut.where("email = ? OR (user_id = ? AND user_type = ?)", email, id, self.class.name)
        end

        def subscribed?
          opt_outs.where(active: true).empty?
        end

        def subscribe
          opt_outs.where(active: true).each do |opt_out|
            opt_out.active = false
            opt_out.save!
          end
          true
        end

        def unsubscribe
          if subscribed?
            OptOut.create! do |o|
              o.email = email
              o.user = self
              o.reason = "unsubscribe"
              o.save!
            end
          end
          true
        end

      end
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mailkick-0.0.1 lib/mailkick/model.rb