Class: Munificent::Donator

Inherits:
ApplicationRecord show all
Includes:
Authenticable
Defined in:
app/models/munificent/donator.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ApplicationRecord

#to_s

Instance Attribute Details

#password_confirmationObject

Returns the value of attribute password_confirmation.


7
8
9
# File 'app/models/munificent/donator.rb', line 7

def password_confirmation
  @password_confirmation
end

#require_password=(value) ⇒ Object (writeonly)

Sets the attribute require_password

Parameters:

  • value

    the value to set the attribute require_password to.


8
9
10
# File 'app/models/munificent/donator.rb', line 8

def require_password=(value)
  @require_password = value
end

Class Method Details

.create_from_omniauth!(auth_hash) ⇒ Object


45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/models/munificent/donator.rb', line 45

def self.create_from_omniauth!(auth_hash)
  case (provider = auth_hash["provider"])
  when "twitch"
    Donator.create!(
      chosen_name: auth_hash.dig("info", "nickname"),
      email_address: auth_hash.dig("info", "email"),
      name: auth_hash.dig("info", "name"),
      twitch_id: auth_hash["uid"],
    )
  else
    raise "Unsupported provider: #{provider}"
  end
end

Instance Method Details

#anonymous?Boolean

Returns:

  • (Boolean)

90
91
92
# File 'app/models/munificent/donator.rb', line 90

def anonymous?
  name.blank? && chosen_name.blank?
end

#confirmObject


102
103
104
105
106
107
108
109
110
# File 'app/models/munificent/donator.rb', line 102

def confirm
  return unless confirmed? || unconfirmed_email_address.present?

  update(
    confirmed: true,
    email_address: unconfirmed_email_address,
    unconfirmed_email_address: nil,
  )
end

#display_name(current_donator: nil) ⇒ Object


84
85
86
87
88
# File 'app/models/munificent/donator.rb', line 84

def display_name(current_donator: nil)
  return I18n.t("common.abstract.you") if current_donator == self

  chosen_name.presence || name.presence || I18n.t("common.abstract.anonymous")
end

#email_address=(new_email_address) ⇒ Object


59
60
61
62
# File 'app/models/munificent/donator.rb', line 59

def email_address=(new_email_address)
  super(new_email_address.presence)
  @token_with_email_address = nil
end

#no_identifying_marks?Boolean

Returns:

  • (Boolean)

98
99
100
# File 'app/models/munificent/donator.rb', line 98

def no_identifying_marks?
  email_address.blank? && twitch_id.blank?
end

#require_password?Boolean

Returns:

  • (Boolean)

10
11
12
# File 'app/models/munificent/donator.rb', line 10

def require_password?
  !!@require_password
end

#tokenObject


72
73
74
75
76
# File 'app/models/munificent/donator.rb', line 72

def token
  @token ||= HMAC::Generator
    .new(context: "sessions")
    .generate(id:)
end

#token_with_email_addressObject


78
79
80
81
82
# File 'app/models/munificent/donator.rb', line 78

def token_with_email_address
  @token_with_email_address ||= HMAC::Generator
    .new(context: "sessions")
    .generate(id:, extra_fields: { email_address: })
end

#total_donations(fundraiser: nil) ⇒ Object


64
65
66
67
68
69
70
# File 'app/models/munificent/donator.rb', line 64

def total_donations(fundraiser: nil)
  if fundraiser
    donations.where(fundraiser:)
  else
    donations
  end.map(&:amount).reduce(Money.new(0), :+)
end

#twitch_connected?Boolean

Returns:

  • (Boolean)

94
95
96
# File 'app/models/munificent/donator.rb', line 94

def twitch_connected?
  twitch_id.present?
end