Skip to content

Commit 027c18e

Browse files
committed
Updates
1 parent 65e0fba commit 027c18e

16 files changed

+47
-70
lines changed

1-value-objects/1_constant.rb

+5-11
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,11 @@ def rating_higher_than?(other_rating)
1414
end
1515

1616
def rating_string
17-
if remediation_cost <= 2
18-
"A"
19-
elsif remediation_cost <= 4
20-
"B"
21-
elsif remediation_cost <= 8
22-
"C"
23-
elsif remediation_cost <= 16
24-
"D"
25-
else
26-
"F"
17+
if remediation_cost <= 2 then "A"
18+
elsif remediation_cost <= 4 then "B"
19+
elsif remediation_cost <= 8 then "C"
20+
elsif remediation_cost <= 16 then "D"
21+
else "F"
2722
end
2823
end
29-
3024
end

1-value-objects/2_rating.rb

+13-18
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,26 @@ class Rating
22
include Comparable
33

44
def self.from_cost(cost)
5-
if cost <= 2
6-
new("A")
7-
elsif cost <= 4
8-
new("B")
9-
elsif cost <= 8
10-
new("C")
11-
elsif cost <= 16
12-
new("D")
13-
else
14-
new("F")
5+
if cost <= 2 then new("A")
6+
elsif cost <= 4 then new("B")
7+
elsif cost <= 8 then new("C")
8+
elsif cost <= 16 then new("D")
9+
else new("F")
1510
end
1611
end
1712

1813
def initialize(letter)
1914
@letter = letter
2015
end
2116

17+
def to_s
18+
@letter.to_s
19+
end
20+
21+
def <=>(other)
22+
other.to_s <=> to_s
23+
end
24+
2225
def worse
2326
return nil if @letter == "F"
2427
self.class.new(@letter.succ)
@@ -28,19 +31,11 @@ def higher_than?(other)
2831
self > other
2932
end
3033

31-
def <=>(other)
32-
other.to_s <=> to_s
33-
end
34-
3534
def hash
3635
@letter.hash
3736
end
3837

3938
def eql?(other)
4039
to_s == other.to_s
4140
end
42-
43-
def to_s
44-
@letter.to_s
45-
end
4641
end

2-service-objects/2_password_authenticator.rb

+7-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@ def initialize(user)
44
end
55

66
def authenticate(unencrypted_password)
7-
return false unless @user
8-
BCrypt::Password.new(@user.password_digest) == unencrypted_password
7+
@user && bcrypt_password == unencrypted_password
8+
end
9+
10+
private
11+
12+
def bcrypt_password
13+
BCrypt::Password.new(@user.password_digest)
914
end
1015
end

2-service-objects/2_token_authenticator.rb

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ def authenticate(provided_token)
1010

1111
private
1212

13-
# constant-time comparison algorithm to prevent timing attacks
1413
def secure_compare(a, b)
1514
return false unless a.bytesize == b.bytesize
1615
l = a.unpack "C#{a.bytesize}"

3-form-objects/1_user.rb

-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
# 1. What if the user fails to save after saving the company?
2-
# 2. wtf does user#company return? strings?
3-
# 3. how do you create _just_ a fscking user
41
class User < ActiveRecord::Base
52
attr_accessor :company
63

4-query-objects/1_account.rb

+2-3
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,15 @@ class Account < ActiveRecord::Base
44
def self.importable_accounts
55
where(enabled: true).
66
where("failed_attempts_count <= 3").
7-
joins("LEFT OUTER JOIN import_attempts ON account_id = accounts.id").
7+
joins("LEFT JOIN import_attempts ON account_id = accounts.id").
88
order('last_attempt_at ASC').
99
preload(:credentials)
1010
end
1111

1212
def self.import_failed_accounts
1313
where("failed_attempts_count >= 3")
14-
joins("LEFT OUTER JOIN import_attempts ON account_id = accounts.id")
14+
joins("LEFT JOIN import_attempts ON account_id = accounts.id")
1515
order('failed_attempts_count DESC').
1616
preload(:credentials)
1717
end
18-
1918
end

4-query-objects/2_importable_accounts_query.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ def find_each(&block)
77
@relation.
88
where(enabled: true).
99
where("failed_attempts_count <= 3").
10-
joins("LEFT OUTER JOIN import_attempts ON account_id = accounts.id").
10+
joins("LEFT JOIN imports ON account_id = accounts.id").
1111
order('last_attempt_at ASC').
1212
preload(:credentials).
1313
find_each(&block)

4-query-objects/3_import_accounts_job.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
class ImportAccountsJob < Job
22
def run
3-
ImportableAccountsQuery.new.find_each do |account|
3+
query = ImportableAccountsQuery.new
4+
query.find_each do |account|
45
AccountImporter.new(account).run
56
end
67
end
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
old_accounts = Account.where("created_at < ?", 1.month.ago)
2-
old_importable_accounts = ImportableAccountsQuery.new(old_accounts)
2+
ImportableAccountsQuery.new(old_accounts)

5-view-objects/1_user.rb

-1
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,4 @@ def onboarding_progress_percent
1515
score += 1 if @user.sent_invites?
1616
(score / 2.0 * 100)
1717
end
18-
1918
end

6-policy-objects/1_user.rb

-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,4 @@ def deliver_notification?(notification_type, project = nil)
66
receive_notification_type?(notification_type) &&
77
(!project || receives_notifications_for?(project))
88
end
9-
109
end
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
class EmailNotificationPolicy
2-
32
def initialize(user, notification_type, project = nil)
43
@user = user
54
@notification_type = notification_type
65
@project = project
76
end
87

9-
def email?
8+
def deliver?
109
!@user.hard_bounce? &&
1110
@user.receive_notification_type?(notification_type) &&
1211
receives_project_emails?
@@ -15,7 +14,7 @@ def email?
1514
private
1615

1716
def receives_project_emails?
18-
(!@project || @user.receives_notifications_for?(@project))
17+
!@project ||
18+
@user.receives_notifications_for?(@project)
1919
end
20-
2120
end

7-decorators/1_order.rb

-14
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,11 @@ class Order < ActiveRecord::Base
22
# ...
33
attr_accessor :placed_online
44

5-
after_create :notify_warehouse
65
after_create :email_receipt, if: :placed_online
7-
after_create :post_to_facebook, if: :send_to_facebook?
86

97
private
108

11-
def notify_warehouse
12-
WarehouseApi.notify(sku, quanity)
13-
end
14-
159
def email_receipt
1610
OrdersMailer.receipt(self).deliver
1711
end
18-
19-
def send_to_facebook?
20-
placed_online && customer.facebook_enabled?
21-
end
22-
23-
def post_to_facebook
24-
FacebookApi.post_to_wall("I just bought #{product_name} from MyStoreCo!")
25-
end
2612
end
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class OrderEmailNotifier
2+
def initialize(order)
3+
@order = order
4+
end
5+
6+
def save
7+
@order.save &&
8+
OrdersMailer.receipt(@order).deliver
9+
end
10+
end

7-decorators/2_warehouse_notifier.rb

-9
This file was deleted.

7-decorators/3_orders_controller.rb

+3
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,6 @@ def build_order
1919
end
2020

2121
end
22+
23+
24+
# Can you read this?

0 commit comments

Comments
 (0)